package com.ssm.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.github.pagehelper.PageInfo;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.core.util.StrComparatorUtils;
import com.ssm.manage.entity.Salesman;
import com.ssm.manage.excelEntity.ExcelSaleman;
import com.ssm.manage.excelEntity.ExcelStaffAchi;
import com.ssm.manage.query.SalesmanRequest;
import com.ssm.manage.response.Executives;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmSalesmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.*;

@Api(value="单据业务员controller",tags={"单据业务员控制器"})
@RestController
@RequestMapping("/demay/ssm/salesman")
@Slf4j
public class SsmSalesmanController {
    @Autowired
    private SsmSalesmanService ssmSalesmanService;

    @ApiOperation(value = "查询员工业绩和提成", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectOne")
    public GenericResponse selectOne(@RequestBody Salesman salesman) {
        GenericResponse<Salesman> genericResponse = new GenericResponse();
        List<Salesman> salesmen = null;
        try {
            salesmen = ssmSalesmanService.selectList(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "分页查询高管信息",httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody SalesmanRequest salesmanRequest,HttpSession session){
        PageResponse<SalesmanRequest> pageResponse = new PageResponse<>();
        PageInfo<SalesmanRequest> pageInfo = new PageInfo<>();
        if(null == salesmanRequest.getStoreId() || "".equals(salesmanRequest.getStoreId())){
            salesmanRequest.setStoreId(SessionHelp.getStoreId(session));
        }
        try {
            pageInfo = ssmSalesmanService.selectPage(salesmanRequest);
        }catch (Exception e){
            log.error("SsmStaffController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "查询高管业绩(传staffType:0=高管，1=门店管理员)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectList")
    public GenericResponse selectList(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        List<Executives> salesmen = null;
        salesman.setStoreId(SessionHelp.getStoreId(session));
        try {
            salesmen = ssmSalesmanService.selectList(salesman);
            if(salesmen!=null&&salesmen.size()>0) {
                Collections.sort(salesmen, new StrComparatorUtils<>("storeNum", false));
            }
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "查询高管业绩(传staffType:0=高管，1=门店管理员)(只查看当前门店)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectListone")
    public GenericResponse selectListone(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        List<Executives> salesmen = null;
        try {
            salesmen = ssmSalesmanService.selectList(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "高管业绩导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/exportachi")
    public void exportachi(@RequestBody SalesmanRequest salesman, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        List<Executives> salesmen = null;
        try {
            String fileName = "projectType";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/achievement.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            List<ExcelStaffAchi> excelBaseProjectTypeList = new ArrayList<>();
//            List<BaseProjectType> list = ssmProjectTypeService.select(baseProjectType);
            salesman.setStoreId(SessionHelp.getStoreId(session));
            salesmen = ssmSalesmanService.selectList(salesman);
            for (Executives executives : salesmen) {
                ExcelStaffAchi excelBaseProjectType = new ExcelStaffAchi();
                BeanUtils.copyProperties(executives, excelBaseProjectType);
                excelBaseProjectTypeList.add(excelBaseProjectType);
            }
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(excelBaseProjectTypeList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "查询高管业绩明细(传staffType:0=高管，1=门店管理员)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectListMx")
    public GenericResponse selectListMx(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        List<Executives> salesmen = null;
        try {
            salesmen = ssmSalesmanService.selectLists(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }


    @ApiOperation(value = "查询高管业绩明细(传staffType:0=高管，1=门店管理员)(查询单门店)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectListMxone")
    public GenericResponse selectListMxone(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        salesman.setStoreId(SessionHelp.getStoreId(session));
        List<Executives> salesmen = null;
        try {
            salesmen = ssmSalesmanService.selectLists(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "高管业绩明细导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/exportachiMX")
    public void exportachiMX(@RequestBody SalesmanRequest salesman, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        List<Executives> salesmen = null;
        try {
            String fileName = "projectType";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/achievementMX.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            List<ExcelStaffAchi> excelBaseProjectTypeList = new ArrayList<>();
//            List<BaseProjectType> list = ssmProjectTypeService.select(baseProjectType);
            if(salesman.getStoreIds()!=null&&salesman.getStoreIds().size()>0) {
                salesman.setStoreIdsOrder(salesman.getStoreIds());
                salesman.setStoreIds(null);
            }
            salesmen = ssmSalesmanService.selectLists(salesman);
            for (Executives executives : salesmen) {
                ExcelStaffAchi excelBaseProjectType = new ExcelStaffAchi();
                BeanUtils.copyProperties(executives, excelBaseProjectType);
                excelBaseProjectType.setStartDate(salesman.getStartDate());
                excelBaseProjectType.setEndDate(salesman.getEndDate());
                excelBaseProjectType.setMoney(executives.getMoney());
                excelBaseProjectType.setTotalAchi(excelBaseProjectType.getChia().add(excelBaseProjectType.getCooperationAchi()==null?BigDecimal.ZERO:excelBaseProjectType.getCooperationAchi()));
                excelBaseProjectTypeList.add(excelBaseProjectType);
                //（0=收银单，1=充值单，2=疗程单，3=加价转疗程单，4=充值退款单，5=疗程退款单,6=消费退，7=还款）
                if(executives.getOrderType()==0){
                    excelBaseProjectType.setOrderType("收银单");
                }else if(executives.getOrderType()==1){
                    excelBaseProjectType.setOrderType("充值单");
                }else if(executives.getOrderType()==2){
                    excelBaseProjectType.setOrderType("疗程单");
                }else if(executives.getOrderType()==3){
                    excelBaseProjectType.setOrderType("加价转疗程单");
                }else if(executives.getOrderType()==4){
                    excelBaseProjectType.setOrderType("充值退款单");
                }else if(executives.getOrderType()==5){
                    excelBaseProjectType.setOrderType("疗程退款单");
                }else if(executives.getOrderType()==6){
                    excelBaseProjectType.setOrderType("消费退");
                }else if(executives.getOrderType()==7){
                    excelBaseProjectType.setOrderType("还款");
                }
            }
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(excelBaseProjectTypeList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "高管业绩明细导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/exportachiMXwhole")
    public void exportachiMXwhole(@RequestBody SalesmanRequest salesman, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        List<Executives> salesmen = null;
        salesman.setStoreId(SessionHelp.getStoreId(session));
        try {
            String fileName = "projectType";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/achievementMX.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            List<ExcelStaffAchi> excelBaseProjectTypeList = new ArrayList<>();
//            List<BaseProjectType> list = ssmProjectTypeService.select(baseProjectType);
            salesmen = ssmSalesmanService.selectListswhole(salesman);
            for (Executives executives : salesmen) {
                ExcelStaffAchi excelBaseProjectType = new ExcelStaffAchi();
                BeanUtils.copyProperties(executives, excelBaseProjectType);
                excelBaseProjectType.setStartDate(salesman.getStartDate());
                excelBaseProjectType.setEndDate(salesman.getEndDate());
                excelBaseProjectType.setMoney(executives.getMoney());
                excelBaseProjectTypeList.add(excelBaseProjectType);
                //（0=收银单，1=充值单，2=疗程单，3=加价转疗程单，4=充值退款单，5=疗程退款单,6=消费退，7=还款）
                if(executives.getOrderType()==0){
                    excelBaseProjectType.setOrderType("收银单");
                }else if(executives.getOrderType()==1){
                    excelBaseProjectType.setOrderType("充值单");
                }else if(executives.getOrderType()==2){
                    excelBaseProjectType.setOrderType("疗程单");
                }else if(executives.getOrderType()==3){
                    excelBaseProjectType.setOrderType("加价转疗程单");
                }else if(executives.getOrderType()==4){
                    excelBaseProjectType.setOrderType("充值退款单");
                }else if(executives.getOrderType()==5){
                    excelBaseProjectType.setOrderType("疗程退款单");
                }else if(executives.getOrderType()==6){
                    excelBaseProjectType.setOrderType("消费退");
                }else if(executives.getOrderType()==7){
                    excelBaseProjectType.setOrderType("还款");
                }
            }
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(excelBaseProjectTypeList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "查询高管业绩(传staffType:0=高管，1=门店管理员)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectLists")
    public GenericResponse selectLists(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        List<Executives> salesmen = null;
        if(salesman.getStoreId()==null){
            salesman.setStoreId(SessionHelp.getStoreId(session));
        }
        try {
            salesmen = ssmSalesmanService.selectList(salesman);
            if(salesmen!=null&&salesmen.size()>0) {
                Collections.sort(salesmen, new StrComparatorUtils<>("storeNum", false));
            }
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "查询高管业绩(传staffType:0=高管，1=门店管理员)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectListsNew")
    public GenericResponse selectListsNew(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        List<Executives> salesmen = null;
        if(salesman.getStoreId()==null){
            salesman.setStoreId(SessionHelp.getStoreId(session));
        }
        try {
            salesmen = ssmSalesmanService.selectListNew(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "查询高管业绩(传staffType:0=高管，1=门店管理员)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectLists/export")
    public void export(@RequestBody SalesmanRequest salesman,HttpSession session) {
        if(salesman.getStoreId()==null){
            salesman.setStoreId(SessionHelp.getStoreId(session));
        }
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        try {
            String fileName = "project";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/salesman.xlsx");
            List<Executives> salesmen = ssmSalesmanService.selectList(salesman);
            List<ExcelSaleman> salemanList = new ArrayList<>();
            for (Executives executives : salesmen) {
                ExcelSaleman excelSaleman = new ExcelSaleman();
                BeanUtils.copyProperties(executives, excelSaleman);
                excelSaleman.setAllAmount(executives.getCooperationAchi().add(executives.getChia()));
                salemanList.add(excelSaleman);
            }
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(salemanList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
        }finally {
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "查询高管业绩(传staffType:0=高管，1=门店管理员)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectListsone")
    public GenericResponse selectListsone(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        salesman.setStoreId(SessionHelp.getStoreId(session));
        List<Executives> salesmen = null;
        try {
            salesmen = ssmSalesmanService.selectList(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "门店管理层业绩导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/exportachis")
    public void exportachis(@RequestBody SalesmanRequest salesman, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        List<Executives> salesmen = null;
        salesman.setStoreId(SessionHelp.getStoreId(session));
        try {
            String fileName = "projectType";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/achievement.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            List<ExcelStaffAchi> excelBaseProjectTypeList = new ArrayList<>();
//            List<BaseProjectType> list = ssmProjectTypeService.select(baseProjectType);
            salesmen = ssmSalesmanService.selectList(salesman);
            for (Executives executives : salesmen) {
                ExcelStaffAchi excelBaseProjectType = new ExcelStaffAchi();
                BeanUtils.copyProperties(executives, excelBaseProjectType);
                excelBaseProjectType.setTotalAchi(excelBaseProjectType.getChia().add(excelBaseProjectType.getCooperationAchi()==null? BigDecimal.ZERO:excelBaseProjectType.getCooperationAchi()));
                excelBaseProjectType.setCooperationAchi(excelBaseProjectType.getCooperationAchi().setScale(2,BigDecimal.ROUND_HALF_UP));
                excelBaseProjectType.setChia(excelBaseProjectType.getChia().setScale(2,BigDecimal.ROUND_HALF_UP));
                excelBaseProjectTypeList.add(excelBaseProjectType);
            }
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(excelBaseProjectTypeList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }


    @ApiOperation(value = "查询高管业绩明细(传staffType:0=高管，1=门店管理员)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectListMxs")
    public GenericResponse selectListMxs(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        List<Executives> salesmen = null;
        try {
            salesmen = ssmSalesmanService.selectLists(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "查询高管业绩明细(传staffType:0=高管，1=门店管理员)(查询单门店)", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectListMxsone")
    public GenericResponse selectListMxsone(@RequestBody SalesmanRequest salesman,HttpSession session) {
        GenericResponse<Executives> genericResponse = new GenericResponse();
        List<Executives> salesmen = null;
        salesman.setStoreId(SessionHelp.getStoreId(session));
        try {
            salesmen = ssmSalesmanService.selectLists(salesman);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(salesmen);
        return genericResponse;
    }

    @ApiOperation(value = "高管业绩明细导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/exportachiMXs")
    public void exportachiMXs(@RequestBody SalesmanRequest salesman, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        List<Executives> salesmen = null;
        salesman.setStoreId(SessionHelp.getStoreId(session));
        try {
            String fileName = "projectType";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/achievementMX.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            List<ExcelStaffAchi> excelBaseProjectTypeList = new ArrayList<>();
//            List<BaseProjectType> list = ssmProjectTypeService.select(baseProjectType);
            salesmen = ssmSalesmanService.selectLists(salesman);
            for (Executives executives : salesmen) {
                ExcelStaffAchi excelBaseProjectType = new ExcelStaffAchi();
                BeanUtils.copyProperties(executives, excelBaseProjectType);
                excelBaseProjectType.setStartDate(salesman.getStartDate());
                excelBaseProjectType.setEndDate(salesman.getEndDate());
                excelBaseProjectType.setMoney(executives.getMoney());
                excelBaseProjectTypeList.add(excelBaseProjectType);
                excelBaseProjectType.setTotalAchi(excelBaseProjectType.getCooperationAchi().add(excelBaseProjectType.getChia()));
                //（0=收银单，1=充值单，2=疗程单，3=加价转疗程单，4=充值退款单，5=疗程退款单,6=消费退，7=还款）
                if(executives.getOrderType()==0){
                    excelBaseProjectType.setOrderType("收银单");
                }else if(executives.getOrderType()==1){
                    excelBaseProjectType.setOrderType("充值单");
                }else if(executives.getOrderType()==2){
                    excelBaseProjectType.setOrderType("疗程单");
                }else if(executives.getOrderType()==3){
                    excelBaseProjectType.setOrderType("加价转疗程单");
                }else if(executives.getOrderType()==4){
                    excelBaseProjectType.setOrderType("充值退款单");
                }else if(executives.getOrderType()==5){
                    excelBaseProjectType.setOrderType("疗程退款单");
                }else if(executives.getOrderType()==6){
                    excelBaseProjectType.setOrderType("消费退");
                }else if(executives.getOrderType()==7){
                    excelBaseProjectType.setOrderType("还款");
                }
            }
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(excelBaseProjectTypeList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "高层业绩明细导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/exportachiMXswhole")
    public void exportachiMXswhole(@RequestBody SalesmanRequest salesman, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        List<Executives> salesmen = null;
        salesman.setStoreId(SessionHelp.getStoreId(session));
        try {
            String fileName = "projectType";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/achievementMX.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            List<ExcelStaffAchi> excelBaseProjectTypeList = new ArrayList<>();
//            List<BaseProjectType> list = ssmProjectTypeService.select(baseProjectType);
            salesmen = ssmSalesmanService.selectListswhole(salesman);
            for (Executives executives : salesmen) {
                ExcelStaffAchi excelBaseProjectType = new ExcelStaffAchi();
                BeanUtils.copyProperties(executives, excelBaseProjectType);
                excelBaseProjectType.setStartDate(salesman.getStartDate());
                excelBaseProjectType.setEndDate(salesman.getEndDate());
                excelBaseProjectType.setMoney(executives.getMoney());
                excelBaseProjectTypeList.add(excelBaseProjectType);
                //（0=收银单，1=充值单，2=疗程单，3=加价转疗程单，4=充值退款单，5=疗程退款单,6=消费退，7=还款）
                if(executives.getOrderType()==0){
                    excelBaseProjectType.setOrderType("收银单");
                }else if(executives.getOrderType()==1){
                    excelBaseProjectType.setOrderType("充值单");
                }else if(executives.getOrderType()==2){
                    excelBaseProjectType.setOrderType("疗程单");
                }else if(executives.getOrderType()==3){
                    excelBaseProjectType.setOrderType("加价转疗程单");
                }else if(executives.getOrderType()==4){
                    excelBaseProjectType.setOrderType("充值退款单");
                }else if(executives.getOrderType()==5){
                    excelBaseProjectType.setOrderType("疗程退款单");
                }else if(executives.getOrderType()==6){
                    excelBaseProjectType.setOrderType("消费退");
                }else if(executives.getOrderType()==7){
                    excelBaseProjectType.setOrderType("还款");
                }
            }
            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            //EasyExcel.write(out, LiveRoomData.class).sheet("模板").doWrite(list);
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            //FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(excelBaseProjectTypeList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }


}
