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.ErpSettlementMaintainRepairFormBo;
import com.maintain.business.domain.vo.ErpSettlementMaintainRepairFormVo;
import com.maintain.business.domain.ErpSettlementMaintainRepairForm;
import com.maintain.business.mapper.ErpSettlementMaintainRepairFormMapper;
import com.maintain.business.service.IErpSettlementMaintainRepairFormService;

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

/**
 * 结算单与报修单关联Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-03
 */
@RequiredArgsConstructor
@Service
public class ErpSettlementMaintainRepairFormServiceImpl implements IErpSettlementMaintainRepairFormService {

    private final ErpSettlementMaintainRepairFormMapper baseMapper;

    /**
     * 查询结算单与报修单关联
     */
    @Override
    public ErpSettlementMaintainRepairFormVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询结算单与报修单关联列表
     */
    @Override
    public TableDataInfo<ErpSettlementMaintainRepairFormVo> queryPageList(ErpSettlementMaintainRepairFormBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSettlementMaintainRepairForm> lqw = buildQueryWrapper(bo);
        Page<ErpSettlementMaintainRepairFormVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询结算单与报修单关联列表
     */
    @Override
    public List<ErpSettlementMaintainRepairFormVo> queryList(ErpSettlementMaintainRepairFormBo bo) {
        LambdaQueryWrapper<ErpSettlementMaintainRepairForm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSettlementMaintainRepairForm> buildQueryWrapper(ErpSettlementMaintainRepairFormBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSettlementMaintainRepairForm> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSettlementMaintainId() != null, ErpSettlementMaintainRepairForm::getSettlementMaintainId, bo.getSettlementMaintainId());
        lqw.eq(bo.getRepairFormId() != null, ErpSettlementMaintainRepairForm::getRepairFormId, bo.getRepairFormId());
        return lqw;
    }

    /**
     * 新增结算单与报修单关联
     */
    @Override
    public Boolean insertByBo(ErpSettlementMaintainRepairFormBo bo) {
        ErpSettlementMaintainRepairForm add = BeanUtil.toBean(bo, ErpSettlementMaintainRepairForm.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insertBatch(List<ErpSettlementMaintainRepairFormBo> bo) {
        bo.forEach(this::insertByBo);
        List<ErpSettlementMaintainRepairForm> add = BeanUtil.copyToList(bo, ErpSettlementMaintainRepairForm.class);
        return baseMapper.insertBatch(add);
    }

    /**
     * 修改结算单与报修单关联
     */
    @Override
    public Boolean updateByBo(ErpSettlementMaintainRepairFormBo bo) {
        ErpSettlementMaintainRepairForm update = BeanUtil.toBean(bo, ErpSettlementMaintainRepairForm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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

    @Override
    public Boolean deleteBySettlementMaintainIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.delete(new LambdaQueryWrapper<ErpSettlementMaintainRepairForm>()
            .in(ErpSettlementMaintainRepairForm::getSettlementMaintainId,ids)) > 0;
    }

    @Override
    public Boolean deleteByRepairFormIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.delete(new LambdaQueryWrapper<ErpSettlementMaintainRepairForm>()
            .in(ErpSettlementMaintainRepairForm::getRepairFormId,ids)) > 0;
    }
}
