package com.ssm.manage.service.impl;

import com.github.pagehelper.PageInfo;
import com.ssm.core.service.impl.BaseServiceImpl;
import com.ssm.core.utils.AESUtils;
import com.ssm.core.utils.BigDecimalUtils;
import com.ssm.manage.dao.SsmDocPromotionDao;
import com.ssm.manage.dao.SsmSalesmanDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.extendEntity.AchiCommEntity;
import com.ssm.manage.mapper.SalesmanMapper;
import com.ssm.manage.query.SalesmanRequest;
import com.ssm.manage.query.StaffRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.Executives;
import com.ssm.manage.response.StaffResponse;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class SsmSalesmanServiceImpl  extends BaseServiceImpl<Salesman> implements SsmSalesmanService {

    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmCashService ssmCashService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;
    @Autowired
    private SsmBaseGrouponCardService ssmBaseGrouponCardService;

    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private SsmRepayTradeService ssmRepayTradeService;

    @Autowired
    private SsmRefundRechargeService ssmRefundRechargeService;

    @Autowired
    private SsmRefundCourseService ssmRefundCourseService;


    @Autowired
    private SsmProjectService ssmProjectService;
    @Autowired
    private SsmRechargeTypeSevice ssmRechargeTypeSevice;

    @Autowired
    private SsmRefundOrderServiceImpl ssmRefundOrderService;

    @Autowired
    private SsmRefundCashService ssmRefundCashService;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;

    @Autowired
    private SsmCardChangeInProjectService ssmCardChangeInProjectService;

    @Autowired
    private SsmCardChangeService ssmCardChangeService;

    @Autowired
    private SsmSalesmanDao dao;

    @Autowired
    private SalesmanMapper salesmanMapper;

    @Autowired

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

    @Override
    public List<Executives> selectList(SalesmanRequest salesman) {
        StoreRequest storeRequest1 = new StoreRequest();
        storeRequest1.setId(salesman.getStoreId());
        Long storeId= null;
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest1);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                storeId = pagesorganization.getId();
                continue;
            }
        }
        if(salesman.getStoreIds()!=null && salesman.getStoreIds().size()>0) {
            if (storeId.equals(salesman.getStoreId())) {
                salesman.setStoreId(null);
            }
        }

        List<Salesman> salesmens = new ArrayList<>();
        List<Executives> executives = new ArrayList<>();
        salesman.setState(0);
        List<Long> storeIds = salesman.getStoreIds();
        if(salesman.getStaffType()==0) {
            if (storeIds != null && storeIds.size() > 0) {
                salesman.setState(0);
                salesman.setStoreIds(null);
                salesman.setStoreIdsOrder(storeIds);
                List<Salesman> salesmen1 = dao.selectList(salesman);
                //按员工水牌号分组
                List<Salesman> salesmen3 = salesmen1.stream().filter(item -> item.getOrderType() == 2 || item.getOrderType() == 3 ||item.getOrderType().equals("3") ||item.getOrderType() == 5 || item.getOrderType() == 7 || item.getOrderType().equals("2") || item.getOrderType().equals("5") || item.getOrderType().equals("7")).collect(Collectors.toList());
                for (Salesman salesman1 : salesmen3) {
                    Salesman salesman2 = new Salesman();
                    BeanUtils.copyProperties(salesman1, salesman2);
                    //如果没有项目id
                    if(salesman1.getOrderType()==5 || salesman1.getOrderType().equals("5")){
                        RefundCourse refundCourse = new RefundCourse();
                        refundCourse.setRefundNum(salesman1.getOrderNum());
                        List<RefundCourse> select = ssmRefundCourseService.select(refundCourse);
                        for (RefundCourse course : select) {
                            BaseProject baseProject = ssmProjectService.selectByPrimaryKey(course.getCourseProjectId());
                            if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                                salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                                salesmen1.add(salesman2);
                            } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                                salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                                salesmen1.add(salesman2);
                            }
                        }
                    }
                    if (salesman1.getOrderType() == 7 || salesman1.getOrderType().equals("7")) {
//                    if (salesman1.getProjectId() == 7 ) {
                        RepayTradeList repayTradeList = ssmRepayTradeService.selectByPrimaryKey(Long.valueOf(salesman1.getOrderId()));
                        if(repayTradeList!=null) {
                            String docNum = repayTradeList.getDocNum();
                            if (docNum.startsWith("LC") || docNum.startsWith("ZLC")) {
                                BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
                                if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                                    salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                                    salesmen1.add(salesman2);
                                } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                                    salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                                    salesmen1.add(salesman2);
                                }
                            }
                        }
                    }
                    if (salesman1.getOrderType()==3) {
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(salesman1.getProjectId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                            salesmen1.add(salesman2);
                        }
                    }
                    if(salesman1.getOrderNum().startsWith("CS") && salesman1.getOrderType()==2){
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(salesman1.getProjectId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney().negate()));

                            salesmen1.add(salesman2);
                        }
                        break;
                    }
                    String orderNum = salesman1.getOrderNum();
                    String substring ="";
                    if(orderNum.startsWith("LCT")){
                        substring = orderNum.substring(orderNum.length() - 12, orderNum.length() - 4);
                    }else
                    substring = orderNum.substring(orderNum.length() - 11, orderNum.length() - 3);
                    StringBuilder stringBuilder = new StringBuilder(substring);
                    StringBuilder date = stringBuilder.insert(6, "-").insert(4, "-");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date parse;
