KpiStarSpecialRuleBizService.java 19.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
package cn.fw.morax.service.biz.kpi;

import cn.fw.common.exception.BusinessException;
import cn.fw.common.web.annotation.DisLock;
import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.morax.common.constant.Constant;
import cn.fw.morax.common.pojo.event.ApprovalResultEvent;
import cn.fw.morax.common.pojo.event.KpiStarAdjustmentEvent;
import cn.fw.morax.common.utils.EventBusUtil;
import cn.fw.morax.common.utils.PublicUtil;
import cn.fw.morax.domain.db.ApprovalRecord;
import cn.fw.morax.domain.db.kpi.KpiPool;
import cn.fw.morax.domain.db.kpi.KpiStarSpecialRule;
import cn.fw.morax.domain.db.salary.SalaryClosure;
import cn.fw.morax.domain.dto.kpi.KpiStarSpecialRuleDTO;
import cn.fw.morax.domain.dto.query.SalaryGroupStaffQueryDTO;
import cn.fw.morax.domain.enums.SettingStatusEnum;
import cn.fw.morax.domain.enums.StarLevelEnum;
import cn.fw.morax.domain.vo.kpi.KpiStarSpecialRuleVO;
import cn.fw.morax.domain.vo.salary.StaffSelectVO;
import cn.fw.morax.rpc.ehr.EhrRpcService;
import cn.fw.morax.rpc.ehr.dto.PerformanceStaffDTO;
import cn.fw.morax.rpc.ehr.dto.StaffBaseInfoDTO;
import cn.fw.morax.service.biz.ApprovalBizService;
import cn.fw.morax.service.biz.salary.SalaryGeneralSettinBizService;
import cn.fw.morax.service.biz.salary.SalarySettingCommonService;
import cn.fw.morax.service.data.ApprovalRecordService;
import cn.fw.morax.service.data.kpi.KpiPoolService;
import cn.fw.morax.service.data.kpi.KpiStarSpecialRuleService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;

import static cn.fw.common.businessvalidator.Validator.BV;

@RequiredArgsConstructor
@Service
@Slf4j
public class KpiStarSpecialRuleBizService {

    private final SalarySettingCommonService salarySettingCommonService;
    private final SalaryGeneralSettinBizService salaryGeneralSettinBizService;
    private final KpiStarSpecialRuleService kpiStarSpecialRuleService;
    private final ApprovalRecordService approvalRecordService;
    private final ApprovalBizService approvalBizService;
    private final KpiPoolService kpiPoolService;
    private final KpiPoolBizService kpiPoolBizService;
    private final EhrRpcService ehrRpcService;

    @Value("${spring.cache.custom.global-prefix}:kpi:star:adjustment:")
    @Getter
    private String savePrefix;

