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.ErpRepairFormClaimBo;
import com.maintain.business.domain.vo.ErpRepairFormClaimVo;
import com.maintain.business.domain.ErpRepairFormClaim;
import com.maintain.business.mapper.ErpRepairFormClaimMapper;
import com.maintain.business.service.IErpRepairFormClaimService;

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

/**
 * 报修单-理赔Service业务层处理
 *
 * @author liushuai
 * @date 2025-04-01
 */
@RequiredArgsConstructor
@Service
public class ErpRepairFormClaimServiceImpl implements IErpRepairFormClaimService {

    private final ErpRepairFormClaimMapper baseMapper;

    /**
     * 查询报修单-理赔
     */
    @Override
    public ErpRepairFormClaimVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询报修单-理赔列表
     */
    @Override
    public TableDataInfo<ErpRepairFormClaimVo> queryPageList(ErpRepairFormClaimBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpRepairFormClaim> lqw = buildQueryWrapper(bo);
        Page<ErpRepairFormClaimVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询报修单-理赔列表
     */
    @Override
    public List<ErpRepairFormClaimVo> queryList(ErpRepairFormClaimBo bo) {
        LambdaQueryWrapper<ErpRepairFormClaim> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpRepairFormClaim> buildQueryWrapper(ErpRepairFormClaimBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpRepairFormClaim> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRepairFormId() != null, ErpRepairFormClaim::getRepairFormId, bo.getRepairFormId());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceCompany()), ErpRepairFormClaim::getInsuranceCompany, bo.getInsuranceCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceType()), ErpRepairFormClaim::getInsuranceType, bo.getInsuranceType());
        lqw.eq(StringUtils.isNotBlank(bo.getInsuranceNumber()), ErpRepairFormClaim::getInsuranceNumber, bo.getInsuranceNumber());
        lqw.like(StringUtils.isNotBlank(bo.getClaimServerName()), ErpRepairFormClaim::getClaimServerName, bo.getClaimServerName());
        lqw.eq(StringUtils.isNotBlank(bo.getClaimServerPhone()), ErpRepairFormClaim::getClaimServerPhone, bo.getClaimServerPhone());
        lqw.eq(bo.getAssessMoney() != null, ErpRepairFormClaim::getAssessMoney, bo.getAssessMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getClaimMoney()), ErpRepairFormClaim::getClaimMoney, bo.getClaimMoney());
        lqw.eq(StringUtils.isNotBlank(bo.getCaseNumber()), ErpRepairFormClaim::getCaseNumber, bo.getCaseNumber());
        lqw.like(StringUtils.isNotBlank(bo.getInsurerName()), ErpRepairFormClaim::getInsurerName, bo.getInsurerName());
        lqw.eq(StringUtils.isNotBlank(bo.getCarOwner()), ErpRepairFormClaim::getCarOwner, bo.getCarOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getCertificateOfLiability()), ErpRepairFormClaim::getCertificateOfLiability, bo.getCertificateOfLiability());
        lqw.eq(StringUtils.isNotBlank(bo.getAccidentDescribe()), ErpRepairFormClaim::getAccidentDescribe, bo.getAccidentDescribe());
        return lqw;
    }

    /**
     * 新增报修单-理赔
     */
    @Override
    public Boolean insertByBo(ErpRepairFormClaimBo bo) {
        ErpRepairFormClaim add = BeanUtil.toBean(bo, ErpRepairFormClaim.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改报修单-理赔
     */
    @Override
    public Boolean updateByBo(ErpRepairFormClaimBo bo) {
        ErpRepairFormClaim update = BeanUtil.toBean(bo, ErpRepairFormClaim.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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