package com.hotwj.platform.resourceManagement.companyInsurance.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotwj.platform.resourceManagement.companyInsurance.domain.HotCompanyInsurance;
import com.hotwj.platform.resourceManagement.companyInsurance.domain.bo.HotCompanyInsuranceBo;
import com.hotwj.platform.resourceManagement.companyInsurance.domain.vo.HotCompanyInsuranceVo;
import com.hotwj.platform.resourceManagement.companyInsurance.mapper.HotCompanyInsuranceMapper;
import com.hotwj.platform.resourceManagement.companyInsurance.service.IHotCompanyInsuranceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

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

/**
 * 公司企业保险信息Service业务层处理
 *
 * @author shihongwei
 * @date 2025-12-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotCompanyInsuranceServiceImpl implements IHotCompanyInsuranceService {

    private final HotCompanyInsuranceMapper baseMapper;

    /**
     * 查询公司企业保险信息
     *
     * @param id 主键
     * @return 公司企业保险信息
     */
    @Override
    public HotCompanyInsuranceVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询公司企业保险信息列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 公司企业保险信息分页列表
     */
    @Override
    public TableDataInfo<HotCompanyInsuranceVo> queryPageList(HotCompanyInsuranceBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotCompanyInsurance> lqw = buildQueryWrapper(bo);
        Page<HotCompanyInsuranceVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的公司企业保险信息列表
     *
     * @param bo 查询条件
     * @return 公司企业保险信息列表
     */
    @Override
    public List<HotCompanyInsuranceVo> queryList(HotCompanyInsuranceBo bo) {
        LambdaQueryWrapper<HotCompanyInsurance> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotCompanyInsurance> buildQueryWrapper(HotCompanyInsuranceBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotCompanyInsurance> lqw = Wrappers.lambdaQuery();
        Object beginEndDate = params == null ? null : params.get("beginEndDate");
        Object endEndDate = params == null ? null : params.get("endEndDate");
        lqw.orderByAsc(HotCompanyInsurance::getId);
        lqw.in(StringUtils.isNotBlank(bo.getIds()), HotCompanyInsurance::getId, StringUtils.splitTo(bo.getIds(), Convert::toLong));
        lqw.eq(bo.getCompanyId() != null, HotCompanyInsurance::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getInsuranceType()), HotCompanyInsurance::getInsuranceType, bo.getInsuranceType());
        lqw.like(StringUtils.isNotBlank(bo.getInsurerName()), HotCompanyInsurance::getInsurerName, bo.getInsurerName());
        lqw.eq(bo.getStartDate() != null, HotCompanyInsurance::getStartDate, bo.getStartDate());
        lqw.eq(bo.getEndDate() != null, HotCompanyInsurance::getEndDate, bo.getEndDate());
        lqw.between(beginEndDate != null && endEndDate != null, HotCompanyInsurance::getEndDate, beginEndDate, endEndDate);
        lqw.like(StringUtils.isNotBlank(bo.getChannelPerson()), HotCompanyInsurance::getChannelPerson, bo.getChannelPerson());
        lqw.like(StringUtils.isNotBlank(bo.getChannelPhone()), HotCompanyInsurance::getChannelPhone, bo.getChannelPhone());
        lqw.eq(bo.getPremiumAmount() != null, HotCompanyInsurance::getPremiumAmount, bo.getPremiumAmount());
        lqw.eq(StringUtils.isNotBlank(bo.getAnnexUrl()), HotCompanyInsurance::getAnnexUrl, bo.getAnnexUrl());
        lqw.eq(bo.getIsArchived() != null, HotCompanyInsurance::getIsArchived, bo.getIsArchived());
        lqw.eq(bo.getIsDeleted() != null, HotCompanyInsurance::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增公司企业保险信息
     *
     * @param bo 公司企业保险信息
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotCompanyInsuranceBo bo) {
        HotCompanyInsurance add = MapstructUtils.convert(bo, HotCompanyInsurance.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改公司企业保险信息
     *
     * @param bo 公司企业保险信息
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotCompanyInsuranceBo bo) {
        HotCompanyInsurance update = MapstructUtils.convert(bo, HotCompanyInsurance.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotCompanyInsurance entity) {
        // 校验日期：终保日期必须大于起保日期
        if (entity.getStartDate() != null && entity.getEndDate() != null) {
            if (!entity.getEndDate().after(entity.getStartDate())) {
                throw new IllegalArgumentException("终保日期必须大于起保日期");
            }
        }
    }

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