//            Salesman salesman2 = new Salesman();
//            BeanUtils.copyProperties(salesman1,salesman2);
                    try {
                        parse = simpleDateFormat.parse(String.valueOf(date));

                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }

                    CourseList courseList = new CourseList();
                    courseList.setDate(parse);
                    courseList.setCourseReatmentNum(orderNum);
                    courseList.setProjctId(salesman1.getProjectId());
                    List<CourseList> courseLists1 = ssmCourseService.selectCourseByProjectAndDocId(courseList);
                    List<CourseList> courseLists = new ArrayList<>();
                    if (courseLists1 != null && courseLists1.size() > 0) {
                        for (int i = 0; i < courseLists1.size(); i++) {
                            if (courseLists1.get(i).getCollectionCashAmount().compareTo(salesman2.getMoney()) == 0) {
                                courseLists.add(courseLists1.get(i));
                                break;
                            }
                        }
                    }
                    for (CourseList list : courseLists) {
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(list.getProjctId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        }
//                        if (list.getIsCooperation() != null && list.getIsCooperation() == 1) {
//                            salesman2.setMoney(list.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
//                            salesmen1.add(salesman2);
//                        } else if (list.getIsCooperation() != null && list.getIsCooperation() == 2) {
//                            salesman2.setMoney(list.getBrokerage().divide(list.getSalesPrice().multiply(BigDecimal.valueOf(list.getMinumumTimes())), 4, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
//                            salesmen1.add(salesman2);
//                        }
                    }
                }
                Map<String, List<Salesman>> map = salesmen1.stream().collect(Collectors.groupingBy(Salesman::getBrandNumber));
                Iterator<Map.Entry<String, List<Salesman>>> entryIterator = map.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    Map.Entry<String, List<Salesman>> entry = entryIterator.next();
                    List<Salesman> salesmen = entry.getValue();
                    //按高管每一个门店进行分组
                    Map<Long, List<Salesman>> map1 = salesmen.stream().collect(Collectors.groupingBy(Salesman::getStoreIdOrder));
                    Iterator<Map.Entry<Long, List<Salesman>>> entryIterators = map1.entrySet().iterator();
                    while (entryIterators.hasNext()) {
                        Map.Entry<Long, List<Salesman>> entrys = entryIterators.next();
                        List<Salesman> salesmen2 = entrys.getValue();
                        List<Salesman> collect = salesmen2.stream().sorted(Comparator.comparing(Salesman::getOrderDate)).collect(Collectors.toList());
                        Executives executives1 = new Executives();
                        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
                        executives1.setStartDate(dFormat.format(collect.get(0).getOrderDate()));
                        executives1.setEndDate(dFormat.format(collect.get(collect.size() - 1).getOrderDate()));
                        StoreRequest storeRequest = new StoreRequest();
                        storeRequest.setId(collect.get(0).getStoreIdOrder());
                        BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                        executives1.setStoreNum(baseStore.getStoreNum());
                        executives1.setStoreName(baseStore.getStoreName());
                        executives1.setBrandNumber(collect.get(0).getBrandNumber());
                        executives1.setStaffName(collect.get(0).getStaffName());
                        executives1.setChia(BigDecimal.valueOf(0));
                        executives1.setCooperationAchi(BigDecimal.valueOf(0));
                        for (Salesman salesman1 : collect) {
                            if (salesman1.getMoney().compareTo(BigDecimal.ZERO) == -1 && !salesman1.getOrderNum().startsWith("LCT") && !salesman1.getOrderNum().startsWith("CZT")&& !salesman1.getOrderNum().startsWith("XFT")) {
                                executives1.setCooperationAchi(executives1.getCooperationAchi().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                            }
                            executives1.setChia(executives1.getChia().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                        }
                        executives1.setCooperationAchi(executives1.getCooperationAchi().negate());
//                executives1.setCooperationAchi(new BigDecimal("1111.1111111111111111111111111111111"));
                        executives.add(executives1);
                    }
                }
            }
            else {
                salesman.setStoreName(null);
                salesman.setStoreNum(null);
                if(salesman.getStoreId()!=null){
                    List<Long> ids = new ArrayList<>();
                    ids.add(salesman.getStoreId());
                    salesman.setStoreIdsOrder(ids);
                }
                List<Salesman> salesmen1 = dao.selectList(salesman);
                //按员工水牌号分组
                List<Salesman> salesmen3 = salesmen1.stream().filter(item -> item.getOrderType() == 2 || item.getOrderType() == 3 ||item.getOrderType().equals("3") || item.getOrderType() == 5 || item.getOrderType() == 7 || item.getOrderType().equals("2") ||  item.getOrderType().equals("5") || item.getOrderType().equals("7")).collect(Collectors.toList());
                for (Salesman salesman1 : salesmen3) {
                    Salesman salesman2 = new Salesman();
                    BeanUtils.copyProperties(salesman1, salesman2);
                    //如果没有项目id
                    if (salesman1.getOrderType()==7) {
                        RepayTradeList repayTradeList = ssmRepayTradeService.selectByPrimaryKey(Long.valueOf(salesman1.getOrderId()));
                        String docNum = repayTradeList.getDocNum();
                        if (docNum.startsWith("LC") || docNum.startsWith("ZLC")) {
                            BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
                            if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                                salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                                salesmen1.add(salesman2);
                            } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                                salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                                salesmen1.add(salesman2);
                            }
                        }
                    }
                    if (salesman1.getOrderType()==3) {
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(salesman1.getProjectId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                            salesmen1.add(salesman2);
                        }
                    }
                    if(salesman1.getOrderNum().startsWith("CS") && salesman1.getOrderType()==2){
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(salesman1.getProjectId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        }
                        break;
                    }
                    String orderNum = salesman1.getOrderNum();
                    String substring = "";
                    if(orderNum.startsWith("LCT")){
                        substring = orderNum.substring(orderNum.length() - 12, orderNum.length() - 4);
                    }else
                    substring = orderNum.substring(orderNum.length() - 11, orderNum.length() - 3);
                    StringBuilder stringBuilder = new StringBuilder(substring);
                    StringBuilder date = stringBuilder.insert(6, "-").insert(4, "-");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date parse;
//            Salesman salesman2 = new Salesman();
//            BeanUtils.copyProperties(salesman1,salesman2);
                    try {
                        parse = simpleDateFormat.parse(String.valueOf(date));

                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }

                    CourseList courseList = new CourseList();
                    courseList.setDate(parse);
                    courseList.setCourseReatmentNum(orderNum);
                    courseList.setProjctId(salesman1.getProjectId());
                    List<CourseList> courseLists1 = ssmCourseService.selectCourseByProjectAndDocId(courseList);
                    List<CourseList> courseLists = new ArrayList<>();
                    if (courseLists1 != null && courseLists1.size() > 0) {
                        for (int i = 0; i < courseLists1.size(); i++) {
                            if (courseLists1.get(i).getCollectionCashAmount().compareTo(salesman2.getMoney()) == 0) {
                                courseLists.add(courseLists1.get(i));
                                break;
                            }
                        }
                    }
                    for (CourseList list : courseLists) {
//                        ssmProjectService.selectByPrimaryKey()
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(list.getProjctId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        }
                    }
                }
                Map<String, List<Salesman>> map = salesmen1.stream().collect(Collectors.groupingBy(Salesman::getBrandNumber));
                Iterator<Map.Entry<String, List<Salesman>>> entryIterator = map.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    Map.Entry<String, List<Salesman>> entry = entryIterator.next();
                    List<Salesman> salesmen = entry.getValue();
                    //按高管每一个门店进行分组
                    Map<Long, List<Salesman>> map1 = salesmen.stream().collect(Collectors.groupingBy(Salesman::getStoreIdOrder));
                    Iterator<Map.Entry<Long, List<Salesman>>> entryIterators = map1.entrySet().iterator();
                    while (entryIterators.hasNext()) {
                        Map.Entry<Long, List<Salesman>> entrys = entryIterators.next();
                        List<Salesman> salesmen2 = entrys.getValue();
                        List<Salesman> collect = salesmen2.stream().sorted(Comparator.comparing(Salesman::getOrderDate)).collect(Collectors.toList());
                        Executives executives1 = new Executives();
                        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
                        executives1.setStartDate(dFormat.format(collect.get(0).getOrderDate()));
                        executives1.setEndDate(dFormat.format(collect.get(collect.size() - 1).getOrderDate()));
                        StoreRequest storeRequest = new StoreRequest();
                        storeRequest.setId(collect.get(0).getStoreIdOrder());
                        BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                        executives1.setStoreNum(baseStore.getStoreNum());
                        executives1.setStoreName(baseStore.getStoreName());
                        executives1.setBrandNumber(collect.get(0).getBrandNumber());
                        executives1.setStaffName(collect.get(0).getStaffName());
                        executives1.setChia(BigDecimal.valueOf(0));
                        executives1.setCooperationAchi(BigDecimal.valueOf(0));
                        for (Salesman salesman1 : collect) {
                            if (salesman1.getMoney().compareTo(BigDecimal.ZERO) == -1 && !salesman1.getOrderNum().startsWith("LCT") && !salesman1.getOrderNum().startsWith("CZT")&& !salesman1.getOrderNum().startsWith("XFT")) {
                                executives1.setCooperationAchi(executives1.getCooperationAchi().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                            }
                            executives1.setChia(executives1.getChia().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                        }
                        executives1.setCooperationAchi(executives1.getCooperationAchi().negate());
//                executives1.setCooperationAchi(new BigDecimal("1111.1111111111111111111111111111111"));
                        executives.add(executives1);
                    }
                }
            }
        }
        else {
                salesman.setStoreName(null);
                salesman.setStoreNum(null);
                if(salesman.getStoreId()!=null){
                    List<Long> ids = new ArrayList<>();
                    ids.add(salesman.getStoreId());
                    salesman.setStoreIds(ids);
                }
                List<Salesman> salesmen1 = dao.selectList(salesman);
                //按员工水牌号分组
                List<Salesman> salesmen3 = salesmen1.stream().filter(item -> item.getOrderType() == 2 || item.getOrderType() == 3 ||item.getOrderType().equals("3") || item.getOrderType() == 5 || item.getOrderType() == 7 || item.getOrderType().equals("2") || item.getOrderType().equals("5") || item.getOrderType().equals("7")).collect(Collectors.toList());
                for (Salesman salesman1 : salesmen3) {
                    Salesman salesman2 = new Salesman();
                    BeanUtils.copyProperties(salesman1, salesman2);
                    //如果没有项目id
                    if(salesman1.getOrderType()==5 || salesman1.getOrderType().equals("5")){
                        RefundCourse refundCourse = new RefundCourse();
                        refundCourse.setRefundNum(salesman1.getOrderNum());
                        refundCourse.setId(Long.valueOf(salesman1.getOrderId()));
                        List<RefundCourse> select = ssmRefundCourseService.select(refundCourse);
                        if(select==null ||select.size()==0){
                            refundCourse.setId(null);
                            select = ssmRefundCourseService.select(refundCourse);
                        }
                        for (RefundCourse course : select) {
                            if(course.getCourseRefundMoney().compareTo(salesman1.getMoney().negate())==0) {
                                BaseProject baseProject = ssmProjectService.selectByPrimaryKey(course.getCourseProjectId());
                                if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                                    salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney().negate()));
                                    salesmen1.add(salesman2);
                                } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                                    salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney().negate()));
                                    salesmen1.add(salesman2);
                                }
                            }
                        }
                    }
                    if (salesman1.getOrderType()==7) {
                        RepayTradeList repayTradeList = ssmRepayTradeService.selectByPrimaryKey(Long.valueOf(salesman1.getOrderId()));
                        String docNum = repayTradeList.getDocNum();
                        if (docNum.startsWith("LC") || docNum.startsWith("ZLC")) {
                            BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
                            if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                                salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                                salesmen1.add(salesman2);
                            } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                                salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                                salesmen1.add(salesman2);
                            }
                        }
                    }
                    if (salesman1.getOrderType()==3) {
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(salesman1.getProjectId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                            salesmen1.add(salesman2);
                        }
                    }
                    String orderNum = salesman1.getOrderNum();
                    String substring = "";
                    if(orderNum.startsWith("LCT")||orderNum.startsWith("XFT")||orderNum.startsWith("CZT")){
                        substring = orderNum.substring(orderNum.length() - 12, orderNum.length() - 4);
                    }else {
                        substring = orderNum.substring(orderNum.length() - 11, orderNum.length() - 3);
                    }
                    StringBuilder stringBuilder = new StringBuilder(substring);
                    StringBuilder date = stringBuilder.insert(6, "-").insert(4, "-");
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
                    Date parse;
