package com.pusong.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.pusong.business.domain.PsContractBusinessDetail;
import com.pusong.business.mapper.PsContractBusinessDetailMapper;
import com.pusong.common.core.utils.MapstructUtils;
import com.pusong.common.core.utils.StringUtils;
import com.pusong.common.mybatis.core.page.TableDataInfo;
import com.pusong.common.mybatis.core.page.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.pusong.business.domain.bo.PsContractBusinessBo;
import com.pusong.business.domain.vo.PsContractBusinessVo;
import com.pusong.business.domain.PsContractBusiness;
import com.pusong.business.mapper.PsContractBusinessMapper;
import com.pusong.business.service.IPsContractBusinessService;

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

/**
 * 合同服务类别Service业务层处理
 *
 * @author wls
 * @date 2024-07-29
 */
@RequiredArgsConstructor
@Service
public class PsContractBusinessServiceImpl implements IPsContractBusinessService {

    private final PsContractBusinessMapper baseMapper;
    private final PsContractBusinessDetailMapper businessDetailMapper;
    /**
     * 新增合同服务类别
     *
     * @param boList 合同服务类别集合
     * @param contractCode 合同编号
     * @return 是否新增成功
     */
    @Override
    public void saveList(List<PsContractBusinessBo> boList, String contractCode) {
        //1.删除所有服务类型和服务项目
        baseMapper.delete(Wrappers.<PsContractBusiness>lambdaQuery()
                .eq(PsContractBusiness::getContractCode,contractCode));
        businessDetailMapper.delete(Wrappers.<PsContractBusinessDetail>lambdaQuery()
                .eq(PsContractBusinessDetail::getContractCode,contractCode));
        //2.插入服务类型
        for (PsContractBusinessBo businessBo : boList) {
            PsContractBusiness businessAdd = MapstructUtils.convert(businessBo, PsContractBusiness.class);
            businessAdd.setContractCode(contractCode);
            baseMapper.insert(businessAdd);
            if (CollectionUtils.isEmpty(businessBo.getDetailBos())) {
                continue;
            }
            //6.插入服务项目
            List<PsContractBusinessDetail> details = MapstructUtils.convert(businessBo.getDetailBos(), PsContractBusinessDetail.class);
            details.forEach(item -> {
                item.setContractCode(contractCode);
                item.setBusinessId(businessAdd.getId());
            });
            businessDetailMapper.insertBatch(details);
        }
    }

    /**
     * 查询符合条件的合同服务类别列表
     *
     * @param bo 查询条件
     * @return 合同服务类别列表
     */
    @Override
    public List<PsContractBusinessVo> queryList(PsContractBusinessBo bo) {
        LambdaQueryWrapper<PsContractBusiness> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PsContractBusiness> buildQueryWrapper(PsContractBusinessBo bo) {
        LambdaQueryWrapper<PsContractBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessType()), PsContractBusiness::getBusinessType, bo.getBusinessType());
        lqw.eq( PsContractBusiness::getDelFlag, "0");
        return lqw;
    }

    public List<PsContractBusinessVo> selectBusinessList(List<String> contractCode){
        return this.selectBusinessList(contractCode, null);
    }
    /**
     * 根据合同编码查询服务信息
     * @param contractCode
     * @return
     */
    public List<PsContractBusinessVo> selectBusinessList(List<String> contractCode, Long serviceCompanyId) {
        if(CollectionUtils.isEmpty(contractCode)){return new ArrayList<>();}
       return baseMapper.selectBusinessList(contractCode, serviceCompanyId);
    }


  /*  *//**
     * 查询合同服务类别
     *
     * @param id 主键
     * @return 合同服务类别
     *//*
    @Override
    public PsContractBusinessVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    *//**
     * 分页查询合同服务类别列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 合同服务类别分页列表
     *//*
    @Override
    public TableDataInfo<PsContractBusinessVo> queryPageList(PsContractBusinessBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PsContractBusiness> lqw = buildQueryWrapper(bo);
        Page<PsContractBusinessVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    *//**
     * 查询符合条件的合同服务类别列表
     *
     * @param bo 查询条件
     * @return 合同服务类别列表
     *//*
    @Override
    public List<PsContractBusinessVo> queryList(PsContractBusinessBo bo) {
        LambdaQueryWrapper<PsContractBusiness> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PsContractBusiness> buildQueryWrapper(PsContractBusinessBo bo) {
        LambdaQueryWrapper<PsContractBusiness> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getContractCode() != null, PsContractBusiness::getContractCode, bo.getContractCode());
        lqw.eq(StringUtils.isNotBlank(bo.getBusinessType()), PsContractBusiness::getBusinessType, bo.getBusinessType());
        lqw.eq(bo.getBusinessAmount() != null, PsContractBusiness::getBusinessAmount, bo.getBusinessAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getIsOld()), PsContractBusiness::getIsOld, bo.getIsOld());
        lqw.eq(StringUtils.isNotBlank(bo.getIsDue()), PsContractBusiness::getIsDue, bo.getIsDue());
        lqw.eq( PsContractBusiness::getDelFlag, "0");
        return lqw;
    }

    *//**
     * 新增合同服务类别
     *
     * @param bo 合同服务类别
     * @return 是否新增成功
     *//*
    @Override
    public Boolean insertByBo(PsContractBusinessBo bo) {
        PsContractBusiness add = MapstructUtils.convert(bo, PsContractBusiness.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    *//**
     * 修改合同服务类别
     *
     * @param bo 合同服务类别
     * @return 是否修改成功
     *//*
    @Override
    public Boolean updateByBo(PsContractBusinessBo bo) {
        PsContractBusiness update = MapstructUtils.convert(bo, PsContractBusiness.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    *//**
     * 校验并批量删除合同服务类别信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     *//*
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }*/
}
