SalaryPoolBizService.java 40 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 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
package cn.fw.morax.service.biz.salary;

import cn.fw.common.data.mybatis.pagination.PageData;
import cn.fw.common.page.AppPage;
import cn.fw.common.web.auth.LoginAuthBean;
import cn.fw.morax.common.constant.Constant;
import cn.fw.morax.common.utils.PublicUtil;
import cn.fw.morax.common.utils.StringUtils;
import cn.fw.morax.common.utils.ThreadPoolUtil;
import cn.fw.morax.domain.db.kpi.*;
import cn.fw.morax.domain.db.salary.*;
import cn.fw.morax.domain.dto.query.PaySalaryQueryDTO;
import cn.fw.morax.domain.dto.query.SalaryPoolQueryDTO;
import cn.fw.morax.domain.enums.*;
import cn.fw.morax.domain.vo.SelectorVO;
import cn.fw.morax.domain.vo.salary.*;
import cn.fw.morax.rpc.erp.ErpRpcService;
import cn.fw.morax.service.biz.CommonService;
import cn.fw.morax.service.data.kpi.IndicatorUserValueService;
import cn.fw.morax.service.data.salary.*;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.YearMonth;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.function.Function;
import java.util.stream.Collectors;

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

/**
 * @author : kurisu
 * @version : 1.0
 * @className : SalaryPoolBizService
 * @description : 薪酬池业务service
 * @date : 2022-05-05 11:10
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SalaryPoolBizService {
    private final SalaryGroupProjectSettinService salaryGroupProjectSettinService;
    private final SalaryGroupProjectService salaryGroupProjectService;
    private final SalaryGeneralSettinService salaryGeneralSettinService;
    private final SalaryGroupProjectHitLogService salaryGroupProjectHitLogService;
    private final SalaryPoolDetailService salaryPoolDetailService;
    private final SalaryGroupProjectPreconditionService salaryGroupProjectPreconditionService;
    private final SalaryGroupProjectParamService salaryGroupProjectParamService;
    private final SalaryGroupProjectTargetHitLogService salaryGroupProjectTargetHitLogService;
    private final IndicatorUserValueService indicatorUserValueService;
    private final SalaryGroupUserService salaryGroupUserService;
    private final SalaryPoolService salaryPoolService;
    private final CommonService commonService;
    private final ErpRpcService erpRpcService;

    @Value("${special-indicator-code.work-age}")
    @Getter
    private String workAgeCode;
    @Value("${special-indicator-code.base-salary}")
    @Getter
    private String baseSalaryCode;

    /**
     * 查询薪酬池
     *
     * @param queryDTO 查询条件
     * @return 薪酬池列表
     */
    public AppPage<SalaryPoolVO> queryPool(final SalaryPoolQueryDTO queryDTO) {
        SalaryGeneralSettin settin = salaryGeneralSettinService.getOne(Wrappers.<SalaryGeneralSettin>lambdaQuery()
                        .eq(SalaryGeneralSettin::getGroupId, queryDTO.getGroupId())
                        .eq(SalaryGeneralSettin::getYn, Boolean.TRUE)
                , Boolean.FALSE);
        if (Objects.nonNull(settin)) {
            Boolean leaderVisibility = settin.getLeaderVisibility();
            BV.isTrue(leaderVisibility, () -> "本集团不允许管理层查看员工薪资, 请联系集团管理员开通");
        }

        if (PublicUtil.isNotEmpty(queryDTO.getManagerUserId())) {
            queryDTO.setManagerStaffList(commonService.queryManageStaffIds(queryDTO.getManagerUserId(), queryDTO.getMonthly()));
        } else {
            queryDTO.setManagerStaffList(commonService.queryManageStaffIds(queryDTO.getCurrentUserId(), queryDTO.getMonthly()));
        }
        prepareParams(queryDTO);
        log.info("薪酬池查询参数:{}", JSON.toJSONString(queryDTO));

        AppPage<SalaryPoolVO> page = AppPage.empty(queryDTO);
        long total = salaryPoolService.queryPagePoolTotal(queryDTO);
        if (total > 0) {
            page.setTotal(total);
            List<SalaryPoolVO> salaryPoolList = salaryPoolService.queryPagePool(queryDTO);
            page.setData(salaryPoolList);
        }
        return page;
    }

    /**
     * 查询薪酬池详情
     *
     * @param poolId 薪酬池id
     * @return 薪酬池详情
     */
    public StaffSalaryVO queryPoolDetail(final Long poolId, final YearMonth yearMonth) {
        SalaryPool pool = salaryPoolService.getById(poolId);
        boolean flag = Objects.nonNull(pool) && Boolean.TRUE.equals(pool.getYn());
        BV.isTrue(flag, () -> "该人员薪酬数据不存在");
        if (PublicUtil.isNotEmpty(yearMonth) && (!yearMonth.equals(pool.getMonthly()))) {
            List<SalaryPool> list = userSalaryPool(pool.getUserId(), yearMonth);
            BV.isNotEmpty(list, () -> "人员薪酬数据不存在");
            pool = list.get(0);
        }
        return getPoolDetail(pool);
    }

    /**
     * 查询薪酬池详情
     *
     * @param userId    用户id
     * @param yearMonth 月度
     * @return 薪酬池详情
     */
    public StaffSalaryVO queryUserPoolDetail(final Long userId, final YearMonth yearMonth) {
        List<SalaryPool> list = userSalaryPool(userId, yearMonth);
        BV.isNotEmpty(list, () -> "人员薪酬数据不存在");
        SalaryPool salaryPool = list.get(0);
        return getPoolDetail(salaryPool);
    }

    /**
     * 查询薪酬池选项
     *
     * @param userId    用户id
     * @param yearMonth 月度
     * @return 薪酬池选项
     */
    public Set<SelectorVO> salaryPoolSelector(final Long userId, final YearMonth yearMonth) {
        List<SalaryPool> list = userSalaryPool(userId, yearMonth);
        if (CollectionUtils.isEmpty(list)) {
            return new HashSet<>();
        }
        return list.stream().map(r -> SelectorVO.whit(r.getId(), r.getShopName(), r.getPostName())).collect(Collectors.toSet());
    }

    /**
     * 查询用户月度薪酬池列表
     *
     * @param userId    用户id
     * @param yearMonth 月度
     * @return 返回结果
     */
    @Cacheable(cacheNames = "salary", key = "':user:pool:'+ #yearMonth.toString() +':'+ #userId", unless = "#result.size() < 1")
    public List<SalaryPool> userSalaryPool(final Long userId, final YearMonth yearMonth) {
        List<SalaryPool> list = salaryPoolService.list(Wrappers.<SalaryPool>lambdaQuery()
                .eq(SalaryPool::getUserId, userId)
                .eq(SalaryPool::getMonthly, yearMonth)
                .eq(SalaryPool::getYn, Boolean.TRUE)
                .orderByDesc(SalaryPool::getId)
        );
        if (CollectionUtils.isEmpty(list)) {
            return new ArrayList<>();
        }
        return list;
    }

    /**
     * 查询详情
     *
     * @param pool 薪酬池
     * @return 详情
     */
    private StaffSalaryVO getPoolDetail(SalaryPool pool) {
        StaffSalaryVO vo = this.convertToSalaryVO(pool);
        buildPoolData(pool, vo);
        LocalDate dataDate = LocalDate.from(vo.getEndTime());
        List<SalaryPoolDetailVO> salaryDetailList = salaryPoolDetailService.queryByPoolId(pool.getId(), pool.getSalaryGroupId(), dataDate);
        BV.isNotEmpty(salaryDetailList, () -> "该人员薪酬数据不存在");
        Map<SalaryTypeEnum, List<SalaryPoolDetailVO>> salaryTypeMap = salaryDetailList.stream().collect(Collectors.groupingBy(SalaryPoolDetailVO::getType));
        for (Map.Entry<SalaryTypeEnum, List<SalaryPoolDetailVO>> salaryDetail : salaryTypeMap.entrySet()) {
            detailClassification(vo, salaryDetail.getKey(), salaryDetail.getValue());
        }
        Collections.sort(vo.getIncome());
        Collections.sort(vo.getDeduct());
        return vo;
    }

    /**
     * 查询薪酬开始日期,结束日期
     *
     * @param salaryPool
     * @return
     */
    public void buildPoolData(SalaryPool salaryPool, StaffSalaryVO vo) {
        SalaryGroupUser salaryGroupUser = salaryGroupUserService.getMonthLastData(salaryPool.getSgc(),
                salaryPool.getUserId(), salaryPool.getMonthly());
        if (PublicUtil.isEmpty(salaryGroupUser)) {
            log.error("员工薪酬池:{},不存在绩效组人员数据", JSON.toJSONString(salaryPool));
            vo.setEndTime(PublicUtil.getYesterdayOrEndOfMonthDay(salaryPool.getMonthly()).atTime(23, 59, 59));
            vo.setStartTime(salaryPool.getMonthly().atDay(1).atTime(00, 00, 00));
            return;
        }
        vo.setEndTime(salaryGroupUser.getDataDate().atTime(23, 59, 59));
        SalaryGroupUser firstSalaryGroupUser = salaryGroupUserService.getMonthFirstData(salaryPool.getSgc(),
                salaryPool.getUserId(), salaryPool.getMonthly());
        vo.setStartTime(firstSalaryGroupUser.getDataDate().atTime(00, 00, 00));
    }

    /**
     * 查询薪酬结束日期
     *
     * @param salaryPool
     * @return
     */
    public LocalDate getPoolLastData(SalaryPool salaryPool) {
        SalaryGroupUser salaryGroupUser = salaryGroupUserService.getMonthLastData(salaryPool.getSgc(),
                salaryPool.getUserId(), salaryPool.getMonthly());
        if (PublicUtil.isEmpty(salaryGroupUser)) {
            log.error("员工薪酬池:{},不存在绩效组人员数据", JSON.toJSONString(salaryPool));
            return PublicUtil.getYesterdayOrEndOfMonthDay(salaryPool.getMonthly());
        }
        return salaryGroupUser.getDataDate();
    }

    /**
     * 详情分类
     *
     * @param vo
     * @param type
     * @param list
     */
    private void detailClassification(StaffSalaryVO vo, SalaryTypeEnum type, List<SalaryPoolDetailVO> list) {
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        summarySalaryRewards(vo, type, list);
        BigDecimal income = BigDecimal.ZERO;
        BigDecimal salaryTypeAmount = BigDecimal.ZERO;
        if (type.isPlus()) {
            List<IncomeProjectVO> incomes = Lists.newArrayList();
            for (SalaryPoolDetailVO detail : list) {
                income = income.add(detail.getActualSalaryAmount());
                salaryTypeAmount = salaryTypeAmount.add(detail.getSalaryAmount());
                IncomeProjectVO incomeProjectVO = this.convertToIncomeVO(detail);
                incomeProjectVO.setBaseWage(this.checkIsBaseWage(detail.getSalaryGroupProjectId()));
                incomes.add(incomeProjectVO);
            }
            vo.getIncome().add(new IncomeVO(type, salaryTypeAmount, incomes));
        } else {
            List<DeductProjectVO> deducts = Lists.newArrayList();
            for (SalaryPoolDetailVO detail : list) {
                salaryTypeAmount = salaryTypeAmount.add(detail.getSalaryAmount());
                deducts.add(this.convertToDeductVO(detail));
            }
            vo.getDeduct().add(new DeductVO(type, salaryTypeAmount, deducts));
        }
        vo.setTotalWages(vo.getTotalWages().add(income));
    }


    /**
     * 准备分页参数
     *
     * @param queryVO
     */
    private void prepareParams(SalaryPoolQueryDTO queryVO) {
        if (Objects.isNull(queryVO.getMonthly())) {
            queryVO.setMonthly(YearMonth.now());
        }
        if (Objects.nonNull(queryVO.getOrder()) && StringUtils.isValid(queryVO.getOrderAtt())) {
            StringBuilder sb = new StringBuilder(" order by ");
            sb.append(StringUtils.toColumnName(queryVO.getOrderAtt()).toLowerCase());
            if (queryVO.getOrder() == 1) {
                sb.append(" asc ");
            } else {
                sb.append(" desc ");
            }
            queryVO.setOrderString(sb.toString());
        }
        if (PublicUtil.isEmpty(queryVO.getQueryStaffs()) && PublicUtil.isNotEmpty(queryVO.getQueryStaffs())) {
            List<Long> queryStaffIds = Arrays.stream(queryVO.getQueryStaffs().split(",")).map(Long::parseLong).collect(Collectors.toList());
            queryVO.getManagerStaffList().retainAll(queryStaffIds);
        }
    }

    /**
     * 获取员工薪酬
     *
     * @param staffIds
     * @param shopIds
     * @param yearMonth
     * @return
     */
    public List<StaffSalaryDetailVO> getStaffsSalary(List<Long> staffIds, List<Long> shopIds, YearMonth yearMonth) {
        List<SalaryPool> pools = salaryPoolService.list(Wrappers.<SalaryPool>lambdaQuery()
                .in(PublicUtil.isNotEmpty(staffIds), SalaryPool::getUserId, staffIds)
                .in(PublicUtil.isNotEmpty(shopIds), SalaryPool::getShopId, shopIds)
                .eq(SalaryPool::getMonthly, yearMonth)
                .eq(SalaryPool::getYn, Boolean.TRUE)
        );
        if (PublicUtil.isEmpty(pools)) {
            return new ArrayList<>();
        }
        List<StaffSalaryDetailVO> staffSalaryVos = Lists.newArrayListWithCapacity(pools.size());
        List<Long> poolIds = pools.stream().map(SalaryPool::getId).collect(Collectors.toList());
        List<SalaryPoolDetail> poolDetails = salaryPoolDetailService.list(Wrappers.<SalaryPoolDetail>lambdaQuery()
                .in(SalaryPoolDetail::getSalaryPoolId, poolIds)
                .eq(SalaryPoolDetail::getYn, Boolean.TRUE)
        );
        Map<Long, List<SalaryPoolDetail>> poolDetailMap = poolDetails.stream().collect(Collectors.groupingBy(SalaryPoolDetail::getSalaryPoolId));
        StaffSalaryDetailVO staffSalaryVo = null;
        for (SalaryPool salaryPool : pools) {
            staffSalaryVo = this.convertVo(salaryPool, poolDetailMap);
            staffSalaryVos.add(staffSalaryVo);
        }
        return staffSalaryVos;
    }

    /**
     * 汇总数据
     *
     * @param vo
     * @param type
     * @param list
     */
    private void summarySalaryRewards(StaffSalaryVO vo, SalaryTypeEnum type, List<SalaryPoolDetailVO> list) {

        switch (type) {
            case NECESSARY: {
                BigDecimal socialSecurity = list.stream()
                        .map(SalaryPoolDetailVO::getActualSalaryAmount)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                vo.setSocialSecurity(socialSecurity);
                break;
            }
            case AWARD: {
                BigDecimal paidRewards = list.stream()
                        .map(SalaryPoolDetailVO::getProcessedAmount)
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                vo.setPaidRewards(paidRewards);
                break;
            }
            case PENALTY: {
                BigDecimal paidRewards = list.stream()
                        .map(detail -> {
                            return detail.getActualSalaryAmount().subtract(detail.getProcessedAmount());
                        })
                        .reduce(BigDecimal.ZERO, BigDecimal::add);
                vo.setNoPaidPunishRewards(paidRewards);
                break;
            }


        }
    }

    /**
     * 转换为收入对象
     *
     * @param detail
     * @return
     */
    private IncomeProjectVO convertToIncomeVO(SalaryPoolDetailVO detail) {
        IncomeProjectVO incomeProjectVO = new IncomeProjectVO();
        if (PublicUtil.isNotEmpty(detail.getSalaryGroupProjectId())) {
            incomeProjectVO.setSalaryGroupProjectId(detail.getSalaryGroupProjectId());
            incomeProjectVO.setSalaryGroupProjectName(detail.getSalaryGroupProjectName());
        }
        incomeProjectVO.setSalaryType(detail.getType());
        incomeProjectVO.setCalMethod(detail.getCalMethod());
        incomeProjectVO.setTotalAmount(detail.getSalaryAmount());
        incomeProjectVO.setSendAmount(detail.getProcessedAmount());
        incomeProjectVO.setNoSendAmount(detail.getActualSalaryAmount());
        return incomeProjectVO;
    }

    /**
     * 转换为支出对象
     *
     * @param detail
     * @return
     */
    private DeductProjectVO convertToDeductVO(SalaryPoolDetailVO detail) {
        DeductProjectVO deductProjectVO = new DeductProjectVO();
        if (PublicUtil.isNotEmpty(detail.getSalaryGroupProjectId())) {
            deductProjectVO.setSalaryGroupProjectId(detail.getSalaryGroupProjectId());
            deductProjectVO.setSalaryGroupProjectName(detail.getSalaryGroupProjectName());
        }
        deductProjectVO.setSalaryType(detail.getType());
        deductProjectVO.setCalMethod(detail.getCalMethod());
        deductProjectVO.setTotalAmount(detail.getSalaryAmount());
        deductProjectVO.setPaidAmount(detail.getProcessedAmount());
        deductProjectVO.setNoPaidAmount(detail.getActualSalaryAmount());
        return deductProjectVO;
    }

    /**
     * 转换为员工薪酬vo
     *
     * @param pool
     * @return
     */
    private StaffSalaryVO convertToSalaryVO(SalaryPool pool) {
        StaffSalaryVO vo = new StaffSalaryVO();
        vo.setSalaryPoolId(pool.getId());
        vo.setUserId(pool.getUserId());
        vo.setUserName(pool.getUserName());
        vo.setMonthly(pool.getMonthly());
        vo.setPostId(pool.getPostId());
        vo.setPostName(pool.getPostName());
        vo.setShopId(pool.getShopId());
        vo.setShopName(pool.getShopName());
        vo.setWages(pool.getReward());
        vo.setStarLevel(pool.getStarLevel());

        vo.setTotalWages(BigDecimal.ZERO);
        vo.setIncome(new LinkedList<>());
        vo.setDeduct(new LinkedList<>());
        return vo;
    }

    /**
     * 转换为vo
     *
     * @param salaryPool
     * @param poolDetailMap
     * @return
     */
    private StaffSalaryDetailVO convertVo(SalaryPool salaryPool, Map<Long, List<SalaryPoolDetail>> poolDetailMap) {
        Long userId = salaryPool.getUserId();
        Long poolId = salaryPool.getId();
        StaffSalaryDetailVO staffSalaryVo = new StaffSalaryDetailVO();
        staffSalaryVo.setId(salaryPool.getId());
        staffSalaryVo.setSalaryGroupId(salaryPool.getSalaryGroupId());
        staffSalaryVo.setUserId(userId);
        staffSalaryVo.setUserName(salaryPool.getUserName());
        staffSalaryVo.setPostId(salaryPool.getPostId());
        staffSalaryVo.setPostName(salaryPool.getPostName());
        staffSalaryVo.setShopId(salaryPool.getShopId());
        staffSalaryVo.setShopName(salaryPool.getShopName());
        staffSalaryVo.setStarLevel(salaryPool.getStarLevel());
        staffSalaryVo.setWages(salaryPool.getReward());
        staffSalaryVo.setMonthly(salaryPool.getMonthly());
        staffSalaryVo.setMonthlyStr(salaryPool.getMonthly().toString());

        staffSalaryVo.setAward(BigDecimal.ZERO);
        staffSalaryVo.setRoyalties(BigDecimal.ZERO);
        staffSalaryVo.setSubsidy(BigDecimal.ZERO);
        staffSalaryVo.setNecessary(BigDecimal.ZERO);
        staffSalaryVo.setPenalty(BigDecimal.ZERO);
        if (!poolDetailMap.containsKey(poolId)) {
            return staffSalaryVo;
        }
        List<SalaryPoolDetail> staffSalaryDetails = poolDetailMap.get(poolId);
        for (SalaryPoolDetail salaryDetail : staffSalaryDetails) {
            BigDecimal amount = salaryDetail.getSalaryAmount();
            switch (salaryDetail.getType()) {
                case AWARD: {
                    staffSalaryVo.setAward(staffSalaryVo.getAward().add(amount));
                    break;
                }
                case ROYALTIES: {
                    staffSalaryVo.setRoyalties(staffSalaryVo.getRoyalties().add(amount));
                    break;
                }
                case SUBSIDY: {
                    staffSalaryVo.setSubsidy(staffSalaryVo.getSubsidy().add(amount));
                    break;
                }
                case NECESSARY: {
                    staffSalaryVo.setNecessary(staffSalaryVo.getNecessary().add(amount));
                    break;
                }
                case PENALTY: {
                    staffSalaryVo.setPenalty(staffSalaryVo.getPenalty().add(amount));
                    break;
                }
            }
        }
        return staffSalaryVo;
    }

    /**
     * 下载员工excel文件
     *
     * @param currentUser
     * @param yearMonth
     * @param request
     * @param response
     */
    public void downloadStaffsSalary(LoginAuthBean currentUser, YearMonth yearMonth, HttpServletRequest request, HttpServletResponse response) {
        BV.isTrue(erpRpcService.hasRole(currentUser.getUserId(), Constant.ROLE_CODE_WAGE_PAYMENT), "员工没有工资发放角色");
        List<Long> shopIds = erpRpcService.queryUserRoleShopIds(currentUser.getUserId(), Constant.ROLE_CODE_WAGE_PAYMENT);
        List<StaffSalaryDetailVO> staffSalaryDetailVOS = this.getStaffsSalary(null, shopIds, yearMonth);
        String fileName = "工资清单.xlsx";
        String encodeFileName = CommonService.getEncodeName(request, fileName);
        CommonService.downloadExcel(response, staffSalaryDetailVOS, encodeFileName, StaffSalaryDetailVO.class);
    }

    /**
     * 查询员工工资
     *
     * @param dto
     * @return
     */
    public AppPage<StaffSalaryInfoVO> getStaffsWages(PaySalaryQueryDTO dto) {
        if (!erpRpcService.hasRole(dto.getUserId(), Constant.ROLE_CODE_WAGE_PAYMENT)) {
            return new AppPage<>();
        }
//        BV.isTrue(erpRpcService.hasRole(dto.getUserId(), Constant.ROLE_CODE_WAGE_PAYMENT), "员工没有工资发放角色");
        List<Long> shopIds = erpRpcService.queryUserRoleShopIds(dto.getUserId(), Constant.ROLE_CODE_WAGE_PAYMENT);
        if (PublicUtil.isEmpty(shopIds)) {
            return new AppPage<>();
        }

        PageData<SalaryPool> pageData = salaryPoolService.page(new PageData<>(dto.getCurrent(), dto.getPageSize()),
                Wrappers.<SalaryPool>lambdaQuery()
                        .in(PublicUtil.isNotEmpty(shopIds), SalaryPool::getShopId, shopIds)
                        .eq(SalaryPool::getMonthly, dto.getYearMonth())
                        .eq(SalaryPool::getYn, Boolean.TRUE)
        );
        return PublicUtil.toPage(pageData, salaryPool -> {
            StaffSalaryInfoVO salaryInfoVO = PublicUtil.copy(salaryPool, StaffSalaryInfoVO.class);
            return salaryInfoVO;
        });
    }

    /**
     * 绩效项目阶梯详情
     *
     * @param salaryPoolId
     * @param salaryGroupProjectId
     * @param yearMonth
     * @return
     */
    public SalaryGroupIndicatorDetailVO getHitProjectSetting(Long salaryPoolId, Long salaryGroupProjectId, YearMonth yearMonth) {
        SalaryPool pool = salaryPoolService.getById(salaryPoolId);
        BV.notNull(pool, "薪酬池不存在");
        Long userId = pool.getUserId();
        LocalDate localDate = getPoolLastData(pool);

        SalaryGroupIndicatorDetailVO detailVO = new SalaryGroupIndicatorDetailVO();
        SalaryGroupProject salaryGroupProject = salaryGroupProjectService.getById(salaryGroupProjectId);
        BV.notNull(salaryGroupProject, "薪酬项目不存在");
        if (PublicUtil.isNotEmpty(salaryGroupProject.getLaddersType())) {
            detailVO.setLaddersType(salaryGroupProject.getLaddersType());
        }


        CountDownLatch countDownLatch = new CountDownLatch(3);

        ThreadPoolExecutor threadPool = ThreadPoolUtil.getInstance().getThreadPool();

        threadPool.execute(() -> this.setSalaryGroupProjectParam(salaryGroupProjectId, userId, localDate, detailVO, countDownLatch));
        threadPool.execute(() -> this.setSalaryGroupProjectPreconditionHitVos(salaryGroupProjectId, userId, localDate, detailVO, countDownLatch));
        threadPool.execute(() -> this.setSalaryGroupProjectSetting(salaryGroupProject, userId, localDate, detailVO, countDownLatch));

        detailVO.setName(salaryGroupProject.getName());
        detailVO.setLadderParamAlias(salaryGroupProject.getLadderParamAlias());
        detailVO.setCommissionParamAlias(salaryGroupProject.getCommissionParamAlias());
        detailVO.setPreconditionAlias(salaryGroupProject.getPreconditionAlias());
        detailVO.setCalMethod(salaryGroupProject.getCalMethod());
        detailVO.setBeginDate(localDate.with(TemporalAdjusters.firstDayOfMonth()).atStartOfDay());
        detailVO.setEndDate(localDate.atTime(23, 59, 59));
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            log.error("绩效项目阶梯详情查询失败 -> salaryPoolId: [{}], salaryGroupProjectId: [{}], msg: [{}]",
                    salaryPoolId, salaryGroupProjectId, e);
        }
        return detailVO;
    }

    /**
     * 设置阶梯
     *
     * @param salaryGroupProject
     * @param userId
     * @param localDate
     * @param detailVO
     * @param countDownLatch
     */
    public void setSalaryGroupProjectSetting(SalaryGroupProject salaryGroupProject, Long userId, LocalDate localDate,
                                             SalaryGroupIndicatorDetailVO detailVO,
                                             CountDownLatch countDownLatch) {
        Long salaryGroupProjectId = salaryGroupProject.getId();
        List<SalaryGroupProjectSettin> settings = salaryGroupProjectSettinService.list(Wrappers.<SalaryGroupProjectSettin>lambdaQuery()
                .eq(SalaryGroupProjectSettin::getSalaryGroupProjectId, salaryGroupProjectId)
                .eq(SalaryGroupProjectSettin::getYn, Boolean.TRUE)
                .orderByAsc(SalaryGroupProjectSettin::getId)
        );
        List<SalaryGroupProjectHitLog> salaryProjectHitLogs = salaryGroupProjectHitLogService.list(Wrappers.<SalaryGroupProjectHitLog>lambdaQuery()
                .eq(SalaryGroupProjectHitLog::getUserId, userId)
                .eq(SalaryGroupProjectHitLog::getSalaryGroupProjectId, salaryGroupProjectId)
                .eq(SalaryGroupProjectHitLog::getDataDate, localDate)
                .eq(SalaryGroupProjectHitLog::getYn, Boolean.TRUE)
        );

        //固定提成 车系
        switch (salaryGroupProject.getCalMethod()) {
            case CAR_SERIES: {
                this.setCarSeriesProjectDetail(salaryProjectHitLogs, settings, detailVO);
                break;
            }
            case LADDER:
            case FIXATION:
            case STAR: {
                this.setProjectDetail(salaryProjectHitLogs, settings, detailVO);
                break;
            }
        }
        countDownLatch.countDown();
    }


    /**
     * 设置车系详情
     *
     * @param salaryProjectHitLogs
     * @param settings
     * @param detailVO
     */
    private void setProjectDetail(List<SalaryGroupProjectHitLog> salaryProjectHitLogs, List<SalaryGroupProjectSettin> settings, SalaryGroupIndicatorDetailVO detailVO) {

        if (PublicUtil.isEmpty(salaryProjectHitLogs)) {
            detailVO.setSettings(settings.stream().map(setting -> {
                return PublicUtil.copy(setting, SalaryGroupProjectSettinVO.class);
            }).collect(Collectors.toList()));
            detailVO.setSalaryValue(new BigDecimal("0"));
            return;
        }

        SalaryGroupProjectHitLog salaryProjectHitLog = salaryProjectHitLogs.stream().findFirst().get();
        Long hitSettingId = salaryProjectHitLog.getSalaryGroupProjectSettinId();

        Boolean ratio = PublicUtil.isNotEmpty(detailVO.getLaddersType()) && DataTypeEnum.RATIO.equals(detailVO.getLaddersType());
        detailVO.setSettings(settings.stream().map(setting -> {
            SalaryGroupProjectSettinVO settingVO = PublicUtil.copy(setting, SalaryGroupProjectSettinVO.class);
            settingVO.setHit((hitSettingId.equals(settingVO.getId()) ? Boolean.TRUE : Boolean.FALSE));
            if (hitSettingId.equals(settingVO.getId())) {
                settingVO.setHitCommissionValue(salaryProjectHitLog.getHitCommissionValue());
                settingVO.setHitLadderValue(salaryProjectHitLog.getHitLadderValue());
            }
            if (ratio) {
                settingVO.processPercent();
            }
            return settingVO;
        }).collect(Collectors.toList()));
        detailVO.setSalaryValue(salaryProjectHitLog.getSalaryValue());
        if (PublicUtil.isEmpty(detailVO.getStarLevel())) {
            detailVO.setStarLevel(detailVO.getStarLevel());
        }

    }

    /**
     * 设置车系详情
     *
     * @param salaryProjectHitLogs
     * @param settings
     * @param detailVO
     */
    private void setCarSeriesProjectDetail(List<SalaryGroupProjectHitLog> salaryProjectHitLogs, List<SalaryGroupProjectSettin> settings, SalaryGroupIndicatorDetailVO detailVO) {

        Map<Long, SalaryGroupProjectHitLog> hitMap = new HashMap<>();
        if (PublicUtil.isNotEmpty(salaryProjectHitLogs)) {
            hitMap = salaryProjectHitLogs.stream().collect(
                    Collectors.toMap(SalaryGroupProjectHitLog::getSalaryGroupProjectSettinId, Function.identity(), (v1, v2) -> v1));
        }
        BigDecimal totalSalaryValue = new BigDecimal("0");
        List<SalaryGroupProjectSettinVO> settingVos = new ArrayList<>();
        for (SalaryGroupProjectSettin setting : settings) {
            SalaryGroupProjectSettinVO settingVO = PublicUtil.copy(setting, SalaryGroupProjectSettinVO.class);
            if (hitMap.containsKey(setting.getId())) {
                SalaryGroupProjectHitLog hitLog = hitMap.get(setting.getId());
                if (PublicUtil.isNotEmpty(hitLog)) {
                    totalSalaryValue = totalSalaryValue.add(hitLog.getSalaryValue());
                    settingVO.setHit(Boolean.TRUE);
                    settingVO.setSalaryValue(hitLog.getSalaryValue());
                    settingVO.setHitCommissionValue(hitLog.getHitCommissionValue());
                }
            }
            settingVos.add(settingVO);
        }
        detailVO.setSettings(settingVos);
        detailVO.setSalaryValue(totalSalaryValue);
    }

    /**
     * 获取绩效参数
     *
     * @param salaryGroupProjectId
     * @param userId
     * @param localDate
     * @return
     */
    public void setSalaryGroupProjectParam(Long salaryGroupProjectId, Long userId, LocalDate localDate,
                                           SalaryGroupIndicatorDetailVO detailVO,
                                           CountDownLatch countDownLatch) {

        List<SalaryGroupProjectParamVO> params = salaryGroupProjectParamService.getSalaryGroupProjectParamVO(salaryGroupProjectId);
        if (PublicUtil.isEmpty(params)) {
            countDownLatch.countDown();
            return ;
        }

        List<Long> targetParamIds = params.stream()
                .filter(param -> ! TargetTypeEnum.NO.equals(param.getTargetType()))
                .map(SalaryGroupProjectParamVO::getId).collect(Collectors.toList());
        Map<Long, SalaryGroupProjectTargetHitLog> paramTargetHitLogMap = new HashMap<>();
        if (PublicUtil.isNotEmpty(targetParamIds)) {
            List<SalaryGroupProjectTargetHitLog> targetHitLogs = salaryGroupProjectTargetHitLogService.list(Wrappers.<SalaryGroupProjectTargetHitLog>lambdaQuery()
                    .in(SalaryGroupProjectTargetHitLog::getReferId, targetParamIds)
                    .in(SalaryGroupProjectTargetHitLog::getTargetType, IndicatorTypeEnum.EXAMINE)
                    .eq(SalaryGroupProjectTargetHitLog::getDataDate, localDate)
                    .eq(SalaryGroupProjectTargetHitLog::getUserId, userId)
                    .eq(SalaryGroupProjectTargetHitLog::getYn, Boolean.TRUE)
            );
            paramTargetHitLogMap = targetHitLogs.stream()
                    .collect(Collectors.toMap(SalaryGroupProjectTargetHitLog::getReferId, Function.identity(), (v1, v2) -> v1));
        }

        List<String> indicatorCodes = params.stream()
                .filter(param -> TargetTypeEnum.NO.equals(param.getTargetType()))
                .map(SalaryGroupProjectParamVO::getIndicatorCode).collect(Collectors.toList());
        Map<String, IndicatorUserValue> indicatorCodeValueMap = new HashMap<>();
        if (PublicUtil.isNotEmpty(indicatorCodes)) {
            List<IndicatorUserValue> indicatorUserValues = indicatorUserValueService.list(Wrappers.<IndicatorUserValue>lambdaQuery()
                    .in(IndicatorUserValue::getIndicatorCode, indicatorCodes)
                    .eq(IndicatorUserValue::getDataDate, localDate)
                    .eq(IndicatorUserValue::getUserId, userId)
                    .eq(IndicatorUserValue::getYn, Boolean.TRUE)
            );
            indicatorCodeValueMap = indicatorUserValues.stream()
                    .collect(Collectors.toMap(IndicatorUserValue::getIndicatorCode, Function.identity(), (v1, v2) -> v1));
        }

        List<String> baseWageIndicatorCodes = new ArrayList<String>(){{add(getWorkAgeCode());add(getBaseSalaryCode());}};
        for (SalaryGroupProjectParamVO paramVO : params) {
            //有目标
            if (paramTargetHitLogMap.containsKey(paramVO.getId())) {
                SalaryGroupProjectTargetHitLog targetHitLog = paramTargetHitLogMap.get(paramVO.getId());
                paramVO.setReachValue(targetHitLog.getReachValue());
                paramVO.setIndicatorValue(targetHitLog.getValue());

                if (! TargetTypeEnum.NO.equals(paramVO.getTargetType())) {
                    paramVO.setReachValue(targetHitLog.getReachValue().multiply(Constant.ONE_HUNDRED));
                }
                if (DataTypeEnum.RATIO.equals(paramVO.getDataType())) {
                    paramVO.setIndicatorValue(targetHitLog.getValue().multiply(Constant.ONE_HUNDRED));
                }
                continue;
            }
            //无目标
            if(indicatorCodeValueMap.containsKey(paramVO.getIndicatorCode())) {
                IndicatorUserValue indicatorUserValue = indicatorCodeValueMap.get(paramVO.getIndicatorCode());
                BigDecimal indicatorOriginValue = commonService.queryIndicatorValue(paramVO.getIndicatorCode(), indicatorUserValue);
                switch (indicatorUserValue.getValueType()) {
                    case WORK_AGE:
                    case QUANTITY: {
                        paramVO.setIndicatorValue(indicatorOriginValue);
                        break;
                    }
                    case RATIO: {
                        paramVO.setIndicatorValue(indicatorOriginValue.multiply(Constant.ONE_HUNDRED));
                        break;
                    }
                }
            }
            paramVO.setBaseWage(baseWageIndicatorCodes.contains(paramVO.getIndicatorCode()));
        }

        Map<ParamTypeEnum, List<SalaryGroupProjectParamVO>> indicatorParamMap = params.stream()
                .collect(Collectors.groupingBy(SalaryGroupProjectParamVO::getParamType));

        detailVO.setLadderParams(indicatorParamMap.getOrDefault(ParamTypeEnum.LADDER, new ArrayList<>()));
        detailVO.setCommissionParams(indicatorParamMap.getOrDefault(ParamTypeEnum.COMMISSION, new ArrayList<>()));
        countDownLatch.countDown();
    }

    /**
     * 获取前置条件命中记录
     *
     * @param salaryGroupProjectId
     * @param userId
     * @param localDate
     * @return
     */
    public void setSalaryGroupProjectPreconditionHitVos(Long salaryGroupProjectId, Long userId, LocalDate localDate,
                                                        SalaryGroupIndicatorDetailVO detailVO, CountDownLatch countDownLatch) {
        List<SalaryGroupProjectPreconditionVO> preconditionVos = salaryGroupProjectPreconditionService.getHitVos(salaryGroupProjectId, userId, localDate);
        if (PublicUtil.isEmpty(preconditionVos)) {
            countDownLatch.countDown();
            return;
        }

        List<Long> targetPreconditionIds = preconditionVos.stream()
                .filter(param -> ! TargetTypeEnum.NO.equals(param.getTargetType()))
                .map(SalaryGroupProjectPreconditionVO::getId).collect(Collectors.toList());
        Map<Long, SalaryGroupProjectTargetHitLog> condTargetHitLogMap = new HashMap<>();
        if (PublicUtil.isNotEmpty(targetPreconditionIds)) {
            List<SalaryGroupProjectTargetHitLog> targetHitLogs = salaryGroupProjectTargetHitLogService.list(Wrappers.<SalaryGroupProjectTargetHitLog>lambdaQuery()
                    .in(SalaryGroupProjectTargetHitLog::getReferId, targetPreconditionIds)
                    .in(SalaryGroupProjectTargetHitLog::getTargetType, IndicatorTypeEnum.PRE)
                    .eq(SalaryGroupProjectTargetHitLog::getDataDate, localDate)
                    .eq(SalaryGroupProjectTargetHitLog::getUserId, userId)
                    .eq(SalaryGroupProjectTargetHitLog::getYn, Boolean.TRUE)
            );
            condTargetHitLogMap = targetHitLogs.stream()
                    .collect(Collectors.toMap(SalaryGroupProjectTargetHitLog::getReferId, Function.identity(), (v1, v2) -> v1));
        }

        List<String> indicatorCodes = preconditionVos.stream()
                .filter(param -> TargetTypeEnum.NO.equals(param.getTargetType()))
                .map(SalaryGroupProjectPreconditionVO::getIndicatorCode).collect(Collectors.toList());
        Map<String, IndicatorUserValue> indicatorCodeValueMap = new HashMap<>();
        if (PublicUtil.isNotEmpty(indicatorCodes)) {
            List<IndicatorUserValue> indicatorUserValues = indicatorUserValueService.list(Wrappers.<IndicatorUserValue>lambdaQuery()
                    .in(IndicatorUserValue::getIndicatorCode, indicatorCodes)
                    .eq(IndicatorUserValue::getDataDate, localDate)
                    .eq(IndicatorUserValue::getUserId, userId)
                    .eq(IndicatorUserValue::getYn, Boolean.TRUE)
            );
            indicatorCodeValueMap = indicatorUserValues.stream()
                    .collect(Collectors.toMap(IndicatorUserValue::getIndicatorCode, Function.identity(), (v1, v2) -> v1));
        }

        for (SalaryGroupProjectPreconditionVO preconditionVO : preconditionVos) {
            preconditionVO.setHit(Boolean.FALSE);
            //有目标
            if (condTargetHitLogMap.containsKey(preconditionVO.getId())) {
                SalaryGroupProjectTargetHitLog targetHitLog = condTargetHitLogMap.get(preconditionVO.getId());
                preconditionVO.setReachValue(targetHitLog.getReachValue());
                preconditionVO.setIndicatorValue(targetHitLog.getValue());

                if (! TargetTypeEnum.NO.equals(preconditionVO.getTargetType())) {
                    preconditionVO.setReachValue(targetHitLog.getReachValue().multiply(Constant.ONE_HUNDRED));
                }
                if (DataTypeEnum.RATIO.equals(preconditionVO.getDataType())) {
                    preconditionVO.setIndicatorValue(targetHitLog.getValue().multiply(Constant.ONE_HUNDRED));
                }
                preconditionVO.checkHit();
                continue;
            }
            //无目标
            if(indicatorCodeValueMap.containsKey(preconditionVO.getIndicatorCode())) {
                IndicatorUserValue indicatorUserValue = indicatorCodeValueMap.get(preconditionVO.getIndicatorCode());
                BigDecimal indicatorOriginValue = commonService.queryIndicatorValue(preconditionVO.getIndicatorCode(), indicatorUserValue);
                switch (indicatorUserValue.getValueType()) {
                    case WORK_AGE:
                    case QUANTITY: {
                        preconditionVO.setIndicatorValue(indicatorOriginValue);
                        break;
                    }
                    case RATIO: {
                        preconditionVO.setIndicatorValue(indicatorOriginValue.multiply(Constant.ONE_HUNDRED));
                        break;
                    }
                }
                preconditionVO.checkHit();
            }
        }
        detailVO.setConds(preconditionVos);
        countDownLatch.countDown();
    }

    /**
     * 检查薪酬项是否是工龄工资、基本工资
     *
     * @param salaryGroupProjectId
     * @return
     */
    public Boolean checkIsBaseWage(Long salaryGroupProjectId) {
        List<String> baseWageIndicatorCodes = new ArrayList<String>(){{add(getWorkAgeCode());add(getBaseSalaryCode());}};
        List<SalaryGroupProjectParam> salaryGroupProjectParams = salaryGroupProjectParamService.list(Wrappers.<SalaryGroupProjectParam>lambdaQuery()
                .eq(SalaryGroupProjectParam::getSalaryGroupProjectId, salaryGroupProjectId)
                .in(SalaryGroupProjectParam::getIndicatorCode, baseWageIndicatorCodes)
                .eq(SalaryGroupProjectParam::getYn, Boolean.TRUE)
        );
        return PublicUtil.isNotEmpty(salaryGroupProjectParams);
    }

}