package com.hotwj.platform.operationManagement.contractManage.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.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 lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.hotwj.platform.operationManagement.contractManage.domain.bo.HotContractManageBo;
import com.hotwj.platform.operationManagement.contractManage.domain.vo.HotContractManageVo;
import com.hotwj.platform.operationManagement.contractManage.domain.HotContractManage;
import com.hotwj.platform.operationManagement.contractManage.mapper.HotContractManageMapper;
import com.hotwj.platform.operationManagement.contractManage.service.IHotContractManageService;

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

/**
 * 合同管理Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotContractManageServiceImpl implements IHotContractManageService {

    private final HotContractManageMapper baseMapper;

    /**
     * 查询合同管理
     *
     * @param id 主键
     * @return 合同管理
     */
    @Override
    public HotContractManageVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询合同管理列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 合同管理分页列表
     */
    @Override
    public TableDataInfo<HotContractManageVo> queryPageList(HotContractManageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotContractManage> lqw = buildQueryWrapper(bo);
        Page<HotContractManageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的合同管理列表
     *
     * @param bo 查询条件
     * @return 合同管理列表
     */
    @Override
    public List<HotContractManageVo> queryList(HotContractManageBo bo) {
        LambdaQueryWrapper<HotContractManage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotContractManage> buildQueryWrapper(HotContractManageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotContractManage> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotContractManage::getId);
        lqw.eq(bo.getCompanyId() != null, HotContractManage::getCompanyId, bo.getCompanyId());
        lqw.eq(StringUtils.isNotBlank(bo.getContractNo()), HotContractManage::getContractNo, bo.getContractNo());
        lqw.like(StringUtils.isNotBlank(bo.getContractName()), HotContractManage::getContractName, bo.getContractName());
        lqw.eq(bo.getContractAmount() != null, HotContractManage::getContractAmount, bo.getContractAmount());
        lqw.like(StringUtils.isNotBlank(bo.getPartyAName()), HotContractManage::getPartyAName, bo.getPartyAName());
        lqw.like(StringUtils.isNotBlank(bo.getPartyBName()), HotContractManage::getPartyBName, bo.getPartyBName());
        lqw.eq(StringUtils.isNotBlank(bo.getPartyBOwner()), HotContractManage::getPartyBOwner, bo.getPartyBOwner());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), HotContractManage::getContactPhone, bo.getContactPhone());
        lqw.eq(bo.getContractSignTime() != null, HotContractManage::getContractSignTime, bo.getContractSignTime());
        lqw.eq(bo.getContractEffectiveTime() != null, HotContractManage::getContractEffectiveTime, bo.getContractEffectiveTime());
        lqw.eq(bo.getContractExpireTime() != null, HotContractManage::getContractExpireTime, bo.getContractExpireTime());
        lqw.eq(bo.getContractStatus() != null, HotContractManage::getContractStatus, bo.getContractStatus());
        lqw.eq(bo.getSignChannel() != null, HotContractManage::getSignChannel, bo.getSignChannel());
        lqw.eq(StringUtils.isNotBlank(bo.getAgreementUrl()), HotContractManage::getAgreementUrl, bo.getAgreementUrl());
        lqw.eq(bo.getSourceOrderId() != null, HotContractManage::getSourceOrderId, bo.getSourceOrderId());
        lqw.eq(StringUtils.isNotBlank(bo.getSourceOrderNo()), HotContractManage::getSourceOrderNo, bo.getSourceOrderNo());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotContractManage::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotContractManage::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotContractManage::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

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

    /**
     * 修改合同管理
     *
     * @param bo 合同管理
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotContractManageBo bo) {
        HotContractManage update = MapstructUtils.convert(bo, HotContractManage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

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