package com.ssm.manage.service.impl;

import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.core.utils.SnowflakeUtil;
import com.ssm.manage.dao.SsmCourseDao;
import com.ssm.manage.dao.SsmCoursePayListDao;
import com.ssm.manage.dao.SsmRechargeTurnCourseDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.query.CardChangeOutProjectRequest;
import com.ssm.manage.query.CoursePayListRequest;
import com.ssm.manage.query.StaffRequest;
import com.ssm.manage.response.CardChangeOutProjectResponse;
import com.ssm.manage.response.CoursePayListResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmCourseAccountService;
import com.ssm.manage.service.SsmCoursePayListService;
import com.ssm.manage.service.SsmProjectService;
import com.ssm.manage.utils.Constant;
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.interceptor.TransactionAspectSupport;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SsmCoursePayListServiceImpl  extends BaseServiceImpl<CoursePayList> implements SsmCoursePayListService {

    @Autowired
    private SsmCoursePayListDao dao;
    @Autowired
    private SsmRechargeTurnCourseDao rechargeTurnCourseDao;
    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;
    @Autowired
    private SsmCoursePayListService ssmCoursePayListService;

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

    @Override
    public int insert(List<CoursePayListRequest> coursePayListRequests,RechargeTurnCourse rechargeTurnCourse) throws SmsException {
        int i = 0;
        for (CoursePayListRequest coursePayListRequest : coursePayListRequests) {
            CoursePayList coursePayList = new CoursePayList();
            BeanUtils.copyProperties(coursePayListRequest, coursePayList);

            BaseProject baseProject = new BaseProject();
            baseProject.setId(Long.valueOf(coursePayList.getCourseProjectId()));
            BaseProject baseProject1 = ssmProjectService.selectByPrimaryKey(baseProject.getId());
            coursePayList.setId(new SnowflakeUtil(new Random().nextInt(1023)).nextId());
            coursePayList.setRechargeTurnCourseId(rechargeTurnCourse.getId());
            coursePayList.setDocNum(rechargeTurnCourse.getDocNum());
            coursePayList.setDocDate(rechargeTurnCourse.getDocDate());
            coursePayList.setStoreId(rechargeTurnCourse.getStoreId());
            coursePayList.setStoreName(rechargeTurnCourse.getStoreName());
            coursePayList.setSalesAchievement(baseProject1.getSalesAchievement());
            coursePayList.setSalesCommission(baseProject1.getSalesCommission());
            coursePayList.setCardSalesAchievement(baseProject1.getCardSalesAchievement());
            coursePayList.setCardSalesCommission(baseProject1.getCardSalesCommission());

            //设置业务员
            List<StaffRequest> staffLists = coursePayListRequest.getStaffLists();
            if(null != staffLists){
                this.setStaffValue(staffLists,coursePayList);
                //计算业绩
                coursePayList = this.setAchiComm(coursePayList, coursePayList.getRollOffMoney());

                try {
                    i = ssmCoursePayListService.saveNotNull(coursePayList);

                }catch (Exception e){
                    log.error("SsmCoursePayListServiceImpl.insert:", e);
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    throw new SmsException(ResponseCode.UNKNOWN_SYSTEM_EXCEPTION);
                }
            }
        }
        return i;
    }

    @Override
    public List<CoursePayListResponse> selectOutList(Long storeId, String date) {
        List<CoursePayListResponse> listResponses = new ArrayList<>();
        RechargeTurnCourse rechargeTurnCourse = new RechargeTurnCourse();
        rechargeTurnCourse.setStoreId(storeId);
        rechargeTurnCourse.setState(0);
        List<RechargeTurnCourse> rechargeTurnCourses = rechargeTurnCourseDao.select(rechargeTurnCourse);
        List<String> collect = rechargeTurnCourses.stream().map(item -> item.getDocNum()).collect(Collectors.toList());
        Example example = new Example(CoursePayList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("storeId",storeId);
        criteria.andLike("docDate", date + "%");
//        criteria.andEqualTo("state", 0);
        if(collect!=null && collect.size()>0){

            criteria.andIn("docNum",collect);
            List<CoursePayList> coursePayLists = dao.selectByExample(example);
            for (CoursePayList coursePayList : coursePayLists) {
                CoursePayListResponse coursePayListResponse = new CoursePayListResponse();
                BeanUtils.copyProperties(coursePayList,coursePayListResponse);
                //设置业务人员列表
                List<AchiCommEntity> entities = this.getAchiCommList(coursePayList);
                coursePayListResponse.setEntities(entities);
                for (AchiCommEntity entity : entities) {
                    entity.setDocId(coursePayList.getId());
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(Long.valueOf(entity.getProjectId()));
                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                    entity.setProjectId(baseProject1.getProjectNum());
                }
                listResponses.add(coursePayListResponse);
            }
        }
        /*List<RechargeTurnCourse> rechargeTurnCourses = rechargeTurnCourseDao.selectByExample(example);
        List<CoursePayList> coursePayLists = new ArrayList<>();*/

        /*for (RechargeTurnCourse rechargeTurnCours : rechargeTurnCourses) {
            Example examples = new Example(CoursePayList.class);
            Example.Criteria criterias = examples.createCriteria();
            criterias.andEqualTo("docNum",rechargeTurnCours.getDocNum());
            List<CoursePayList> coursePayLists1 = dao.selectByExample(examples);
            for (CoursePayList coursePayList : coursePayLists1) {
                coursePayLists.add(coursePayList);
            }
        }*/




        /*for (CardChangeList cardChangeList : cardChangeLists) {
            Example examples = new Example(CardChangeOutProject.class);
            Example.Criteria criterias = examples.createCriteria();
            criterias.andEqualTo("docNum",cardChangeList.getDocNum());
            //List<CardChangeInProject> cardChangeInProjectlist = ssmCardChangeInProjectService.selectByExample(examples);
            List<CardChangeOutProject> cardChangeOutProjectlist = ssmCardChangeOutProjectService.selectByExample(examples);
            for (CardChangeOutProject cardChangeOutProject : cardChangeOutProjectlist) {
                cardChangeOutProjects.add(cardChangeOutProject);
            }
            *//*for (CardChangeInProject cardChangeInProject : cardChangeOutProjectlist) {
                cardChangeOutProjects.add(cardChangeInProject);
            }*//*
        }
        List<CardChangeOutProjectResponse> listResponses = new ArrayList<>();
        for (CardChangeOutProject cardChangeOutProject :cardChangeOutProjects) {
            CardChangeOutProjectResponse courseListResponse = new CardChangeOutProjectResponse();
            BeanUtils.copyProperties(cardChangeOutProject,courseListResponse);
            //设置业务人员列表
            List<AchiCommEntity> entities = this.getAchiCommList2(cardChangeOutProject);
            courseListResponse.setEntities(entities);
            for (AchiCommEntity entity : entities) {
                entity.setDocId(cardChangeOutProject.getId());
                BaseProject baseProject = new BaseProject();
                baseProject.setId(Long.valueOf(entity.getProjectId()));
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                entity.setProjectId(baseProject1.getProjectNum());
            }
            listResponses.add(courseListResponse);
        }
        return listResponses;*/
        return listResponses;
    }

    @Override
    public List<CoursePayList> selectCourseTurnRecharge(List<String> rechargeTurnCoursesDocNum) {
        Example example = new Example(CoursePayList.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("docNum",rechargeTurnCoursesDocNum);
        return dao.selectByExample(example);
    }

    private List<AchiCommEntity> getAchiCommList(CoursePayList courseList) {
        List<AchiCommEntity> entities = new ArrayList<>();
        BigDecimal hundredBD = com.ssm.manage.utils.BigDecimalUtils.bigDecimal("100");
        Long id = courseList.getCourseAccountId();
        CourseAccount courseAccount = ssmCourseAccountService.selectByPrimaryKey(id);
        if (null != courseList.getSaleOneStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleOneNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleOneStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleOneStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleOneIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleOneRatio());

            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
//                }
            }else {
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                }
            }


            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleTwoStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleTwoNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleTwoStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleTwoStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleTwoIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleTwoRatio());

            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)));
