package cn.iocoder.yudao.module.system.service.laborcontract;

import cn.hutool.core.collection.CollectionUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.system.controller.admin.laborcontract.vo.LaborContractPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.laborcontract.vo.LaborContractRespVO;
import cn.iocoder.yudao.module.system.controller.admin.laborcontract.vo.LaborContractSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.dept.DeptDO;
import cn.iocoder.yudao.module.system.dal.dataobject.laborcontract.LaborContractDO;
import cn.iocoder.yudao.module.system.dal.mysql.laborcontract.LaborContractMapper;
import cn.iocoder.yudao.module.system.dal.mysql.user.AdminUserMapper;
import cn.iocoder.yudao.module.system.service.dept.DeptService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.LABOR_CONTRACT_NOT_EXISTS;

/**
 * 劳动合同管理 Service 实现类
 *
 * @author 符溶馨
 */
@Service
@Validated
public class LaborContractServiceImpl implements LaborContractService {

    @Resource
    private LaborContractMapper laborContractMapper;

    @Resource
    private AdminUserMapper userMapper;

    @Resource
    private DeptService deptService;

    @Override
    public Long createLaborContract(LaborContractSaveReqVO createReqVO) {
        // 插入
        LaborContractDO laborContract = BeanUtils.toBean(createReqVO, LaborContractDO.class);
        // 设置到期时间
        laborContract.setExpirationDate(laborContract.getSigningDate().plusYears(laborContract.getContractDuration()));
        laborContract.setStatus(2);
        laborContractMapper.insert(laborContract);

        // 返回
        return laborContract.getId();
    }

    @Override
    public void updateLaborContract(LaborContractSaveReqVO updateReqVO) {
        // 校验存在
        validateLaborContractExists(updateReqVO.getId());
        // 更新
        LaborContractDO updateObj = BeanUtils.toBean(updateReqVO, LaborContractDO.class);
        laborContractMapper.updateById(updateObj);
    }

    @Override
    public void updateLaborContractList(List<Long> ids, Integer status) {

        List<LaborContractDO> updateVO = ids.stream()
                .map(id -> new LaborContractDO()
                        .setId(id)
                        .setStatus(status))
                .collect(Collectors.toList());

        laborContractMapper.updateBatch(updateVO);
    }

    @Override
    public void deleteLaborContract(Long id) {
        // 校验存在
        validateLaborContractExists(id);
        // 删除
        laborContractMapper.deleteById(id);
    }

    private void validateLaborContractExists(Long id) {
        if (laborContractMapper.selectById(id) == null) {
            throw exception(LABOR_CONTRACT_NOT_EXISTS);
        }
    }

    @Override
    public LaborContractDO getLaborContract(Long id) {
        return laborContractMapper.selectById(id);
    }

    @Override
    public PageResult<LaborContractRespVO> getLaborContractPage(LaborContractPageReqVO pageReqVO) {

        if (pageReqVO.getDeptId() != null) {

            // 获取所有子级部门
            List<DeptDO> deptDOS = deptService.getChildDept(pageReqVO.getDeptId());
            pageReqVO.setDeptIds(convertList(deptDOS, DeptDO::getId));
        }

        Page<LaborContractRespVO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<LaborContractRespVO> pageList = userMapper.selectContractPage(page, pageReqVO);

        return new PageResult<>(pageList.getRecords(), pageList.getTotal());
    }

    @Override
    public List<LaborContractDO> getHistoryList(Long userId) {

        return laborContractMapper.selectHistoryList(userId);
    }

    @Override
    public List<LaborContractDO> getListByUserIds(List<Long> userIds) {

        return laborContractMapper.selectList(LaborContractDO::getUserId, userIds);
    }

    @Override
    public List<LaborContractDO> getListByEndTime(LocalDate now) {

        return laborContractMapper.selectList(new LambdaQueryWrapperX<LaborContractDO>()
                .lt(LaborContractDO::getExpirationDate, now)
                .eq(LaborContractDO::getStatus, 3));
    }

    @Override
    public LaborContractDO getSigningDate(Long userId) {

        List<LaborContractDO> list = laborContractMapper.selectList(new LambdaQueryWrapperX<LaborContractDO>()
                .eq(LaborContractDO::getUserId, userId)
                .isNotNull(LaborContractDO::getProbationPeriodTime)
                .orderByDesc(LaborContractDO::getCreateTime));

        return CollectionUtil.isEmpty(list) ? null : list.get(0);
    }
}