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

import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil;
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.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.StaffingPlanRecordPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.biz.vo.StaffingPlanRecordSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.biz.StaffingPlanRecordDO;
import cn.iocoder.yudao.module.system.dal.mysql.biz.StaffingPlanRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;

import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_EXIST_ERROR;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.COMMON_NOT_FOUND;

/**
 * 编制计划记录 Service 实现类
 */
@Service
@Validated
public class StaffingPlanRecordServiceImpl implements StaffingPlanRecordService {

    @Resource
    private StaffingPlanRecordMapper staffingPlanRecordMapper;

    @Override
    @Transactional
    public Long createStaffingPlanRecord(StaffingPlanRecordSaveReqVO createReqVO) {
        Long tenantId = createReqVO.getTenantId() != null ? createReqVO.getTenantId() : TenantContextHolder.getTenantId();
        Long overlap = staffingPlanRecordMapper.selectOverlapCount(createReqVO.getBeginYearMonth(), createReqVO.getEndYearMonth(), tenantId, null);
        if (overlap != null && overlap > 0) {
            throw ServiceExceptionUtil.exception(COMMON_EXIST_ERROR);
        }
        StaffingPlanRecordDO recordDO = BeanUtils.toBean(createReqVO, StaffingPlanRecordDO.class);
        recordDO.setTenantId(tenantId);
        staffingPlanRecordMapper.insert(recordDO);
        return recordDO.getId();
    }

    @Override
    @Transactional
    public void updateStaffingPlanRecord(StaffingPlanRecordSaveReqVO updateReqVO) {
        validateExists(updateReqVO.getId());
        Long tenantId = updateReqVO.getTenantId() != null ? updateReqVO.getTenantId() : TenantContextHolder.getTenantId();
        Long overlap = staffingPlanRecordMapper.selectOverlapCount(updateReqVO.getBeginYearMonth(), updateReqVO.getEndYearMonth(), tenantId, updateReqVO.getId());
        if (overlap != null && overlap > 0) {
            throw ServiceExceptionUtil.exception(COMMON_EXIST_ERROR);
        }
        StaffingPlanRecordDO recordDO = BeanUtils.toBean(updateReqVO, StaffingPlanRecordDO.class);
        recordDO.setTenantId(tenantId);
        staffingPlanRecordMapper.updateById(recordDO);
    }

    @Override
    public void deleteStaffingPlanRecord(Long id) {
        validateExists(id);
        staffingPlanRecordMapper.deleteById(id);
    }

    @Override
    public StaffingPlanRecordDO getStaffingPlanRecord(Long id) {
        return staffingPlanRecordMapper.selectById(id);
    }

    @Override
    public PageResult<StaffingPlanRecordDO> getStaffingPlanRecordPage(StaffingPlanRecordPageReqVO pageReqVO) {
        return staffingPlanRecordMapper.selectPage(pageReqVO);
    }

    private void validateExists(Long id) {
        if (id == null || staffingPlanRecordMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(COMMON_NOT_FOUND);
        }
    }
}