    /**
     * 申请绩效星级特殊调整
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    @DisLock(prefix = "#this.getSavePrefix()", key = "#dto.getUserId()", message = "保存中,请勿重复操作")
    public void applyKpiStarAdjustment(KpiStarSpecialRuleDTO dto, LoginAuthBean user) {
        Date expTime = this.checkAppealDate(dto.getUserId());
        this.checkAdjustment(dto.getUserId());
        KpiStarSpecialRule specialRule = this.convertToPo(dto);
        Boolean result = kpiStarSpecialRuleService.save(specialRule);
        BV.isTrue(result, Constant.SAVE_FAIL_RETRY_PROMPT);
        approvalBizService.applyKpiStarAdjustment(specialRule, user, expTime);
    }

    /**
     * 查询薪酬系统配置
     *
     * @param
     * @return
     */
    public Date checkAppealDate(Long userId) {
        StaffBaseInfoDTO staffBaseInfo = ehrRpcService.queryStaffBaseInfo(userId);
        BV.notNull(staffBaseInfo, "员工信息为空");
        SalaryClosure salaryClosure = salarySettingCommonService.queryShopClosure(staffBaseInfo.getShopId(), YearMonth.now().minusMonths(1));
        if (PublicUtil.isEmpty(salaryClosure)) {
            throw new BusinessException("上月还未关单,不能申请");
        }

        LocalDateTime expTime = salaryClosure.getApproveEndTime().atTime(23,59,59);
        return Date.from(expTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 检查是否有审批中的星级特殊调整
     *
     * @param userId
     */
    public void checkAdjustment(Long userId) {
        Integer approvingRecords = kpiStarSpecialRuleService.count(Wrappers.<KpiStarSpecialRule>lambdaQuery()
                .eq(KpiStarSpecialRule::getStatus, SettingStatusEnum.APPROVING)
                .eq(KpiStarSpecialRule::getUserId, userId)
                .eq(KpiStarSpecialRule::getYn, Boolean.TRUE)
        );
        BV.isFalse(approvingRecords > 0, "已有申请中的星级考核调整");
    }

    /**
     * 检查并转换dto
     *
     * @param dto
     * @return
     */
    private KpiStarSpecialRule convertToPo(KpiStarSpecialRuleDTO dto) {
        Long groupId = dto.getGroupId();

        KpiStarSpecialRule kpiStarSpecialRule = new KpiStarSpecialRule();
        kpiStarSpecialRule.setUserId(dto.getUserId());
        kpiStarSpecialRule.setUserName(dto.getUserName());
        //几月开始绩效考核
        if (PublicUtil.isNotEmpty(dto.getStartingKpiEnum())) {
            switch (dto.getStartingKpiEnum()) {
                case CURRENT_MONTH: {
                    kpiStarSpecialRule.setEarliestStartingMonthly(PublicUtil.getCurMonthFirstDay());
                    break;
                }
                case NEXT_MONTH: {
                    kpiStarSpecialRule.setEarliestStartingMonthly(PublicUtil.getNextMonthFirstDay());
                    break;
                }
            }
        }

        //未核发工资月度星级调整
        if (PublicUtil.isNotEmpty(dto.getRevisedMonthly())) {
            kpiStarSpecialRule.setRevisedMonthly(dto.getRevisedMonthly().atDay(1));
            kpiStarSpecialRule.setRevisedLevel(dto.getRevisedLevel());
        }

        //上个月绩效考核不算,按C级计算工资
        if (PublicUtil.isNotEmpty(dto.getExclusionLastMonth()) && dto.getExclusionLastMonth()) {
            kpiStarSpecialRule.setExclusionMonthly(PublicUtil.getCurMonthEndDay().plusMonths(-1));
        }

        //免除D级次数
        if (PublicUtil.isNotEmpty(dto.getRevokedTimes())) {
            Boolean revokedCorrect = dto.getRevokedTimes() >= 1 && dto.getRevokedTimes() <= 3;
            BV.isTrue(revokedCorrect, "免除D级次数只能在1-3次之间");
            kpiStarSpecialRule.setRevokedTimes(dto.getRevokedTimes());
        }
        kpiStarSpecialRule.setReason(dto.getReason());
        kpiStarSpecialRule.setAttachment(dto.getAttachment());
        kpiStarSpecialRule.setGroupId(groupId);
        kpiStarSpecialRule.setStatus(SettingStatusEnum.APPROVING);
        return kpiStarSpecialRule;
    }

    /**
     * 绩效星级考核调整审批
     *
     * @param approvalRecord
     * @param resultEvent
     */
    @Transactional(rollbackFor = Exception.class)
    public void approvalKpiStarAdjustment(ApprovalRecord approvalRecord, ApprovalResultEvent resultEvent) {
        log.info("收到绩效星级考核调整审批信息:{}", JSON.toJSONString(resultEvent));
        boolean agree = Boolean.TRUE.equals(resultEvent.getAgree());
        KpiStarSpecialRule specialRule = kpiStarSpecialRuleService.getById(approvalRecord.getDataId());
        if (Objects.isNull(specialRule)) {
            return;
        }
        if (!agree) {
            specialRule.setStatus(SettingStatusEnum.INEFFECTIVE);
        } else {
            specialRule.setStatus(SettingStatusEnum.EFFECTIVE);
        }
        kpiStarSpecialRuleService.updateById(specialRule);
        this.adjustmentKpiData(specialRule);
    }

    /**
     * 调整绩效星级
     * 1. 开始绩效考核月度
     * 2. 未核发工资月度星级调整
     * 3. 上个月绩效考核不算,按C级计算工资
     * 4. 免除D级次数
     *
     * @param specialRule
     */
    @Transactional(rollbackFor = Exception.class)
    public void adjustmentKpiData(KpiStarSpecialRule specialRule) {
        if (!SettingStatusEnum.EFFECTIVE.equals(specialRule.getStatus())) {
            return;
        }
        Long userId = specialRule.getUserId();
        //未设置开始绩效考核月度
        Boolean noSetKpiStartMonthRule = true;
        //开始考核日期
        if (PublicUtil.isNotEmpty(specialRule.getEarliestStartingMonthly())) {
            noSetKpiStartMonthRule = false;
            this.resetKpiDate(userId, YearMonth.from(specialRule.getEarliestStartingMonthly()));
        }
        //修正未核发工资月度星级
        if (PublicUtil.isNotEmpty(specialRule.getRevisedMonthly())) {
            this.starAdjustmentEvent(specialRule.getRevisedLevel(), userId, YearMonth.from(specialRule.getRevisedMonthly()));
        }
        //哪个月绩效考核不算,按C级计算工资(只能算上月)
        if (PublicUtil.isNotEmpty(specialRule.getExclusionMonthly())) {
            this.starAdjustmentEvent(StarLevelEnum.C, userId, YearMonth.from(specialRule.getExclusionMonthly()));
        }
        //免除D级次数
        if (PublicUtil.isNotEmpty(specialRule.getRevokedTimes())) {
            this.revokedKpiTimes(specialRule);
        }

        //没有开始绩效考核月度设置,就将绩效星级规则状态设置为失效
        if (noSetKpiStartMonthRule) {
            kpiStarSpecialRuleService.update(Wrappers.<KpiStarSpecialRule>lambdaUpdate()
                    .set(KpiStarSpecialRule::getStatus, SettingStatusEnum.INEFFECTIVE)
                    .eq(KpiStarSpecialRule::getId, specialRule.getId())
            );
        }
    }

    /**
     * 失效旧绩效星级特殊规则(开始绩效考核月度设置 使用)
     *
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public void inEffectiveOldSpecialRule(Long userId, Long specialRuleId) {
        List<KpiStarSpecialRule> specialRules = kpiStarSpecialRuleService.list(Wrappers.<KpiStarSpecialRule>lambdaQuery()
                .eq(KpiStarSpecialRule::getStatus, SettingStatusEnum.EFFECTIVE)
                .eq(KpiStarSpecialRule::getUserId, userId)
                .eq(KpiStarSpecialRule::getYn, Boolean.TRUE)
                .ne(KpiStarSpecialRule::getId, specialRuleId)
        );
        if (PublicUtil.isEmpty(specialRules)) {
            return;
        }
        List<Long> inEffectiveRulesIds = Lists.newArrayList();
        for (KpiStarSpecialRule rule : specialRules) {
            if (PublicUtil.isNotEmpty(rule.getEarliestStartingMonthly())) {
                inEffectiveRulesIds.add(rule.getId());
            }
        }
        if (PublicUtil.isNotEmpty(inEffectiveRulesIds)) {
            kpiStarSpecialRuleService.update(Wrappers.<KpiStarSpecialRule>lambdaUpdate()
                    .set(KpiStarSpecialRule::getStatus, SettingStatusEnum.INEFFECTIVE)
                    .in(KpiStarSpecialRule::getId, inEffectiveRulesIds)
            );
        }
    }


    /**
     * 免除D级次数(最近一年之内)
     *
     * @param specialRule
     */
    @Transactional(rollbackFor = Exception.class)
    public void revokedKpiTimes(KpiStarSpecialRule specialRule) {
        Long userId = specialRule.getUserId();
        int revokedTimes = specialRule.getRevokedTimes();

        YearMonth yearMonth = YearMonth.now().minusMonths(1L);
        List<KpiPool> list = kpiPoolService.list(Wrappers.<KpiPool>lambdaQuery()
                .eq(KpiPool::getGroupId, specialRule.getGroupId())
                .eq(KpiPool::getUserId, userId)
                .eq(KpiPool::getInclusion, Boolean.TRUE)
                .eq(KpiPool::getYn, Boolean.TRUE)
                .le(KpiPool::getMonthly, yearMonth)
                .orderByDesc(KpiPool::getMonthly)
                .last(" limit 12 ")
        );

        for (KpiPool kpiPool : list) {
            if (StarLevelEnum.D.equals(kpiPool.getStarLevel()) && revokedTimes > 0) {
                kpiPool.setRevoked(Boolean.TRUE);
                revokedTimes--;
            }
        }
        kpiPoolService.updateBatchById(list);
    }

    /**
     * 绩效星级特殊规则详情
     *
     * @param kpiStarSpecialRuleId
     * @return
     */
    public KpiStarSpecialRuleVO getKpiStarSpecialRuleDetail(Long kpiStarSpecialRuleId) {
        KpiStarSpecialRule kpiStarSpecialRule = kpiStarSpecialRuleService.getById(kpiStarSpecialRuleId);
        BV.notNull(kpiStarSpecialRule, "绩效星级特殊规则不存在");
        KpiStarSpecialRuleVO kpiStarSpecialRuleVo = PublicUtil.copy(kpiStarSpecialRule, KpiStarSpecialRuleVO.class);
        KpiPool kpiPool = kpiPoolService.getOne(Wrappers.<KpiPool>lambdaUpdate()
                        .eq(KpiPool::getUserId, kpiStarSpecialRule.getUserId())
                        .eq(KpiPool::getYn, Boolean.TRUE)
                        .orderByDesc(KpiPool::getId)
                        .last("limit 1")
//                .ge(KpiPool::getMonthly, kpiStarSpecialRule.getRevisedMonthly())
        );
        kpiStarSpecialRuleVo.setRevisedMonthLevel(kpiPool.getStarLevel());
        return kpiStarSpecialRuleVo;
    }

    /**
     * 绩效星级特殊调整人员
     *
     * @param dto
     * @return
     */
    public List<StaffSelectVO> queryStarAdjustmentStaffs(SalaryGroupStaffQueryDTO dto) {
        List<PerformanceStaffDTO> staffs = ehrRpcService.queryKpiStaffByName(dto.getPostId(), dto.getShopIds(), dto.getStaffName());
        LocalDate lastMonth = LocalDate.now().plusMonths(-1);
        List<KpiPool> poolList = kpiPoolService.list(Wrappers.<KpiPool>lambdaQuery()
                .eq(KpiPool::getPostId, dto.getPostId())
                .in(KpiPool::getShopId, dto.getShopIds())
                .eq(KpiPool::getMonthly, lastMonth)
                .like(PublicUtil.isNotEmpty(dto.getStaffName()), KpiPool::getUserName, dto.getStaffName())
                .eq(KpiPool::getYn, Boolean.TRUE)
        );
        Set<Long> staffIds = Sets.newHashSetWithExpectedSize(staffs.size() + poolList.size());
        List<StaffSelectVO> selectVOS = staffs.stream().map(staffDTO -> {
            StaffSelectVO staffSelectVO = new StaffSelectVO();
            staffIds.add(staffDTO.getId());
            staffSelectVO.setId(staffDTO.getId());
            staffSelectVO.setName(staffDTO.getName());
            return staffSelectVO;
        }).collect(Collectors.toList());

        poolList.forEach(pool -> {
            if (staffIds.contains(pool.getUserId())) {
                return;
            }
            StaffSelectVO staffSelectVO = new StaffSelectVO();
            staffIds.add(pool.getUserId());
            staffSelectVO.setId(pool.getUserId());
            staffSelectVO.setName(pool.getUserName());
            selectVOS.add(staffSelectVO);
        });
        return selectVOS;
    }

    /**
     * 绩效星级特殊调整人员
     *
     * @param user
     * @return
     */
    public List<StaffSelectVO> queryLastMonthManageStaffs(LoginAuthBean user) {
        Long userId = user.getUserId();
        this.checkAppealDate(userId);
        List<StaffBaseInfoDTO> managerStaffs = ehrRpcService.queryManageStaffs(userId);
        if (PublicUtil.isEmpty(managerStaffs)) {
            managerStaffs = new ArrayList<>();
        }
        LocalDateTime lastMonth = LocalDateTime.now().minusMonths(1);
        Date firstDayOfLastMonth = Date.from(lastMonth.with(TemporalAdjusters.firstDayOfMonth()).atZone(ZoneId.systemDefault()).toInstant());
        Date endDayOfLastMonth = Date.from(lastMonth.with(TemporalAdjusters.lastDayOfMonth()).atZone(ZoneId.systemDefault()).toInstant());
        List<StaffBaseInfoDTO> leaveManagerStaffs = ehrRpcService.queryLeaveManageStaffs(userId, firstDayOfLastMonth, endDayOfLastMonth);
        if (PublicUtil.isNotEmpty(leaveManagerStaffs)) {
            managerStaffs.addAll(leaveManagerStaffs);
        }
        List<StaffSelectVO> selectVOS = managerStaffs.stream()
                .filter(staff -> (!staff.getId().equals(userId)))
                .map(staff -> new StaffSelectVO(staff.getId(), staff.getName())).collect(Collectors.toList());
        return selectVOS;
    }

    /**
     * 查询员工绩效星级
     *
     * @param userId
     * @return
     */
    public String getStaffKpiStar(Long userId, YearMonth yearMonth) {
        KpiPool pool = kpiPoolService.getOne(Wrappers.<KpiPool>lambdaQuery()
                .eq(KpiPool::getMonthly, yearMonth)
                .eq(KpiPool::getUserId, userId)
                .eq(KpiPool::getInclusion, Boolean.TRUE)
                .eq(KpiPool::getYn, Boolean.TRUE)
                .orderByAsc(KpiPool::getId)
                .last("limit 1")
        );
        if (PublicUtil.isEmpty(pool)) {
            return null;
        }
        //当实际星级 和  绩效星级不一致,可能绩效星级已经被调整,返回实际星级
        if (PublicUtil.isNotEmpty(pool.getActualStar()) && (!pool.getStarLevel().equals(pool.getActualStar()))) {
            return pool.getActualStar().getName();
        }
        return pool.getStarLevel().getName();
    }

    /**
     * 绩效星级特殊调整详情
     *
     * @param approvalNo
     */
    public KpiStarSpecialRuleVO getKpiStarAdjustment(String approvalNo) {
        ApprovalRecord approvalRecord = approvalRecordService.getOne(Wrappers.<ApprovalRecord>lambdaQuery()
                .eq(ApprovalRecord::getApprovalNo, approvalNo));
        BV.notNull(approvalRecord, "绩效星级特殊调整审批不存在");
        KpiStarSpecialRule specialRule = kpiStarSpecialRuleService.getById(approvalRecord.getDataId());
        BV.notNull(specialRule, "绩效星级特殊调整审批不存在");
        KpiStarSpecialRuleVO kpiStarSpecialRuleVO = PublicUtil.copy(specialRule, KpiStarSpecialRuleVO.class);
        if (PublicUtil.isNotEmpty(specialRule.getRevisedMonthly())) {
            KpiPool kpiPool = kpiPoolService.getOne(Wrappers.<KpiPool>lambdaUpdate()
                            .eq(KpiPool::getUserId, specialRule.getUserId())
                            .eq(KpiPool::getMonthly, YearMonth.from(specialRule.getRevisedMonthly()))
                            .eq(KpiPool::getYn, Boolean.TRUE)
                            .orderByDesc(KpiPool::getId)
                            .last("limit 1")
//                .ge(KpiPool::getMonthly, kpiStarSpecialRule.getRevisedMonthly())
            );
            if (PublicUtil.isNotEmpty(kpiPool)) {
                kpiStarSpecialRuleVO.setRevisedMonthLevel(kpiPool.getStarLevel());
            }
        }
        return kpiStarSpecialRuleVO;
    }

    /**
     * 修正未核发工资月度星级
     *
     * @param revisedLevel
     * @param userId
     * @param monthly
     */
    @Transactional(rollbackFor = Exception.class)
    public void starAdjustmentEvent(StarLevelEnum revisedLevel, Long userId, YearMonth monthly) {
        kpiPoolService.update(Wrappers.<KpiPool>lambdaUpdate()
                .set(KpiPool::getActualStar, revisedLevel)
                .eq(KpiPool::getUserId, userId)
                .eq(KpiPool::getMonthly, monthly)
        );
        EventBusUtil.asyncPost(KpiStarAdjustmentEvent.builder()
                .userId(userId)
                .yearMonth(monthly)
                .build());
    }

    /**
     * 几月开始考核,将几月之前绩效设置为不参与计算
     *
     * @param userId
     * @param monthly
     */
    @Transactional(rollbackFor = Exception.class)
    public void resetKpiDate(Long userId, YearMonth monthly) {
        kpiPoolService.update(Wrappers.<KpiPool>lambdaUpdate()
                .set(KpiPool::getInclusion, Boolean.FALSE)
                .eq(KpiPool::getUserId, userId)
                .lt(KpiPool::getMonthly, monthly)
                .eq(KpiPool::getYn, Boolean.TRUE)
        );
    }

}