package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.core.utils.StoreParaUtils;
import com.ssm.manage.dao.SsmStaffSalaryDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.extendEntity.StaffStatistic;
import com.ssm.manage.query.StaffSalaryRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.*;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.BigDecimalUtils;
import com.ssm.manage.utils.Constant;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SsmStaffSalaryServiceImpl extends BaseServiceImpl<StaffSalary> implements SsmStaffSalaryService {
    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmCashService ssmCashService;

    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private SsmCourseAccountService  ssmCourseAccountService;

    @Autowired
    private SsmBaseStoreServiceImpl  ssmBaseStoreService;

    @Autowired
    private SsmRefundRechargeService ssmRefundRechargeService;

    @Autowired
    private SsmRefundCourseService ssmRefundCourseService;

    @Autowired
    private SsmRefundCashService ssmRefundCashService;

    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;


    @Autowired
    private SsmStaffSalaryDao dao;

    @Override
    public SsmStaffSalaryDao getDao() {
        return dao;
    }

    @Override
    public List<StaffSalary> selectSalary(StaffSalaryRequest staffSalaryRequest) throws ParseException, SmsException {

        List<StaffSalary> staffSalaryList = new ArrayList<>();
        //先从工资表查询
        StaffSalary staffSalary = new StaffSalary();
        staffSalary.setDate(staffSalaryRequest.getDate());
        staffSalary.setStoreId(staffSalaryRequest.getStoreId());
        staffSalaryList = dao.select(staffSalary);
        if (staffSalaryList.size() == 0) {
            StoreParament storeParament = StoreParaUtils.getStorePara(staffSalaryRequest.getStoreId(), Constant.payroll);
            BigDecimal achievementAmount = BigDecimalUtils.bigDecimal("0");
            BigDecimal commissionAmount = BigDecimalUtils.bigDecimal("0");
            if (null != storeParament) {
                //转换时间为年月
                Date date = DateUtils.ymParse(staffSalaryRequest.getDate());
                //查询充值业务员信息
                List<RechargeListResponse> recList = ssmRechargeService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询疗程业务员信息
                List<CourseListResponse> courseList = ssmCourseService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询服务单业务员信息
                List<CashListResponse> cashList = ssmCashService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询还款单业务员信息
                List<RepayTradeResponse> RepayLists = ssmRepayService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), null);

                //查询充值退款单业务员
                List<RefundRechargeResponse> refundRecList = ssmRefundRechargeService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询疗程退款单业务员
                List<RefundCourseResponse> refundCourseList = ssmRefundCourseService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询收银退款单业务员
                List<RefundCashResponse> refundCashList = ssmRefundCashService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询促销单业务员
                List<DocPromotionResponse> promotionList = ssmDocPromotionService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());

                List<AchiCommEntity> totalList = new ArrayList<>();
                //合并list
                recList.forEach(rechargeListResponse -> rechargeListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                courseList.forEach(courseListResponse -> courseListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                cashList.forEach(cashListResponse -> cashListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                RepayLists.forEach(repayTradeResponse -> repayTradeResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundRecList.forEach(refundRechargeResponse -> refundRechargeResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundCourseList.forEach(refundCourseResponse -> refundCourseResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundCashList.forEach(refundCashResponse -> refundCashResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                promotionList.forEach(promotionResponse -> promotionResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                //按员工ID进行分组
                Map<Long, List<AchiCommEntity>> resultMap = totalList.stream().collect(Collectors.groupingBy(AchiCommEntity::getSaleStaffId));
                //计算每个分组员工的业绩
                Iterator<Map.Entry<Long, List<AchiCommEntity>>> entryIterator = resultMap.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    Map.Entry<Long, List<AchiCommEntity>> entry = entryIterator.next();
                    List<AchiCommEntity> list = entry.getValue();

                    //充值 + 充值还款 提成
                    BigDecimal recComm = list.stream().filter(achiCommEntity -> Constant.doc_type_recharge.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //充值退款 提成
                    BigDecimal recRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_recharge.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    //疗程 + 疗程还款 提成
                    BigDecimal courseComm = list.stream().filter(achiCommEntity -> Constant.doc_type_card_use.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //疗程退款 提成
                    BigDecimal courseRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_course.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    //收银 提成
                    BigDecimal cashComm = list.stream().filter(achiCommEntity -> Constant.doc_type_store_cash.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //消费退 提成
                    BigDecimal cashRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_cash.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //还款提成
                    BigDecimal repayComm = list.stream().filter(achiCommEntity -> Constant.doc_type_repay.equals(achiCommEntity.getType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    //促销提成
                    BigDecimal promotionComm = list.stream().filter(achiCommEntity -> Constant.doc_type_promotion.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    commissionAmount = BigDecimalUtils.subtract(BigDecimalUtils.subtract(BigDecimalUtils.subtract(
                            BigDecimalUtils.add(promotionComm, BigDecimalUtils.add(repayComm, BigDecimalUtils.add(BigDecimalUtils.add(recComm, courseComm), cashComm))), recRefundComm), courseRefundComm), cashRefundComm);

                    StaffSalary salary = new StaffSalary();
                    BeanUtils.copyProperties(staffSalaryRequest, salary);
                    //底薪+计件工资
                    if ("3".equals(storeParament.getParamentValue())) {
                        BaseStaff baseStaff = ssmBaseStaffService.selectByPrimaryKey(entry.getKey());
                        if(baseStaff != null){
                            salary.setBaseSalary(baseStaff.getBaseSalary());
                        }

                    }
                    salary.setCommSalary(commissionAmount);
                    salary.setStaffId(list.get(0).getSaleStaffId());
                    salary.setStaffName(list.get(0).getSaleStaffName());
                    salary.setBrandNumber(list.get(0).getBrandNumber());
                    staffSalaryList.add(salary);
                }
            }
        }
        //暂时去掉
        //staffSalaryList.sort(Comparator.comparing(StaffSalary::getBrandNumber));
        return staffSalaryList;
    }

    @Override
    public List<StaffSalary> selectSalarys(StaffSalaryRequest staffSalaryRequest) throws ParseException, SmsException {

        List<StaffSalary> staffSalaryList = new ArrayList<>();
        //先从工资表查询
        StaffSalary staffSalary = new StaffSalary();
        staffSalary.setDate(staffSalaryRequest.getDate());
        staffSalary.setStoreId(staffSalaryRequest.getStoreId());
        staffSalaryList = dao.select(staffSalary);
        if (staffSalaryList.size() == 0) {
            StoreParament storeParament = StoreParaUtils.getStorePara(staffSalaryRequest.getStoreId(), Constant.payroll);
            BigDecimal achievementAmount = BigDecimalUtils.bigDecimal("0");
            BigDecimal commissionAmount = BigDecimalUtils.bigDecimal("0");
            if (null != storeParament) {
                //转换时间为年月
                Date date = DateUtils.ymParse(staffSalaryRequest.getDate());
                //查询充值业务员信息
                List<RechargeListResponse> recList = ssmRechargeService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询疗程业务员信息
                List<CourseListResponse> courseList = ssmCourseService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询服务单业务员信息
                List<CashListResponse> cashList = ssmCashService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询还款单业务员信息
                List<RepayTradeResponse> RepayLists = ssmRepayService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), null);

                //查询充值退款单业务员
                List<RefundRechargeResponse> refundRecList = ssmRefundRechargeService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询疗程退款单业务员
                List<RefundCourseResponse> refundCourseList = ssmRefundCourseService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询收银退款单业务员
                List<RefundCashResponse> refundCashList = ssmRefundCashService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询促销单业务员
                List<DocPromotionResponse> promotionList = ssmDocPromotionService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());

                List<AchiCommEntity> totalList = new ArrayList<>();
                //合并list
                recList.forEach(rechargeListResponse -> rechargeListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                courseList.forEach(courseListResponse -> courseListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                cashList.forEach(cashListResponse -> cashListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                RepayLists.forEach(repayTradeResponse -> repayTradeResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundRecList.forEach(refundRechargeResponse -> refundRechargeResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundCourseList.forEach(refundCourseResponse -> refundCourseResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundCashList.forEach(refundCashResponse -> refundCashResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                promotionList.forEach(promotionResponse -> promotionResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                //按员工ID进行分组
                Map<Long, List<AchiCommEntity>> resultMap = totalList.stream().collect(Collectors.groupingBy(AchiCommEntity::getSaleStaffId));
                //计算每个分组员工的业绩
                Iterator<Map.Entry<Long, List<AchiCommEntity>>> entryIterator = resultMap.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    Map.Entry<Long, List<AchiCommEntity>> entry = entryIterator.next();
                    List<AchiCommEntity> list = entry.getValue();

                    //充值 + 充值还款 提成
                    BigDecimal recComm = list.stream().filter(achiCommEntity -> Constant.doc_type_recharge.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //充值退款 提成
                    BigDecimal recRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_recharge.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    //疗程 + 疗程还款 提成
                    BigDecimal courseComm = list.stream().filter(achiCommEntity -> Constant.doc_type_card_use.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //疗程退款 提成
                    BigDecimal courseRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_course.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    //收银 提成
                    BigDecimal cashComm = list.stream().filter(achiCommEntity -> Constant.doc_type_store_cash.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //消费退 提成
                    BigDecimal cashRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_cash.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                    //还款提成
                    BigDecimal repayComm = list.stream().filter(achiCommEntity -> Constant.doc_type_repay.equals(achiCommEntity.getType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    //促销提成
                    BigDecimal promotionComm = list.stream().filter(achiCommEntity -> Constant.doc_type_promotion.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                    commissionAmount = BigDecimalUtils.subtract(BigDecimalUtils.subtract(BigDecimalUtils.subtract(
                            BigDecimalUtils.add(promotionComm, BigDecimalUtils.add(repayComm, BigDecimalUtils.add(BigDecimalUtils.add(recComm, courseComm), cashComm))), recRefundComm), courseRefundComm), cashRefundComm);

                    StaffSalary salary = new StaffSalary();
                    BeanUtils.copyProperties(staffSalaryRequest, salary);
                    //底薪+计件工资
                    if ("3".equals(storeParament.getParamentValue())) {
                        BaseStaff baseStaff = ssmBaseStaffService.selectByPrimaryKey(entry.getKey());
                        if(baseStaff != null){
                            salary.setBaseSalary(baseStaff.getBaseSalary());
                        }

                    }
                    salary.setCommSalary(commissionAmount);
                    salary.setStaffId(list.get(0).getSaleStaffId());
                    salary.setStaffName(list.get(0).getSaleStaffName());
                    salary.setBrandNumber(list.get(0).getBrandNumber());
                    staffSalaryList.add(salary);
                }
            }
        }
        //暂时去掉
        //staffSalaryList.sort(Comparator.comparing(StaffSalary::getBrandNumber));
        return staffSalaryList;
    }


//    @Override
//    public List<StaffSalary> selectseniorExecutive(StaffSalaryRequest staffSalaryRequest) throws ParseException, SmsException {
//
//        //先查询总店
//        StoreRequest storeRequest = new StoreRequest();
//        Long storeId = null;
//        storeRequest.setId(staffSalaryRequest.getStoreId());
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
//        for (BaseStores pagesorganization : pagesorganizations) {
//            if(pagesorganization.getParentId()==null){
//                storeId = pagesorganization.getId();
//            }
//        }
//
//        List<StaffSalary> staffSalaryList = new ArrayList<>();
//        //先从工资表查询
//        StaffSalary staffSalary = new StaffSalary();
//        staffSalary.setStoreId(storeId);
//        staffSalaryList = dao.select(staffSalary);
//        return staffSalaryList;
//    }


    @Override
    public StaffSalaryResponse selectSalaryByStaff(StaffSalaryRequest staffSalaryRequest) throws ParseException, SmsException {
        StaffSalaryResponse staffSalaryResponse = new StaffSalaryResponse();
//        //先从工资表查询
//        StaffSalary staffSalary = new StaffSalary();
//        staffSalary.setDate(staffSalaryRequest.getDate());
//        staffSalary.setStoreId(staffSalaryRequest.getStoreId());
//        staffSalary.setStaffId(staffSalaryRequest.getStaffId());
//        List<StaffSalary> staffSalaryList = dao.select(staffSalary);
//        if (staffSalaryList.size() == 0) {
            StoreParament storeParament = StoreParaUtils.getStorePara(staffSalaryRequest.getStoreId(), Constant.payroll);
            BigDecimal achievementAmount = BigDecimalUtils.bigDecimal("0");
            BigDecimal commissionAmount = BigDecimalUtils.bigDecimal("0");

            if (null != storeParament) {
                //转换时间为年月
                Date date = DateUtils.ymParse(staffSalaryRequest.getDate());
                //查询充值业务员信息
                List<RechargeListResponse> recList = ssmRechargeService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询疗程业务员信息
                List<CourseListResponse> courseList = ssmCourseService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询服务单业务员信息
                List<CashListResponse> cashList = ssmCashService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), date);
                //查询还款单业务员信息
                List<RepayTradeResponse> RepayLists = ssmRepayService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate(), null);

                //查询充值退款单业务员
                List<RefundRechargeResponse> refundRecList = ssmRefundRechargeService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询疗程退款单业务员
                List<RefundCourseResponse> refundCourseList = ssmRefundCourseService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询收银退款单业务员
                List<RefundCashResponse> refundCashList = ssmRefundCashService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());
                //查询促销单业务员
                List<DocPromotionResponse> promotionList = ssmDocPromotionService.selectList(staffSalaryRequest.getStoreId(), staffSalaryRequest.getDate());

                List<AchiCommEntity> totalList = new ArrayList<>();
                //合并list
                recList.forEach(rechargeListResponse -> rechargeListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                courseList.forEach(courseListResponse -> courseListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                cashList.forEach(cashListResponse -> cashListResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                RepayLists.forEach(repayTradeResponse -> repayTradeResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundRecList.forEach(refundRechargeResponse -> refundRechargeResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundCourseList.forEach(refundCourseResponse -> refundCourseResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                refundCashList.forEach(refundCashResponse -> refundCashResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                promotionList.forEach(promotionResponse -> promotionResponse.getEntities().stream().sequential().collect(Collectors.toCollection(() -> totalList)));
                //按员工ID进行分组
                Map<Long, List<AchiCommEntity>> resultMap = totalList.stream().collect(Collectors.groupingBy(AchiCommEntity::getSaleStaffId));
                //计算每个分组员工的业绩
                Iterator<Map.Entry<Long, List<AchiCommEntity>>> entryIterator = resultMap.entrySet().iterator();

                while (entryIterator.hasNext()) {
                    Map.Entry<Long, List<AchiCommEntity>> entry = entryIterator.next();
                    if (entry.getKey().equals(staffSalaryRequest.getStaffId())) {
                        List<AchiCommEntity> list = entry.getValue();

                        //充值 + 充值还款 提成
                        BigDecimal recComm = list.stream().filter(achiCommEntity -> Constant.doc_type_recharge.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                        //充值退款 提成
                        BigDecimal recRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_recharge.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                        //疗程 + 疗程还款 提成
                        BigDecimal courseComm = list.stream().filter(achiCommEntity -> Constant.doc_type_card_use.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                        //疗程退款 提成
                        BigDecimal courseRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_course.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);

                        //收银 提成
                        BigDecimal cashComm = list.stream().filter(achiCommEntity -> Constant.doc_type_store_cash.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                        //消费退 提成
                        BigDecimal cashRefundComm = list.stream().filter(achiCommEntity -> Constant.doc_type_refund_cash.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                        //还款提成
                        BigDecimal repayComm = list.stream().filter(achiCommEntity -> Constant.doc_type_repay.equals(achiCommEntity.getType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                        //促销提成
                        BigDecimal promotionComm = list.stream().filter(achiCommEntity -> Constant.doc_type_promotion.equals(achiCommEntity.getDocType())).map(AchiCommEntity::getSaleCommissionAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
                        commissionAmount = BigDecimalUtils.subtract(BigDecimalUtils.subtract(BigDecimalUtils.subtract(
                                BigDecimalUtils.add( promotionComm,BigDecimalUtils.add(repayComm, BigDecimalUtils.add(BigDecimalUtils.add(recComm, courseComm), cashComm))), recRefundComm), courseRefundComm), cashRefundComm);


                        BeanUtils.copyProperties(staffSalaryRequest, staffSalaryResponse);
                        //底薪+计件工资
                        if ("3".equals(storeParament.getParamentValue())) {
                            BaseStaff baseStaff = ssmBaseStaffService.selectByPrimaryKey(entry.getKey());
                            staffSalaryResponse.setBaseSalary(baseStaff.getBaseSalary());
                        }
                        staffSalaryResponse.setCommSalary(commissionAmount);
                        staffSalaryResponse.setRecComm(recComm);
                        staffSalaryResponse.setCourseComm(courseComm);
                        staffSalaryResponse.setCashComm(cashComm);
                        staffSalaryResponse.setRepayComm(repayComm);
                        staffSalaryResponse.setRecRefundComm(recRefundComm);
                        staffSalaryResponse.setCourseRefundComm(courseRefundComm);
                        staffSalaryResponse.setCashRefundComm(cashRefundComm);
                        staffSalaryResponse.setPromotionComm(promotionComm);
                    }
                }
            }
//        }
        return staffSalaryResponse;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int add(List<StaffSalaryRequest> staffSalaryRequests) {
        int i = 0;
        //将上次当前查询月份插入的数据删除
        StaffSalaryRequest salaryRequest = staffSalaryRequests.get(0);
        StaffSalary salary = new StaffSalary();
        salary.setDate(salaryRequest.getDate());
        salary.setStoreId(salaryRequest.getStoreId());
        try {
            i = dao.delete(salary);
        }catch (Exception e){
            log.error("SsmStaffSalaryServiceImpl.add", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        //将当前月份新数据添加
        for (StaffSalaryRequest staffSalaryRequest : staffSalaryRequests) {
            StaffSalary staffSalary = new StaffSalary();
            BeanUtils.copyProperties(staffSalaryRequest, staffSalary);
            staffSalary.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            i = dao.saveNotNull(staffSalary);
        }

        return i;
    }

    @Override
    public int delete(StaffSalaryRequest staffSalaryRequest) {
        int i = 0;
        //将上次当前查询月份插入的数据删除
        StaffSalary salary = new StaffSalary();
        salary.setDate(staffSalaryRequest.getDate());
        salary.setStoreId(staffSalaryRequest.getStoreId());
        try {
            i = dao.delete(salary);
        }catch (Exception e){
            log.error("SsmStaffSalaryServiceImpl.delete", e);
        }
        return i;
    }
}
