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.dao.SsmRefundOrderDao;
import com.ssm.manage.entity.*;
import com.ssm.manage.entity.BaseZengkaRequest;
import com.ssm.manage.excelEntity.ExcelCourseMoney;
import com.ssm.manage.excelEntity.ExcelRechargeMoney;
import com.ssm.manage.extendEntity.*;
import com.ssm.manage.query.*;
import com.ssm.manage.response.*;
import com.ssm.manage.service.*;
import com.ssm.manage.utils.Constant;
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.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;
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.text.SimpleDateFormat;
import java.util.*;

@Api(value="负卡金统计controller",tags={"负卡金统计controller相关接口"})
@RequestMapping("/demay/ssm/negativeCard")
@RestController
@Slf4j
public class SsmNegativeCardController  {


    @Autowired
    private SsmBaseZengkaService ssmBaseZengkaService;
    @Autowired
    private SsmStoreAccountOutinService ssmStoreAccountOutinService;

    @Autowired
    private SsmRefundOrderDao ssmRefundOrderDao;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;

    @Autowired
    private SsmStoreTurnTreatmentService ssmStoreTurnTreatmentService;

    @Autowired
    private SsmBaseGrouponCardService ssmBaseGrouponCardService;

    @Autowired
    private SsmRechargeTurnCourseService ssmRechargeTurnCourseService;

    @Autowired
    private SsmRefundCourseService ssmRefundCourseService;
    @Autowired
    private SsmCashService ssmCashService;



    @Autowired
    private SsmRefundCashService ssmRefundCashService;

    @Autowired
    private SsmCardChangeService ssmCardChangeService;

    @Autowired
    private SsmNegativeCardService ssmNegativeCardService;

    @Autowired
    private SsmRefundOrderService ssmRefundOrderService;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;




