package com.maintain.business.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.maintain.common.utils.StringUtils;
import com.maintain.common.core.page.TableDataInfo;
import com.maintain.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.maintain.business.domain.bo.ErpMaintainCauseBo;
import com.maintain.business.domain.vo.ErpMaintainCauseVo;
import com.maintain.business.domain.ErpMaintainCause;
import com.maintain.business.mapper.ErpMaintainCauseMapper;
import com.maintain.business.service.IErpMaintainCauseService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 维修原因Service业务层处理
 *
 * @author liushuai
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class ErpMaintainCauseServiceImpl implements IErpMaintainCauseService {

    private final ErpMaintainCauseMapper baseMapper;

    /**
     * 查询维修原因
     */
    @Override
    public ErpMaintainCauseVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询维修原因列表
     */
    @Override
    public TableDataInfo<ErpMaintainCauseVo> queryPageList(ErpMaintainCauseBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpMaintainCause> lqw = buildQueryWrapper(bo);
        Page<ErpMaintainCauseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询维修原因列表
     */
    @Override
    public List<ErpMaintainCauseVo> queryList(ErpMaintainCauseBo bo) {
        LambdaQueryWrapper<ErpMaintainCause> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpMaintainCause> buildQueryWrapper(ErpMaintainCauseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpMaintainCause> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMaintainCauseName()), ErpMaintainCause::getMaintainCauseName, bo.getMaintainCauseName());
        lqw.like(StringUtils.isNotBlank(bo.getMaintainCauseCode()), ErpMaintainCause::getMaintainCauseCode, bo.getMaintainCauseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ErpMaintainCause::getType, bo.getType());
        lqw.eq(bo.getIsDelete() != null, ErpMaintainCause::getIsDelete, bo.getIsDelete());
        lqw.eq(bo.getIsEnable() != null, ErpMaintainCause::getIsEnable, bo.getIsEnable());
        return lqw;
    }

    /**
     * 新增维修原因
     */
    @Override
    public Boolean insertByBo(ErpMaintainCauseBo bo) {
        ErpMaintainCause add = BeanUtil.toBean(bo, ErpMaintainCause.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改维修原因
     */
    @Override
    public Boolean updateByBo(ErpMaintainCauseBo bo) {
        ErpMaintainCause update = BeanUtil.toBean(bo, ErpMaintainCause.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpMaintainCause entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除维修原因
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
