/*
|
* Copyright 2019-2020 Zheng Jie
|
*
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
* you may not use this file except in compliance with the License.
|
* You may obtain a copy of the License at
|
*
|
* http://www.apache.org/licenses/LICENSE-2.0
|
*
|
* Unless required by applicable law or agreed to in writing, software
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
* See the License for the specific language governing permissions and
|
* limitations under the License.
|
*/
|
package me.zhengjie.modules.project.service.impl;
|
|
import cn.hutool.core.collection.CollectionUtil;
|
import lombok.RequiredArgsConstructor;
|
import me.zhengjie.modules.constant.Constant;
|
import me.zhengjie.modules.project.domain.AnimationSettlement;
|
import me.zhengjie.modules.project.domain.Project;
|
import me.zhengjie.modules.project.domain.ProjectType;
|
import me.zhengjie.modules.project.repository.AnimationSettlementRepository;
|
import me.zhengjie.modules.project.repository.ProjectRepository;
|
import me.zhengjie.modules.project.service.AnimationSettlementService;
|
import me.zhengjie.modules.project.service.dto.AnimationSettlementDto;
|
import me.zhengjie.modules.project.service.dto.AnimationSettlementQueryCriteria;
|
import me.zhengjie.modules.project.service.mapstruct.AnimationSettlementMapper;
|
import me.zhengjie.modules.task.domain.TaskDaliyMongo;
|
import me.zhengjie.modules.task.domain.TaskMongo;
|
import me.zhengjie.modules.task.domain.TaskUserMongo;
|
import me.zhengjie.modules.task.service.dto.TaskMongoDto;
|
import me.zhengjie.modules.treatment.domain.UserLevelWage;
|
import me.zhengjie.modules.treatment.repository.UserLevelWageRepository;
|
import me.zhengjie.utils.*;
|
import org.apache.commons.lang3.ObjectUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.data.domain.*;
|
import org.springframework.data.mongodb.core.MongoTemplate;
|
import org.springframework.data.mongodb.core.query.Criteria;
|
import org.springframework.data.mongodb.core.query.Query;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import javax.persistence.EntityManager;
|
import javax.persistence.TypedQuery;
|
import javax.persistence.criteria.CriteriaBuilder;
|
import javax.persistence.criteria.CriteriaQuery;
|
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Root;
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.IOException;
|
import java.math.BigDecimal;
|
import java.math.RoundingMode;
|
import java.sql.Timestamp;
|
import java.time.format.DateTimeFormatter;
|
import java.util.*;
|
import java.util.function.Function;
|
import java.util.stream.Collectors;
|
|
/**
|
* @website https://eladmin.vip
|
* @description 服务实现
|
* @author baolh
|
* @date 2026-09-28
|
**/
|
@Service
|
@RequiredArgsConstructor
|
public class AnimationSettlementServiceImpl implements AnimationSettlementService {
|
|
private final AnimationSettlementRepository animationSettlementRepository;
|
private final UserLevelWageRepository userLevelWageRepository;
|
private final ProjectRepository projectRepository;
|
private final MongoTemplate mongoTemplate;
|
private final AnimationSettlementMapper animationSettlementMapper;
|
@Autowired
|
private EntityManager entityManager;
|
|
@Override
|
public Map<String,Object> queryAll(AnimationSettlementQueryCriteria criteria, Pageable pageable){
|
Page<AnimationSettlement> page = animationSettlementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
|
return PageUtil.toPage(page.map(animationSettlementMapper::toDto));
|
}
|
|
@Override
|
public List<AnimationSettlementDto> queryAll(AnimationSettlementQueryCriteria criteria){
|
return animationSettlementMapper.toDto(animationSettlementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
|
}
|
|
@Override
|
@Transactional
|
public AnimationSettlementDto findById(Long id) {
|
AnimationSettlement animationSettlement = animationSettlementRepository.findById(id).orElseGet(AnimationSettlement::new);
|
ValidationUtil.isNull(animationSettlement.getId(),"AnimationSettlement","id",id);
|
return animationSettlementMapper.toDto(animationSettlement);
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public AnimationSettlementDto create(AnimationSettlement resources) {
|
return animationSettlementMapper.toDto(animationSettlementRepository.save(resources));
|
}
|
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void update(AnimationSettlement resources) {
|
AnimationSettlement animationSettlement = animationSettlementRepository.findById(resources.getId()).orElseGet(AnimationSettlement::new);
|
ValidationUtil.isNull( animationSettlement.getId(),"AnimationSettlement","id",resources.getId());
|
animationSettlement.copy(resources);
|
animationSettlementRepository.save(animationSettlement);
|
}
|
|
@Override
|
public void deleteAll(Long[] ids) {
|
for (Long id : ids) {
|
animationSettlementRepository.deleteById(id);
|
}
|
}
|
|
@Override
|
public void download(List<AnimationSettlementDto> all, HttpServletResponse response) throws IOException {
|
List<Map<String, Object>> list = new ArrayList<>();
|
for (AnimationSettlementDto animationSettlement : all) {
|
Map<String,Object> map = new LinkedHashMap<>();
|
map.put("用户ID", animationSettlement.getUserId());
|
map.put("项目ID", animationSettlement.getProjectId());
|
map.put("任务详情ID", animationSettlement.getTaskId());
|
map.put("任务分配ID", animationSettlement.getTaskUserId());
|
map.put("验收时间", animationSettlement.getAcceptanceTime());
|
map.put("秒数", animationSettlement.getTaskSeconds());
|
map.put("消耗积分", animationSettlement.getPointsConsumed());
|
map.put("问题数量", animationSettlement.getIssueCount());
|
map.put("驳回数量", animationSettlement.getRejectCount());
|
map.put("结算状态", animationSettlement.getSettlementStatus());
|
map.put("创建者", animationSettlement.getCreateBy());
|
map.put("创建时间", animationSettlement.getCreateTime());
|
list.add(map);
|
}
|
FileUtil.downloadExcel(list, response);
|
}
|
|
@Override
|
public void generateByTaskUser(TaskUserMongo taskUserMongo, List<ProjectType> projectTypeList) {
|
// 漫剧结算计费
|
if (projectTypeList.size() > 0 && ObjectUtils.isNotEmpty(taskUserMongo.getTask().getProjectId())) {
|
// 查询项目信息
|
Project project = projectRepository.findById(taskUserMongo.getTask().getProjectId()).orElseGet(Project::new);
|
if (StringUtils.isNotBlank(project.getProjectDistinguish()) && Constant.PROJECT_DISTINGUISH_VISION.equals(project.getProjectDistinguish()) && projectTypeList.stream().map(ProjectType::getId).collect(Collectors.toList()).contains(project.getProjectTypeId())) {
|
List<TaskDaliyMongo> taskDaliyMongoList = mongoTemplate.find(Query.query(new Criteria("taskUserMongo._id").is(taskUserMongo.getId())).with(Sort.by(Sort.Order.desc("fillDate"))), TaskDaliyMongo.class);
|
TaskDaliyMongo taskDaliyMongo = taskDaliyMongoList.stream().findFirst().orElseGet(TaskDaliyMongo::new);
|
|
// 查询漫剧结算记录
|
AnimationSettlementQueryCriteria animationSettlementQueryCriteria = new AnimationSettlementQueryCriteria();
|
animationSettlementQueryCriteria.setTaskUserId(taskUserMongo.getId());
|
AnimationSettlement animationSettlement = animationSettlementRepository.findOne((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,animationSettlementQueryCriteria,criteriaBuilder)).orElseGet(AnimationSettlement::new);
|
|
// 填充数据
|
animationSettlement.setUserId(taskUserMongo.getUserId());
|
animationSettlement.setProjectId(taskUserMongo.getTask().getProjectId());
|
animationSettlement.setTaskId(taskUserMongo.getTask().getId());
|
animationSettlement.setTaskUserId(taskUserMongo.getId());
|
animationSettlement.setTaskSeconds(Optional.ofNullable(taskDaliyMongo.getTaskSeconds()).orElse(0));
|
animationSettlement.setPointsConsumed(Optional.ofNullable(taskDaliyMongo.getPointsConsumed()).orElse(0));
|
animationSettlement.setIssueCount(Optional.ofNullable(taskDaliyMongo.getIssueCount()).orElse(0));
|
animationSettlement.setRejectCount(Optional.ofNullable(taskUserMongo.getRejectCount()).orElse(0));
|
animationSettlement.setSettlementStatus(Constant.ANIME_SETTLEMENT_STATUS_UNSETTLED);
|
animationSettlement.setTaskType(Optional.ofNullable(taskUserMongo.getRid()).map(rid -> Constant.ANIME_TASK_TYPE_REVIEW).orElse(Constant.ANIME_TASK_TYPE_NORMAL));
|
animationSettlement.setCostHours(taskDaliyMongoList.stream().map(tdm -> BigDecimal.valueOf(tdm.getConsumedHours())).reduce(BigDecimal.ZERO, BigDecimal::add));
|
|
// 保存数据
|
animationSettlementRepository.save(animationSettlement);
|
}
|
}
|
}
|
|
@Override
|
public Map<String,Object> queryPersonalAll(AnimationSettlementQueryCriteria criteria, Pageable pageable) {
|
Long currentUserId = Optional.ofNullable(criteria.getUserId()).orElse(SecurityUtils.getCurrentUserId());
|
UserLevelWage userLevelWage = userLevelWageRepository.findUserLevelWageByUserId(currentUserId).stream().findFirst().orElseGet(UserLevelWage::new);
|
Page<Timestamp> page = this.queryPersonalAcceptanceTime(criteria, pageable);
|
|
Function<Timestamp, AnimationSettlementDto> personalFunc = new Function<Timestamp, AnimationSettlementDto>() {
|
@Override
|
public AnimationSettlementDto apply(Timestamp time) {
|
AnimationSettlementDto settlementDto = new AnimationSettlementDto();
|
settlementDto.setAcceptanceTime(time);
|
settlementDto.setUserId(currentUserId);
|
|
// 查询漫剧结算计费
|
AnimationSettlementQueryCriteria queryCriteria = new AnimationSettlementQueryCriteria();
|
queryCriteria.setUserId(currentUserId);
|
queryCriteria.setAcceptanceTime(time);
|
List<AnimationSettlement> settlementList = animationSettlementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, queryCriteria, criteriaBuilder));
|
|
// 计算参数
|
BigDecimal monthlyProduceMinutes = Optional.ofNullable(userLevelWage.getMonthlyProduceMinutes()).orElse(Constant.ANIME_MONTHLY_PRODUCE_MINUTE);
|
BigDecimal monthlyReviewMinutes = Optional.ofNullable(userLevelWage.getMonthlyReviewMinutes()).orElse(Constant.ANIME_MONTHLY_REVIEW_MINUTE);
|
BigDecimal normalTaskSeconds = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getTaskSeconds())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal normalCostHours = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(AnimationSettlement::getCostHours).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal normalPointsConsumed = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getPointsConsumed())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal normalRejectCount = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getRejectCount())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal reviewTaskSeconds = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_REVIEW.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getTaskSeconds())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal reviewCostHours = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_REVIEW.equals(s.getTaskType())).map(AnimationSettlement::getCostHours).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal reviewIssueCount = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_REVIEW.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getIssueCount())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
// 计算
|
BigDecimal produceCommission = calcProduceCommission(normalTaskSeconds, normalCostHours, monthlyProduceMinutes, normalPointsConsumed, settlementDto);
|
BigDecimal produceDeduction = calcProduceDeduction(normalTaskSeconds, reviewIssueCount, normalRejectCount);
|
BigDecimal reviewCommission = calcReviewCommission(reviewTaskSeconds, reviewCostHours, monthlyReviewMinutes, reviewIssueCount, normalRejectCount);
|
BigDecimal reviewDeduction = calcReviewDeduction(reviewTaskSeconds, normalRejectCount);
|
|
// 填充数据
|
settlementDto.setProduceCommission(produceCommission);
|
settlementDto.setProduceDeduction(produceDeduction);
|
settlementDto.setReviewCommission(reviewCommission);
|
settlementDto.setReviewDeduction(reviewDeduction);
|
|
return settlementDto;
|
}
|
};
|
|
return PageUtil.toPage(page.map(personalFunc));
|
}
|
|
@Override
|
public Map<String,Object> queryAnimeAll(AnimationSettlementQueryCriteria criteria, Pageable pageable) {
|
Page<String> page = this.queryAnimeTaskId(criteria, pageable);
|
|
Function<String, TaskMongoDto> animeFunc = new Function<String, TaskMongoDto>() {
|
@Override
|
public TaskMongoDto apply(String taskId) {
|
TaskMongo taskMongo = mongoTemplate.findById(taskId, TaskMongo.class);
|
// 转换类型
|
TaskMongoDto taskMongoDto = new TaskMongoDto();
|
taskMongoDto.setId(taskMongo.getId());
|
taskMongoDto.setName(taskMongo.getName());
|
taskMongoDto.setOld_id(taskMongo.getOld_id());
|
taskMongoDto.setProjectId(taskMongo.getProjectId());
|
taskMongoDto.setPerformanceType(taskMongo.getPerformanceType());
|
taskMongoDto.setPlanStartTime(taskMongo.getPlanStartTime());
|
taskMongoDto.setPlanEndTime(taskMongo.getPlanEndTime());
|
taskMongoDto.setPlanHours(taskMongo.getPlanHours());
|
taskMongoDto.setTaskStatus(taskMongo.getTaskStatus());
|
taskMongoDto.setDescription(taskMongo.getDescription());
|
taskMongoDto.setUserIdList(taskMongo.getUserIdList());
|
taskMongoDto.setContainUserList(taskMongo.getContainUserList());
|
taskMongoDto.setTagList(taskMongo.getTagList());
|
taskMongoDto.setVersion(taskMongo.getVersion());
|
if (taskMongo.getPlanWorkload() != null) {
|
taskMongoDto.setPlanWorkload(taskMongo.getPlanWorkload());
|
}
|
if (taskMongo.getPlanWorkloadUnit() != null) {
|
taskMongoDto.setPlanWorkloadUnit(taskMongo.getPlanWorkloadUnit());
|
}
|
taskMongoDto.setConsumedHours(0.0D);
|
|
// 项目信息
|
Project project = projectRepository.findById(taskMongo.getProjectId()).orElseGet(Project::new);
|
taskMongoDto.setProjectName(project.getName());
|
// 查询漫剧结算信息
|
AnimationSettlementQueryCriteria queryCriteria = new AnimationSettlementQueryCriteria();
|
queryCriteria.setTaskId(taskId);
|
List<AnimationSettlement> animationSettlementList = animationSettlementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, queryCriteria, criteriaBuilder));
|
taskMongoDto.setAcceptanceTime(animationSettlementList.stream().findFirst().map(AnimationSettlement::getAcceptanceTime).orElse(null));
|
taskMongoDto.setSettlementStatus(animationSettlementList.stream().findFirst().map(AnimationSettlement::getSettlementStatus).orElse(Constant.ANIME_SETTLEMENT_STATUS_UNSETTLED));
|
|
return taskMongoDto;
|
}
|
};
|
|
return PageUtil.toPage(page.map(animeFunc));
|
}
|
|
@Override
|
public List<AnimationSettlementDto> queryPersonalAll(AnimationSettlementQueryCriteria criteria) {
|
Long currentUserId = Optional.ofNullable(criteria.getUserId()).orElse(SecurityUtils.getCurrentUserId());
|
UserLevelWage userLevelWage = userLevelWageRepository.findUserLevelWageByUserId(currentUserId).stream().findFirst().orElseGet(UserLevelWage::new);
|
// 不分页
|
Pageable pageable = Pageable.unpaged();
|
Page<Timestamp> page = this.queryPersonalAcceptanceTime(criteria, pageable);
|
|
Function<Timestamp, AnimationSettlementDto> personalFunc = new Function<Timestamp, AnimationSettlementDto>() {
|
@Override
|
public AnimationSettlementDto apply(Timestamp time) {
|
AnimationSettlementDto settlementDto = new AnimationSettlementDto();
|
settlementDto.setAcceptanceTime(time);
|
settlementDto.setUserId(currentUserId);
|
|
// 查询漫剧结算计费
|
AnimationSettlementQueryCriteria queryCriteria = new AnimationSettlementQueryCriteria();
|
queryCriteria.setUserId(currentUserId);
|
queryCriteria.setAcceptanceTime(time);
|
List<AnimationSettlement> settlementList = animationSettlementRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, queryCriteria, criteriaBuilder));
|
|
// 计算参数
|
BigDecimal monthlyProduceMinutes = Optional.ofNullable(userLevelWage.getMonthlyProduceMinutes()).orElse(Constant.ANIME_MONTHLY_PRODUCE_MINUTE);
|
BigDecimal monthlyReviewMinutes = Optional.ofNullable(userLevelWage.getMonthlyReviewMinutes()).orElse(Constant.ANIME_MONTHLY_REVIEW_MINUTE);
|
BigDecimal normalTaskSeconds = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getTaskSeconds())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal normalCostHours = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(AnimationSettlement::getCostHours).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal normalPointsConsumed = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getPointsConsumed())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal normalRejectCount = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_NORMAL.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getRejectCount())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal reviewTaskSeconds = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_REVIEW.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getTaskSeconds())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal reviewCostHours = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_REVIEW.equals(s.getTaskType())).map(AnimationSettlement::getCostHours).reduce(BigDecimal.ZERO, BigDecimal::add);
|
BigDecimal reviewIssueCount = settlementList.stream().filter(s -> Constant.ANIME_TASK_TYPE_REVIEW.equals(s.getTaskType())).map(s -> BigDecimal.valueOf(s.getIssueCount())).reduce(BigDecimal.ZERO, BigDecimal::add);
|
|
// 计算
|
BigDecimal produceCommission = calcProduceCommission(normalTaskSeconds, normalCostHours, monthlyProduceMinutes, normalPointsConsumed, settlementDto);
|
BigDecimal produceDeduction = calcProduceDeduction(normalTaskSeconds, reviewIssueCount, normalRejectCount);
|
BigDecimal reviewCommission = calcReviewCommission(reviewTaskSeconds, reviewCostHours, monthlyReviewMinutes, reviewIssueCount, normalRejectCount);
|
BigDecimal reviewDeduction = calcReviewDeduction(reviewTaskSeconds, normalRejectCount);
|
|
// 填充数据
|
settlementDto.setProduceCommission(produceCommission);
|
settlementDto.setProduceDeduction(produceDeduction);
|
settlementDto.setReviewCommission(reviewCommission);
|
settlementDto.setReviewDeduction(reviewDeduction);
|
|
return settlementDto;
|
}
|
};
|
|
return page.map(personalFunc).getContent();
|
}
|
|
private static final Map<Double, BigDecimal> coefficientUnitPriceMap = new LinkedHashMap() {{
|
put(0.0, BigDecimal.valueOf(20));
|
put(1.3, BigDecimal.valueOf(19));
|
put(1.4, BigDecimal.valueOf(18));
|
put(1.5, BigDecimal.valueOf(15));
|
put(1.6, BigDecimal.ZERO);
|
}};
|
private static final BigDecimal BUG_LIMIT_MINUTE = BigDecimal.valueOf(2);
|
private static final BigDecimal PRICE_SINGLE_BUG = BigDecimal.valueOf(3);
|
private static final BigDecimal PRICE_SINGLE_REVIEW = BigDecimal.valueOf(5);
|
private static final BigDecimal MONTHLY_STANDARD_HOUR = BigDecimal.valueOf(160);
|
private static final BigDecimal MINUTE_STANDARD_POINT = BigDecimal.valueOf(60);
|
|
/**
|
* 计算制作提成
|
* @param taskSeconds / 任务秒数
|
* @param totalProduceHours / 月度总制作小时数
|
* @param monthlyProduceMinutes / 月度标准制作分钟数
|
* @param totalPoints / 消耗总积分
|
* @return
|
*/
|
private BigDecimal calcProduceCommission(BigDecimal taskSeconds, BigDecimal totalProduceHours, BigDecimal monthlyProduceMinutes, BigDecimal totalPoints, AnimationSettlementDto animationSettlement) {
|
// 工时
|
BigDecimal taskMinutes = taskSeconds.divide(BigDecimal.valueOf(60), 2, RoundingMode.HALF_UP);
|
BigDecimal monthlyProduceRatio = totalProduceHours.divide(MONTHLY_STANDARD_HOUR);
|
BigDecimal excessProduceMinutes = taskMinutes.subtract(monthlyProduceMinutes.multiply(monthlyProduceRatio)).max(BigDecimal.ZERO);
|
// 单价
|
BigDecimal gachaCoefficient = taskMinutes.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO : totalPoints.divide(taskMinutes).divide(MINUTE_STANDARD_POINT);
|
BigDecimal commissionUnitPrice = coefficientUnitPriceMap.entrySet().stream().filter(e -> gachaCoefficient.doubleValue() > e.getKey()).reduce((first, second) -> second).map(Map.Entry::getValue).orElse(BigDecimal.ZERO);
|
|
// 保存额外信息
|
if (animationSettlement != null) {
|
animationSettlement.setGachaCoefficient(gachaCoefficient);
|
animationSettlement.setCommissionPrice(commissionUnitPrice);
|
animationSettlement.setExcessProduceMinutes(excessProduceMinutes);
|
}
|
|
return excessProduceMinutes.multiply(commissionUnitPrice);
|
}
|
|
/**
|
* 计算制作扣除
|
* @param taskSeconds / 任务秒数
|
* @param issueCount / 问题数量
|
* @param rejectCount / 驳回数量
|
* @return
|
*/
|
private BigDecimal calcProduceDeduction(BigDecimal taskSeconds, BigDecimal issueCount, BigDecimal rejectCount) {
|
// 工时
|
BigDecimal taskMinutes = taskSeconds.divide(BigDecimal.valueOf(60), 0, RoundingMode.HALF_UP);
|
BigDecimal excessBugDetected = issueCount.subtract(rejectCount).subtract(taskMinutes.multiply(BUG_LIMIT_MINUTE)).max(BigDecimal.ZERO);
|
|
return excessBugDetected.multiply(PRICE_SINGLE_BUG);
|
}
|
|
/**
|
* 计算审核提成
|
* @param taskSeconds / 任务秒数
|
* @param totalReviewHours / 月度总审核小时数
|
* @param monthlyReviewMinutes / 月度标准审核分钟数
|
* @param issueCount / 问题数量
|
* @param rejectCount / 驳回数量
|
* @return
|
*/
|
private BigDecimal calcReviewCommission(BigDecimal taskSeconds, BigDecimal totalReviewHours, BigDecimal monthlyReviewMinutes, BigDecimal issueCount, BigDecimal rejectCount) {
|
// 审核工时
|
BigDecimal taskMinutes = taskSeconds.divide(BigDecimal.valueOf(60), 2, RoundingMode.HALF_UP);
|
BigDecimal monthlyReviewRatio = totalReviewHours.divide(MONTHLY_STANDARD_HOUR);
|
BigDecimal excessReviewMinutes = taskMinutes.subtract(monthlyReviewMinutes.multiply(monthlyReviewRatio)).max(BigDecimal.ZERO);
|
// 超额BUG提成
|
BigDecimal produceDeduction = calcProduceDeduction(taskSeconds, issueCount, rejectCount);
|
|
return excessReviewMinutes.multiply(PRICE_SINGLE_REVIEW).add(produceDeduction);
|
}
|
|
/**
|
* 计算审核扣除
|
* @param taskSeconds / 任务秒数
|
* @param rejectCount / 任务秒数
|
* @return
|
*/
|
private BigDecimal calcReviewDeduction(BigDecimal taskSeconds, BigDecimal rejectCount) {
|
BigDecimal taskMinutes = taskSeconds.divide(BigDecimal.valueOf(60), 0, RoundingMode.HALF_UP);
|
BigDecimal deductionCount = rejectCount.subtract(taskMinutes.multiply(BUG_LIMIT_MINUTE)).max(BigDecimal.ZERO);
|
|
return deductionCount.multiply(PRICE_SINGLE_BUG);
|
}
|
|
/**
|
* 获取个人提成月份
|
* @param criteria / 限制条件
|
* @param pageable / 分页参数
|
* @return
|
*/
|
private Page<Timestamp> queryPersonalAcceptanceTime(AnimationSettlementQueryCriteria criteria, Pageable pageable) {
|
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
CriteriaQuery<Timestamp> query = cb.createQuery(Timestamp.class);
|
Root<AnimationSettlement> root = query.from(AnimationSettlement.class);
|
|
// 构建检索条件
|
List<Predicate> predicates = new ArrayList<>();
|
predicates.add(cb.equal(root.get("userId"), Optional.ofNullable(criteria.getUserId()).orElse(SecurityUtils.getCurrentUserId())));
|
predicates.add(cb.equal(root.get("settlementStatus"), Constant.ANIME_SETTLEMENT_STATUS_SETTLED));
|
if (ObjectUtils.isNotEmpty(criteria.getAcceptanceTime())) {
|
predicates.add(cb.equal(root.get("acceptanceTime"), criteria.getAcceptanceTime()));
|
}
|
if (CollectionUtil.isNotEmpty(criteria.getAcceptanceTimeRange())) {
|
predicates.add(cb.between(root.get("acceptanceTime"), criteria.getAcceptanceTimeRange().get(0), criteria.getAcceptanceTimeRange().get(1)));
|
}
|
query.where(cb.and(predicates.toArray(new Predicate[0])));
|
// 构建结果项
|
query.select(root.get("acceptanceTime").as(Timestamp.class));
|
// 构建分组
|
query.groupBy(root.get("acceptanceTime"));
|
|
// 构建分页
|
TypedQuery<Timestamp> typedQuery = entityManager.createQuery(query);
|
if (pageable.isPaged()) {
|
typedQuery.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
|
typedQuery.setMaxResults(pageable.getPageSize());
|
}
|
List<Timestamp> content = typedQuery.getResultList();
|
|
// 构建Count查询
|
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
|
Root<AnimationSettlement> countRoot = countQuery.from(AnimationSettlement.class);
|
countQuery.where(cb.and(predicates.toArray(new Predicate[0])));
|
countQuery.select(cb.countDistinct(countRoot.get("acceptanceTime")));
|
Long total = entityManager.createQuery(countQuery).getSingleResult();
|
|
return new PageImpl<>(content, pageable, total);
|
}
|
|
/**
|
* 获取漫剧任务ID
|
* @param criteria / 限制条件
|
* @param pageable / 分页参数
|
* @return
|
*/
|
private Page<String> queryAnimeTaskId(AnimationSettlementQueryCriteria criteria, Pageable pageable) {
|
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
|
CriteriaQuery<String> query = cb.createQuery(String.class);
|
Root<AnimationSettlement> root = query.from(AnimationSettlement.class);
|
|
// 构建检索条件
|
List<Predicate> predicates = new ArrayList<>();
|
if (StringUtils.isNotBlank(criteria.getTaskId())) {
|
predicates.add(cb.like(root.get("taskId"), criteria.getTaskId()));
|
}
|
if (ObjectUtils.isNotEmpty(criteria.getAcceptanceTime())) {
|
predicates.add(cb.equal(root.get("acceptanceTime"), criteria.getAcceptanceTime()));
|
}
|
if (CollectionUtil.isNotEmpty(criteria.getTaskIds())) {
|
predicates.add(root.get("taskId").in(criteria.getTaskIds()));
|
}
|
if (CollectionUtil.isNotEmpty(criteria.getAcceptanceTimeRange())) {
|
predicates.add(cb.between(root.get("acceptanceTime"), criteria.getAcceptanceTimeRange().get(0), criteria.getAcceptanceTimeRange().get(1)));
|
}
|
query.where(cb.and(predicates.toArray(new Predicate[0])));
|
// 构建结果项
|
query.select(root.get("taskId"));
|
// 构建分组
|
query.groupBy(root.get("taskId"));
|
|
// 构建分页
|
TypedQuery<String> typedQuery = entityManager.createQuery(query);
|
if (pageable.isPaged()) {
|
typedQuery.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
|
typedQuery.setMaxResults(pageable.getPageSize());
|
}
|
List<String> content = typedQuery.getResultList();
|
|
// 构建Count查询
|
CriteriaQuery<Long> countQuery = cb.createQuery(Long.class);
|
Root<AnimationSettlement> countRoot = countQuery.from(AnimationSettlement.class);
|
countQuery.where(cb.and(predicates.toArray(new Predicate[0])));
|
countQuery.select(cb.countDistinct(countRoot.get("taskId")));
|
Long total = entityManager.createQuery(countQuery).getSingleResult();
|
|
return new PageImpl<>(content, pageable, total);
|
}
|
}
|