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.ssm.manage.response.GenericResponse;
import com.ssm.manage.utils.SessionHelp;
import com.ssm.core.util.StrComparatorUtils;
import com.ssm.manage.entity.*;
import com.ssm.manage.excelEntity.ExcelStoreByDay;
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.BigDecimalUtils;
import com.ssm.manage.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
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={"门店统计相关接口"})
@RestController
@RequestMapping("/demay/ssm/storeStatistic")
@Slf4j
public class SsmStoreStatisticController {
    @Autowired
    private SsmStoreStatisticService ssmStoreStatisticService;

    @Autowired
    private SsmBaseGrouponCardService ssmBaseGrouponCardService;

    @Autowired
    private SsmRepayTradeService ssmRepayTradeService;

    @Autowired
    private SsmCollectionTypeService ssmCollectionTypeService;

    @Autowired
    private SsmNegativeCardService ssmNegativeCardService;

    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private SsmRefundOrderService ssmRefundOrderService;

    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmCashService ssmCashService;

    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;

    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;


    @ApiOperation(value = "门店会员统计列表  门店端-门店统计-门店会员统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/memberList")
    public GenericResponse memberList(@RequestParam String dateStr,@RequestParam(required = false) Long storeId, HttpSession session){
        GenericResponse<MemberStatistic> genericResponse = new GenericResponse();
        if(storeId == null || "".equals(storeId)){
            storeId = SessionHelp.getStoreId(session);
        }
        List<MemberStatistic> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectMemberList(storeId, dateStr);
        }catch (Exception e){
            log.error("SsmStoreStatisticController.selectMemberList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }



    @ApiOperation(value = "门店会员统计列表  门店端-门店统计-会员排行榜充值统计",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/memberRecharge")
    public GenericResponse memberRecharge(@RequestParam(required = false) Long storeId, HttpSession session){
        GenericResponse<RechargeAccountResponse> genericResponse = new GenericResponse();
        RechargeAccountRequest rechargeAccountRequest = new RechargeAccountRequest();
        if(null == storeId || "".equals(storeId)){
            storeId = SessionHelp.getStoreId(session);
        }

        rechargeAccountRequest.setStoreId(storeId);
        rechargeAccountRequest.setState(1);
        List<RechargeAccountResponse> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.memberRecharge(rechargeAccountRequest);
        }catch (Exception e){
            log.error("SsmStoreStatisticController.memberRecharge",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }
    @ApiOperation(value = "门店会员统计列表  门店端-门店统计-会员排行榜疗程统计",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/memberCourse")
    public GenericResponse memberCourse(@RequestParam(required = false) Long storeId ,HttpSession session){
        GenericResponse<CourseAccountResponse> genericResponse = new GenericResponse();
       // Long storeId = SessionHelp.getStoreId(session);
        CourseAccountRequest courseAccountRequest = new CourseAccountRequest();
        if(null == storeId ||"".equals(storeId)){
            storeId = SessionHelp.getStoreId(session);
        }
        courseAccountRequest.setStoreId(storeId);
        courseAccountRequest.setState(1);
        List<CourseAccountResponse> list = new ArrayList<>();
        try {
            list = ssmCourseAccountService.memberCourse(courseAccountRequest);
        }catch (Exception e){
            log.error("SsmStoreStatisticController.memberCourse",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店项目统计列表  门店端-门店统计-当日门店项目统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/projectList")
    public GenericResponse projectList(@RequestBody ProjectStatisticRequest projectStatisticRequest, HttpSession session){
        GenericResponse<ProjectStatistic> genericResponse = new GenericResponse();
        Long storeId = null;
        if(null == projectStatisticRequest.getStoreId()|| "".equals(projectStatisticRequest.getStoreId())){
            storeId = SessionHelp.getStoreId(session);
        }else{
            storeId = projectStatisticRequest.getStoreId();
        }
        List<ProjectStatistic> list = new ArrayList<>();
        String dateStr = DateUtils.ymdFormat(new Date());
        try {
            list = ssmStoreStatisticService.selectProjectList(storeId, dateStr,projectStatisticRequest.getType());
        }catch (Exception e){
            log.error("SsmStoreStatisticController.projectList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店项目统计  门店端-门店统计-按月份统计和门店购买项目次数和金额",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/projectLists")
    public GenericResponse projectLists(@RequestBody ProjectListsRequest projectListsRequest, HttpSession session){
        GenericResponse<ProjectLists> genericResponse = new GenericResponse();
        if(projectListsRequest.getStoreId().size()<=0){
            List<Long> a=new ArrayList<>();
            a.add(SessionHelp.getStoreId(session));
            projectListsRequest.setStoreId(a);
        }
        if(projectListsRequest.getDate().size()<=0){
            List<String> a=new ArrayList<>();
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
            String time = simpleDateFormat.format(date);
            a.add(time);
            projectListsRequest.setDate(a);
        }
        List<ProjectLists> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectProjectLists(projectListsRequest);
        }catch (Exception e){
            log.error("SsmStoreStatisticController.projectList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店项目统计  门店端-门店统计-按门店统计项目结存金额和次数",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/balance")
    public GenericResponse balance(@RequestBody ProjectListsRequest projectListsRequest, HttpSession session){
        GenericResponse<ProjectLists> genericResponse = new GenericResponse();
        if(projectListsRequest.getStoreId().size()<=0){
            List<Long> a=new ArrayList<>();
            a.add(SessionHelp.getStoreId(session));
            projectListsRequest.setStoreId(a);
        }
        List<ProjectLists> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectProjectLists2(projectListsRequest);
        }catch (Exception e){
            log.error("SsmStoreStatisticController.projectList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店项目统计  门店端-门店统计-按月份和门店统计项目服务金额和次数",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/service")
    public GenericResponse service(@RequestBody ProjectListsRequest projectListsRequest, HttpSession session){
        GenericResponse<ProjectLists> genericResponse = new GenericResponse();
        if(projectListsRequest.getStoreId().size()<=0){
            List<Long> a=new ArrayList<>();
            a.add(SessionHelp.getStoreId(session));
            projectListsRequest.setStoreId(a);
        }
        if(projectListsRequest.getDate().size()<=0){
            List<String> a=new ArrayList<>();
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMM");
            String time = simpleDateFormat.format(date);
            a.add(time);
            projectListsRequest.setDate(a);
        }
        List<ProjectLists> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectProjectLists3(projectListsRequest);
        }catch (Exception e){
            log.error("SsmStoreStatisticController.projectList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }



    @ApiOperation(value = "门店项目统计列表  门店端-门店统计-月度门店项目统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/projectMonthList")
    public GenericResponse projectMonthList(@RequestBody ProjectStatisticRequest projectStatisticRequest, HttpSession session){
        GenericResponse<ProjectStatistic> genericResponse = new GenericResponse();
        Long storeId = null;
        if(null == projectStatisticRequest.getStoreId() || "".equals(projectStatisticRequest.getStoreId())){
            storeId = SessionHelp.getStoreId(session);
        }else{
            storeId =projectStatisticRequest.getStoreId();
        }

        List<ProjectStatistic> list = new ArrayList<>();

        try {
            list = ssmStoreStatisticService.selectProjectList(storeId, projectStatisticRequest.getDate() ,projectStatisticRequest.getType(),projectStatisticRequest.getEndDate());
        }catch (Exception e){
            log.error("SsmStoreStatisticController.projectMonthList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工统计列表  门店端-门店统计-门店员工统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/staffMonthList")
    public GenericResponse staffMonthList(@RequestBody StaffStatistic staffStatistic, HttpSession session){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();
        Long storeId = null;
        if(null == staffStatistic.getStoreId() || "".equals(staffStatistic.getStoreId())){
            storeId =  SessionHelp.getStoreId(session);
        }else{
            storeId = staffStatistic.getStoreId();
        }

        List<StaffStatistic> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectMonthStaffList(storeId, staffStatistic.getDate());
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStoreStatisticController.staffMonthList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工业绩提成统计列表  门店端-首页-门店员工业绩提成统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/staffDayList")
    public GenericResponse staffDayList( HttpSession session){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        List<StaffStatistic> list = new ArrayList<>();
        try {

            /*Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DAY_OF_MONTH,-1);*/

            list = ssmStoreStatisticService.selectDayStaffList(storeId, new Date());
        }catch (Exception e){
            log.error("SsmStoreStatisticController.staffDayList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店日报", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
    @RequestMapping(path="/storeDaily", method= RequestMethod.POST)
    public GenericResponse dayStatistic(@RequestBody StoreDailyRequest storeDailyRequest) {
        List<StoreDailyResponse> storeDailyResponses = new ArrayList<>();
        GenericResponse<StoreDailyResponse> genericResponse = new GenericResponse<>();
        StoreDailyResponse storeDailyResponse = new StoreDailyResponse();
        List<NegativeDayStatistic> list = new ArrayList<>();
        Date date = new Date();
        storeDailyRequest.setIsCancel(0);
        storeDailyRequest.setDateStr(DateUtils.ymdFormat(date));
        storeDailyRequest.setDate(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        try{
            //查询收银人头数和收银服务金额
            List<StoreDaily> storeDailyCashList = ssmCashService.selectDayRecord(storeDailyRequest);
            StoreDaily storeDailyCash = new StoreDaily();
            if (null != storeDailyCashList && !storeDailyCashList.isEmpty()){
                storeDailyCash = storeDailyCashList.get(0);
            }
            //查询充值单数和充值金额
            StoreDaily storeDailyRec = ssmRechargeService.selectDayRecord(storeDailyRequest);
            //查询疗程单数和疗程金额
            StoreDaily storeDailyCourse = ssmCourseService.selectDayRecord(storeDailyRequest);
            //查询还款金额
            StoreDaily storeDailyRepay = ssmRepayService.selectDayRecord(storeDailyRequest);
            //查询已审核退款单 退款金额
                storeDailyRequest.setState(3);
            StoreDaily storeDailyRefund = ssmRefundOrderService.selectDayRecord(storeDailyRequest);

            //查询门店每日负卡金
            list = ssmNegativeCardService.selectDayList(storeDailyRequest.getStoreId(), date);
            //查询当天新添加的会员数
            storeDailyRequest.setState(1);
            StoreDaily storeDailyNewMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
            //查询会员总数
            storeDailyRequest.setDateStr(null);
            storeDailyRequest.setState(null);
            StoreDaily storeDailyMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
            //查询启用会员数
            storeDailyRequest.setDateStr(null);
            storeDailyRequest.setState(1);
            StoreDaily storeDailyStartMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);

            //查询促销单数和充值金额
            storeDailyRequest.setDateStr(format.format(new Date()));
            StoreDaily storeDailyPromotion = ssmDocPromotionService.selectDayRecord(storeDailyRequest);

            storeDailyResponse.setCashMoney(null == storeDailyCash.getCashMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashMoney());
            storeDailyResponse.setCashPeopleNum(storeDailyCash.getCashPeopleNum());
            storeDailyResponse.setCourseMoney(null == storeDailyCourse.getCourseMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCourse.getCourseMoney());
            storeDailyResponse.setCourseOrderNum(storeDailyCourse.getCourseOrderNum());
            storeDailyResponse.setRechargeMoney(null == storeDailyRec.getRechargeMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyRec.getRechargeMoney());
            storeDailyResponse.setRechargeOrderNum(storeDailyRec.getRechargeOrderNum());
            storeDailyResponse.setRepayMoney(null == storeDailyRepay? BigDecimalUtils.bigDecimal("0"):storeDailyRepay.getRepayMoney());
            storeDailyResponse.setRefundMoney(null == storeDailyRefund?BigDecimalUtils.bigDecimal("0"):storeDailyRefund.getRefundMoney());
            storeDailyResponse.setRecNegativeMoney(list.get(0).getEndMoney());
            storeDailyResponse.setNewMemberNum(storeDailyNewMem.getNewMemberNum());
            storeDailyResponse.setMemberNum(storeDailyMem.getMemberNum());
            storeDailyResponse.setStartMemberNum(storeDailyStartMem.getStartMemberNum());
            if(null ==storeDailyCash.getWoman()){
                storeDailyResponse.setWoman(0);
            }else{
                storeDailyResponse.setWoman(storeDailyCash.getWoman());
            }

            if(null == storeDailyCash.getMan()){
                storeDailyResponse.setMan(0);
            }else{
                storeDailyResponse.setMan(storeDailyCash.getMan());
            }

            if(null != storeDailyPromotion){
                storeDailyResponse.setCourseMoney(null == storeDailyPromotion.getCourseMoney() ? storeDailyResponse.getCourseMoney():storeDailyResponse.getCourseMoney().add(storeDailyPromotion.getCourseMoney()));
                storeDailyResponse.setCourseOrderNum(storeDailyCourse.getCourseOrderNum()+storeDailyPromotion.getCourseOrderNum());
            }



        }catch (Exception e){
            log.error("SsmStaffController.storeDaily:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

        genericResponse.setData(storeDailyResponse);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
    @ApiOperation(value = "门店日报按月统计随日查询", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
    @RequestMapping(path="/storeDailys", method= RequestMethod.POST)
    public GenericResponse dayStatistics(@RequestBody StoreDailyRequest storeDailyRequest,HttpSession session) {
        Long storeId = storeDailyRequest.getStoreId();
        if(storeId==null){
            storeId = SessionHelp.getStoreId(session);
        }
        BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(storeId);

        StoreDailyResponses lists=new StoreDailyResponses();
        List<StoreDailyResponse> storeDailyResponses = new ArrayList<>();

        GenericResponse<StoreDailyResponses> genericResponse = new GenericResponse<>();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");

        Calendar start = Calendar.getInstance();
        start.setTime(storeDailyRequest.getStartDate());
//        start.set(2019, 8, 11);
        Long startTIme = start.getTimeInMillis();

        Calendar end = Calendar.getInstance();
        end.setTime(storeDailyRequest.getEndDate());
//        end.set(2019, 10, 10);
        Long endTime = end.getTimeInMillis();

        Long oneDay = 1000 * 60 * 60 * 24l;



        Long time = startTIme;
        List<String> ids=new ArrayList<>();
        while (time <= endTime) {
            List<String> ids1=new ArrayList<>();
            ArrayList<String> objects = new ArrayList<>();
            Date date = new Date(time);
            StoreDailyResponse storeDailyResponse = new StoreDailyResponse();
            List<NegativeDayStatistic> list = new ArrayList<>();
            storeDailyRequest.setIsCancel(0);
            storeDailyRequest.setDateStr(DateUtils.ymdFormat(date));
            storeDailyRequest.setDate(date);
            try{
                //查询收银人头数和收银服务金额
                List<StoreDaily> storeDailyCashList = ssmCashService.selectDayRecordManAndWoman(storeDailyRequest);
                if(storeDailyCashList.size()>0)
                    for(String id:storeDailyCashList.get(0).getIds()){
                        ids.add(id);
                        ids1.add(id);
                        objects.add(id);
                    }
                StoreDaily storeDailyCash = new StoreDaily();
                if(storeDailyCashList.size()>0)
                    if (null != storeDailyCashList && !storeDailyCashList.isEmpty()){
                        storeDailyCash = storeDailyCashList.get(0);
                    }
                //查询充值单数和充值金额
                StoreDaily storeDailyRec = ssmRechargeService.selectDayRecord(storeDailyRequest);
                //查询团购卡单数和团购金额
                StoreDaily grouponTuan = ssmRechargeService.grouponTuan(storeDailyRequest);
                BaseGrouponCard baseGrouponCard = new BaseGrouponCard();
                baseGrouponCard.setStoreId(storeId);
                baseGrouponCard.setStartTime(storeDailyRequest.getStartDate());
                baseGrouponCard.setEndTime(storeDailyRequest.getEndDate());
                List<BaseGrouponCard> baseGrouponCards = ssmBaseGrouponCardService.selectFKJByDate(baseGrouponCard);
                if(baseGrouponCards!=null&&baseGrouponCards.size()>0){
                    for (BaseGrouponCard grouponCard : baseGrouponCards) {
                        ids.add(grouponCard.getDocNum());
                        ids1.add(grouponCard.getDocNum());
                        objects.add(grouponCard.getDocNum());
                    }
                }
                if(storeDailyRec.getIds()!=null){
                    for(String id:storeDailyRec.getIds()){
                        ids.add(id);
                        ids1.add(id);
                        objects.add(id);
                    }
                }

                //查询疗程单数和疗程现付金额、卡付金额
                StoreDaily storeDailyCourse = ssmCourseService.selectDocId(storeDailyRequest);
                if(storeDailyCourse.getCoursekMoney()==null){storeDailyCourse.setCoursekMoney(BigDecimal.valueOf(0));}
                if(storeDailyCourse.getCardAcievementAmount()==null){storeDailyCourse.setCardAcievementAmount(BigDecimal.valueOf(0));}
                if(storeDailyCourse.getCoursexMoney()==null){storeDailyCourse.setCoursexMoney(BigDecimal.valueOf(0));}
                if(storeDailyCourse.getAchievementAmount()==null){storeDailyCourse.setAchievementAmount(BigDecimal.valueOf(0));}
                storeDailyResponse.setCardCooperateAmount(storeDailyCourse.getCoursekMoney().subtract(storeDailyCourse.getCardAcievementAmount()));
                storeDailyResponse.setCooperateMoneys(storeDailyCourse.getCooperateAmount()==null?BigDecimal.ZERO:storeDailyCourse.getCooperateAmount());
                storeDailyResponse.setCardCooperateAmount(storeDailyCourse.getCoursekMoney().subtract(storeDailyCourse.getCardAcievementAmount()));
                if(storeDailyCourse.getIds()!=null){
                    for(String id:storeDailyCourse.getIds()){
                        ids.add(id);
                        ids1.add(id);
                        objects.add(id);
                    }
                }
                //查询加价转疗程金额和id
                StoreDaily storeDailyOutcourse = ssmCourseService.selectOutcourse(storeDailyRequest);
                if(storeDailyOutcourse!=null&&storeDailyOutcourse.getIds()!=null){
                    for(String id:storeDailyOutcourse.getIds()){
                        ids.add(id);
                        ids1.add(id);
                        objects.add(id);
                    }
                }
                //查询还款金额
                StoreDaily storeDailyRepay = ssmRepayService.selectDayRecord(storeDailyRequest);
                //查询合作方还款业绩


                //查询还款类型金额
                StoreDaily selectRepay = ssmRepayService.selectRepay(storeDailyRequest);
                if(selectRepay.getIds()!=null){
                    for(String id:selectRepay.getIds()){
                        ids.add(id);
                        ids1.add(id);
                        objects.add(id);
                    }
                }

                //查询促销金额
                StoreDaily selectCashRechargeMoney = ssmRepayService.selectCashRechargeMoney(storeDailyRequest);
                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getIds()!=null){
                    for(String id:selectCashRechargeMoney.getIds()){
                        ids.add(id);
                        ids1.add(id);
                        objects.add(id);
                    }
                }
                //还款类型单数
                StoreDaily selectRepayType=ssmRepayService.selectRepayType(storeDailyRequest);

                StoreDaily repayCooperate = ssmRepayTradeService.selectrepayCooperate(storeDailyRequest);
                if(repayCooperate!=null){
//                    if(repayCooperate.getCooperateAmount()==null){
//                        repayCooperate.setCooperateAmount(BigDecimal.valueOf(0));
//                    }
                    if(repayCooperate.getRepayMoney()==null){
                        repayCooperate.setRepayMoney(BigDecimal.valueOf(0));
                    }
                    if(repayCooperate.getHkCooperateAmount()==null){
                        repayCooperate.setHkCooperateAmount(BigDecimal.valueOf(0));
                    }
                    storeDailyResponse.setCooperateMoneys(repayCooperate.getRepayMoney().subtract(repayCooperate.getHkCooperateAmount()).add( storeDailyResponse.getCooperateMoneys()));
                }
                //查询已审核退款单 退款金额
                storeDailyRequest.setState(3);
                StoreDaily storeDailyRefund = ssmRefundOrderService.selectDayRecord(storeDailyRequest);
                List<StoreDaily> selectRefund=ssmRefundOrderService.selectRefund(storeDailyRequest);
                //查询门店每日负卡金
                list = ssmNegativeCardService.selectDayList(storeDailyRequest.getStoreId(), date);
                //查询当天新添加的会员数
                storeDailyRequest.setState(1);
                StoreDaily storeDailyNewMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
                //查询会员总数
                storeDailyRequest.setDateStr(null);
                storeDailyRequest.setState(null);
                StoreDaily storeDailyMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
                //查询启用会员数
                storeDailyRequest.setDateStr(null);
                storeDailyRequest.setState(1);
                StoreDaily storeDailyStartMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);

                //查询促销单数和充值金额
                storeDailyRequest.setDateStr(df.format(date));
                StoreDaily storeDailyPromotion = ssmDocPromotionService.selectDayRecord(storeDailyRequest);
                storeDailyResponse.setCashMoney(null == storeDailyCash.getCashMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashMoney());
                storeDailyResponse.setCashManNum(storeDailyCash.getCashManNum()==null?0:storeDailyCash.getCashManNum());
                storeDailyResponse.setCashWomanNum(storeDailyCash.getCashWomanNum()==null?0:storeDailyCash.getCashWomanNum());
                storeDailyResponse.setCashPeopleNum(storeDailyResponse.getCashManNum()+storeDailyResponse.getCashWomanNum());
                storeDailyResponse.setCashierCashMoney(null == storeDailyCash.getCashierCashMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashierCashMoney());
                storeDailyResponse.setCashlMoney(null == storeDailyCash.getCashlMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashlMoney());
                storeDailyResponse.setCashierCardMoney((null == storeDailyCash.getCashierCardMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashierCardMoney())
                        .add(null == storeDailyCash.getCashlMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashlMoney())
                );

                //疗程
                storeDailyResponse.setCourseMoney(null == storeDailyCourse.getCourseMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCourse.getCourseMoney());

                storeDailyResponse.setCourseOrderNum(storeDailyCourse.getCourseOrderNum()+selectCashRechargeMoney.getCourseOrderNum());
                storeDailyResponse.setCoursexMoney(null == storeDailyCourse.getCoursexMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCourse.getCoursexMoney()
                                .subtract(storeDailyResponse.getCooperateMoneys())
//                        .add(selectCashRechargeMoney.getCashRechargeMoneys()==null?new BigDecimal("0"):selectCashRechargeMoney.getCashRechargeMoneys())
                );
                storeDailyResponse.setCoursekMoney(null == storeDailyCourse.getCoursekMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCourse.getCoursekMoney());

                //促销卡付金额CollectionRechargeAmount
                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getCollectionRechargeAmount()!=null){
                    storeDailyResponse.setCoursekMoney(storeDailyResponse.getCoursekMoney().add(selectCashRechargeMoney.getCollectionRechargeAmount()));
                    //storeDailyResponse.setCollectionCashAmount(selectCashRechargeMoney.getCollectionCashAmount());
                }
                //促销现付
                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getCollectionCashAmount()!=null){
                    storeDailyResponse.setCoursexMoney(storeDailyResponse.getCoursexMoney().add(selectCashRechargeMoney.getCollectionCashAmount()));
                    //storeDailyResponse.setCollectionRechargeAmount(selectCashRechargeMoney.getCollectionRechargeAmount());
                }
                //促销合计
                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getCashRechargeMoneys()!=null){
                    storeDailyResponse.setCourseMoney(storeDailyResponse.getCourseMoney().add(selectCashRechargeMoney.getCashRechargeMoneys()));
                    //storeDailyResponse.setCashRechargeMoneys(selectCashRechargeMoney.getCashRechargeMoneys());
                }


                storeDailyResponse.setRechargeMoney(null == storeDailyRec.getRechargeMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyRec.getRechargeMoney());
                storeDailyResponse.setRechargeOrderNum(storeDailyRec.getRechargeOrderNum()==null?0:storeDailyRec.getRechargeOrderNum());
                storeDailyResponse.setRepayMoney(null == storeDailyRepay? BigDecimalUtils.bigDecimal("0"):storeDailyRepay.getRepayMoney());

                storeDailyResponse.setRefundMoney(BigDecimalUtils.bigDecimal("0"));
                storeDailyResponse.setCashRefundMoney(BigDecimalUtils.bigDecimal("0"));
                for(StoreDaily storeDaily:selectRefund){
                    if(storeDaily.getRefundType()==2){
                        storeDailyResponse.setCashRefundMoney(storeDailyResponse.getCashRefundMoney().add(storeDaily.getRefundMoney()));
                    }else{
                        storeDailyResponse.setRefundMoney(storeDailyResponse.getRefundMoney().add(storeDaily.getRefundMoney()));
                    }

                }
                storeDailyResponse.setRecNegativeMoney(list.get(0).getEndMoney()==null?BigDecimalUtils.bigDecimal("0"):list.get(0).getEndMoney());
                storeDailyResponse.setNewMemberNum(storeDailyNewMem.getNewMemberNum()==null?0:storeDailyNewMem.getNewMemberNum());
                storeDailyResponse.setMemberNum(storeDailyMem.getMemberNum()==null?0:storeDailyMem.getMemberNum());
                storeDailyResponse.setStartMemberNum(storeDailyStartMem.getStartMemberNum()==null?0:storeDailyStartMem.getStartMemberNum());

                //<还款类型>

                storeDailyResponse.setRecRepay(selectRepay.getRecRepay());
                storeDailyResponse.setRecRepayMoney(selectRepay.getRecRepayMoney()==null?BigDecimalUtils.bigDecimal("0"):selectRepay.getRecRepayMoney());


                storeDailyResponse.setCourseRepay(selectRepay.getCourseRepay());
                storeDailyResponse.setCourseRepayMoney(selectRepay.getCourseRepayMoney()==null?BigDecimalUtils.bigDecimal("0"):selectRepay.getCourseRepayMoney());


                storeDailyResponse.setPromotionCourseRepay(selectRepay.getPromotionCourseRepay());
                storeDailyResponse.setPromotionCourseRepayMoney(selectRepay.getPromotionCourseRepayMoney()==null?BigDecimalUtils.bigDecimal("0"):selectRepay.getPromotionCourseRepayMoney());

                //</还款类型>

                //加价转疗程金额
                if(storeDailyOutcourse!=null&&storeDailyOutcourse.getChangeInPayMoney()!=null){
                    storeDailyResponse.setChangeInPayMoney(storeDailyOutcourse.getChangeInPayMoney());
                }
                else{
                    storeDailyResponse.setChangeInPayMoney(new BigDecimal(0));
                }
                storeDailyResponse.setWoman(storeDailyCash.getWoman()==null?0:storeDailyCash.getWoman());
                storeDailyResponse.setMan(null == storeDailyCash.getMan()?0:storeDailyCash.getMan());
                /*if(null ==storeDailyCash.getWoman()){
                    storeDailyResponse.setWoman(0);
                }else{
                    storeDailyResponse.setWoman(storeDailyCash.getWoman());
                }*/

                /*if(null == storeDailyCash.getMan()){
                    storeDailyResponse.setMan(0);
                }else{
                    storeDailyResponse.setMan(storeDailyCash.getMan());
                }*/


                if(null != storeDailyPromotion){
                    storeDailyResponse.setCourseMoney(null == storeDailyPromotion.getCourseMoney() ? storeDailyResponse.getCourseMoney():storeDailyResponse.getCourseMoney().add(storeDailyPromotion.getCourseMoney()));
                    storeDailyResponse.setCourseOrderNum(storeDailyCourse.getCourseOrderNum()+storeDailyPromotion.getCourseOrderNum());
                }

                //充值现付总计
                storeDailyResponse.setRechargeMoneys(
                        (storeDailyRec.getRechargeMoney()==null?new BigDecimal("0"):storeDailyRec.getRechargeMoney())
                                .add(selectRepay.getRecRepayMoney()==null?new BigDecimal("0"):selectRepay.getRecRepayMoney())
                                .add(grouponTuan==null||grouponTuan.getGrouponMoney()==null?new BigDecimal("0"):grouponTuan.getGrouponMoney())
                );

                //疗程现付总计
                storeDailyResponse.setCoursexMoneys(
                        (storeDailyCourse.getCoursexMoney()==null?new BigDecimal("0"):storeDailyCourse.getCoursexMoney())
                                .add(selectRepay.getCourseRepayMoney()==null?new BigDecimal("0"):selectRepay.getCourseRepayMoney())
                                .add(selectRepay.getPromotionCourseRepayMoney()==null?new BigDecimal("0"):selectRepay.getPromotionCourseRepayMoney())
                                .add(selectCashRechargeMoney.getCollectionCashAmount()==null?new BigDecimal("0"):selectCashRechargeMoney.getCollectionCashAmount())
                                .add(storeDailyOutcourse==null||storeDailyOutcourse.getChangeInPayMoney()==null?new BigDecimal("0"):storeDailyOutcourse.getChangeInPayMoney()
//                                        .add(selectCashRechargeMoney.getCashRechargeMoneys()==null?new BigDecimal("0"):selectCashRechargeMoney.getCashRechargeMoneys())
                                )
                );
                storeDailyResponse.setCoursexMoney(storeDailyResponse.getCoursexMoney().add(selectRepay.getCourseRepayMoney()==null?new BigDecimal("0"):selectRepay.getCourseRepayMoney()));



                //服务现付总计
                storeDailyResponse.setCashierCashMoneys(storeDailyCash.getCashierCashMoney()==null?new BigDecimal("0"):storeDailyCash.getCashierCashMoney());

                //还款单数分类统计
                storeDailyResponse.setCourseOrderNum(storeDailyResponse.getCourseOrderNum()+(selectRepayType.getCourseRepayNum()==null?0:selectRepayType.getCourseRepayNum()));
                storeDailyResponse.setRechargeOrderNum(storeDailyResponse.getRechargeOrderNum()+(selectRepayType.getRecRepayNum()==null?0:selectRepayType.getRecRepayNum()));

                //汇总
                storeDailyResponse.setSum(new BigDecimal(0));
                storeDailyResponse.setSum(storeDailyResponse.getSum()
                        .add(storeDailyResponse.getCoursexMoneys())
                        .add(storeDailyResponse.getRechargeMoneys())
                        .add(storeDailyResponse.getCashierCashMoneys()));
                if(objects.size()>0){
                    storeDailyRequest.setIds(objects);
                    storeDailyRequest.setStoreId(storeId);
                    List<PayList> selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);

            /*List<String> typeName=new ArrayList<>();
            List<Long> typeMoneys=new ArrayList<>();
            if(selectTypeMoneys.size()>0&&selectTypeMoneys!=null)
                for(PayList storeDaily:selectTypeMoneys){
                    typeName.add(storeDaily.getPayName());
                    typeMoneys.add(storeDaily.getPayMoney().longValue());
                }
            storeDailyResponses.get(0).setName(typeName);
            storeDailyResponses.get(0).setMoneys(typeMoneys);*/
                    //storeDailyResponses.get(0).setPayLists(selectTypeMoneys);
                    //实际支付的总金额
//                    BigDecimal reduce = selectTypeMoneys.stream().map(PayList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
                    lists.setPayLists(selectTypeMoneys);
                    //充值金额

//                    storeDailyResponse.setCooperateMoneys(reduce.subtract(storeDailyResponse.getRechargeMoney())
//                            .subtract(storeDailyResponse.getCoursexMoney())
//                            .subtract(storeDailyResponse.getCashMoney()));
                }
                storeDailyResponse.setDate(df.format(date));
//                storeDailyResponse.setCoursexMoney(storeDailyCourse.getAchievementAmount());
//                storeDailyResponse.setCoursexMoneys(storeDailyCourse.getAchievementAmount().add(selectCashRechargeMoney.getCashRechargeMoneys()==null?new BigDecimal("0"):selectCashRechargeMoney.getCashRechargeMoneys())
//                        );//.add(selectCashRechargeMoney.getCashRechargeMoneys())
//                .add(storeDailyOutcourse.getChangeInPayMoney()==null?new BigDecimal("0"):storeDailyOutcourse.getChangeInPayMoney())
                if(storeDailyOutcourse !=null && storeDailyOutcourse.getChangeInPayMoney()==null && storeDailyOutcourse.getChangeInPayMoney().compareTo(new BigDecimal("0"))>0){
                    storeDailyResponse.setCoursexMoneys(storeDailyResponse.getCoursexMoneys().add(storeDailyOutcourse.getChangeInPayMoney()));
                }
                //每天的门店日报
                //查询现付类型
                if(ids1.size()>0){
                    storeDailyRequest.setIds(ids1);
                    storeDailyRequest.setStoreId(storeId);
                    List<PayList> selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);
                    List<BaseCollectionType> select = ssmCollectionTypeService.select(null);
                    for (PayList selectTypeMoney : selectTypeMoneys) {
                        for (BaseCollectionType type : select) {
                            if(selectTypeMoney.getPayId().equals(type.getId())){
                                selectTypeMoney.setPayName(type.getCollectionTypeName());
                            }
                        }
                    }
                    storeDailyResponse.setPayLists(selectTypeMoneys);
                }

                storeDailyResponse.setCoursexMoney(storeDailyResponse.getCoursexMoney().add(storeDailyResponse.getChangeInPayMoney()));
//                storeDailyResponse.setCoursexMoneys(storeDailyResponse.getCoursexMoneys().add(storeDailyResponse.getChangeInPayMoney().add(storeDailyResponse.getCourseRepayMoney())));

                storeDailyResponse.setStoreNum(baseStore.getStoreNum());
                storeDailyResponse.setStoreName(baseStore.getStoreName());


                storeDailyResponses.add(storeDailyResponse);

            }catch (Exception e){
                log.error("SsmStaffController.storeDaily:", e);
                genericResponse.setCodeAndMsg(ResponseCode.FAILED);
                return genericResponse;
            }
//            System.out.println(df.format(date));
            time += oneDay;
        }
        //查询现付类型
        List<PayList> selectTypeMoneys = new ArrayList<>();
        if(ids.size()>0){
            storeDailyRequest.setIds(ids);
            storeDailyRequest.setStoreId(storeId);
            selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);
//            List<BaseCollectionType> select = ssmCollectionTypeService.select(null);
//            for (PayList selectTypeMoney : selectTypeMoneys) {
//                for (BaseCollectionType type : select) {
//                    if(selectTypeMoney.getPayId().equals(type.getId())){
//                        selectTypeMoney.setPayName(type.getCollectionTypeName());
//                    }
//                }
//            }
            /*List<String> typeName=new ArrayList<>();
            List<Long> typeMoneys=new ArrayList<>();
            if(selectTypeMoneys.size()>0&&selectTypeMoneys!=null)
                for(PayList storeDaily:selectTypeMoneys){
                    typeName.add(storeDaily.getPayName());
                    typeMoneys.add(storeDaily.getPayMoney().longValue());
                }
            storeDailyResponses.get(0).setName(typeName);
            storeDailyResponses.get(0).setMoneys(typeMoneys);*/
            //storeDailyResponses.get(0).setPayLists(selectTypeMoneys);
            //实际支付的总金额
//            BigDecimal reduce = selectTypeMoneys.stream().map(PayList::getPayMoney).reduce(BigDecimal.ZERO, BigDecimal::add);
            lists.setPayLists(selectTypeMoneys);
//            //充值金额
//            StoreDailyResponse storeDailyResponse = storeDailyResponses.get(0);
//            storeDailyResponses.get(0).setCooperateMoneys(reduce.subtract(storeDailyResponse.getRechargeMoney())
//                    .subtract(storeDailyResponse.getCoursexMoney())
//                    .subtract(storeDailyResponse.getCashMoney()));
        }
        StoreDailyResponse response = new StoreDailyResponse();
        response.setCashPeopleNum(0);
        response.setRefundMoney(BigDecimal.ZERO);
        response.setCashManNum(0);
        response.setCashWomanNum(0);
        response.setCashMoney(BigDecimal.valueOf(0));
        response.setCourseMoney(BigDecimal.valueOf(0));
        response.setCourseOrderNum(0);
        response.setRechargeMoney(BigDecimal.valueOf(0));
        response.setRechargeOrderNum(0);
        response.setRepayMoney(BigDecimal.valueOf(0));
        response.setCashRefundMoney(BigDecimal.valueOf(0));
        response.setNewMemberNum(0);
        response.setMemberNum(0);
        response.setRecNegativeMoney(BigDecimal.valueOf(0));
        response.setStartMemberNum(0);
        response.setWoman(0);
        response.setMan(0);
        response.setDate("汇总");
        response.setCashierCashMoney(BigDecimal.valueOf(0));
        response.setCashierCardMoney(BigDecimal.valueOf(0));
        response.setCashlMoney(BigDecimal.valueOf(0));
        response.setCoursexMoney(BigDecimal.valueOf(0));
        response.setCoursekMoney(BigDecimal.valueOf(0));
        response.setRecRepay("");
        response.setRecRepayMoney(BigDecimal.valueOf(0));
        response.setCourseRepay("");
        response.setCourseRepayMoney(BigDecimal.valueOf(0));
        response.setPromotionCourseRepay("");
        response.setPromotionCourseRepayMoney(BigDecimal.valueOf(0));
        response.setChangeInPayMoney(BigDecimal.valueOf(0));
        response.setRechargeMoneys(BigDecimal.valueOf(0));
        response.setCoursexMoneys(BigDecimal.valueOf(0));
        response.setCashierCashMoneys(BigDecimal.valueOf(0));
        response.setCooperateMoneys(BigDecimal.valueOf(0));
        response.setCardCooperateAmount(BigDecimal.valueOf(0));
        response.setGrouponCounts(0);
        response.setGrouponMoney(0);
        response.setSum(BigDecimal.valueOf(0));
        response.setPayLists(selectTypeMoneys);
        response.setStoreNum(baseStore.getStoreNum());
        response.setStoreName(baseStore.getStoreName());
        for (StoreDailyResponse storeDailyRespons : storeDailyResponses) {
            response.setCashPeopleNum(response.getCashPeopleNum()+storeDailyRespons.getCashPeopleNum());
            response.setCashManNum(response.getCashManNum()+storeDailyRespons.getCashManNum());
            response.setCashWomanNum(response.getCashWomanNum()+storeDailyRespons.getCashWomanNum());
            response.setCashMoney(response.getCashMoney().add(storeDailyRespons.getCashMoney()));
            response.setCourseMoney(response.getCourseMoney().add(storeDailyRespons.getCourseMoney()));
            response.setCourseOrderNum(response.getCourseOrderNum()+storeDailyRespons.getCourseOrderNum());
            response.setRechargeMoney(response.getRechargeMoney().add(storeDailyRespons.getRechargeMoney()));
            response.setRechargeOrderNum(response.getRechargeOrderNum()+storeDailyRespons.getRechargeOrderNum());
            response.setRepayMoney(response.getRepayMoney().add(storeDailyRespons.getRepayMoney()));
            response.setCashRefundMoney(response.getCashRefundMoney().add(storeDailyRespons.getCashRefundMoney()));
            response.setNewMemberNum(response.getNewMemberNum()+storeDailyRespons.getNewMemberNum());
            response.setMemberNum(response.getMemberNum()+storeDailyRespons.getMemberNum());
            response.setRecNegativeMoney(response.getRecNegativeMoney().add(storeDailyRespons.getRecNegativeMoney()));
            response.setStartMemberNum(response.getStartMemberNum()+storeDailyRespons.getStartMemberNum());
            response.setWoman(response.getWoman()+storeDailyRespons.getWoman());
            response.setMan(response.getMan()+storeDailyRespons.getMan());
//            response.setDate("汇总");
            response.setCashierCashMoney(response.getCashierCashMoney().add(storeDailyRespons.getCashierCashMoney()));
            response.setCashierCardMoney(response.getCashierCardMoney().add(storeDailyRespons.getCashierCardMoney()));
            response.setCashlMoney(response.getCashlMoney().add(storeDailyRespons.getCashlMoney()));
            response.setCoursexMoney(response.getCoursexMoney().add(storeDailyRespons.getCoursexMoney()));
            response.setCoursekMoney(response.getCoursekMoney().add(storeDailyRespons.getCoursekMoney()));
//            response.setRecRepay("");
            response.setRecRepayMoney(response.getRecRepayMoney().add(storeDailyRespons.getRecRepayMoney()));
//            response.setCourseRepay("");
            response.setCourseRepayMoney(response.getCourseRepayMoney().add(storeDailyRespons.getCourseRepayMoney()));
//            response.setPromotionCourseRepay("");
            response.setPromotionCourseRepayMoney(response.getPromotionCourseRepayMoney().add(storeDailyRespons.getPromotionCourseRepayMoney()));
            response.setChangeInPayMoney(response.getChangeInPayMoney().add(storeDailyRespons.getChangeInPayMoney()));
            response.setRechargeMoneys(response.getRechargeMoneys().add(storeDailyRespons.getRechargeMoneys()));
            response.setCoursexMoneys(response.getCoursexMoneys().add(storeDailyRespons.getCoursexMoneys()));
            response.setCashierCashMoneys(response.getCashierCashMoneys().add(storeDailyRespons.getCashierCashMoneys()));
            response.setCooperateMoneys(response.getCooperateMoneys().add(storeDailyRespons.getCooperateMoneys()));
            response.setCardCooperateAmount(response.getCardCooperateAmount().add(storeDailyRespons.getCardCooperateAmount()));
            response.setGrouponCounts(response.getGrouponCounts()+storeDailyRespons.getGrouponCounts());
            response.setGrouponMoney(response.getGrouponMoney()+storeDailyRespons.getGrouponMoney());
            response.setSum(response.getSum().add(storeDailyRespons.getSum()));
            response.setCoursexMoney(response.getCoursexMoney().add(storeDailyRespons.getPromotionCourseRepayMoney()));
            response.setRefundMoney(response.getRefundMoney().add(storeDailyRespons.getRefundMoney()));
        }
        storeDailyResponses.add(response);
        lists.setLists(storeDailyResponses);



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

    @ApiOperation(value = "门店日报按月统计随日查询", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
    @RequestMapping(path="/storeByDayDailys", method= RequestMethod.POST)
    public GenericResponse storeByDayDailys(@RequestBody StoreDailyRequest storeDailyRequest,HttpSession session){
        GenericResponse<List<StoreByDayResponse>> genericResponse = new GenericResponse<>();
        try {
            List<StoreByDayResponse> list = ssmStoreStatisticService.storeByDayDailys(storeDailyRequest);
            Collections.sort(list,new StrComparatorUtils<>("storeNum",false));
            genericResponse.setData(list);
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }catch (Exception e){
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "门店日报按月统计随日查询", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
    @RequestMapping(path="/storeByDayDailys/export", method= RequestMethod.POST)
    public void storeByDayDailysExport(@RequestBody StoreDailyRequest storeDailyRequest,HttpSession session){
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        try {
            String fileName = "project";
            HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            response.setHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("utf-8"), "ISO8859-1") + ".xlsx");
            out = response.getOutputStream();
            //ClassPathResource classPathResource = new ClassPathResource("static/live_room_data3.xls");
            //InputStream inputStream = classPathResource.getInputStream();
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/storeByDay.xlsx");
            List<StoreByDayResponse> list = ssmStoreStatisticService.storeByDayDailys(storeDailyRequest);
            Collections.sort(list,new StrComparatorUtils<>("storeNum",false));
            List<ExcelStoreByDay> storeByDays = new ArrayList<>();
            for (StoreByDayResponse storeByDayResponse : list) {
                ExcelStoreByDay excelStoreByDay = new ExcelStoreByDay();
                BeanUtils.copyProperties(storeByDayResponse,excelStoreByDay);
                excelStoreByDay.setStartTime(storeDailyRequest.getStartTime());
                excelStoreByDay.setEndTime(storeDailyRequest.getEndTime());
                storeByDays.add(excelStoreByDay);
                List<PayListVo> payLists = storeByDayResponse.getPayLists();
                for (PayListVo payList : payLists) {
                    if(payList.getPayName().contains("现金")){
                        excelStoreByDay.setXianJin(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("厂家收款")){
                        excelStoreByDay.setChangJiaShouKuan(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("民E付")){
                        excelStoreByDay.setMinEFu(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("汇卡支付")){
                        excelStoreByDay.setHuiKaZhiFu(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("美团支付")){
                        excelStoreByDay.setMeiTuanZhiFu(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("YP")){
                        excelStoreByDay.setXianJinYpl(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("抖音自营")){
                        excelStoreByDay.setDouYinZiYing(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("抖音刀刀购")){
                        excelStoreByDay.setDouYinDaoDaoGou(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("九江支付")){
                        excelStoreByDay.setJiuJiangZhiFu(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("钱宝")){
                        excelStoreByDay.setQianBao(payList.getPayMoneyAmount());
                    }
                    if(payList.getPayName().contains("LKL")){
                        excelStoreByDay.setLkl(payList.getPayMoneyAmount());
                    }
                }
            }

            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(storeByDays, writeSheet);
            out.flush();
        }catch (Exception e){
        }finally {
            excelWriter.finish();
        }
    }

//    @ApiOperation(value = "门店日报按月统计随日查询", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
//    @RequestMapping(path="/storeDailys", method= RequestMethod.POST)
//    public GenericResponse dayStatistics(@RequestBody StoreDailyRequest storeDailyRequest,HttpSession session) {
//        StoreDailyResponses lists=new StoreDailyResponses();
//        Long storeId = SessionHelp.getStoreId(session);
//        List<StoreDailyResponse> storeDailyResponses = new ArrayList<>();
//
//        GenericResponse<StoreDailyResponses> genericResponse = new GenericResponse<>();
//        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
//
//        Calendar start = Calendar.getInstance();
//        start.setTime(storeDailyRequest.getStartDate());
////        start.set(2019, 8, 11);
//        Long startTIme = start.getTimeInMillis();
//
//        Calendar end = Calendar.getInstance();
//        end.setTime(storeDailyRequest.getEndDate());
////        end.set(2019, 10, 10);
//        Long endTime = end.getTimeInMillis();
//
//        Long oneDay = 1000 * 60 * 60 * 24l;
//
//
//
//        Long time = startTIme;
//        List<String> ids=new ArrayList<>();
//        while (time <= endTime) {
//            Date date = new Date(time);
//            StoreDailyResponse storeDailyResponse = new StoreDailyResponse();
//            List<NegativeDayStatistic> list = new ArrayList<>();
//            storeDailyRequest.setIsCancel(0);
//            storeDailyRequest.setDateStr(DateUtils.ymdFormat(date));
//            storeDailyRequest.setDate(date);
//            try{
//                //查询收银人头数和收银服务金额
//                List<StoreDaily> storeDailyCashList = ssmCashService.selectDayRecord(storeDailyRequest);
//                if(storeDailyCashList.size()>0)
//                for(String id:storeDailyCashList.get(0).getIds()){
//                    ids.add(id);
//                }
//                StoreDaily storeDailyCash = new StoreDaily();
//                if(storeDailyCashList.size()>0)
//                if (null != storeDailyCashList && !storeDailyCashList.isEmpty()){
//                    storeDailyCash = storeDailyCashList.get(0);
//                }
//                //查询充值单数和充值金额
//                StoreDaily storeDailyRec = ssmRechargeService.selectDayRecord(storeDailyRequest);
//                //查询团购卡单数和团购金额
//                StoreDaily grouponTuan = ssmRechargeService.grouponTuan(storeDailyRequest);
//                if(storeDailyRec.getIds()!=null){
//                    for(String id:storeDailyRec.getIds()){
//                        ids.add(id);
//                    }
//                }
//                //查询疗程单数和疗程现付金额、卡付金额
//                StoreDaily storeDailyCourse = ssmCourseService.selectDayRecord(storeDailyRequest);
//                if(storeDailyCourse.getIds()!=null){
//                    for(String id:storeDailyCourse.getIds()){
//                        ids.add(id);
//                    }
//                }
//                //查询加价转疗程金额和id
//                StoreDaily storeDailyOutcourse = ssmCourseService.selectOutcourse(storeDailyRequest);
//                if(storeDailyOutcourse!=null&&storeDailyOutcourse.getIds()!=null){
//                    for(String id:storeDailyOutcourse.getIds()){
//                        ids.add(id);
//                    }
//                }
//                //查询还款金额
//                StoreDaily storeDailyRepay = ssmRepayService.selectDayRecord(storeDailyRequest);
//
//                //查询还款类型金额
//                StoreDaily selectRepay = ssmRepayService.selectRepay(storeDailyRequest);
//                if(selectRepay.getIds()!=null){
//                    for(String id:selectRepay.getIds()){
//                        ids.add(id);
//                    }
//                }
//
//
//
//                //查询促销金额
//                StoreDaily selectCashRechargeMoney = ssmRepayService.selectCashRechargeMoney(storeDailyRequest);
//                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getIds()!=null){
//                    for(String id:selectCashRechargeMoney.getIds()){
//                        ids.add(id);
//                    }
//                }
//                //还款类型单数
//                StoreDaily selectRepayType=ssmRepayService.selectRepayType(storeDailyRequest);
//
//
//
//                //查询已审核退款单 退款金额
//                storeDailyRequest.setState(3);
//                StoreDaily storeDailyRefund = ssmRefundOrderService.selectDayRecord(storeDailyRequest);
//                List<StoreDaily> selectRefund=ssmRefundOrderService.selectRefund(storeDailyRequest);
//                //查询门店每日负卡金
//                list = ssmNegativeCardService.selectDayList(storeDailyRequest.getStoreId(), date);
//                //查询当天新添加的会员数
//                storeDailyRequest.setState(1);
//                StoreDaily storeDailyNewMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
//                //查询会员总数
//                storeDailyRequest.setDateStr(null);
//                storeDailyRequest.setState(null);
//                StoreDaily storeDailyMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
//                //查询启用会员数
//                storeDailyRequest.setDateStr(null);
//                storeDailyRequest.setState(1);
//                StoreDaily storeDailyStartMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
//
//                //查询促销单数和充值金额
//                storeDailyRequest.setDateStr(df.format(date));
//                StoreDaily storeDailyPromotion = ssmDocPromotionService.selectDayRecord(storeDailyRequest);
//                storeDailyResponse.setCashMoney(null == storeDailyCash.getCashMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashMoney());
//                storeDailyResponse.setCashPeopleNum(storeDailyCash.getCashPeopleNum()==null?0:storeDailyCash.getCashPeopleNum());
//
//                storeDailyResponse.setCashierCashMoney(null == storeDailyCash.getCashierCashMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashierCashMoney());
//                storeDailyResponse.setCashlMoney(null == storeDailyCash.getCashlMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashlMoney());
//                storeDailyResponse.setCashierCardMoney((null == storeDailyCash.getCashierCardMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashierCardMoney())
//                                .add(null == storeDailyCash.getCashlMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCash.getCashlMoney())
//                );
//
//                //疗程
//                storeDailyResponse.setCourseMoney(null == storeDailyCourse.getCourseMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCourse.getCourseMoney());
//
//                storeDailyResponse.setCourseOrderNum(storeDailyCourse.getCourseOrderNum()+selectCashRechargeMoney.getCourseOrderNum());
//                storeDailyResponse.setCoursexMoney(null == storeDailyCourse.getCoursexMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCourse.getCoursexMoney());
//                storeDailyResponse.setCoursekMoney(null == storeDailyCourse.getCoursekMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyCourse.getCoursekMoney());
//
//                //促销卡付金额CollectionRechargeAmount
//                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getCollectionRechargeAmount()!=null){
//                    storeDailyResponse.setCoursekMoney(storeDailyResponse.getCoursekMoney().add(selectCashRechargeMoney.getCollectionRechargeAmount()));
//                    //storeDailyResponse.setCollectionCashAmount(selectCashRechargeMoney.getCollectionCashAmount());
//                }
//                //促销现付
//                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getCollectionCashAmount()!=null){
//                    storeDailyResponse.setCoursexMoney(storeDailyResponse.getCoursexMoney().add(selectCashRechargeMoney.getCollectionCashAmount()));
//                    //storeDailyResponse.setCollectionRechargeAmount(selectCashRechargeMoney.getCollectionRechargeAmount());
//                }
//                //促销合计
//                if(selectCashRechargeMoney!=null&&selectCashRechargeMoney.getCashRechargeMoneys()!=null){
//                    storeDailyResponse.setCourseMoney(storeDailyResponse.getCourseMoney().add(selectCashRechargeMoney.getCashRechargeMoneys()));
//                    //storeDailyResponse.setCashRechargeMoneys(selectCashRechargeMoney.getCashRechargeMoneys());
//                }
//
//
//                storeDailyResponse.setRechargeMoney(null == storeDailyRec.getRechargeMoney() ? BigDecimalUtils.bigDecimal("0"):storeDailyRec.getRechargeMoney());
//                storeDailyResponse.setRechargeOrderNum(storeDailyRec.getRechargeOrderNum()==null?0:storeDailyRec.getRechargeOrderNum());
//                storeDailyResponse.setRepayMoney(null == storeDailyRepay? BigDecimalUtils.bigDecimal("0"):storeDailyRepay.getRepayMoney());
//
//                storeDailyResponse.setRefundMoney(BigDecimalUtils.bigDecimal("0"));
//                storeDailyResponse.setCashRefundMoney(BigDecimalUtils.bigDecimal("0"));
//                for(StoreDaily storeDaily:selectRefund){
//                    if(storeDaily.getRefundType()==2){
//                        storeDailyResponse.setCashRefundMoney(storeDailyResponse.getCashRefundMoney().add(storeDaily.getRefundMoney()));
//                    }else{
//                        storeDailyResponse.setRefundMoney(storeDailyResponse.getRefundMoney().add(storeDaily.getRefundMoney()));
//                    }
//
//                }
//                storeDailyResponse.setRecNegativeMoney(list.get(0).getEndMoney()==null?BigDecimalUtils.bigDecimal("0"):list.get(0).getEndMoney());
//                storeDailyResponse.setNewMemberNum(storeDailyNewMem.getNewMemberNum()==null?0:storeDailyNewMem.getNewMemberNum());
//                storeDailyResponse.setMemberNum(storeDailyMem.getMemberNum()==null?0:storeDailyMem.getMemberNum());
//                storeDailyResponse.setStartMemberNum(storeDailyStartMem.getStartMemberNum()==null?0:storeDailyStartMem.getStartMemberNum());
//
//                //<还款类型>
//
//                    storeDailyResponse.setRecRepay(selectRepay.getRecRepay());
//                    storeDailyResponse.setRecRepayMoney(selectRepay.getRecRepayMoney()==null?BigDecimalUtils.bigDecimal("0"):selectRepay.getRecRepayMoney());
//
//
//                    storeDailyResponse.setCourseRepay(selectRepay.getCourseRepay());
//                    storeDailyResponse.setCourseRepayMoney(selectRepay.getCourseRepayMoney()==null?BigDecimalUtils.bigDecimal("0"):selectRepay.getCourseRepayMoney());
//
//
//                    storeDailyResponse.setPromotionCourseRepay(selectRepay.getPromotionCourseRepay());
//                    storeDailyResponse.setPromotionCourseRepayMoney(selectRepay.getPromotionCourseRepayMoney()==null?BigDecimalUtils.bigDecimal("0"):selectRepay.getPromotionCourseRepayMoney());
//
//                //</还款类型>
//
//                //加价转疗程金额
//                if(storeDailyOutcourse!=null&&storeDailyOutcourse.getChangeInPayMoney()!=null){
//                    storeDailyResponse.setChangeInPayMoney(storeDailyOutcourse.getChangeInPayMoney());
//                }
//                else{
//                    storeDailyResponse.setChangeInPayMoney(new BigDecimal(0));
//                }
//                storeDailyResponse.setWoman(storeDailyCash.getWoman()==null?0:storeDailyCash.getWoman());
//                storeDailyResponse.setMan(null == storeDailyCash.getMan()?0:storeDailyCash.getMan());
//                /*if(null ==storeDailyCash.getWoman()){
//                    storeDailyResponse.setWoman(0);
//                }else{
//                    storeDailyResponse.setWoman(storeDailyCash.getWoman());
//                }*/
//
//                /*if(null == storeDailyCash.getMan()){
//                    storeDailyResponse.setMan(0);
//                }else{
//                    storeDailyResponse.setMan(storeDailyCash.getMan());
//                }*/
//
//
//                if(null != storeDailyPromotion){
//                    storeDailyResponse.setCourseMoney(null == storeDailyPromotion.getCourseMoney() ? storeDailyResponse.getCourseMoney():storeDailyResponse.getCourseMoney().add(storeDailyPromotion.getCourseMoney()));
//                    storeDailyResponse.setCourseOrderNum(storeDailyCourse.getCourseOrderNum()+storeDailyPromotion.getCourseOrderNum());
//                }
//
//                //充值现付总计
//                storeDailyResponse.setRechargeMoneys(
//                        (storeDailyRec.getRechargeMoney()==null?new BigDecimal("0"):storeDailyRec.getRechargeMoney())
//                        .add(selectRepay.getRecRepayMoney()==null?new BigDecimal("0"):selectRepay.getRecRepayMoney())
//                        .add(grouponTuan==null||grouponTuan.getGrouponMoney()==null?new BigDecimal("0"):grouponTuan.getGrouponMoney())
//                        );
//
//                //疗程现付总计
//                storeDailyResponse.setCoursexMoneys(
//                        (storeDailyCourse.getCoursexMoney()==null?new BigDecimal("0"):storeDailyCourse.getCoursexMoney())
//                        .add(selectRepay.getCourseRepayMoney()==null?new BigDecimal("0"):selectRepay.getCourseRepayMoney())
//                        .add(selectRepay.getPromotionCourseRepayMoney()==null?new BigDecimal("0"):selectRepay.getPromotionCourseRepayMoney())
//                        .add(selectCashRechargeMoney.getCollectionCashAmount()==null?new BigDecimal("0"):selectCashRechargeMoney.getCollectionCashAmount())
//                        .add(storeDailyOutcourse==null||storeDailyOutcourse.getChangeInPayMoney()==null?new BigDecimal("0"):storeDailyOutcourse.getChangeInPayMoney())
//                );
//
//
//
//                //服务现付总计
//                storeDailyResponse.setCashierCashMoneys(storeDailyCash.getCashierCashMoney()==null?new BigDecimal("0"):storeDailyCash.getCashierCashMoney());
//
//                //还款单数分类统计
//                storeDailyResponse.setCourseOrderNum(storeDailyResponse.getCourseOrderNum()+(selectRepayType.getCourseRepayNum()==null?0:selectRepayType.getCourseRepayNum()));
//                storeDailyResponse.setRechargeOrderNum(storeDailyResponse.getRechargeOrderNum()+(selectRepayType.getRecRepayNum()==null?0:selectRepayType.getRecRepayNum()));
//
//                //汇总
//                storeDailyResponse.setSum(new BigDecimal(0));
//                storeDailyResponse.setSum(storeDailyResponse.getSum().add(storeDailyResponse.getCoursexMoneys()).add(storeDailyResponse.getRechargeMoneys()).add(storeDailyResponse.getCashierCashMoneys()));
//
//                storeDailyResponse.setDate(df.format(date));
//                storeDailyResponses.add(storeDailyResponse);
//
//            }catch (Exception e){
//                log.error("SsmStaffController.storeDaily:", e);
//                genericResponse.setCodeAndMsg(ResponseCode.FAILED);
//                return genericResponse;
//            }
////            System.out.println(df.format(date));
//            time += oneDay;
//        }
//        //查询现付类型
//        if(ids.size()>0){
//            storeDailyRequest.setIds(ids);
//            storeDailyRequest.setStoreId(storeId);
//            List<PayList> selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);
//
//            /*List<String> typeName=new ArrayList<>();
//            List<Long> typeMoneys=new ArrayList<>();
//            if(selectTypeMoneys.size()>0&&selectTypeMoneys!=null)
//                for(PayList storeDaily:selectTypeMoneys){
//                    typeName.add(storeDaily.getPayName());
//                    typeMoneys.add(storeDaily.getPayMoney().longValue());
//                }
//            storeDailyResponses.get(0).setName(typeName);
//            storeDailyResponses.get(0).setMoneys(typeMoneys);*/
//            //storeDailyResponses.get(0).setPayLists(selectTypeMoneys);
//
//            lists.setPayLists(selectTypeMoneys);
//        }
//        lists.setLists(storeDailyResponses);
//
//
//
//        genericResponse.setData(lists);
//        genericResponse.setCodeAndMsg(ResponseCode.OK);
//        return genericResponse;
//    }

    @ApiOperation(value = "门店员工业绩提成统计列表  门店端-首页-门店收款来源统计",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/collectingSource")
    public GenericResponse collectingSource( HttpSession session){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();
        Long storeId = SessionHelp.getStoreId(session);
        List<StaffStatistic> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.collectingSource(storeId, new Date());
        }catch (Exception e){
            log.error("SsmStoreStatisticController.staffDayList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }
//    @RequestMapping("/courseScore/export.action")
//    @ResponseBody
//    public void export(HttpServletResponse response) {
//        List<ExcelCourseMoney> data = Http.data();
//        ExportExcel<ExcelCourseMoney> ee = new ExportExcel<ExcelCourseMoney>();
//        String[] headers = {"编号", "学年", "学号", "姓名", "班级", "成绩"};
//        String fileName =  "课程成绩表";
//        ee.exportExcel(headers, data, fileName, response);
//    }

}