    @ApiOperation(value = "当日负卡金记录查询", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
    @RequestMapping(path="/dayStatistic", method= RequestMethod.POST)
    public GenericResponse dayStatistic(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<NegativeDayStatistic> genericResponse = new GenericResponse<>();
        List<NegativeDayStatistic> list = new ArrayList<>();
        Long storeId = null;
        if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
            storeId = SessionHelp.getStoreId(session);
        }else{
            storeId=negativeDayStatistic.getStoreId();
        }

        try{
            list = ssmNegativeCardService.selectDayList(storeId, negativeDayStatistic.getDate());
        }catch (Exception e){
            log.error("SsmNegativeCardController.dayStatistic:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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

    @ApiOperation(value = "当日充值负卡金记录查询", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
    @RequestMapping(path="/rechargeDayStatistic", method= RequestMethod.POST)
    public GenericResponse dayStatisticNew(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<RechargeNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> negativeKazin = new ArrayList<>();
        RechargeNegative negativeKazin = new RechargeNegative();
        Long storeId = null;
        if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
            storeId = SessionHelp.getStoreId(session);
        }else{
            storeId=negativeDayStatistic.getStoreId();
        }

        try{
            negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
        }catch (Exception e){
            log.error("SsmNegativeCardController.dayStatistic:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setData(negativeKazin);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "当日充值负卡金记录查询--选择门店", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
    @RequestMapping(path="/rechargeDayStatistics", method= RequestMethod.POST)
    public GenericResponse dayStatisticNews(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<RechargeNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> negativeKazin = new ArrayList<>();
        RechargeNegative negativeKazin = new RechargeNegative();
        List<RechargeNegative> rechargeNegativeList = new ArrayList<>();
        Long storeId = null;
        /*if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
            storeId = SessionHelp.getStoreId(session);
        }else{
            storeId=negativeDayStatistic.getStoreId();
        }*/

        try{
            List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
            if (storeIdl.size() == 0){
                storeId = SessionHelp.getStoreId(session);
                negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
                rechargeNegativeList.add(negativeKazin);
            }else {
                for (Long id : storeIdl) {
                    negativeKazin = ssmNegativeCardService.rechargeDayStatistic(id, negativeDayStatistic.getDate());
                    rechargeNegativeList.add(negativeKazin);
                }
            }
            /*for (NegativeDayStatistic negativeDayStatistic : negativeDayStatistics) {
                if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
                    storeId = SessionHelp.getStoreId(session);
                    negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
                }else{
                    storeId=negativeDayStatistic.getStoreId();
                    negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
                }
                rechargeNegativeList.add(negativeKazin);
            }*/
            //negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
        }catch (Exception e){
            log.error("SsmNegativeCardController.dayStatistic:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        //genericResponse.setData(negativeKazin);
        genericResponse.setRows(rechargeNegativeList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "当日充值负卡金记录查询--选择门店", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
    @RequestMapping(path="/rechargeDayStatisticsByDate", method= RequestMethod.POST)
    public GenericResponse dayStatisticNewsByDate(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<RechargeNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> negativeKazin = new ArrayList<>();
        RechargeNegative negativeKazin = new RechargeNegative();
        List<RechargeNegative> rechargeNegativeList = new ArrayList<>();
        Long storeId = null;
        /*if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
            storeId = SessionHelp.getStoreId(session);
        }else{
            storeId=negativeDayStatistic.getStoreId();
        }*/

        try{
            List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
            if (storeIdl.size() == 0){
                storeId = SessionHelp.getStoreId(session);
                negativeKazin = ssmNegativeCardService.rechargeDayStatisticByDate(storeId, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                rechargeNegativeList.add(negativeKazin);
            }else {
                for (Long id : storeIdl) {
                    negativeKazin = ssmNegativeCardService.rechargeDayStatisticByDate(id, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                    rechargeNegativeList.add(negativeKazin);
                }
            }
            /*for (NegativeDayStatistic negativeDayStatistic : negativeDayStatistics) {
                if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
                    storeId = SessionHelp.getStoreId(session);
                    negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
                }else{
                    storeId=negativeDayStatistic.getStoreId();
                    negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
                }
                rechargeNegativeList.add(negativeKazin);
            }*/
            //negativeKazin = ssmNegativeCardService.rechargeDayStatistic(storeId, negativeDayStatistic.getDate());
        }catch (Exception e){
            log.error("SsmNegativeCardController.dayStatistic:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        //genericResponse.setData(negativeKazin);
        genericResponse.setRows(rechargeNegativeList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "当日疗程负卡金记录查询", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
    @RequestMapping(path="/CourseDayStatistic", method= RequestMethod.POST)
    public GenericResponse CourseDayStatistic(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<CourseNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> list = new ArrayList<>();
        CourseNegative negativeKazin = new CourseNegative();
        Long storeId = null;
        if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
            storeId = SessionHelp.getStoreId(session);
        }else{
            storeId=negativeDayStatistic.getStoreId();
        }

        try{
            negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
        }catch (Exception e){
            log.error("SsmNegativeCardController.dayStatistic:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setData(negativeKazin);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    @ApiOperation(value = "当日疗程负卡金记录查询--选择门店", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
    @RequestMapping(path="/CourseDayStatistics", method= RequestMethod.POST)
    public GenericResponse CourseDayStatistics(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<CourseNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> list = new ArrayList<>();
        CourseNegative negativeKazin = new CourseNegative();
        List<CourseNegative> negatives = new ArrayList<>();
        Long storeId = null;
        /*List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
        if (storeIdl.size() == 0){
            storeId = SessionHelp.getStoreId(session);
            negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
            negatives.add(negativeKazin);
        }else {
            for (Long id : storeIdl) {
                negativeKazin = ssmNegativeCardService.CourseDayStatistic(id, negativeDayStatistic.getDate());
                negatives.add(negativeKazin);
            }
        }*/
        try{
            List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
            if (storeIdl.size() == 0){
                storeId = SessionHelp.getStoreId(session);
                negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                negatives.add(negativeKazin);
            }else {
                for (Long id : storeIdl) {
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(id, negativeDayStatistic.getDate());
                    negatives.add(negativeKazin);
                }
            }
            /*for (NegativeDayStatistic negativeDayStatistic : negativeDayStatistics) {
                if (null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
                    storeId = SessionHelp.getStoreId(session);
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }else {
                    storeId = negativeDayStatistic.getStoreId();
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }
                negatives.add(negativeKazin);
            }*/
        }catch (Exception e){
            log.error("SsmNegativeCardController.dayStatistic:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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

    @ApiOperation(value = "疗程负卡金记录查询--选择门店", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
    @RequestMapping(path="/CourseDayStatisticsByDate", method= RequestMethod.POST)
    public GenericResponse CourseDayStatisticsByDate(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<CourseNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> list = new ArrayList<>();
        CourseNegative negativeKazin = new CourseNegative();
        List<CourseNegative> negatives = new ArrayList<>();
        Long storeId = null;
        /*List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
        if (storeIdl.size() == 0){
            storeId = SessionHelp.getStoreId(session);
            negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
            negatives.add(negativeKazin);
        }else {
            for (Long id : storeIdl) {
                negativeKazin = ssmNegativeCardService.CourseDayStatistic(id, negativeDayStatistic.getDate());
                negatives.add(negativeKazin);
            }
        }*/
        try{
            List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
            if (storeIdl.size() == 0){
                storeId = SessionHelp.getStoreId(session);
                negativeKazin = ssmNegativeCardService.CourseDayStatisticByDate(storeId, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                negatives.add(negativeKazin);
            }else {
                for (Long id : storeIdl) {
                    negativeKazin = ssmNegativeCardService.CourseDayStatisticByDate(id, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                    negatives.add(negativeKazin);
                }
            }
            /*for (NegativeDayStatistic negativeDayStatistic : negativeDayStatistics) {
                if (null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
                    storeId = SessionHelp.getStoreId(session);
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }else {
                    storeId = negativeDayStatistic.getStoreId();
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }
                negatives.add(negativeKazin);
            }*/
        }catch (Exception e){
            log.error("SsmNegativeCardController.dayStatistic:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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



    @ApiOperation(value = "选择日期进行负卡金记录添加", notes = "平台端-选择日期进行负卡金记录添加", response = GenericResponse.class)
    @RequestMapping(path="/saveNegative", method= RequestMethod.POST)
    public GenericResponse saveNegative(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse<>();
        int i = 0;
        Long storeId = SessionHelp.getStoreId(session);
        try{
            i = ssmNegativeCardService.saveNegativeRecord(negativeDayStatistic.getDate());
        }catch (Exception e){
            log.error("SsmNegativeCardController.saveNegative:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }

    @ApiOperation(value = "每月一号保存一次所有门店一个月的负卡金", notes = "门店统计报表-每月负卡金记录保存", response = GenericResponse.class)
    @Scheduled(cron = "0 0 0 1 1/1 ?" )
    public void saveNegativeMonthly() {
        ssmNegativeCardService.saveNegativeTask();
    }

//    @ApiOperation(value = "每月一号保存一次所有门店一个月的负卡金", notes = "门店统计报表-每月负卡金记录保存", response = GenericResponse.class)
//    @Scheduled(cron = "0 0 04 ? * MON" )
//    public void saveNegativeMonthlys() {
//        ssmNegativeCardService.saveNegativeTask();
//    }


//    @ApiOperation(value = "当日负卡金记录查询s", notes = "门店统计报表-当日负卡金记录查询", response = GenericResponse.class)
//    @RequestMapping(path="/dayStatisticNews", method= RequestMethod.POST)
//    public GenericResponse dayStatisticNews(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
//        GenericResponse<NegativeDayStatistic> genericResponse = new GenericResponse<>();
//        List<NegativeDayStatistic> list = new ArrayList<>();
//        Long storeId = null;
//        if(null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
//            storeId = SessionHelp.getStoreId(session);
//        }else{
//            storeId=negativeDayStatistic.getStoreId();
//        }
//
//        try{
//            list = ssmNegativeCardService.selectDayListNews(storeId, negativeDayStatistic.getDate());
//        }catch (Exception e){
//            log.error("SsmNegativeCardController.dayStatistic:", e);
//            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
//            return genericResponse;
//        }
//
//        genericResponse.setRows(list);
//        genericResponse.setCodeAndMsg(ResponseCode.OK);
//        return genericResponse;
//    }

        @ApiOperation(value = "充值负卡金", notes = "平台端-选择日期进行负卡金记录添加", response = GenericResponse.class)
        @RequestMapping(path="/fixedNegativeCardByRecharge", method= RequestMethod.POST)
        public GenericResponse fixedNegativeCardByRecharge(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
            GenericResponse genericResponse = new GenericResponse<>();
            List<RechargeMoney> rechargeMoneyList ;
            Long storeId = SessionHelp.getStoreId(session);
            if(negativeDayStatistic.getStoreIdl()==null||negativeDayStatistic.getStoreIdl().size()==0){
                List<Long> list = new ArrayList<>();
                list.add(storeId);
                negativeDayStatistic.setStoreIdl(list);
            }
            try{
                List<BaseStore> baseStoreList = ssmBaseStoreService.selectAll();
                rechargeMoneyList = ssmNegativeCardService.fixedNegativeCardByRecharge(negativeDayStatistic);
                for (RechargeMoney rechargeMoney : rechargeMoneyList) {
                    for (BaseStore baseStore : baseStoreList) {
                        if(rechargeMoney.getStoreId() == baseStore.getId() || rechargeMoney.getStoreId().toString().equals(baseStore.getId().toString())){
                            rechargeMoney.setStoreName(baseStore.getStoreNum()+"-"+baseStore.getStoreName());
                        }
                    }
                }

            }catch (Exception e){
                log.error("SsmNegativeCardController.saveNegative:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FAILED);
                return genericResponse;
            }
            genericResponse.setRows(rechargeMoneyList);
            genericResponse.setCodeAndMsg(ResponseCode.OK);
            return genericResponse;
    }


    @ApiOperation(value = "疗程负卡金", notes = "平台端-选择日期进行负卡金记录添加", response = GenericResponse.class)
    @RequestMapping(path="/fixedNegativeCardByCourse", method= RequestMethod.POST)
    public GenericResponse fixedNegativeCardByCourse(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse genericResponse = new GenericResponse<>();
        List<CourseMoney> courseMoneyList ;
        Long storeId = SessionHelp.getStoreId(session);
        if(negativeDayStatistic.getStoreIdl()==null||negativeDayStatistic.getStoreIdl().size()==0){
            List<Long> list = new ArrayList<>();
            list.add(storeId);
            negativeDayStatistic.setStoreIdl(list);
        }
        try{
            List<BaseStore> baseStoreList = ssmBaseStoreService.selectAll();
            courseMoneyList = ssmNegativeCardService.fixedNegativeCardByCourse(negativeDayStatistic);
            for (CourseMoney courseMoney : courseMoneyList) {
                for (BaseStore baseStore : baseStoreList) {
                    if(courseMoney.getStoreId() == baseStore.getId() || courseMoney.getStoreId().toString().equals(baseStore.getId().toString())){
                        courseMoney.setStoreName(baseStore.getStoreNum()+"-"+baseStore.getStoreName());
                    }
                }
            }
        }catch (Exception e){
            log.error("SsmNegativeCardController.saveNegative:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setRows(courseMoneyList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "单据导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/courseByDate/exportachis")
    public void courseByDate(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//        courseListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            String startTime = simpleDateFormat.format(negativeDayStatistic.getStartTime());
            String endTime = simpleDateFormat.format(negativeDayStatistic.getEndTime());
            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/courseMoney.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            GenericResponse<CourseNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> list = new ArrayList<>();
            CourseNegative negativeKazin = new CourseNegative();
            List<CourseNegative> negatives = new ArrayList<>();
            List<ExcelCourseMoney> excelCourseMonies = new ArrayList<>();
            Long storeId = null;
        /*List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
        if (storeIdl.size() == 0){
            storeId = SessionHelp.getStoreId(session);
            negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
            negatives.add(negativeKazin);
        }else {
            for (Long id : storeIdl) {
                negativeKazin = ssmNegativeCardService.CourseDayStatistic(id, negativeDayStatistic.getDate());
                negatives.add(negativeKazin);
            }
        }*/
                List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
                if (storeIdl.size() == 0){
                    storeId = SessionHelp.getStoreId(session);
                    negativeKazin = ssmNegativeCardService.CourseDayStatisticByDate(storeId, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                    negatives.add(negativeKazin);
                }else {
                    for (Long id : storeIdl) {
                        negativeKazin = ssmNegativeCardService.CourseDayStatisticByDate(id, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                        negatives.add(negativeKazin);
                    }
                }
            /*for (NegativeDayStatistic negativeDayStatistic : negativeDayStatistics) {
                if (null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
                    storeId = SessionHelp.getStoreId(session);
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }else {
                    storeId = negativeDayStatistic.getStoreId();
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }
                negatives.add(negativeKazin);
            }*/

            for (CourseNegative negative : negatives) {
                ExcelCourseMoney excelCourseMoney = new ExcelCourseMoney();
                BeanUtils.copyProperties(negative,excelCourseMoney);
                excelCourseMoney.setStartTime(startTime);
                excelCourseMoney.setEndTime(endTime);
                excelCourseMonies.add(excelCourseMoney);
            }
            Collections.sort(excelCourseMonies, new StrComparatorUtils<>("storeName", false));
            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(excelCourseMonies, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "单据导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/rechargeByDate/exportachis")
    public void rechargeByDate(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//        courseListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            String startTime = simpleDateFormat.format(negativeDayStatistic.getStartTime());
            String endTime = simpleDateFormat.format(negativeDayStatistic.getEndTime());
            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/rechargeMoney.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            GenericResponse<CourseNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> list = new ArrayList<>();
            RechargeNegative negativeKazin = new RechargeNegative();
            List<RechargeNegative> negatives = new ArrayList<>();
            List<ExcelRechargeMoney> excelCourseMonies = new ArrayList<>();
            Long storeId = null;
        /*List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
        if (storeIdl.size() == 0){
            storeId = SessionHelp.getStoreId(session);
            negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
            negatives.add(negativeKazin);
        }else {
            for (Long id : storeIdl) {
                negativeKazin = ssmNegativeCardService.CourseDayStatistic(id, negativeDayStatistic.getDate());
                negatives.add(negativeKazin);
            }
        }*/
            List<Long> storeIdl = negativeDayStatistic.getStoreIdl();
            if (storeIdl.size() == 0){
                storeId = SessionHelp.getStoreId(session);
                negativeKazin = ssmNegativeCardService.rechargeDayStatisticByDate(storeId, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                negatives.add(negativeKazin);
            }else {
                for (Long id : storeIdl) {
                    negativeKazin = ssmNegativeCardService.rechargeDayStatisticByDate(id, negativeDayStatistic.getStartTime(),negativeDayStatistic.getEndTime());
                    negatives.add(negativeKazin);
                }
            }
            /*for (NegativeDayStatistic negativeDayStatistic : negativeDayStatistics) {
                if (null == negativeDayStatistic.getStoreId() || "".equals(negativeDayStatistic.getStoreId())){
                    storeId = SessionHelp.getStoreId(session);
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }else {
                    storeId = negativeDayStatistic.getStoreId();
                    negativeKazin = ssmNegativeCardService.CourseDayStatistic(storeId, negativeDayStatistic.getDate());
                }
                negatives.add(negativeKazin);
            }*/

            for (RechargeNegative negative : negatives) {
                ExcelRechargeMoney excelCourseMoney = new ExcelRechargeMoney();
                BeanUtils.copyProperties(negative,excelCourseMoney);
                excelCourseMoney.setStartTime(startTime);
                excelCourseMoney.setEndTime(endTime);
                excelCourseMonies.add(excelCourseMoney);
            }
            Collections.sort(excelCourseMonies, new StrComparatorUtils<>("storeName", false));
            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(excelCourseMonies, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }


    @ApiOperation(value = "单据导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/courseMoney/exportachis")
    public void courseMoney(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//        courseListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            String startTime = simpleDateFormat.format(negativeDayStatistic.getStartTime());
            String endTime = simpleDateFormat.format(negativeDayStatistic.getEndTime());
            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/courseMoney.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            GenericResponse<CourseNegative> genericResponse = new GenericResponse<>();
//        List<NegativeKazin> list = new ArrayList<>();
            CourseNegative negativeKazin = new CourseNegative();
            List<ExcelCourseMoney> excelCourseMonies = new ArrayList<>();
            List<CourseMoney> courseMoneyList ;
            Long storeId = SessionHelp.getStoreId(session);
            if(negativeDayStatistic.getStoreIdl()==null||negativeDayStatistic.getStoreIdl().size()==0){
                List<Long> list = new ArrayList<>();
                list.add(storeId);
                negativeDayStatistic.setStoreIdl(list);
            }
            List<BaseStore> baseStoreList = ssmBaseStoreService.selectAll();
            courseMoneyList = ssmNegativeCardService.fixedNegativeCardByCourse(negativeDayStatistic);
            for (CourseMoney courseMoney : courseMoneyList) {
                for (BaseStore baseStore : baseStoreList) {
                    if(courseMoney.getStoreId() == baseStore.getId() || courseMoney.getStoreId().toString().equals(baseStore.getId().toString())){
                        courseMoney.setStoreName(baseStore.getStoreNum()+"-"+baseStore.getStoreName());
                    }
                }
            }

            for (CourseMoney negative : courseMoneyList) {
                ExcelCourseMoney excelCourseMoney = new ExcelCourseMoney();
                BeanUtils.copyProperties(negative,excelCourseMoney);
                excelCourseMoney.setStartTime(startTime);
                excelCourseMoney.setEndTime(endTime);
                excelCourseMonies.add(excelCourseMoney);
            }
            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(excelCourseMonies, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    @ApiOperation(value = "单据导出", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/rechargeMoney/exportachis")
    public void rechargeMoney(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

//        courseListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            String startTime = simpleDateFormat.format(negativeDayStatistic.getStartTime());
            String endTime = simpleDateFormat.format(negativeDayStatistic.getEndTime());
            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/rechargeMoney.xlsx");
//            BeanUtils.copyProperties(projectTypeRequest, baseProjectType);
            List<RechargeMoney> rechargeMoneyList ;
            List<ExcelRechargeMoney> excelCourseMonies = new ArrayList<>();
            Long storeId = SessionHelp.getStoreId(session);
            if(negativeDayStatistic.getStoreIdl()==null||negativeDayStatistic.getStoreIdl().size()==0){
                List<Long> list = new ArrayList<>();
                list.add(storeId);
                negativeDayStatistic.setStoreIdl(list);
            }
            List<BaseStore> baseStoreList = ssmBaseStoreService.selectAll();
            rechargeMoneyList = ssmNegativeCardService.fixedNegativeCardByRecharge(negativeDayStatistic);
            for (RechargeMoney rechargeMoney : rechargeMoneyList) {
                for (BaseStore baseStore : baseStoreList) {
                    if(rechargeMoney.getStoreId() == baseStore.getId() || rechargeMoney.getStoreId().toString().equals(baseStore.getId().toString())){
                        rechargeMoney.setStoreName(baseStore.getStoreNum()+"-"+baseStore.getStoreName());
                    }
                }
            }

            for (RechargeMoney negative : rechargeMoneyList) {
                ExcelRechargeMoney excelCourseMoney = new ExcelRechargeMoney();
                BeanUtils.copyProperties(negative,excelCourseMoney);
                excelCourseMoney.setStartTime(startTime);
                excelCourseMoney.setEndTime(endTime);
                excelCourseMonies.add(excelCourseMoney);
            }
            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(excelCourseMonies, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }

    //充值还款
    @ApiOperation(value = "分页查询  门店端-还款单-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/repayRechargeList")
    public PageResponse repayRechargeList(@RequestBody RepayListRequest repayListRequest, HttpSession session){
        PageResponse<RepayListResponse> pageResponse = new PageResponse<>();
        PageInfo<RepayListResponse> pageInfo = new PageInfo<>();
        List<Long> listStoreId=new ArrayList<>();
        if(repayListRequest.getStoreId() == null && (null == repayListRequest.getStoreList() || repayListRequest.getStoreList().size() == 0)){
            //自动查询
            repayListRequest.setSelectType(0);
            repayListRequest.setStoreId(SessionHelp.getStoreId(session));
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(repayListRequest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            repayListRequest.setStoreList(collect);
            listStoreId.add(SessionHelp.getStoreId(session));
            repayListRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            repayListRequest.setSelectType(0);
        }

        try {
            if(repayListRequest.getStoreList()!=null && repayListRequest.getStoreList().size()>0){

                repayListRequest.setStoreId(repayListRequest.getStoreList().get(0));
            }
            repayListRequest.setDocType(Constant.doc_type_recharge);
            pageInfo = ssmRepayService.selectPageNewByRepay(repayListRequest);
        }catch (Exception e){
            log.error("SsmRepayController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //查询充值还款到账赠送金额
    @ApiOperation(value = "分页查询  门店端-还款单-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/repayRechargeGiftList")
    public PageResponse repayRechargeGiftList(@RequestBody RepayListRequest repayListRequest, HttpSession session){
        PageResponse<RepayListResponse> pageResponse = new PageResponse<>();
        PageInfo<RepayListResponse> pageInfo = new PageInfo<>();
        List<Long> listStoreId=new ArrayList<>();
        if(repayListRequest.getStoreId() == null && (null == repayListRequest.getStoreList() || repayListRequest.getStoreList().size() == 0)){
            //自动查询
            repayListRequest.setSelectType(0);
            repayListRequest.setStoreId(SessionHelp.getStoreId(session));
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(repayListRequest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            repayListRequest.setStoreList(collect);
            listStoreId.add(SessionHelp.getStoreId(session));
            repayListRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            repayListRequest.setSelectType(0);
        }

        try {
            if(repayListRequest.getStoreList()!=null && repayListRequest.getStoreList().size()>0){

                repayListRequest.setStoreId(repayListRequest.getStoreList().get(0));
            }
            repayListRequest.setIsGift(0);
            repayListRequest.setDocType(Constant.doc_type_recharge);
            pageInfo = ssmRepayService.selectPageNewByRepay(repayListRequest);
        }catch (Exception e){
            log.error("SsmRepayController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //查询充值赠送
    @ApiOperation(value = "分页查询  门店端-还款单-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/rechargeGifList")
    public PageResponse rechargeGifList(@RequestBody RechargeListRequest rechargeListRequest, HttpSession session){
        PageResponse<RechargeListResponse> pageResponse = new PageResponse<>();
        PageInfo<RechargeListResponse> pageInfo = new PageInfo<>();

        Long storeId = SessionHelp.getStoreId(session);
        List<Long> listStoreId=new ArrayList<>();
        if(null == rechargeListRequest.getStoreList() || rechargeListRequest.getStoreList().size() == 0){
            //自动查询
            rechargeListRequest.setSelectType(1);
            rechargeListRequest.setStoreId(storeId);
            listStoreId.add(storeId);
            rechargeListRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            rechargeListRequest.setSelectType(0);
        }

        try {
            rechargeListRequest.setIsDebt(0);
            pageInfo = ssmRechargeService.rechargeGifList(rechargeListRequest);
        }catch (Exception e){
            log.error("SsmRechargeController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }


        //查询疗程列表
        @ApiOperation(value = "分页查询疗程单  (门店端-疗程单-列表分页查询疗程单)",httpMethod = "POST",response = PageResponse.class)
        @RequestMapping("/courseListKaika")
        public PageResponse selectPageNew(@RequestBody CourseListRequest courseListRequest, HttpSession session){
            PageResponse<CourseListResponse> pageResponse = new PageResponse<>();
            PageInfo<CourseListResponse> pageInfo = new PageInfo<>();
            if(SessionHelp.getStoreId(session)==null){
                pageResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
                return pageResponse;
            }
            Long storeId = SessionHelp.getStoreId(session);
            List<Long> listStoreId=new ArrayList<>();
            if(null == courseListRequest.getStoreList()|| courseListRequest.getStoreList().size()==0){
                courseListRequest.setSelectType(1);
                courseListRequest.setStoreId(storeId);
                listStoreId.add(storeId);
                courseListRequest.setStoreList(listStoreId);
            }else{
                courseListRequest.setSelectType(0);
            }
            try {
                courseListRequest.setIsCancel(0);
                pageInfo = ssmCourseService.selectPageNew(courseListRequest);
            }catch (SmsException e){
                pageResponse.setCodeAndMsg(e);
                return pageResponse;
            }catch (Exception e){
                log.error("SsmCourseController.selectPage:",e);
                pageResponse.setCodeAndMsg(ResponseCode.FAILED);
                return pageResponse;
            }
            pageResponse.setCodeAndMsg(ResponseCode.OK);
            pageResponse.setPageInfo(pageInfo);
            return pageResponse;
        }

    //查询疗程还款
    @ApiOperation(value = "分页查询  门店端-还款单-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/repayCourseList")
    public PageResponse repayCourseList(@RequestBody RepayListRequest repayListRequest, HttpSession session){
        PageResponse<RepayListResponse> pageResponse = new PageResponse<>();
        PageInfo<RepayListResponse> pageInfo = new PageInfo<>();
        List<Long> listStoreId=new ArrayList<>();
        if(repayListRequest.getStoreId() == null && (null == repayListRequest.getStoreList() || repayListRequest.getStoreList().size() == 0)){
            //自动查询
            repayListRequest.setSelectType(0);
            repayListRequest.setStoreId(SessionHelp.getStoreId(session));
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(repayListRequest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            repayListRequest.setStoreList(collect);
            listStoreId.add(SessionHelp.getStoreId(session));
            repayListRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            repayListRequest.setSelectType(0);
        }

        try {
            if(repayListRequest.getStoreList()!=null && repayListRequest.getStoreList().size()>0){

                repayListRequest.setStoreId(repayListRequest.getStoreList().get(0));
            }
            repayListRequest.setDocType(Constant.doc_type_card_use);
//            repayListRequest.setTwoDocType(Constant.promotion_card_use);
            pageInfo = ssmRepayService.selectPageNewByRepay(repayListRequest);
        }catch (Exception e){
            log.error("SsmRepayController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }
    //查询疗程促销还款
    @ApiOperation(value = "分页查询  门店端-还款单-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/repayCoursePromotionList")
    public PageResponse repayCoursePromotionList(@RequestBody RepayListRequest repayListRequest, HttpSession session){
        PageResponse<RepayListResponse> pageResponse = new PageResponse<>();
        PageInfo<RepayListResponse> pageInfo = new PageInfo<>();
        List<Long> listStoreId=new ArrayList<>();
        if(repayListRequest.getStoreId() == null && (null == repayListRequest.getStoreList() || repayListRequest.getStoreList().size() == 0)){
            //自动查询
            repayListRequest.setSelectType(0);
            repayListRequest.setStoreId(SessionHelp.getStoreId(session));
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
////            sysStoreRole.setRoleId(repayListRequest.getRoleId());
////            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
////            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
////            repayListRequest.setStoreList(collect);
            listStoreId.add(SessionHelp.getStoreId(session));
            repayListRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            repayListRequest.setSelectType(0);
        }

        try {
            if(repayListRequest.getStoreList()!=null && repayListRequest.getStoreList().size()>0){

                repayListRequest.setStoreId(repayListRequest.getStoreList().get(0));
            }
//            repayListRequest.setDocType(Constant.doc_type_card_use);
            repayListRequest.setDocType(Constant.promotion_card_use);
            pageInfo = ssmRepayService.selectPageNewByRepay(repayListRequest);
        }catch (Exception e){
            log.error("SsmRepayController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }


    //疗程促销
    @ApiOperation(value = "分页查询  门店端-促销活动单-分页查询促销活动单（新）", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/coursePromotionList")
    public PageResponse coursePromotionList(@RequestBody DocPromotionRequest docPromotionRequest, HttpSession session) {
        PageResponse<DocPromotionResponse> pageResponse = new PageResponse<>();
        PageInfo<DocPromotionResponse> pageInfo = new PageInfo<>();

        try {
            docPromotionRequest.setSelectType(0);
            docPromotionRequest.setIsCancel(0);
            pageInfo = ssmDocPromotionService.selectPageNew(docPromotionRequest);
            for (DocPromotionResponse docPromotionResponse : pageInfo.getList()) {
                docPromotionResponse.setAmount(docPromotionResponse.getCollectionRechargeAmount().add(docPromotionResponse.getCollectionCashAmount()));
            }
        } catch (Exception e) {
            log.error("SsmDocPromotionController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //疗程卡付消费退
    @ApiOperation(value = "分页查询  门店端-退款单-分页查询退款单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/refundCourseCash")
    public GenericResponse<List<RefundOrderList>> refundCourseCash(@RequestBody RefundOrderResquest refundOrderResquest, HttpSession session) {
        GenericResponse<List<RefundOrderList>> pageResponse = new GenericResponse<>();
        List<RefundOrderList> pageInfo = new ArrayList<>();
//        if(null == refundOrderResquest.getStoreList()|| refundOrderResquest.getStoreList().size()==0){
//            refundOrderResquest.setSelectType(0);
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(refundOrderResquest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            refundOrderResquest.setStoreList(collect);
////            cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
//        }else{
//            refundOrderResquest.setSelectType(1);
//            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
//        }
        if(null == refundOrderResquest.getStoreList() || refundOrderResquest.getStoreList().size()==0){
            refundOrderResquest.setSelectType(1);
            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
        }else{
            refundOrderResquest.setSelectType(0);
        }

        try {
            refundOrderResquest.setOrderType(1);
            pageInfo = ssmRefundOrderService.refundCourseCash(refundOrderResquest);
        } catch (Exception e) {
            log.error("SsmRefundOrderController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setData(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    //查询加价转疗程
    @ApiOperation(value = "加价转疗程分页列表查询 门店端-加价-加价转疗程分页列表查询",httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/cardchangeList")
    public PageResponse cardchangeList(@RequestBody CardChangeListRequest cardChangeListRequest, HttpSession session) {
        PageResponse<CardChangeList> pageResponse = new PageResponse<>();
        PageInfo<CardChangeList> pageInfo = new PageInfo<>();
        cardChangeListRequest.setSelectType(0);
        cardChangeListRequest.setState(3);
        try {
            cardChangeListRequest.setCourseType(1);
            pageInfo = ssmCardChangeService.selectPageNew(cardChangeListRequest);
//            List<CardChangeList> list = pageInfo.getList();
//            List<CardChangeList> zlc = list.stream().filter(item -> item.getDocNum().startsWith("ZLC")).collect(Collectors.toList());
//            pageInfo.setList(zlc);
        } catch (Exception e) {
            log.error("SsmCardChangeController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //疗程转入门店
    @ApiOperation(value = "疗程转入门店和疗程转出门店", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/courseTurnStore", method = RequestMethod.POST)
    public PageResponse page(@RequestBody StoreTurnTreatmentQuery storeConversion, HttpSession session) {
        PageResponse<StoreTurnTreatmentQuery> genericResponse = new PageResponse();
        PageInfo<StoreTurnTreatmentQuery> pageInfo = new PageInfo<>();
        try {

            pageInfo = ssmStoreTurnTreatmentService.selectPage(storeConversion);
        }catch (SmsException e){
            log.error("SsmStoreTurnTreatmentController page",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmStoreConversionController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setPageInfo(pageInfo);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    //疗程卡付还款
    @ApiOperation(value = "分页查询  门店端-还款单-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/coursePayList")
    public PageResponse coursePayList(@RequestBody RepayListRequest repayListRequest, HttpSession session){
        PageResponse<RepayListResponse> pageResponse = new PageResponse<>();
        PageInfo<RepayListResponse> pageInfo = new PageInfo<>();
        List<Long> listStoreId=new ArrayList<>();
        if(repayListRequest.getStoreId() == null && (null == repayListRequest.getStoreList() || repayListRequest.getStoreList().size() == 0)){
            //自动查询
            repayListRequest.setSelectType(0);
            repayListRequest.setStoreId(SessionHelp.getStoreId(session));
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(repayListRequest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            repayListRequest.setStoreList(collect);
            listStoreId.add(SessionHelp.getStoreId(session));
            repayListRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            repayListRequest.setSelectType(0);
        }

        try {
            if(repayListRequest.getStoreList()!=null && repayListRequest.getStoreList().size()>0){

                repayListRequest.setStoreId(repayListRequest.getStoreList().get(0));
            }
//            repayListRequest.setDocType(Constant.doc_type_card_use);
//            repayListRequest.setTwoDocType(Constant.promotion_card_use);
            repayListRequest.setAccountType(1);
            pageInfo = ssmRepayService.selectPageNewByCoursePay(repayListRequest);
        }catch (Exception e){
            log.error("SsmRepayController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //疗程销卡
    @ApiOperation(value = "查询收银服务单收款列表  (门店端-服务单-查询收银服务单收款列表)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/courseCashList")
    public PageResponse courseCashList(@RequestBody CashListRequest cashListRequest, HttpSession session){
        PageResponse<CashListResponse> pageResponse = new PageResponse<>();
        PageInfo<CashListResponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        if(SessionHelp.getStoreId(session)==null){
            pageResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return pageResponse;
        }
        List<Long> listStoreId=new ArrayList<>();

        cashListRequest.setStoreListboolean(1);
        if( cashListRequest.getStoreList()!=null&&cashListRequest.getStoreList().size()>0)
            cashListRequest.setStoreListboolean(0);

        if(null == cashListRequest.getStoreList()|| cashListRequest.getStoreList().size()<=0){
            cashListRequest.setSelectType(1);
            cashListRequest.setStoreId(storeId);
            listStoreId.add(storeId);
            cashListRequest.setStoreList(listStoreId);
        }else{
            cashListRequest.setSelectType(0);
        }

        try {
            cashListRequest.setCourseAccountId(1L);
            cashListRequest.setIsCancel(0);
            pageInfo = ssmCashService.selectCashListNew(cashListRequest);
        }catch (SmsException e){
            pageResponse.setCodeAndMsg(e);
            return pageResponse;
        }catch (Exception e){
            log.error("SsmCashController.selectCashList:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }
    //疗程退款
    @ApiOperation(value = "分页查询  门店端-退款单-分页查询退款单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/courseRefund")
    public GenericResponse<List<RefundOrderList>> selectPageNew(@RequestBody RefundOrderResquest refundOrderResquest, HttpSession session) {
        GenericResponse<List<RefundOrderList>> pageResponse = new GenericResponse<>();
        List<RefundOrderList> pageInfo = new ArrayList<>();
//        if(null == refundOrderResquest.getStoreList()|| refundOrderResquest.getStoreList().size()==0){
//            refundOrderResquest.setSelectType(0);
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(refundOrderResquest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            refundOrderResquest.setStoreList(collect);
////            cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
//        }else{
//            refundOrderResquest.setSelectType(1);
//            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
//        }
        if(null == refundOrderResquest.getStoreList() || refundOrderResquest.getStoreList().size()==0){
            refundOrderResquest.setSelectType(1);
            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
        }else{
            refundOrderResquest.setSelectType(0);
        }

        try { //充值退@消费退
            RefundOrderList refundOrderList = new RefundOrderList();
            refundOrderList.setRefundDate(refundOrderResquest.getStartTime());
            refundOrderList.setStartTime(refundOrderResquest.getStartTime());
            refundOrderList.setEndTime(refundOrderResquest.getEndTime());
            refundOrderList.setStoreId(refundOrderResquest.getStoreId());
            List<RefundOrderList> refundOrderLists = ssmRefundOrderService.selectFKJByDate(refundOrderList);
            for (RefundOrderList orderList : refundOrderLists) {
                if(orderList.getType()==1){
                    RefundCourse refundRecharge = new RefundCourse();
                    refundRecharge.setStoreId(orderList.getStoreId());
                    refundRecharge.setRefundNum(orderList.getRefundNum());
                    List<RefundCourse> select1 = ssmRefundCourseService.select(refundRecharge);
//                    BigDecimal reduce = select1.stream().map(RefundCourse::getCourseRefundMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    pageInfo.add(orderList);
                }
            }
        } catch (Exception e) {
            log.error("SsmRefundOrderController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setData(pageInfo);
        return pageResponse;
    }

    //疗程转充值
    @ApiOperation(value = "查询疗程转充值列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/courseTurnRecharge")
    public PageResponse selectPageNew(@RequestBody RechargeTurnCourseRequest rechargeTurnCourseRequest, HttpSession session){
        PageResponse<RechargeTurnCourseRequest> pageResponse = new PageResponse<>();
        PageInfo<RechargeTurnCourseRequest> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        List<Long> listStoreId=new ArrayList<>();
        if(null == rechargeTurnCourseRequest.getStoreList() || rechargeTurnCourseRequest.getStoreList().size() == 0){
            //自动查询
            rechargeTurnCourseRequest.setSelectType(1);
            rechargeTurnCourseRequest.setStoreId(storeId);
            listStoreId.add(storeId);
            rechargeTurnCourseRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            rechargeTurnCourseRequest.setSelectType(0);
        }
        try {
            rechargeTurnCourseRequest.setState(0);
            pageInfo = ssmRechargeTurnCourseService.selectPageNew(rechargeTurnCourseRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }


    //充值卡付消费退
    @ApiOperation(value = "分页查询  门店端-退款单-分页查询退款单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/refundRechargeCash")
    public GenericResponse<List<RefundOrderList>> refundRechargeCash(@RequestBody RefundOrderResquest refundOrderResquest, HttpSession session) {
        GenericResponse<List<RefundOrderList>> pageResponse = new GenericResponse<>();
        List<RefundOrderList> pageInfo = new ArrayList<>();
//        if(null == refundOrderResquest.getStoreList()|| refundOrderResquest.getStoreList().size()==0){
//            refundOrderResquest.setSelectType(0);
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(refundOrderResquest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            refundOrderResquest.setStoreList(collect);
////            cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
//        }else{
//            refundOrderResquest.setSelectType(1);
//            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
//        }
        if(null == refundOrderResquest.getStoreList() || refundOrderResquest.getStoreList().size()==0){
            refundOrderResquest.setSelectType(1);
            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
        }else{
            refundOrderResquest.setSelectType(0);
        }

        try {
            refundOrderResquest.setOrderType(2);
            pageInfo = ssmRefundOrderService.refundCourseCash(refundOrderResquest);
        } catch (Exception e) {
            log.error("SsmRefundOrderController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setData(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    //充值赠送卡付消费退
    @ApiOperation(value = "分页查询  门店端-退款单-分页查询退款单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/refundRechargeGiftCash")
    public GenericResponse<List<RefundOrderList>> refundRechargeGiftCash(@RequestBody RefundOrderResquest refundOrderResquest, HttpSession session) {
        GenericResponse<List<RefundOrderList>> pageResponse = new GenericResponse<>();
        List<RefundOrderList> pageInfo = new ArrayList<>();
//        if(null == refundOrderResquest.getStoreList()|| refundOrderResquest.getStoreList().size()==0){
//            refundOrderResquest.setSelectType(0);
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(refundOrderResquest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            refundOrderResquest.setStoreList(collect);
////            cardChangeListRequest.setStoreId(SessionHelp.getStoreId(session));
//        }else{
//            refundOrderResquest.setSelectType(1);
//            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
//        }
        if(null == refundOrderResquest.getStoreList() || refundOrderResquest.getStoreList().size()==0){
            refundOrderResquest.setSelectType(1);
            refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
        }else{
            refundOrderResquest.setSelectType(0);
        }

        try {
            refundOrderResquest.setOrderType(3);
            pageInfo = ssmRefundOrderService.refundCourseCash(refundOrderResquest);
        } catch (Exception e) {
            log.error("SsmRefundOrderController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setData(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }
    //充值卡转出转入
    @ApiOperation(value = "促销仅门店使用卡转店申请查询", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/rechargeOutIn", method = RequestMethod.POST)
    public PageResponse rechargeOutIn(@RequestBody RechargeAccountOutinRequest rechargeAccountOutinRequest, HttpSession session) {
        PageResponse<RechargeAccountOutinRequest> genericResponse = new PageResponse();
        PageInfo<RechargeAccountOutinRequest> pageInfo = new PageInfo<>();
        try {
//            SimpleDateFormat f = new SimpleDateFormat("yyyy-MM");
//            String format = f.format(rechargeAccountOutinRequest.getDocDate());
//            rechargeAccountOutinRequest.setDate(format);
            pageInfo = ssmStoreAccountOutinService.selectPage(rechargeAccountOutinRequest);
        }catch (SmsException e){
            log.error("SsmStoreAccountOutinController page",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmStoreAccountOutinController.page:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setPageInfo(pageInfo);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    //充值卡付消费退
    @ApiOperation(value = "促销仅门店使用卡转店申请查询", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/cashRechargeRefund", method = RequestMethod.POST)
    public GenericResponse<List<RefundOrderList>> cashRechargeRefund(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<List<RefundOrderList>> genericResponse = new GenericResponse();
        List<RefundOrderList> refundOrderListList = new ArrayList<>();
        try {
            //充值退@消费退
            Date startTime = negativeDayStatistic.getStartTime();
            Date endTime = negativeDayStatistic.getEndTime();
            Long storeId = negativeDayStatistic.getStoreId();
            RefundOrderList refundOrderList = new RefundOrderList();
            refundOrderList.setRefundDate(startTime);
            refundOrderList.setStartTime(startTime);
            refundOrderList.setEndTime(endTime);
            refundOrderList.setStoreId(storeId);
            List<RefundOrderList> refundOrderLists = ssmRefundOrderService.selectFKJByDate(refundOrderList);

            for (RefundOrderList orderList : refundOrderLists) {
                if(orderList.getType()==2){
                    RefundCash refundCash = new RefundCash();
                    refundCash.setStoreId(orderList.getStoreId());
                    refundCash.setRefundNum(orderList.getRefundNum());
                    List<RefundCash> select1 = ssmRefundCashService.select(refundCash);
                    BigDecimal rechargeMoney = BigDecimal.ZERO;
                    for (RefundCash cash : select1) {
                        CashList cashList = new CashList();
                        cashList.setId(cash.getCashId());
                        cashList.setCashDate(cash.getCashDate());
                        List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                        rechargeMoney = rechargeMoney.add(cashLists.stream().map(item->item.getCollectionRechargeAmount().subtract(item.getCollectionGiftAmount())).reduce(BigDecimal.ZERO,BigDecimal::add));
                    }
                    if(rechargeMoney.compareTo(BigDecimal.ZERO)==1){
                        orderList.setRefundMoney(rechargeMoney);
                        orderList.setActualRefundMoney(rechargeMoney);
                        refundOrderListList.add(orderList);
                    }
                }
            }
        } catch (Exception e) {
            log.error("SsmStoreAccountOutinController.page:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(refundOrderListList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }


    //充值卡赠送金额消费退
    @ApiOperation(value = "促销仅门店使用卡转店申请查询", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping(value = "/cashRechargeGiftRefund", method = RequestMethod.POST)
    public GenericResponse<List<RefundOrderList>> cashRechargeGiftRefund(@RequestBody NegativeDayStatistic negativeDayStatistic, HttpSession session) {
        GenericResponse<List<RefundOrderList>> genericResponse = new GenericResponse();
        List<RefundOrderList> refundOrderListList = new ArrayList<>();
        try {
            //充值退@消费退
            Date startTime = negativeDayStatistic.getStartTime();
            Date endTime = negativeDayStatistic.getEndTime();
            Long storeId = negativeDayStatistic.getStoreId();
            RefundOrderList refundOrderList = new RefundOrderList();
            refundOrderList.setRefundDate(startTime);
            refundOrderList.setStartTime(startTime);
            refundOrderList.setEndTime(endTime);
            refundOrderList.setStoreId(storeId);
            List<RefundOrderList> refundOrderLists = ssmRefundOrderService.selectFKJByDate(refundOrderList);

            for (RefundOrderList orderList : refundOrderLists) {
                if(orderList.getType()==2){
                    RefundCash refundCash = new RefundCash();
                    refundCash.setStoreId(orderList.getStoreId());
                    refundCash.setRefundNum(orderList.getRefundNum());
                    List<RefundCash> select1 = ssmRefundCashService.select(refundCash);
                    BigDecimal rechargeMoney = BigDecimal.ZERO;
                    for (RefundCash cash : select1) {
                        CashList cashList = new CashList();
                        cashList.setId(cash.getCashId());
                        cashList.setCashDate(cash.getCashDate());
                        List<CashList> cashLists = ssmCashService.selectListSalem(cashList);
                        rechargeMoney = rechargeMoney.add( cashLists.stream().map(CashList::getCollectionGiftAmount).reduce(BigDecimal.ZERO,BigDecimal::add));
                    }
                    if(rechargeMoney.compareTo(BigDecimal.ZERO)==1){
                        orderList.setRefundMoney(rechargeMoney);
                        orderList.setActualRefundMoney(rechargeMoney);
                        refundOrderListList.add(orderList);
                    }
                }
            }

        } catch (Exception e) {
            log.error("SsmStoreAccountOutinController.page:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setData(refundOrderListList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
    //团购卡列表
    @ApiOperation(value = "查询全部员工排班",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/groupCardList")
    public PageResponse groupCardList(@RequestBody BaseGrouponCardRequest baseGrouponCardRequest, HttpSession session){
        PageResponse<BaseGrouponCardResponse> pageResponse = new PageResponse<>();
        PageInfo<BaseGrouponCardResponse> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        baseGrouponCardRequest.setStoreId(storeId);
        try {
            baseGrouponCardRequest.setState(3);
            pageInfo = ssmBaseGrouponCardService.selectPage(baseGrouponCardRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //查询充值卡买疗程列表
    @ApiOperation(value = "分页查询疗程单  (门店端-疗程单-列表分页查询疗程单)",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/courseListByRecharge")
    public PageResponse courseListByRecharge(@RequestBody CourseListRequest courseListRequest, HttpSession session){
        PageResponse<CourseListResponse> pageResponse = new PageResponse<>();
        PageInfo<CourseListResponse> pageInfo = new PageInfo<>();
        if(SessionHelp.getStoreId(session)==null){
            pageResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return pageResponse;
        }
        Long storeId = SessionHelp.getStoreId(session);
        List<Long> listStoreId=new ArrayList<>();
        if(null == courseListRequest.getStoreList()|| courseListRequest.getStoreList().size()==0){
            courseListRequest.setSelectType(1);
            courseListRequest.setStoreId(storeId);
            listStoreId.add(storeId);
            courseListRequest.setStoreList(listStoreId);
        }else{
            courseListRequest.setSelectType(0);
        }
        try {
            courseListRequest.setIsCancel(0);
            courseListRequest.setCollectionRechargeAmount(BigDecimal.ZERO);
            pageInfo = ssmCourseService.selectPageNew(courseListRequest);
        }catch (SmsException e){
            pageResponse.setCodeAndMsg(e);
            return pageResponse;
        }catch (Exception e){
            log.error("SsmCourseController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //充值卡付还款
    @ApiOperation(value = "分页查询  门店端-还款单-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/rechargePayList")
    public PageResponse rechargePayList(@RequestBody RepayListRequest repayListRequest, HttpSession session){
        PageResponse<RepayListResponse> pageResponse = new PageResponse<>();
        PageInfo<RepayListResponse> pageInfo = new PageInfo<>();
        List<Long> listStoreId=new ArrayList<>();
        if(repayListRequest.getStoreId() == null && (null == repayListRequest.getStoreList() || repayListRequest.getStoreList().size() == 0)){
            //自动查询
            repayListRequest.setSelectType(0);
            repayListRequest.setStoreId(SessionHelp.getStoreId(session));
//            SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
//            sysStoreRole.setRoleId(repayListRequest.getRoleId());
//            PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
//            List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
//            repayListRequest.setStoreList(collect);
            listStoreId.add(SessionHelp.getStoreId(session));
            repayListRequest.setStoreList(listStoreId);
        }else{
            //指定查询
            repayListRequest.setSelectType(0);
        }

        try {
            if(repayListRequest.getStoreList()!=null && repayListRequest.getStoreList().size()>0){

                repayListRequest.setStoreId(repayListRequest.getStoreList().get(0));
            }
//            repayListRequest.setDocType(Constant.doc_type_card_use);
//            repayListRequest.setTwoDocType(Constant.promotion_card_use);
            repayListRequest.setAccountType(0);
            pageInfo = ssmRepayService.selectPageNewByCoursePay(repayListRequest);
            for (RepayListResponse repayListResponse : pageInfo.getList()) {
                BigDecimal reduce = repayListResponse.getRepayTradeLists().stream().map(item -> item.getPayRechargeMoney()).reduce(BigDecimal.ZERO, BigDecimal::add);
                repayListResponse.setRechargePayMoney(reduce);
            }
        }catch (Exception e){
            log.error("SsmRepayController.selectPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //充值卡付收银
    @ApiOperation(value = "查询收银服务单收款列表  (门店端-服务单-查询收银服务单收款列表)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/rechargeCashList")
    public PageResponse rechargeCashList(@RequestBody CashListRequest cashListRequest, HttpSession session){
        PageResponse<CashListResponse> pageResponse = new PageResponse<>();
        PageInfo<CashListResponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        if(SessionHelp.getStoreId(session)==null){
            pageResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return pageResponse;
        }
        List<Long> listStoreId=new ArrayList<>();

        cashListRequest.setStoreListboolean(1);
        if( cashListRequest.getStoreList()!=null&&cashListRequest.getStoreList().size()>0)
            cashListRequest.setStoreListboolean(0);

        if(null == cashListRequest.getStoreList()|| cashListRequest.getStoreList().size()<=0){
            cashListRequest.setSelectType(1);
            cashListRequest.setStoreId(storeId);
            listStoreId.add(storeId);
            cashListRequest.setStoreList(listStoreId);
        }else{
            cashListRequest.setSelectType(0);
        }

        try {
            cashListRequest.setIsCancel(0);
            cashListRequest.setCollectionRechargeAmount(BigDecimal.ZERO);
            pageInfo = ssmCashService.selectCashListNew(cashListRequest);
            for (CashListResponse cashListResponse : pageInfo.getList()) {
                BigDecimal reduce = cashListResponse.getProjectList().stream().map(item -> item.getRechargeConsumeList().stream().map(x -> x.getAccountGiftSpendMoney()).reduce(BigDecimal.ZERO, BigDecimal::add)).reduce(BigDecimal.ZERO, BigDecimal::add);
                cashListResponse.setRechargeSum(cashListResponse.getRechargeSum()-reduce.doubleValue());
            }
        }catch (SmsException e){
            pageResponse.setCodeAndMsg(e);
            return pageResponse;
        }catch (Exception e){
            log.error("SsmCashController.selectCashList:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }


    //充值卡付收银
    @ApiOperation(value = "查询收银服务单收款列表  (门店端-服务单-查询收银服务单收款列表)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/rechargeCashGiftList")
    public PageResponse rechargeCashGiftList(@RequestBody CashListRequest cashListRequest, HttpSession session){
        PageResponse<CashListResponse> pageResponse = new PageResponse<>();
        PageInfo<CashListResponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        if(SessionHelp.getStoreId(session)==null){
            pageResponse.setCodeAndMsg(ResponseCode.MSG,"登录失效，请退出重新登录！");
            return pageResponse;
        }
        List<Long> listStoreId=new ArrayList<>();

        cashListRequest.setStoreListboolean(1);
        if( cashListRequest.getStoreList()!=null&&cashListRequest.getStoreList().size()>0)
            cashListRequest.setStoreListboolean(0);

        if(null == cashListRequest.getStoreList()|| cashListRequest.getStoreList().size()<=0){
            cashListRequest.setSelectType(1);
            cashListRequest.setStoreId(storeId);
            listStoreId.add(storeId);
            cashListRequest.setStoreList(listStoreId);
        }else{
            cashListRequest.setSelectType(0);
        }

        try {
            cashListRequest.setIsCancel(0);
            cashListRequest.setCollectionGiftAmount(BigDecimal.ZERO);
            pageInfo = ssmCashService.selectCashListNew(cashListRequest);
            for (CashListResponse cashListResponse : pageInfo.getList()) {
                BigDecimal reduce = cashListResponse.getProjectList().stream().map(item -> item.getRechargeConsumeList().stream().map(x -> x.getAccountGiftSpendMoney()).reduce(BigDecimal.ZERO, BigDecimal::add)).reduce(BigDecimal.ZERO, BigDecimal::add);
                cashListResponse.setRechargeSum(reduce.doubleValue());
            }
        }catch (SmsException e){
            pageResponse.setCodeAndMsg(e);
            return pageResponse;
        }catch (Exception e){
            log.error("SsmCashController.selectCashList:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //充值卡购促销
    @ApiOperation(value = "分页查询  门店端-促销活动单-分页查询促销活动单（新）", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/rechargeByPromotion")
    public PageResponse rechargeByPromotion(@RequestBody DocPromotionRequest docPromotionRequest, HttpSession session) {
        PageResponse<DocPromotionResponse> pageResponse = new PageResponse<>();
        PageInfo<DocPromotionResponse> pageInfo = new PageInfo<>();


        try {
            docPromotionRequest.setSelectType(0);
            docPromotionRequest.setIsCancel(0);
            docPromotionRequest.setCollectionRechargeAmount(BigDecimal.ZERO);
            pageInfo = ssmDocPromotionService.selectPageNew(docPromotionRequest);
            for (DocPromotionResponse docPromotionResponse : pageInfo.getList()) {
                docPromotionResponse.setAmount(docPromotionResponse.getCollectionRechargeAmount());
            }
        } catch (Exception e) {
            log.error("SsmDocPromotionController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }
    //查询充值退款
    @ApiOperation(value = "分页查询  门店端-退款单-分页查询退款单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/rechargeRefund")
    public PageResponse rechargeRefund(@RequestBody RefundOrderResquest refundOrderResquest, HttpSession session) {
        PageResponse<RefundRecharge> pageResponse = new PageResponse<>();
        PageInfo<RefundRecharge> pageInfo = new PageInfo<>();

        try {
            refundOrderResquest.setState(3);
            refundOrderResquest.setRecRefundMoney(BigDecimal.ZERO);
            pageInfo = ssmRefundOrderDao.selectByRechargeRefund(refundOrderResquest);
        } catch (Exception e) {
            log.error("SsmRefundOrderController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    //查询充值赠送退款
    @ApiOperation(value = "分页查询  门店端-退款单-分页查询退款单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/rechargeGifRefund")
    public PageResponse rechargeGifRefund(@RequestBody RefundOrderResquest refundOrderResquest, HttpSession session) {
        PageResponse<RefundRecharge> pageResponse = new PageResponse<>();
        PageInfo<RefundRecharge> pageInfo = new PageInfo<>();

        try {
            refundOrderResquest.setState(3);
            refundOrderResquest.setRefundGiftMoney(BigDecimal.ZERO);
            pageInfo = ssmRefundOrderDao.selectByRechargeRefund(refundOrderResquest);
        } catch (Exception e) {
            log.error("SsmRefundOrderController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "赠卡",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/zengka")
    public PageResponse zengka(@RequestBody BaseZengkaRequest baseZengkaRequest, HttpSession session){
        PageResponse<BaseZengkaResponse> pageResponse = new PageResponse<>();
        PageInfo<BaseZengkaResponse> pageInfo = new PageInfo<>();
        Long storeId = SessionHelp.getStoreId(session);
        if(baseZengkaRequest.getStoreId()==null){
            baseZengkaRequest.setStoreId(storeId);
        }
        try {
            baseZengkaRequest.setState(3);

            pageInfo = ssmBaseZengkaService.selectPage(baseZengkaRequest);
        }catch (Exception e){
            log.error("SsmCashController.selectPage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }
}