//            Salesman salesman2 = new Salesman();
//            BeanUtils.copyProperties(salesman1,salesman2);
                    try {
                        parse = simpleDateFormat.parse(String.valueOf(date));

                    } catch (ParseException e) {
                        throw new RuntimeException(e);
                    }
                    if(salesman1.getOrderNum().startsWith("CS") && salesman1.getOrderType()==2){
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(salesman1.getProjectId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        }
                        break;
                    }
                    CourseList courseList = new CourseList();
                    courseList.setDate(parse);
                    courseList.setCourseReatmentNum(orderNum);
                    courseList.setProjctId(salesman1.getProjectId());
                    List<CourseList> courseLists1 = ssmCourseService.selectCourseByProjectAndDocId(courseList);
                    List<CourseList> courseLists = new ArrayList<>();
                    if (courseLists1 != null && courseLists1.size() > 0) {
                        for (int i = 0; i < courseLists1.size(); i++) {
                            if (courseLists1.get(i).getCollectionCashAmount().compareTo(salesman2.getMoney()) == 0) {
                                courseLists.add(courseLists1.get(i));
                                break;
                            }
                        }
                    }
                    for (CourseList list : courseLists) {
                        BaseProject baseProject = ssmProjectService.selectByPrimaryKey(list.getProjctId());
                        if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                            salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney().negate()));
                            salesmen1.add(salesman2);
                        }
