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.core.utils.AESUtils;
import com.ssm.manage.excelEntity.ExcelCourseList;
import com.ssm.manage.excelEntity.ExcelRechargeList;
import com.ssm.manage.query.CourseAccountRequest;
import com.ssm.manage.response.GenericResponse;
import com.ssm.manage.response.PageResponse;
import com.ssm.manage.service.SsmRechargeTypeSevice;
import com.ssm.manage.service.SsmStoreMemberService;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.manage.entity.*;
import com.ssm.manage.query.RechargeAccountRequest;
import com.ssm.manage.query.StoreRequest;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.service.SsmBaseStoreService;
import com.ssm.manage.service.SsmRechargeAccountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
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.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Api(value="充值账户controller",tags={"充值账户相关接口"})
@RestController
@RequestMapping("/demay/ssm/recAccount")
@Slf4j
public class SsmRechargeAccountController {
    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;

    @Autowired
    private SsmRechargeTypeSevice ssmRechargeTypeSevice;

    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @ApiOperation(value = "查询门店用户所拥有的全部充值账户",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/list")
    public GenericResponse selectList(@RequestBody RechargeAccountRequest rechargeAccountRequest,HttpSession session){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        //多门店背景下充值账户各门店通用 允许查询
//        if (null == rechargeAccountRequest.getStoreId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
//            return genericResponse;
//        }
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        rechargeAccountRequest.setNowStoreId(SessionHelp.getStoreId(session));
        List<RechargeAccount> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.selectList(rechargeAccountRequest);
        }catch (Exception e){
            log.error("SsmRechargeAccountController.selectList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }
    @ApiOperation(value = "查询门店用户所拥有的全部充值账户(查询未过期)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cashListByStore")
    public GenericResponse cashListByStore(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        if (null == rechargeAccountRequest.getStoreId()){
            rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
        }
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(SessionHelp.getStoreId(session));
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
        rechargeAccountRequest.setZDsTOREiD(pagesorganizations.stream().filter(baseStores -> null == baseStores.getParentId()).findAny().orElse(null).getId());
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        rechargeAccountRequest.setStoreIdl(collect);
        List<RechargeAccount> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.cashSelectListByStore(rechargeAccountRequest);
        }catch (SmsException e){
            log.error("SsmRechargeAccountController.selectOne:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeAccountController.cashList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "查询门店用户所拥有的全部充值账户(查询未过期)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cashList")
    public GenericResponse cashList(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        if (null == rechargeAccountRequest.getStoreId()){
            rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
        }
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(SessionHelp.getStoreId(session));
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
        rechargeAccountRequest.setZDsTOREiD(pagesorganizations.stream().filter(baseStores -> null == baseStores.getParentId()).findAny().orElse(null).getId());
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        rechargeAccountRequest.setStoreIdl(collect);
        List<RechargeAccount> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.cashSelectList(rechargeAccountRequest);
        }catch (SmsException e){
            log.error("SsmRechargeAccountController.selectOne:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeAccountController.cashList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "查询门店用户所拥有的全部充值账户(查询未过期)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/AccountList")
    public GenericResponse AccountList(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        //多门店背景下充值账户各门店通用 允许查询
//        if (null == rechargeAccountRequest.getStoreId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
//            return genericResponse;
//        }
        //rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(SessionHelp.getStoreId(session));
        rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        List<Long> collect = pagesorganizations.stream().map(BaseStores::getId).collect(Collectors.toList());
        rechargeAccountRequest.setZDsTOREiD(pagesorganizations.stream().filter(baseStores -> null == baseStores.getParentId()).findAny().orElse(null).getId());
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        rechargeAccountRequest.setStoreIdl(collect);
        List<RechargeAccount> list = new ArrayList<>();
        try {
            rechargeAccountRequest.setSelectType(1);
            list = ssmRechargeAccountService.cashSelectList(rechargeAccountRequest);
        }catch (SmsException e){
            log.error("SsmRechargeAccountController.selectOne:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeAccountController.cashList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "查询门店用户所拥有的全部充值账户(查询全部，包括过期)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cashListall")
    public GenericResponse cashListall(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        //多门店背景下充值账户各门店通用 允许查询
//        if (null == rechargeAccountRequest.getStoreId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
//            return genericResponse;
//        }
        rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        List<RechargeAccount> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.cashSelectListall(rechargeAccountRequest);
        }catch (SmsException e){
            log.error("SsmRechargeAccountController.selectOne:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeAccountController.cashList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }


    @ApiOperation(value = "查询门店用户所拥有的全部充值账户(单单过期得)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/delay")
    public GenericResponse delay(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        //多门店背景下充值账户各门店通用 允许查询
//        if (null == rechargeAccountRequest.getStoreId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
//            return genericResponse;
//        }
        rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        List<RechargeAccount> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.delay(rechargeAccountRequest);
        }catch (SmsException e){
            log.error("SsmRechargeAccountController.selectOne:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmRechargeAccountController.cashList:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }



    @ApiOperation(value = "收银查询门店用户所拥有的一个充值账户  (门店端-充值单-查询所选择充值类别对应会员的充值账户)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/one")
    public GenericResponse selectOne(@RequestBody RechargeAccountRequest rechargeAccountRequest,HttpSession session){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
//        if (null == rechargeAccountRequest.getStoreId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "门店ID");
//            return genericResponse;
//        }
        if(rechargeAccountRequest.getStoreId()==null){
            rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
        }
        if (null == rechargeAccountRequest.getMemberId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"会员ID");
            return genericResponse;
        }
        if (null == rechargeAccountRequest.getRechargeTypeId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S,"充值类别ID");
            return genericResponse;
        }
        RechargeAccount rechargeAccount = new RechargeAccount();
        try {
            rechargeAccount = ssmRechargeAccountService.selectOne(rechargeAccountRequest);
        }catch (Exception e){
            log.error("SsmRechargeAccountController.selectOne:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (null == rechargeAccount){
            genericResponse.setCodeAndMsg(ResponseCode.DATA_DOES_NOT_EXISTS_S,"充值账户");
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(rechargeAccount);
        return genericResponse;
    }

    @ApiOperation(value = "充值账户修改  (门店端-充值单明细-充值账户修改)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse();
        if (null == rechargeAccountRequest.getId()){
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "账户ID");
            return genericResponse;
        }
        String phone = SessionHelp.getLoginAccount(session);
        int i = 0;
        try {
            i = ssmRechargeAccountService.update(rechargeAccountRequest, phone);
        }catch (Exception e){
            log.error("SsmRechargeAccountController.update:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "通过ID查询门店用户所拥有的一个充值账户  (门店端-充值单-通过ID查询所选择充值类别对应会员的充值账户)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/selectById")
    public GenericResponse selectById(@RequestBody RechargeAccountRequest rechargeAccountRequest){
        GenericResponse<RechargeAccount> genericResponse = new GenericResponse<>();
        RechargeAccount rechargeAccount = new RechargeAccount();
        try {
            rechargeAccount = ssmRechargeAccountService.selectByPrimaryKey(rechargeAccountRequest.getId());
        }catch (Exception e){
            log.error("SsmRechargeAccountController.selectById:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (null == rechargeAccount){
            genericResponse.setCodeAndMsg(ResponseCode.DATA_DOES_NOT_EXISTS_S,"充值账户");
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setData(rechargeAccount);
        return genericResponse;
    }

//    @ApiOperation(value = "充值账户模板下载  门店端-充值-充值账户模板下载", httpMethod = "POST", response = GenericResponse.class)
//    @RequestMapping("/download")
//    public void download(HttpSession session, HttpServletResponse response) {
//
//        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
//        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
//        ExcelWriter excelWriter = null;
//        OutputStream out = null;
//        try {
//            String fileName = "recAccount";
//            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/recAccount.xlsx");
//
//            List<RechargeAccount> list = new ArrayList<>();
//            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(list, writeSheet);
//            out.flush();
//        } catch (Exception e) {
//            log.error("SsmRechargeAccountController.download:",e);
//        } finally {
//            //destFile.delete();
//            // 别忘记关闭流
//            excelWriter.finish();
//        }
//    }
//
//    @ApiOperation(value = "充值账户批量导出  门店端-充值-充值账户批量导出", httpMethod = "POST", response = GenericResponse.class)
//    @RequestMapping("/export")
//    public void export(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session) {
//        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
//        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
//        ExcelWriter excelWriter = null;
//        OutputStream out = null;
//        rechargeAccountRequest.setStoreId(SessionHelp.getStoreId(session));
//        rechargeAccountRequest.setStoreName(SessionHelp.getStoreName(session));
//        RechargeAccount rechargeAccount = new RechargeAccount();
//        try {
//            String fileName = "recAccount";
//            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/recAccount.xlsx");
//            BeanUtils.copyProperties(rechargeAccountRequest, rechargeAccount);
//            List<RechargeAccount> list = ssmRechargeAccountService.select(rechargeAccount);
//            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(list, writeSheet);
//            out.flush();
//        } catch (Exception e) {
//            log.error("SsmRechargeAccountController.export:",e);
//        } finally {
//            //destFile.delete();
//            // 别忘记关闭流
//            excelWriter.finish();
//        }
//    }
//
//
//    @ApiOperation(value = "充值账户批量导入  门店端-充值-充值账户批量导入", httpMethod = "POST", response = GenericResponse.class)
//    @RequestMapping("/import")
//    public GenericResponse recAccountImport(MultipartFile multipartFile, HttpSession session) {
//        GenericResponse genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        List<RechargeAccount> list = null;
//        try {
//            list = EasyExcelUtil.simpleWrite(multipartFile, RechargeAccount.class);
//        } catch (SmsException e) {
//            log.error("SsmRechargeAccountController.recAccountImport:",e);
//            return genericResponse;
//        }
//
//        int count = ssmRechargeAccountService.insertBatch(list,storeId, storeName);
//        if (list.size() != count) {
//            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
//            return genericResponse;
//        }
//        genericResponse.setCodeAndMsg(ResponseCode.OK);
//        return genericResponse;
//
//    }

//    @ApiOperation(value = "升级", httpMethod = "POST", response = GenericResponse.class)
//    @RequestMapping("/upgrade")
//    public GenericResponse recAccountImport(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session) {
//        GenericResponse genericResponse = new GenericResponse();
//        if (null == rechargeAccountRequest.getRechargeTypeId()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升级后账户ID");
//            return genericResponse;
//        }
//
//        if (null == rechargeAccountRequest.getIds()){
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "升级前账户ID");
//            return genericResponse;
//        }
//        String phone = SessionHelp.getLoginAccount(session);
//        int i = 0;
//        try {
//            i = ssmRechargeService.addRecharge(rechargeListRequest);
//        }catch (Exception e){
//            log.error("SsmRechargeAccountController.update:",e);
//            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
//            return genericResponse;
//        }
//        if (i > 0) {
//            genericResponse.setCodeAndMsg(ResponseCode.OK);
//        }
//        return genericResponse;
//
//    }

    @ApiOperation(value = "部门业绩提成统计列表  门店端-美容部，美发部业绩",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/exportachi/courseAcount")
    public void departmentPerformanceExportachi(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session){
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        List<RechargeAccount> list = new ArrayList<>();
        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();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/rechargeAccount.xlsx");
            List<ExcelRechargeList> excelBaseProjectTypeList = new ArrayList<>();
            list = ssmRechargeAccountService.exportachi(rechargeAccountRequest);
            //查询项目编码
            if(!CollectionUtils.isEmpty(list)) {
                List<Long> memberId = list.stream().map(RechargeAccount::getMemberId).collect(Collectors.toList());
//                List<Long> storeId = list.stream().map(RechargeAccount::getStoreId).collect(Collectors.toList());
                List<Long> rechargeTypeId = list.stream().map(RechargeAccount::getRechargeTypeId).collect(Collectors.toList());
                List<RechargeType> rechargeTypeList = ssmRechargeTypeSevice.selectById(rechargeTypeId);
                List<BaseStore> baseStores = ssmBaseStoreService.selectAll();
                List<StoreMemberList> storeMemberLists = ssmStoreMemberService.selectList(memberId);
                for (RechargeAccount courseAccount : list) {
                    ExcelRechargeList courseList = new ExcelRechargeList();
                    for (BaseStore baseStore : baseStores) {
                        if(courseAccount.getStoreId().compareTo(baseStore.getId())==0){
                            courseList.setStoreNum(baseStore.getStoreNum());
                            courseList.setStoreName(baseStore.getStoreName());
                        }
                    }
                    setCourseType(courseList,courseAccount.getType().toString());
                    for (StoreMemberList storeMemberList : storeMemberLists) {
                        if(courseAccount.getMemberId().compareTo(storeMemberList.getId())==0){
                            courseList.setMobilePhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).replaceAll("\u0000",""));
                            courseList.setMemberName(storeMemberList.getMemberName());
                            courseList.setMemberNum(storeMemberList.getMemberNum());
                        }
                    }
                    courseList.setState("已锁定");
                    if (courseAccount.getState()==1){
                        courseList.setState("正常");
                    }
                    for (RechargeType rechargeType : rechargeTypeList) {
                        if(courseAccount.getRechargeTypeId().compareTo(rechargeType.getId())==0){
                            courseList.setRechargeNum(rechargeType.getRechargeTypeNum());
                            courseList.setRechargeTypeName(rechargeType.getRechargeTypeName());
                            courseList.setFirstAmount(rechargeType.getFirstAmount());
                            courseList.setRechargeMoney(rechargeType.getRechargeMoney());
                            courseList.setDiscount(rechargeType.getDiscount());
                            courseList.setRechargeDonateType("比例");
                            if(rechargeType.getDonateType()==0){
                                courseList.setRechargeDonateType("固定");
                            }
                            courseList.setRechargeGiftMoney(rechargeType.getGiftMoney());
                        }
                    }
                    courseList.setCardRestMoney(courseAccount.getCardRestMoney());
                    courseList.setCardConsumeMoney(courseAccount.getCardConsumeMoney());
                    courseList.setCardGiftMoney(courseAccount.getCardGiftMoney());
                    courseList.setGiftConsumeMoney(courseAccount.getGiftConsumeMoney());
                    courseList.setLastConsumeDate(courseAccount.getLastConsumeTime());
                    courseList.setEndTime(courseAccount.getEndTime());

                    excelBaseProjectTypeList.add(courseList);
                }
            }

            WriteCellStyle headWriteCellStyle = new WriteCellStyle();
            // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            excelWriter.fill(excelBaseProjectTypeList, writeSheet);
            out.flush();
        } catch (Exception e){
            log.error("SsmStaffController.achiCommList",e);
        }finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "部门业绩提成统计列表  门店端-美容部，美发部业绩",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/courseAcount/list")
    public PageResponse<ExcelRechargeList> courseAccountList(@RequestBody RechargeAccountRequest rechargeAccountRequest, HttpSession session) {
        PageResponse<ExcelRechargeList> pageResponse = new PageResponse<>();
        PageInfo<RechargeAccount> list;
            List<ExcelRechargeList> excelBaseProjectTypeList = new ArrayList<>();
            list = ssmRechargeAccountService.exportachiByPage(rechargeAccountRequest);
            //查询项目编码
        List<RechargeAccount> listList = list.getList();
        if(!CollectionUtils.isEmpty(listList)) {
                List<Long> memberId = listList.stream().map(RechargeAccount::getMemberId).collect(Collectors.toList());
//                List<Long> storeId = listList.stream().map(RechargeAccount::getStoreId).collect(Collectors.toList());
                List<Long> rechargeTypeId = listList.stream().map(RechargeAccount::getRechargeTypeId).collect(Collectors.toList());
                List<RechargeType> rechargeTypeList = ssmRechargeTypeSevice.selectById(rechargeTypeId);
                List<BaseStore> baseStores = ssmBaseStoreService.selectAll();
                List<StoreMemberList> storeMemberLists = ssmStoreMemberService.selectList(memberId);
                for (RechargeAccount courseAccount : listList) {
                    ExcelRechargeList courseList = new ExcelRechargeList();
                    for (BaseStore baseStore : baseStores) {
                        if(courseAccount.getStoreId().compareTo(baseStore.getId())==0){
                            courseList.setStoreNum(baseStore.getStoreNum());
                            courseList.setStoreName(baseStore.getStoreName());
                        }
                    }
                    setCourseType(courseList,courseAccount.getType().toString());
                    for (StoreMemberList storeMemberList : storeMemberLists) {
                        if(courseAccount.getMemberId().compareTo(storeMemberList.getId())==0){
                            try {
                                courseList.setMobilePhone(AESUtils.decrypt(storeMemberList.getMobilePhone()).replaceAll("\u0000",""));
                            } catch (Exception e) {
                                    pageResponse.setCodeAndMsg(ResponseCode.MSG,"会员手机号解析失败");
                                    return pageResponse;
                            }
                            courseList.setMemberName(storeMemberList.getMemberName());
                            courseList.setMemberNum(storeMemberList.getMemberNum());
                        }
                    }
                    courseList.setState("已锁定");
                    if (courseAccount.getState()==1){
                        courseList.setState("正常");
                    }
                    for (RechargeType rechargeType : rechargeTypeList) {
                        if(courseAccount.getRechargeTypeId().compareTo(rechargeType.getId())==0){
                            courseList.setRechargeNum(rechargeType.getRechargeTypeNum());
                            courseList.setRechargeTypeName(rechargeType.getRechargeTypeName());
                            courseList.setFirstAmount(rechargeType.getFirstAmount());
                            courseList.setRechargeMoney(rechargeType.getRechargeMoney());
                            courseList.setDiscount(rechargeType.getDiscount());
                            courseList.setRechargeDonateType("比例");
                            if(rechargeType.getDonateType()==0){
                                courseList.setRechargeDonateType("固定");
                            }
                            courseList.setRechargeGiftMoney(rechargeType.getGiftMoney());
                        }
                    }
                    courseList.setCardRestMoney(courseAccount.getCardRestMoney());
                    courseList.setCardConsumeMoney(courseAccount.getCardConsumeMoney());
                    courseList.setCardGiftMoney(courseAccount.getCardGiftMoney());
                    courseList.setGiftConsumeMoney(courseAccount.getGiftConsumeMoney());
                    courseList.setLastConsumeDate(courseAccount.getLastConsumeTime());
                    courseList.setEndTime(courseAccount.getEndTime());

                    excelBaseProjectTypeList.add(courseList);
                }
            }

        PageInfo<ExcelRechargeList> pageInfo = new PageInfo<>(excelBaseProjectTypeList);
        pageInfo.setPageNum(list.getPageNum());
        pageInfo.setPageSize(list.getPageSize());
        pageInfo.setTotal(list.getTotal());
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;

    }
    private void setCourseType(ExcelRechargeList courseList, String courseType) {
        switch (courseType){
            case "0" : courseList.setRechargeType("赠卡");break;
            case "1" : courseList.setRechargeType("折扣卡");break;
            case "2" : courseList.setRechargeType("促销卡");break;
        }
    }


}
