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

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.companyLaborContract.domain.HotCompanyLaborContract;
import com.hotwj.platform.resourceManagement.companyLaborContract.domain.bo.HotCompanyLaborContractBo;
import com.hotwj.platform.resourceManagement.companyLaborContract.domain.vo.HotCompanyLaborContractVo;
import com.hotwj.platform.resourceManagement.companyLaborContract.mapper.HotCompanyLaborContractMapper;
import com.hotwj.platform.resourceManagement.companyLaborContract.service.IHotCompanyLaborContractService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.exception.ServiceException;
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 HotCompanyLaborContractServiceImpl implements IHotCompanyLaborContractService {

    private final HotCompanyLaborContractMapper baseMapper;

    /**
     * 查询公司劳动合同
     *
     * @param id 主键
     * @return 公司劳动合同
     */
    @Override
    public HotCompanyLaborContractVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询公司劳动合同列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 公司劳动合同分页列表
     */
    @Override
    public TableDataInfo<HotCompanyLaborContractVo> queryPageList(HotCompanyLaborContractBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotCompanyLaborContract> lqw = buildQueryWrapper(bo);
        Page<HotCompanyLaborContractVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的公司劳动合同列表
     *
     * @param bo 查询条件
     * @return 公司劳动合同列表
     */
    @Override
    public List<HotCompanyLaborContractVo> queryList(HotCompanyLaborContractBo bo) {
        LambdaQueryWrapper<HotCompanyLaborContract> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotCompanyLaborContract> buildQueryWrapper(HotCompanyLaborContractBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotCompanyLaborContract> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotCompanyLaborContract::getId);
        lqw.eq(bo.getCompanyId() != null, HotCompanyLaborContract::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getEffectiveDate() != null, HotCompanyLaborContract::getEffectiveDate, bo.getEffectiveDate());
        lqw.eq(bo.getExpireDate() != null, HotCompanyLaborContract::getExpireDate, bo.getExpireDate());
        lqw.eq(bo.getIsArchived() != null, HotCompanyLaborContract::getIsArchived, bo.getIsArchived());
        lqw.eq(StringUtils.isNotBlank(bo.getImageUrls()), HotCompanyLaborContract::getImageUrls, bo.getImageUrls());
        lqw.eq(bo.getIsDeleted() != null, HotCompanyLaborContract::getIsDeleted, bo.getIsDeleted());
        lqw.eq(bo.getSafeUserId() != null, HotCompanyLaborContract::getSafeUserId, bo.getSafeUserId());
        return lqw;
    }

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

    /**
     * 修改公司劳动合同
     *
     * @param bo 公司劳动合同
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotCompanyLaborContractBo bo) {
        // 检查是否已归档
        HotCompanyLaborContract old = baseMapper.selectById(bo.getId());
        if (old != null && Long.valueOf(1).equals(old.getIsArchived())) {
            throw new ServiceException("该劳动合同已归档，无法修改");
        }
        
        HotCompanyLaborContract update = MapstructUtils.convert(bo, HotCompanyLaborContract.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotCompanyLaborContract entity) {
        if (entity.getEffectiveDate() != null
                && entity.getExpireDate() != null
                && !entity.getExpireDate().after(entity.getEffectiveDate())) {
            throw new ServiceException("截至日期必须大于生效日期");
        }
    }

    /**
     * 校验并批量删除公司劳动合同信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // 检查是否已归档
            if (ids != null && !ids.isEmpty()) {
                List<HotCompanyLaborContract> list = baseMapper.selectBatchIds(ids);
                for (HotCompanyLaborContract contract : list) {
                    if (Long.valueOf(1).equals(contract.getIsArchived())) {
                        throw new ServiceException("存在已归档的劳动合同，无法删除");
                    }
                }
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