//                }
            }else {
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleTwoRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                }
            }

            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleThreeStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleThreeNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleThreeStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleThreeStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleThreeIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleThreeRatio());

            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)));
//                }
            }else {
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleThreeRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                }
            }

            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleFourStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleFourNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleFourStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleFourStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleFourIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleFourRatio());

            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)));
//                }
            }else {
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleFourRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                }
            }

            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleFiveStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleFiveNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleFiveStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleFiveStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleFiveIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleFiveRatio());

            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){

                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
                                BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)));
                    }else {
                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
                                BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
                    }
                }
                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)));
                }
            }else {
                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){

                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
                                BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)));
                    }else {
                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
                                BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
                    }
                }
                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleFiveRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
                }
            }

            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleSixStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleSixNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleSixStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleSixStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleSixIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleSixRatio());



            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)));
//                }
            }else {
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleSixRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                }
            }

            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleSevenStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleSevenNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleSevenStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleSevenStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleSevenIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleSevenRatio());

            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)));
//                }
            }else {
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleSevenRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                }
            }

            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        if (null != courseList.getSaleEightStaffId()){
            AchiCommEntity achiCommEntity = new AchiCommEntity();
            achiCommEntity.setMoney(courseList.getRollOffMoney());
            achiCommEntity.setMemberNum(String.valueOf(courseAccount.getMemberId()));
            achiCommEntity.setMemberName(courseAccount.getMemberName());
            achiCommEntity.setProjectId(String.valueOf(courseList.getCourseProjectId()));
            achiCommEntity.setDocNum(courseList.getDocNum());
            achiCommEntity.setDocType(Constant.doc_type_recharge_turn_course);
            achiCommEntity.setDocDate(courseList.getDocDate());
            achiCommEntity.setTradeName(courseList.getCourseProjectName());
            achiCommEntity.setBrandNumber(courseList.getSaleEightNumber());
            achiCommEntity.setSaleStaffId(courseList.getSaleEightStaffId());
            achiCommEntity.setSaleStaffName(courseList.getSaleEightStaffName());
            achiCommEntity.setSaleIdentityCard(courseList.getSaleEightIdentity());
            achiCommEntity.setSaleRatio(courseList.getSaleEightRatio());

            if(courseList.getSalesCommission().compareTo(new BigDecimal("1"))==1){
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)));
//                }
            }else {
//                if(courseList.getCardSalesCommission().compareTo(BigDecimal.ZERO)!=0){
//
//                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getCardSalesAchievement(),
//                        BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//
//                    if(courseList.getCardSalesCommission().compareTo(new BigDecimal("1"))==1){
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)));
//                    }else {
//                        achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getCardSalesCommission(),
//                                BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                    }
//                }
//                else {
                achiCommEntity.setSaleAchievementAmount(BigDecimalUtils.multiply(courseList.getSalesAchievement(),
                        BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));

                    achiCommEntity.setSaleCommissionAmount(BigDecimalUtils.multiply(courseList.getSalesCommission(),
                            BigDecimalUtils.div(courseList.getSaleEightRatio(),hundredBD)).multiply(achiCommEntity.getMoney()));
