package com.hotwj.platform.config.companyDeptConfig.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.config.companyDeptConfig.domain.HotCompanyDeptConfig;
import com.hotwj.platform.config.companyDeptConfig.domain.bo.HotCompanyDeptConfigBo;
import com.hotwj.platform.config.companyDeptConfig.domain.vo.HotCompanyDeptConfigVo;
import com.hotwj.platform.config.companyDeptConfig.mapper.HotCompanyDeptConfigMapper;
import com.hotwj.platform.config.companyDeptConfig.service.IHotCompanyDeptConfigService;
import com.hotwj.platform.resourceManagement.companySafetyManager.domain.HotCompanySafetyManager;
import com.hotwj.platform.resourceManagement.companySafetyManager.mapper.HotCompanySafetyManagerMapper;
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-29
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotCompanyDeptConfigServiceImpl implements IHotCompanyDeptConfigService {

    private final HotCompanyDeptConfigMapper baseMapper;
    private final HotCompanySafetyManagerMapper safetyManagerMapper;

    /**
     * 查询企业部门配置
     *
     * @param id 主键
     * @return 企业部门配置
     */
    @Override
    public HotCompanyDeptConfigVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询企业部门配置列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 企业部门配置分页列表
     */
    @Override
    public TableDataInfo<HotCompanyDeptConfigVo> queryPageList(HotCompanyDeptConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotCompanyDeptConfig> lqw = buildQueryWrapper(bo);
        Page<HotCompanyDeptConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的企业部门配置列表
     *
     * @param bo 查询条件
     * @return 企业部门配置列表
     */
    @Override
    public List<HotCompanyDeptConfigVo> queryList(HotCompanyDeptConfigBo bo) {
        LambdaQueryWrapper<HotCompanyDeptConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotCompanyDeptConfig> buildQueryWrapper(HotCompanyDeptConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotCompanyDeptConfig> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotCompanyDeptConfig::getId);
        lqw.eq(bo.getCompanyId() != null, HotCompanyDeptConfig::getCompanyId, bo.getCompanyId());
        lqw.like(StringUtils.isNotBlank(bo.getDeptName()), HotCompanyDeptConfig::getDeptName, bo.getDeptName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeptCode()), HotCompanyDeptConfig::getDeptCode, bo.getDeptCode());
        lqw.eq(bo.getIsDeleted() != null, HotCompanyDeptConfig::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增企业部门配置
     *
     * @param bo 企业部门配置
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotCompanyDeptConfigBo bo) {
        HotCompanyDeptConfig add = MapstructUtils.convert(bo, HotCompanyDeptConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改企业部门配置
     *
     * @param bo 企业部门配置
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotCompanyDeptConfigBo bo) {
        HotCompanyDeptConfig update = MapstructUtils.convert(bo, HotCompanyDeptConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 校验并批量删除企业部门配置信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            Long count = safetyManagerMapper.selectCount(Wrappers.<HotCompanySafetyManager>lambdaQuery()
                    .in(HotCompanySafetyManager::getDeptId, ids)
                    .eq(HotCompanySafetyManager::getIsResigned, 0L));
            if (count > 0) {
                throw new ServiceException("该部门有未离职人员不可删除");
            }
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