//                        if (list.getIsCooperation() != null && list.getIsCooperation() == 1) {
//                            salesman2.setMoney(list.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
//                            salesmen1.add(salesman2);
//                        } else if (list.getIsCooperation() != null && list.getIsCooperation() == 2) {
//                            salesman2.setMoney(list.getBrokerage().divide(list.getSalesPrice().multiply(BigDecimal.valueOf(list.getMinumumTimes())), 6, BigDecimal.ROUND_HALF_UP)
//                                    .multiply(salesman1.getMoney()).negate());
//                            salesmen1.add(salesman2);
//                        }
                    }
                }
                Map<String, List<Salesman>> map = salesmen1.stream().collect(Collectors.groupingBy(Salesman::getBrandNumber));
                Iterator<Map.Entry<String, List<Salesman>>> entryIterator = map.entrySet().iterator();
                while (entryIterator.hasNext()) {
                    Map.Entry<String, List<Salesman>> entry = entryIterator.next();
                    List<Salesman> salesmen = entry.getValue();
                    //按高管每一个门店进行分组
                    Map<Long, List<Salesman>> map1 = salesmen.stream().collect(Collectors.groupingBy(Salesman::getStoreIdOrder));
                    Iterator<Map.Entry<Long, List<Salesman>>> entryIterators = map1.entrySet().iterator();
                    while (entryIterators.hasNext()) {
                        Map.Entry<Long, List<Salesman>> entrys = entryIterators.next();
                        List<Salesman> salesmen2 = entrys.getValue();
                        List<Salesman> collect = salesmen2.stream().sorted(Comparator.comparing(Salesman::getOrderDate)).collect(Collectors.toList());
                        Executives executives1 = new Executives();
                        DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
                        executives1.setStartDate(dFormat.format(collect.get(0).getOrderDate()));
                        executives1.setEndDate(dFormat.format(collect.get(collect.size() - 1).getOrderDate()));
                        StoreRequest storeRequest = new StoreRequest();
                        storeRequest.setId(collect.get(0).getStoreIdOrder());
                        BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                        executives1.setStoreNum(baseStore.getStoreNum());
                        executives1.setStoreName(baseStore.getStoreName());
                        executives1.setBrandNumber(collect.get(0).getBrandNumber());
                        executives1.setStaffName(collect.get(0).getStaffName());
                        executives1.setChia(BigDecimal.valueOf(0));
                        executives1.setCooperationAchi(BigDecimal.valueOf(0));
                        for (Salesman salesman1 : collect) {
                            System.out.println(salesman1.getOrderType()!=5);
                            System.out.println(!salesman1.getOrderType().toString().equals("5"));
                            if (salesman1.getOrderType()!=4  && salesman1.getMoney().compareTo(BigDecimal.ZERO) == -1 && (salesman1.getOrderType()!=5 || !salesman1.getOrderType().toString().equals("5"))) {

                                executives1.setCooperationAchi(executives1.getCooperationAchi().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                            }
                            if((salesman1.getOrderType()==5 || salesman1.getOrderType().toString().equals("5")) && salesman1.getMoney().compareTo(BigDecimal.ZERO) == 1){
                                executives1.setCooperationAchi(executives1.getCooperationAchi().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                            }
//                            if((salesman1.getOrderType()==7 || salesman1.getOrderType().toString().equals("7")) && salesman1.getMoney().compareTo(BigDecimal.ZERO) == 1){
//                                executives1.setCooperationAchi(executives1.getCooperationAchi().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
//                            }
                            executives1.setChia(executives1.getChia().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                        }
                        executives1.setCooperationAchi(executives1.getCooperationAchi().negate());
//                executives1.setCooperationAchi(new BigDecimal("1111.1111111111111111111111111111111"));
                        executives.add(executives1);
                    }
                }
        }



        return executives;
    }

    @Override
    public List<Executives> selectLists(SalesmanRequest salesman) {
        List<Executives> executives = new ArrayList<>();
        salesman.setState(0);
//        List<Salesman> salesmen1 = dao.selectList(salesman);
        List<Salesman> salesmen1 = dao.selectList(salesman);
        //按时间排序
        List<Salesman> collect = salesmen1.stream().sorted(Comparator.comparing(Salesman::getOrderDate)).collect(Collectors.toList());
        for (Salesman salesman1 : collect) {
            Executives executives1 = new Executives();
            DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
            executives1.setOrderDate(dFormat.format(salesman1.getOrderDate()));
            executives1.setBrandNumber(salesman1.getBrandNumber());
            executives1.setStaffName(salesman1.getStaffName());
            executives1.setOrderType(salesman1.getOrderType());
            executives1.setStoreNum(salesman1.getStoreNum());
            executives1.setStoreName(salesman1.getStoreName());
            executives1.setOperatorBy(salesman1.getOperatorBy());
            executives1.setCooperationAchi(BigDecimal.ZERO);
//            if ("0".equals(staffStatistic.getClassify())) {
//                //查询充值业务员信息
//                recList = ssmRechargeService.selectList(storeId, dateStr, date);
//            } else if ("1".equals(staffStatistic.getClassify())) {c3c
//                //查询疗程业务员信息
//                courseList = ssmCourseService.selectList(storeId, dateStr, date);
//            } else if ("2".equals(staffStatistic.getClassify())) {
//                //查询服务单业务员信息
//                cashList = ssmCashService.selectList(storeId, dateStr, date);
//            } else if ("3".equals(staffStatistic.getClassify())) {
//                //查询还款单业务员信息
//                RepayLists = ssmRepayService.selectList(storeId, dateStr, null);
//            } else if ("4".equals(staffStatistic.getClassify())) {
//                //查询充值退款单业务员
//                refundRecList = ssmRefundRechargeService.selectList(storeId, dateStr);
//            } else if ("5".equals(staffStatistic.getClassify())) {
//                //查询疗程退款单业务员
//                refundCourseList = ssmRefundCourseService.selectList(storeId, dateStr);
//            } else if ("6".equals(staffStatistic.getClassify())) {
//                //查询收银退款单业务员
//                //refundCashList = ssmRefundCashService.selectList(storeId, dateStr);
//                refundCashList = ssmRefundCashService.selectLists(storeId, dateStr,date);
//            }else if ("7".equals(staffStatistic.getClassify())) {
//                //查询促销单业务员
//                promotionList = ssmDocPromotionService.selectList(storeId, dateStr);
//            }else if ("8".equals(staffStatistic.getClassify())) {
//                //查询加价转疗程单业务员
//                cardChangeLists = ssmCardChangeService.selectList(storeId, dateStr);
//            }
            if(salesman1.getOrderType() == 0){
                CashList cashList = new CashList();
                cashList.setId(Long.valueOf(salesman1.getOrderId()));
                cashList.setCashDate(salesman1.getOrderDate());
                //收银单
                List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                if(null != cashLists.get(0).getMemberName()){
                    executives1.setVIPName(cashLists.get(0).getMemberName());
                    if(cashLists.get(0).getMemberId()!=null) {
                        StoreMemberList storeMemberList = new StoreMemberList();
                        storeMemberList.setId(cashLists.get(0).getMemberId());
                        StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                        executives1.setVIPNum(storeMemberList1.getMemberNum());
                    }
                    executives1.setOrderId(cashLists.get(0).getId());
                }
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(cashLists.get(0).getCashNum()));
                executives1.setOrderName(String.valueOf(cashLists.get(0).getProjectName()));
                executives1.setNum(String.valueOf(cashLists.get(0).getProjectNum()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(cashLists.get(0).getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                for (CashList list : cashLists) {
                    //executives1.setMoney(list.getTransactionPrice().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))).add(executives1.getMoney()));
                    executives1.setMoney(list.getCollectionCashAmount().add(executives1.getMoney()));
                    executives1.setOrderId(list.getId());
                }
                executives1.setCooperationAchi(BigDecimal.ZERO);



            }
            else if(salesman1.getOrderType() == 1){
                //充值单
                RechargeList rechargeList = new RechargeList();
                rechargeList.setId(Long.valueOf(salesman1.getOrderId()));
                rechargeList.setRechargeDate(salesman1.getOrderDate());
                List<RechargeList> select = ssmRechargeService.selectListSalem(rechargeList);
                executives1.setVIPName(select.get(0).getMemberName());
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(select.get(0).getRechargeNum()));
                executives1.setOrderName(String.valueOf(select.get(0).getRechargeTypeName()));
                executives1.setNum(String.valueOf(select.get(0).getRechargeTypeNum()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(select.get(0).getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                for (RechargeList list : select) {
                    //executives1.setMoney(list.getCollectionCashAmount().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))).add(executives1.getMoney()));
                    executives1.setMoney(list.getCollectionCashAmount().add(executives1.getMoney()));
                    executives1.setOrderId(list.getId());
                }
                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(select.get(0).getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
                executives1.setCooperationAchi(BigDecimal.ZERO);
            }
            else if(salesman1.getOrderType() == 2 && !salesman1.getOrderNum().startsWith("CS")){
                //疗程单
                CourseList courseList = new CourseList();
                courseList.setId(Long.valueOf(salesman1.getOrderId()));
                courseList.setDate(salesman1.getOrderDate());
                List<CourseList> courseLists = ssmCourseService.selectListSalem(courseList);
                executives1.setVIPName(courseLists.get(0).getMemberName());
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(courseLists.get(0).getCourseReatmentNum()));
                executives1.setOrderName(String.valueOf(courseLists.get(0).getProjectName()));
                executives1.setNum(String.valueOf(courseLists.get(0).getProjectNum()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(courseLists.get(0).getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                executives1.setCooperationAchi(BigDecimal.ZERO);
                for (CourseList list : courseLists) {
                    //如果没有项目id
                    if (salesman1.getProjectId() == null) {
                        RepayTradeList repayTradeList = ssmRepayTradeService.selectByPrimaryKey(Long.valueOf(salesman1.getOrderId()));
                        String docNum = repayTradeList.getDocNum();
                        if (docNum.startsWith("LC") || docNum.startsWith("ZLC")) {
                            BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
                            if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                                executives1.setCooperationAchi(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                            } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                                executives1.setCooperationAchi(baseProject.getBrokerage().divide(baseProject.getPrice().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 4, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                            }
                        }

                    }
                    BaseProject baseProject = ssmProjectService.selectByPrimaryKey(list.getProjctId());
                    if (baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                        executives1.setCooperationAchi(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                    } else if (baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                        executives1.setCooperationAchi(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                    }
                    //executives1.setMoney(BigDecimalUtils.add(list.getCollectionRechargeAmount(),list.getCollectionCashAmount()).multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))).add(executives1.getMoney()));
                    executives1.setMoney(BigDecimalUtils.add(list.getCollectionRechargeAmount(),list.getCollectionCashAmount()).add(executives1.getMoney()));
                    executives1.setOrderId(list.getId());
                }

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(courseLists.get(0).getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());

            } else if(salesman1.getOrderType() == 2 && salesman1.getOrderNum().startsWith("CS")){
                //疗程单
                CourseList courseList = new CourseList();
                courseList.setId(Long.valueOf(salesman1.getOrderId()));
                courseList.setDate(salesman1.getOrderDate());
                List<CourseList> courseLists = ssmCourseService.selectByCourseListMemberInit(courseList);
                executives1.setVIPName(courseLists.get(0).getMemberName());
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(courseLists.get(0).getCourseReatmentNum()));
                executives1.setOrderName(String.valueOf(courseLists.get(0).getProjectName()));
                executives1.setNum(String.valueOf(courseLists.get(0).getProjectNum()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(courseLists.get(0).getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                executives1.setCooperationAchi(BigDecimal.ZERO);
                for (CourseList list : courseLists) {
                    //如果没有项目id
                    if (salesman1.getProjectId() == null) {
                        RepayTradeList repayTradeList = ssmRepayTradeService.selectByPrimaryKey(Long.valueOf(salesman1.getOrderId()));
                        String docNum = repayTradeList.getDocNum();
                        if (docNum.startsWith("LC") || docNum.startsWith("ZLC")) {
                            BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
                            if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                                executives1.setCooperationAchi(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                            } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                                executives1.setCooperationAchi(baseProject.getBrokerage().divide(baseProject.getPrice().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 4, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                            }
                        }

                    }
                    BaseProject baseProject = ssmProjectService.selectByPrimaryKey(list.getProjctId());
                    if (baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 1) {
                        executives1.setCooperationAchi(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                    } else if (baseProject.getSmokeCommissionWay() != null && baseProject.getSmokeCommissionWay() == 0) {
                        executives1.setCooperationAchi(baseProject.getBrokerage().divide(baseProject.getPriceMember().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 10, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                    }
                    //executives1.setMoney(BigDecimalUtils.add(list.getCollectionRechargeAmount(),list.getCollectionCashAmount()).multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))).add(executives1.getMoney()));
                    executives1.setMoney(BigDecimalUtils.add(list.getCollectionRechargeAmount(),list.getCollectionCashAmount()).add(executives1.getMoney()));
                    executives1.setOrderId(list.getId());
                }

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(courseLists.get(0).getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());

            }else if(salesman1.getOrderType() == 3){
                //加价转疗程
                CardChangeInProject cardChangeInProject = new CardChangeInProject();
                cardChangeInProject.setId(Long.valueOf(salesman1.getOrderId()));
                CardChangeInProject cardChangeInProject1 = ssmCardChangeInProjectService.selectOne(cardChangeInProject);
                executives1.setVIPName(cardChangeInProject1.getMemberName());
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(cardChangeInProject1.getDocNum()));
                executives1.setOrderName(String.valueOf(cardChangeInProject1.getProjectName()));
               // executives1.setMoney(cardChangeInProject1.getChangeInPayMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                BaseProject baseProject = new BaseProject();
                baseProject.setId(cardChangeInProject1.getProjectId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                if (baseProject1.getIsCooperation() != null && baseProject1.getIsCooperation() == 1) {
                    executives1.setCooperationAchi(baseProject1.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                } else if (baseProject1.getIsCooperation() != null && baseProject1.getIsCooperation() == 2) {
                    executives1.setCooperationAchi(baseProject1.getBrokerage().divide(baseProject1.getPrice().multiply(BigDecimal.valueOf(baseProject1.getMinumumTimes())), 4, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                }
                executives1.setNum(String.valueOf(baseProject1.getProjectNum()));
                executives1.setMoney(salesman1.getMoney());
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(cardChangeInProject1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                executives1.setOrderId(cardChangeInProject1.getId());
                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(cardChangeInProject1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 4){
                //充值退款单
                RefundRecharge refundRecharge = new RefundRecharge();
                refundRecharge.setRefundNum(salesman1.getOrderNum());
                List<RefundRecharge> select = ssmRefundRechargeService.select(refundRecharge);
                RefundRecharge refundRecharge1 = select.get(0);

                executives1.setVIPName(refundRecharge1.getMemberName());
                executives1.setOrderNum(String.valueOf(refundRecharge1.getRefundNum()));
                executives1.setOrderName(String.valueOf(refundRecharge1.getRechargeTypeName()));
                //executives1.setMoney(refundRecharge1.getRecRefundMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                RechargeType rechargeType = new RechargeType();
                rechargeType.setId(refundRecharge1.getRechargeTypeId());
                RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                executives1.setNum(rechargeType1.getRechargeTypeNum());
                executives1.setOrderId(refundRecharge1.getId());
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundRecharge1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(refundRecharge1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 5){
                //疗程退款单
                RefundCourse refundCourse = new RefundCourse();
                refundCourse.setRefundNum(salesman1.getOrderNum());
                refundCourse.setCourseProjectId(salesman1.getProjectId());
                List<RefundCourse> select = ssmRefundCourseService.select(refundCourse);
                RefundCourse refundCourse1 = new RefundCourse();
                for (RefundCourse course : select) {
                    if(course.getCourseRefundMoney().compareTo(salesman1.getMoney().negate())==0){
                         refundCourse1 = course;
                    }
                }
//                refundCourse.setId(Long.valueOf(salesman1.getOrderId()));
//                RefundCourse refundCourse1 = ssmRefundCourseService.selectOne(refundCourse);
                executives1.setVIPName(refundCourse1.getMemberName());
                executives1.setOrderNum(String.valueOf(refundCourse1.getRefundNum()));
                executives1.setOrderName(String.valueOf(refundCourse1.getCourseProjectName()));
                //executives1.setMoney(refundCourse1.getCourseRefundMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                BaseProject baseProject2 = new BaseProject();
                baseProject2.setId(refundCourse1.getCourseProjectId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject2);
                executives1.setNum(String.valueOf(baseProject1.getProjectNum()));
                executives1.setOrderId(refundCourse1.getId());
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundCourse1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                executives1.setCooperationAchi(BigDecimal.ZERO);
                if (baseProject1.getSmokeCommissionWay() != null && baseProject1.getSmokeCommissionWay() == 1) {
                    executives1.setCooperationAchi(baseProject1.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                } else if (baseProject1.getSmokeCommissionWay() != null && baseProject1.getSmokeCommissionWay() == 0) {
                    executives1.setCooperationAchi(baseProject1.getBrokerage().divide(baseProject1.getPrice().multiply(BigDecimal.valueOf(baseProject1.getMinumumTimes())), 4, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                }

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(refundCourse1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 6){
                //消费退
                RefundCash refundCash = new RefundCash();
                refundCash.setRefundNum(salesman1.getOrderNum());
                RefundCash refundCourse1 = ssmRefundCashService.selectOne(refundCash);
                executives1.setVIPName(refundCourse1.getMemberName());
                executives1.setOrderNum(String.valueOf(refundCourse1.getRefundNum()));
                executives1.setOrderName(String.valueOf(refundCourse1.getProjectName()));
                //executives1.setMoney(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                executives1.setOrderId(refundCourse1.getId());
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundCourse1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                if(refundCourse1.getMemberId()!=null) {
                    storeMemberList.setId(refundCourse1.getMemberId());

                    StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                    executives1.setVIPNum(storeMemberList1.getMemberNum());
                }
            }else if(salesman1.getOrderType() == 7){
                //还款单
                RepayTradeList repayList = new RepayTradeList();
                repayList.setId(Long.valueOf(salesman1.getOrderId()));
                RepayTradeList repayList1 = ssmRepayTradeService.selectOne(repayList);
                executives1.setVIPName(repayList1.getMemberName());
                executives1.setOrderNum(String.valueOf(repayList1.getRepayNum()));

                //executives1.setMoney(repayList1.getPayMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                if("card_recharge".equals(repayList1.getDocType())){
                    RechargeType rechargeType = new RechargeType();
                    rechargeType.setId(repayList1.getTradeId());
                    RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                    executives1.setNum(rechargeType1.getRechargeTypeNum());
                    executives1.setOrderName(String.valueOf(rechargeType1.getRechargeTypeName()));
                }else if("card_use".equals(repayList1.getDocType())){
                    BaseProject baseProject2 = new BaseProject();
                    baseProject2.setId(repayList1.getTradeId());
                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject2);
                    if (salesman1.getProjectId() == null) {
                        RepayTradeList repayTradeList = ssmRepayTradeService.selectByPrimaryKey(Long.valueOf(salesman1.getOrderId()));
                        String docNum = repayTradeList.getDocNum();
                        if (docNum.startsWith("LC") || docNum.startsWith("ZLC")) {
                            BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
                            if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==1) {
                                executives1.setCooperationAchi(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                            } else if (baseProject.getIsCooperation() != null && baseProject.getIsCooperation() == 1 && baseProject.getSmokeCommissionWay()!=null &&baseProject.getSmokeCommissionWay()==0) {
                                executives1.setCooperationAchi(baseProject.getBrokerage().divide(baseProject.getPrice().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())), 4, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                            }
                        }

                    }
                    if (baseProject1.getSmokeCommissionWay() != null && baseProject1.getSmokeCommissionWay() == 1) {
                        executives1.setCooperationAchi(baseProject1.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()));
                    } else if (baseProject1.getSmokeCommissionWay() != null && baseProject1.getSmokeCommissionWay() == 0) {
                        executives1.setCooperationAchi(baseProject1.getBrokerage().divide(baseProject1.getPrice().multiply(BigDecimal.valueOf(baseProject1.getMinumumTimes())), 4, BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()));
                    }
                    executives1.setNum(String.valueOf(baseProject1.getProjectNum()));
                    executives1.setOrderName(String.valueOf(baseProject1.getProjectName()));
                }else{
                    executives1.setOrderName(repayList1.getTradeName());
                }

                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(repayList1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                executives1.setOrderId(repayList1.getId());
                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(repayList1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 8){
                //促销单
                DocPromotion docPromotion = new DocPromotion();
                docPromotion.setId(Long.valueOf(salesman1.getOrderId()));
                DocPromotion docPromotion1 = ssmDocPromotionService.selectOne(docPromotion);
                executives1.setVIPName(docPromotion1.getMemberName());
                executives1.setOrderNum(String.valueOf(docPromotion1.getPromotionNum()));
                executives1.setOrderName(String.valueOf(docPromotion1.getPromotionName()));
                //executives1.setMoney(docPromotion1.getCollectionCashAmount().add(docPromotion1.getCollectionRechargeAmount()).multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                executives1.setOrderId(docPromotion1.getId());
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(docPromotion1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(docPromotion1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 9){
                BaseGrouponCard baseGrouponCard = new BaseGrouponCard();
                baseGrouponCard.setId(Long.valueOf(salesman1.getOrderId()));
                baseGrouponCard = ssmBaseGrouponCardService.selectByPrimaryKey(baseGrouponCard.getId());
                RechargeType rechargeType = ssmRechargeTypeSevice.selectByPrimaryKey(baseGrouponCard.getRechargeTypeId());
                executives1.setOperatorBy(baseGrouponCard.getAuditBy());
                executives1.setMoney(baseGrouponCard.getRechargeMoney().multiply(BigDecimal.valueOf(baseGrouponCard.getQuantity())));
                executives1.setOrderNum(baseGrouponCard.getDocNum());
                executives1.setNum(rechargeType.getRechargeTypeNum());
                executives1.setOrderNum(baseGrouponCard.getDocNum());
                executives1.setOrderName(rechargeType.getRechargeTypeName());
                executives1.setOrderId(baseGrouponCard.getId());
            }
            //会员名称
            //项目编码
            //项目名称
            executives1.setRatio(salesman1.getRatio());
            executives1.setChia(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
            executives1.setCooperationAchi(executives1.getCooperationAchi().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
            executives1.setChia(executives1.getChia().subtract(executives1.getCooperationAchi()));
            executives1.setCooperationAchi(executives1.getCooperationAchi().setScale(3,BigDecimal.ROUND_HALF_UP));
            executives1.setChia(executives1.getChia().setScale(3,BigDecimal.ROUND_HALF_UP));
            executives.add(executives1);
        }



        return executives;
    }

    @Override
    public List<Executives> selectListswhole(SalesmanRequest salesman) {
        List<Executives> executives = new ArrayList<>();
        salesman.setState(0);
        salesman.setStaffName(null);
        salesman.setStaffId(null);
        salesman.setStaffNum(null);
        salesman.setBrandNumber(null);
        List<Long> lists = new ArrayList<>();
        StoreRequest storeRequest1 = new StoreRequest();
        storeRequest1.setId(salesman.getStoreId());
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest1);
        for (BaseStores pagesorganization : pagesorganizations) {
            lists.add(pagesorganization.getId());
        }
        salesman.setStoreIds(lists);
        List<Salesman> salesmen1 = dao.selectList(salesman);
        //按时间排序
        List<Salesman> collect = salesmen1.stream().sorted(Comparator.comparing(Salesman::getOrderDate)).collect(Collectors.toList());
        for (Salesman salesman1 : collect) {
            Executives executives1 = new Executives();
            DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
            executives1.setOrderDate(dFormat.format(salesman1.getOrderDate()));
            executives1.setBrandNumber(salesman1.getBrandNumber());
            executives1.setStaffName(salesman1.getStaffName());
            executives1.setOrderType(salesman1.getOrderType());
            executives1.setStoreNum(salesman1.getStoreNum());
            executives1.setStoreName(salesman1.getStoreName());
            executives1.setOperatorBy(salesman1.getOperatorBy());
//            if ("0".equals(staffStatistic.getClassify())) {
//                //查询充值业务员信息
//                recList = ssmRechargeService.selectList(storeId, dateStr, date);
//            } else if ("1".equals(staffStatistic.getClassify())) {c3c
//                //查询疗程业务员信息
//                courseList = ssmCourseService.selectList(storeId, dateStr, date);
//            } else if ("2".equals(staffStatistic.getClassify())) {
//                //查询服务单业务员信息
//                cashList = ssmCashService.selectList(storeId, dateStr, date);
//            } else if ("3".equals(staffStatistic.getClassify())) {
//                //查询还款单业务员信息
//                RepayLists = ssmRepayService.selectList(storeId, dateStr, null);
//            } else if ("4".equals(staffStatistic.getClassify())) {
//                //查询充值退款单业务员
//                refundRecList = ssmRefundRechargeService.selectList(storeId, dateStr);
//            } else if ("5".equals(staffStatistic.getClassify())) {
//                //查询疗程退款单业务员
//                refundCourseList = ssmRefundCourseService.selectList(storeId, dateStr);
//            } else if ("6".equals(staffStatistic.getClassify())) {
//                //查询收银退款单业务员
//                //refundCashList = ssmRefundCashService.selectList(storeId, dateStr);
//                refundCashList = ssmRefundCashService.selectLists(storeId, dateStr,date);
//            }else if ("7".equals(staffStatistic.getClassify())) {
//                //查询促销单业务员
//                promotionList = ssmDocPromotionService.selectList(storeId, dateStr);
//            }else if ("8".equals(staffStatistic.getClassify())) {
//                //查询加价转疗程单业务员
//                cardChangeLists = ssmCardChangeService.selectList(storeId, dateStr);
//            }
            if(salesman1.getOrderType() == 0){
                CashList cashList = new CashList();
                cashList.setId(Long.valueOf(salesman1.getOrderId()));
                cashList.setCashDate(salesman1.getOrderDate());
                //收银单
                List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                if(null != cashLists.get(0).getMemberName()){
                    executives1.setVIPName(cashLists.get(0).getMemberName());
                    StoreMemberList storeMemberList = new StoreMemberList();
                    storeMemberList.setId(cashLists.get(0).getMemberId());
                    StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                    executives1.setVIPNum(storeMemberList1.getMemberNum());
                }
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(cashLists.get(0).getCashNum()));
                executives1.setOrderName(String.valueOf(cashLists.get(0).getProjectName()));
                executives1.setNum(String.valueOf(cashLists.get(0).getProjectNum()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(cashLists.get(0).getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                for (CashList list : cashLists) {
                    //executives1.setMoney(list.getTransactionPrice().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))).add(executives1.getMoney()));
                    executives1.setMoney(list.getTransactionPrice().add(executives1.getMoney()));
                }



            }else if(salesman1.getOrderType() == 1){
                //充值单
                RechargeList rechargeList = new RechargeList();
                rechargeList.setId(Long.valueOf(salesman1.getOrderId()));
                rechargeList.setRechargeDate(salesman1.getOrderDate());
                List<RechargeList> select = ssmRechargeService.selectListSalem(rechargeList);
                executives1.setVIPName(select.get(0).getMemberName());
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(select.get(0).getRechargeNum()));
                executives1.setOrderName(String.valueOf(select.get(0).getRechargeTypeName()));
                executives1.setNum(String.valueOf(select.get(0).getRechargeTypeNum()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(select.get(0).getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                for (RechargeList list : select) {
                    //executives1.setMoney(list.getCollectionCashAmount().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))).add(executives1.getMoney()));
                    executives1.setMoney(list.getCollectionCashAmount().add(executives1.getMoney()));
                }
                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(select.get(0).getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 2){
                //疗程单
                CourseList courseList = new CourseList();
                courseList.setId(Long.valueOf(salesman1.getOrderId()));
                courseList.setDate(salesman1.getOrderDate());
                List<CourseList> courseLists = ssmCourseService.selectListSalem(courseList);
                executives1.setVIPName(courseLists.get(0).getMemberName());
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(courseLists.get(0).getCourseReatmentNum()));
                executives1.setOrderName(String.valueOf(courseLists.get(0).getProjectName()));
                executives1.setNum(String.valueOf(courseLists.get(0).getProjectNum()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(courseLists.get(0).getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                for (CourseList list : courseLists) {
                    //executives1.setMoney(BigDecimalUtils.add(list.getCollectionRechargeAmount(),list.getCollectionCashAmount()).multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))).add(executives1.getMoney()));
                    executives1.setMoney(BigDecimalUtils.add(list.getCollectionRechargeAmount(),list.getCollectionCashAmount()).add(executives1.getMoney()));
                }

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(courseLists.get(0).getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());

            }else if(salesman1.getOrderType() == 3){
                //加价转疗程
                CardChangeInProject cardChangeInProject = new CardChangeInProject();
                cardChangeInProject.setId(Long.valueOf(salesman1.getOrderId()));
                CardChangeInProject cardChangeInProject1 = ssmCardChangeInProjectService.selectOne(cardChangeInProject);
                executives1.setVIPName(cardChangeInProject1.getMemberName());
                executives1.setMoney(BigDecimal.valueOf(0.00));
                executives1.setOrderNum(String.valueOf(cardChangeInProject1.getDocNum()));
                executives1.setOrderName(String.valueOf(cardChangeInProject1.getProjectName()));
               // executives1.setMoney(cardChangeInProject1.getChangeInPayMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                BaseProject baseProject = new BaseProject();
                baseProject.setId(cardChangeInProject1.getProjectId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                executives1.setNum(String.valueOf(baseProject1.getProjectNum()));
                executives1.setMoney(salesman1.getMoney());
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(cardChangeInProject1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(cardChangeInProject1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 4){
                //充值退款单
                RefundRecharge refundRecharge = new RefundRecharge();
                refundRecharge.setRefundNum(salesman1.getOrderNum());
                RefundRecharge refundRecharge1 = ssmRefundRechargeService.selectOne(refundRecharge);
                executives1.setVIPName(refundRecharge1.getMemberName());
                executives1.setOrderNum(String.valueOf(refundRecharge1.getRefundNum()));
                executives1.setOrderName(String.valueOf(refundRecharge1.getRechargeTypeName()));
                //executives1.setMoney(refundRecharge1.getRecRefundMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                RechargeType rechargeType = new RechargeType();
                rechargeType.setId(refundRecharge1.getRechargeTypeId());
                RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                executives1.setNum(rechargeType1.getRechargeTypeNum());

                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundRecharge1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(refundRecharge1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 5){
                //疗程退款单
                RefundCourse refundCourse = new RefundCourse();
                refundCourse.setRefundNum(salesman1.getOrderNum());
                RefundCourse refundCourse1 = ssmRefundCourseService.selectOne(refundCourse);
                executives1.setVIPName(refundCourse1.getMemberName());
                executives1.setOrderNum(String.valueOf(refundCourse1.getCourseProjectId()));
                executives1.setOrderName(String.valueOf(refundCourse1.getCourseProjectName()));
                //executives1.setMoney(refundCourse1.getCourseRefundMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                BaseProject baseProject = new BaseProject();
                baseProject.setId(refundCourse1.getCourseProjectId());
                BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                executives1.setNum(String.valueOf(baseProject1.getProjectNum()));

                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundCourse1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(refundCourse1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 6){
                //消费退
                RefundCash refundCash = new RefundCash();
                refundCash.setRefundNum(salesman1.getOrderNum());
                RefundCash refundCourse1 = ssmRefundCashService.selectOne(refundCash);
                executives1.setVIPName(refundCourse1.getMemberName());
                executives1.setOrderNum(String.valueOf(refundCourse1.getRefundNum()));
                executives1.setOrderName(String.valueOf(refundCourse1.getProjectName()));
                //executives1.setMoney(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());

                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(refundCourse1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(refundCourse1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 7){
                //还款单
                RepayTradeList repayList = new RepayTradeList();
                repayList.setId(Long.valueOf(salesman1.getOrderId()));
                RepayTradeList repayList1 = ssmRepayTradeService.selectOne(repayList);
                executives1.setVIPName(repayList1.getMemberName());
                executives1.setOrderNum(String.valueOf(repayList1.getDocNum()));

                //executives1.setMoney(repayList1.getPayMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());
                if("card_recharge".equals(repayList1.getDocType())){
                    RechargeType rechargeType = new RechargeType();
                    rechargeType.setId(repayList1.getTradeId());
                    RechargeType rechargeType1 = ssmRechargeTypeSevice.selectOne(rechargeType);
                    executives1.setNum(rechargeType1.getRechargeTypeNum());
                    executives1.setOrderName(String.valueOf(rechargeType1.getRechargeTypeName()));
                }else if("card_use".equals(repayList1.getDocType())){
                    BaseProject baseProject = new BaseProject();
                    baseProject.setId(repayList1.getTradeId());
                    BaseProject baseProject1 = ssmProjectService.selectOne(baseProject);
                    executives1.setNum(String.valueOf(baseProject1.getProjectNum()));
                    executives1.setOrderName(String.valueOf(baseProject1.getProjectName()));
                }else{
                    executives1.setOrderName(repayList1.getTradeName());
                }

                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(repayList1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(repayList1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }else if(salesman1.getOrderType() == 8){
                //促销单
                DocPromotion docPromotion = new DocPromotion();
                docPromotion.setId(Long.valueOf(salesman1.getOrderId()));
                DocPromotion docPromotion1 = ssmDocPromotionService.selectOne(docPromotion);
                executives1.setVIPName(docPromotion1.getMemberName());
                executives1.setOrderNum(String.valueOf(docPromotion1.getPromotionNum()));
                executives1.setOrderName(String.valueOf(docPromotion1.getPromotionName()));
                //executives1.setMoney(docPromotion1.getCollectionCashAmount().add(docPromotion1.getCollectionRechargeAmount()).multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
                executives1.setMoney(salesman1.getMoney());

                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(docPromotion1.getStoreId());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());

                StoreMemberList storeMemberList = new StoreMemberList();
                storeMemberList.setId(docPromotion1.getMemberId());
                StoreMemberList storeMemberList1 = ssmStoreMemberService.selectOne(storeMemberList);
                executives1.setVIPNum(storeMemberList1.getMemberNum());
            }
            //会员名称
            //项目编码
            //项目名称
            executives1.setRatio(salesman1.getRatio());
            executives1.setChia(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100))));
            executives.add(executives1);
        }



        return executives;
    }


    @Override
    public List<Salesman> selectList(Salesman salesman) {
        List<Salesman> salesmen1 = dao.selectList(salesman);
        return salesmen1;
    }

    @Override
    public List<Salesman> selectList(List<Long> courseIds) {
        return salesmanMapper.selectList(courseIds);
    }

    @Override
    public int addBatch(List<Salesman> salesmans) {
        int size = 0;
        Random rand = new Random();
        for (Salesman salesman : salesmans) {
            salesman.setId(salesman.getId()+rand.nextInt(300000)); // 加上随机数避免mybatis_puls生成的ID重复
            int insert = dao.insert(salesman);
            if(insert>0){
                size++;
            }
        }
        return size;
    }

    @Override
    public List<Executives> selectListNew(SalesmanRequest salesman) {
        StoreRequest storeRequest1 = new StoreRequest();
        storeRequest1.setId(salesman.getStoreId());
        Long storeId= null;
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest1);
        for (BaseStores pagesorganization : pagesorganizations) {
            if(null == pagesorganization.getParentId()){
                storeId = pagesorganization.getId();
                continue;
            }
        }
        if(storeId.equals(salesman.getStoreId())){
            salesman.setStoreId(null);
        }

        List<Salesman> salesmens = new ArrayList<>();
        List<Executives> executives = new ArrayList<>();
        salesman.setState(0);
        List<Salesman> salesmen1 = dao.selectList(salesman);
        //按员工水牌号分组
        List<Salesman> salesmen3 = salesmen1.stream().filter(item -> item.getOrderType() == 2 || item.getOrderType() == 5 ||item.getOrderType() == 7 || item.getOrderType().equals("2") || item.getOrderType().equals("5") || item.getOrderType().equals("7")).collect(Collectors.toList());
        for (Salesman salesman1 : salesmen3) {
            Salesman salesman2 = new Salesman();
            BeanUtils.copyProperties(salesman1,salesman2);
            //如果没有项目id
            if(salesman1.getProjectId()==null){
                RepayTradeList repayTradeList = ssmRepayTradeService.selectByPrimaryKey(Long.valueOf(salesman1.getOrderId()));
                String docNum = repayTradeList.getDocNum();
                if(docNum.startsWith("LC")||docNum.startsWith("ZLC")){
                    BaseProject baseProject = ssmProjectService.selectByPrimaryKey(repayTradeList.getTradeId());
                    if(baseProject.getIsCooperation()!=null&&baseProject.getIsCooperation()==1){
                        salesman2.setMoney(baseProject.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                        salesmen1.add(salesman2);
                    }else if(baseProject.getIsCooperation()!=null&&baseProject.getIsCooperation()==2){
                        salesman2.setMoney(baseProject.getBrokerage().divide(baseProject.getPrice().multiply(BigDecimal.valueOf(baseProject.getMinumumTimes())),4,BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                        salesmen1.add(salesman2);
                    }
                }
            }
            String orderNum = salesman1.getOrderNum();
            String substring = orderNum.substring(orderNum.length() - 11, orderNum.length() - 3);
            StringBuilder  stringBuilder = new StringBuilder(substring);
            StringBuilder date = stringBuilder.insert(6, "-").insert(4, "-");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date parse ;
            try {
                parse = simpleDateFormat.parse(String.valueOf(date));

            } catch (ParseException e) {
                throw new RuntimeException(e);
            }
            CourseList courseList = new CourseList();
            courseList.setDate(parse);
            courseList.setCourseReatmentNum(orderNum);
            courseList.setProjctId(salesman1.getProjectId());
            List<CourseList> courseLists1  = ssmCourseService.selectCourseByProjectAndDocId(courseList);
            List<CourseList> courseLists = new ArrayList<>();
            if(courseLists1!=null && courseLists1.size()>0){
                for (int i = 0; i < courseLists1.size(); i++) {
                    if(courseLists1.get(i).getCollectionCashAmount().compareTo(salesman2.getMoney())==0){
                        courseLists.add(courseLists1.get(i));
                        break;
                    }
                }
            }
            for (CourseList list : courseLists) {
                if(list.getIsCooperation()!=null&&list.getIsCooperation()==1){
                    salesman2.setMoney(list.getBrokerage().divide(BigDecimal.valueOf(100)).multiply(salesman1.getMoney()).negate());
                    salesmen1.add(salesman2);
                }else if(list.getIsCooperation()!=null&&list.getIsCooperation()==2){
                    salesman2.setMoney(list.getBrokerage().divide(list.getSalesPrice().multiply(BigDecimal.valueOf(list.getMinumumTimes())),4,BigDecimal.ROUND_HALF_UP).multiply(salesman1.getMoney()).negate());
                    salesmen1.add(salesman2);
                }
            }
        }
        Map<String, List<Salesman>> map = salesmen1.stream().collect(Collectors.groupingBy(Salesman::getBrandNumber));
        Iterator<Map.Entry<String, List<Salesman>>> entryIterator = map.entrySet().iterator();
        while (entryIterator.hasNext()) {
            Map.Entry<String, List<Salesman>> entry = entryIterator.next();
            List<Salesman> salesmen = entry.getValue();
            //按高管每一个门店进行分组
            Map<Long, List<Salesman>> map1 = salesmen.stream().collect(Collectors.groupingBy(Salesman::getStoreIdOrder));
            Iterator<Map.Entry<Long, List<Salesman>>> entryIterators = map1.entrySet().iterator();
            while (entryIterators.hasNext()) {
                Map.Entry<Long, List<Salesman>> entrys = entryIterators.next();
                List<Salesman> salesmen2 = entrys.getValue();
                List<Salesman> collect = salesmen2.stream().sorted(Comparator.comparing(Salesman::getOrderDate)).collect(Collectors.toList());
                Executives executives1 = new Executives();
                DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //HH表示24小时制；
                executives1.setStartDate(dFormat.format(collect.get(0).getOrderDate()));
                executives1.setEndDate(dFormat.format(collect.get(collect.size()-1).getOrderDate()));
                StoreRequest storeRequest = new StoreRequest();
                storeRequest.setId(collect.get(0).getStoreIdOrder());
                BaseStore baseStore = ssmBaseStoreService.selectOne(storeRequest);
                executives1.setStoreNum(baseStore.getStoreNum());
                executives1.setStoreName(baseStore.getStoreName());
                executives1.setBrandNumber(collect.get(0).getBrandNumber());
                executives1.setStaffName(collect.get(0).getStaffName());
                executives1.setChia(BigDecimal.valueOf(0));
                executives1.setCooperationAchi(BigDecimal.valueOf(0));
                for (Salesman salesman1 : collect) {
                    if(salesman1.getMoney().compareTo(BigDecimal.ZERO)==-1){
                        executives1.setCooperationAchi(executives1.getCooperationAchi().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                    }
                    executives1.setChia(executives1.getChia().add(salesman1.getMoney().multiply(salesman1.getRatio().divide(BigDecimal.valueOf(100)))));
                }
                executives1.setCooperationAchi(executives1.getCooperationAchi().negate());
//                executives1.setCooperationAchi(new BigDecimal("1111.1111111111111111111111111111111"));
                executives.add(executives1);
            }
        }




        return executives;
    }

    @Override
    public void updateByDocNum(String initNum) {
        dao.updateByDocNum( initNum);
    }

    @Override
    public PageInfo<SalesmanRequest> selectPage(SalesmanRequest salesmanRequest){

        //PageInfo<SalesmanRequest> pageInfo = dao.selectPage(staffRequest);
        PageInfo<SalesmanRequest> pageInfo = null;
        List<SalesmanRequest> lists = pageInfo.getList();


        return pageInfo;
    }

}