//                }
            }
            /*achiCommEntity.setCashAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeInPayMoney(),courseList.getSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCashComm(BigDecimalUtils.multiply(BigDecimalUtils.multiply(achiCommEntity.getCashAchi(),courseList.getSalesCommission()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));*/

            /*achiCommEntity.setCardAchi(BigDecimalUtils.multiply(BigDecimalUtils.multiply(courseList.getChangeOutMoney(),courseList.getCardSalesAchievement()),BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
            achiCommEntity.setCardComm(achiCommEntity.getCardAchi().multiply(courseList.getCardSalesCommission()).multiply(BigDecimalUtils.div(courseList.getSaleOneRatio(),hundredBD)));
*/
            achiCommEntity.setCardAchi(achiCommEntity.getSaleAchievementAmount());
            achiCommEntity.setCardComm(achiCommEntity.getSaleCommissionAmount());

            if(null == achiCommEntity.getCashAchi()){
                achiCommEntity.setCashAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardAchi() ){
                achiCommEntity.setCardAchi(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCashComm()){
                achiCommEntity.setCashComm(BigDecimal.valueOf(0));
            }
            if(null == achiCommEntity.getCardComm()){
                achiCommEntity.setCardComm(BigDecimal.valueOf(0));
            }
            achiCommEntity.setTotalPerformance(BigDecimalUtils.add(achiCommEntity.getCashAchi(),achiCommEntity.getCardAchi()));
            achiCommEntity.setTotalCommission(BigDecimalUtils.add(achiCommEntity.getCashComm(),achiCommEntity.getCardComm()));

            entities.add(achiCommEntity);
        }
        return entities;
    }

    private CoursePayList setAchiComm(CoursePayList coursePayList, BigDecimal rollOffMoney) {
        //卡付销售业绩标准  >0 <1比率   >1固定金额
        BigDecimal achiRatio = coursePayList.getCardSalesAchievement();
        //卡付销售提成标准  >0 <1比率   >1固定金额
        BigDecimal commRatio = coursePayList.getCardSalesCommission();

        //业绩比率
        if (BigDecimalUtils.compareTo(achiRatio, BigDecimalUtils.bigDecimal("1")) != 1) {
            coursePayList.setCardAchievementAmount(BigDecimalUtils.multiply(achiRatio, rollOffMoney));
        } else {//固定金额
            //cardChangeOutProject.setAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.multiply(BigDecimalUtils.div(changeOutMoney, cardChangeOutProject.getUnitMoney()), achiRatio), times));
            coursePayList.setCardAchievementAmount(BigDecimalUtils.multiply(BigDecimalUtils.div(rollOffMoney, coursePayList.getPrice()), achiRatio));
        }
        //提成比率
        if (BigDecimalUtils.compareTo(commRatio, BigDecimalUtils.bigDecimal("1")) != 1) {
            coursePayList.setCardCommissionAmount(BigDecimalUtils.multiply(commRatio, coursePayList.getCardAchievementAmount()));
        }else {//提成固定金额
            //cardChangeOutProject.setCommissionAmount(BigDecimalUtils.multiply(BigDecimalUtils.multiply(BigDecimalUtils.div(changeOutMoney, cardChangeOutProject.getUnitMoney()), commRatio), times));
            coursePayList.setCardCommissionAmount(BigDecimalUtils.multiply(BigDecimalUtils.div(rollOffMoney, coursePayList.getPrice()), commRatio));
        }

        return coursePayList;
    }

    /**
     * 业务员赋值
     * @param staffLists
     * @param coursePayList
     */
    private void setStaffValue(List<StaffRequest> staffLists, CoursePayList coursePayList) {
        for (int i = 0; i < staffLists.size(); i++) {
            StaffRequest staffRequest = staffLists.get(i);
            if (i == 0){
                coursePayList.setSaleOneStaffId(staffRequest.getId());
                coursePayList.setSaleOneStaffName(staffRequest.getStaffName());
                coursePayList.setSaleOneIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleOneRatio(staffRequest.getRatio());
                coursePayList.setSaleOneNumber(staffRequest.getBrandNumber());
            }else if (i == 1){
                coursePayList.setSaleTwoStaffId(staffRequest.getId());
                coursePayList.setSaleTwoStaffName(staffRequest.getStaffName());
                coursePayList.setSaleTwoIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleTwoRatio(staffRequest.getRatio());
                coursePayList.setSaleTwoNumber(staffRequest.getBrandNumber());
            }else if (i == 2){
                coursePayList.setSaleThreeStaffId(staffRequest.getId());
                coursePayList.setSaleThreeStaffName(staffRequest.getStaffName());
                coursePayList.setSaleThreeIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleThreeRatio(staffRequest.getRatio());
                coursePayList.setSaleThreeNumber(staffRequest.getBrandNumber());
            }else if (i == 3){
                coursePayList.setSaleFourStaffId(staffRequest.getId());
                coursePayList.setSaleFourStaffName(staffRequest.getStaffName());
                coursePayList.setSaleFourIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleFourRatio(staffRequest.getRatio());
                coursePayList.setSaleFourNumber(staffRequest.getBrandNumber());

            }else if (i == 4){
                coursePayList.setSaleFiveStaffId(staffRequest.getId());
                coursePayList.setSaleFiveStaffName(staffRequest.getStaffName());
                coursePayList.setSaleFiveIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleFiveRatio(staffRequest.getRatio());
                coursePayList.setSaleFiveNumber(staffRequest.getBrandNumber());
            }else if (i == 5){
                coursePayList.setSaleSixStaffId(staffRequest.getId());
                coursePayList.setSaleSixStaffName(staffRequest.getStaffName());
                coursePayList.setSaleSixIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleSixRatio(staffRequest.getRatio());
                coursePayList.setSaleSixNumber(staffRequest.getBrandNumber());
            }else if (i == 6){
                coursePayList.setSaleSevenStaffId(staffRequest.getId());
                coursePayList.setSaleSevenStaffName(staffRequest.getStaffName());
                coursePayList.setSaleSevenIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleSevenRatio(staffRequest.getRatio());
                coursePayList.setSaleSevenNumber(staffRequest.getBrandNumber());
            }else if (i == 7){
                coursePayList.setSaleEightStaffId(staffRequest.getId());
                coursePayList.setSaleEightStaffName(staffRequest.getStaffName());
                coursePayList.setSaleEightIdentity(staffRequest.getIdentityCard());
                coursePayList.setSaleEightRatio(staffRequest.getRatio());
                coursePayList.setSaleEightNumber(staffRequest.getBrandNumber());
            }
        }
    }
}
