package com.ssm.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.github.pagehelper.PageInfo;
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.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.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.*;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Api(value="门店统计controller",tags={"门店统计相关接口"})
@RestController
@RequestMapping("/demay/ssm/storeExport")
@Slf4j
public class SsmStoreExportController {
    @Autowired
    private SsmStoreStatisticService ssmStoreStatisticService;

    @Autowired
    private SsmNegativeCardService ssmNegativeCardService;

    @Autowired
    private SsmCardChangeService ssmCardChangeService;

    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private ThreadPoolExecutor executor;

    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private SsmRefundOrderService ssmRefundOrderService;

    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmCashService ssmCashService;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;


//    @ApiOperation(value = "门店日报按月统计随日查询", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
//    @RequestMapping(path="/storeDailys", method= RequestMethod.POST)
//    public GenericResponse dayStatisticsNew(@RequestBody StoreDailyRequest storeDailyRequest,HttpSession session) {
//        GenericResponse genericResponse = new GenericResponse<>();
//
//        StoreDailyResponsesList storeDailyResponsesList = new StoreDailyResponsesList();
//        List<StoreDailyResponses> storeDailyList = new ArrayList<>();
//
//        StoreRequest storeRequest = new StoreRequest();
//        Long storeId = SessionHelp.getStoreId(session);
//        storeRequest.setId(storeId);
//        //.setId(Long.valueOf("310946554386628608"));
////        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.getBaseStoresByChildAndParent(storeId);//修改后
//        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
//        String beginTime = sm.format(storeDailyRequest.getStartDate());
//        String[] beginTime1 = beginTime.split("-");
//        String jieshuTime = sm.format(storeDailyRequest.getEndDate());
//        String[] endTime1 = jieshuTime.split("-");
//
//
//        for (BaseStores baseStores : pagesorganizations) {
//            StoreDailyResponses lists = new StoreDailyResponses();
//            List<StoreDailyResponse> storeDailyResponses = new ArrayList<>();
//            storeDailyRequest.setStoreId(baseStores.getId());
//
//            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;
//
//            //获取月
//            int month = start.get(Calendar.MONTH) + 1;
//            //天数
//
//            int dayNum = Integer.valueOf(String.valueOf((endTime - startTIme) / oneDay)) + 1;
//
////        storeDailyResponsesList.setExportList(this.dayNum(month,dayNum));
//            System.out.println((beginTime1[2]+"======="+ (beginTime1[2].startsWith("0") ? beginTime1[2].replace("0", "") : beginTime1[2])));
//            storeDailyResponsesList.setExportList(this.dayNum1(month,
//                    Integer.valueOf(endTime1[2]),
//                    Integer.valueOf(beginTime1[2].startsWith("0")?beginTime1[2].replace("0",""):beginTime1[2])));
//            Long time = startTIme;
//            List<String> ids = new ArrayList<>();
//            System.out.println(time+"======"+endTime);
//            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 {
//                    //查询收银人头数和收银服务金额
//                    //todo   ssmCashService.selectDayRecord      cash_list_202302
//                    //获取到收银单号、店铺id、店铺名字
//                    CompletableFuture<List<StoreDaily>> storeDailyCashListCompletableFuture = CompletableFuture.supplyAsync(() -> {
//                        try {
//                            return ssmCashService.selectDayRecord(storeDailyRequest);
//                        } catch (SmsException e) {
//                            throw new RuntimeException(e);
//                        }
//                    }, executor);
//
//
//
//
//
//                    //查询充值单数和充值金额
//                    //todo  ssmRechargeService.selectDayRecord    recharge_list_202302
//                    CompletableFuture<StoreDaily> storeDailyRecCompletableFuture = CompletableFuture.supplyAsync(() -> ssmRechargeService.selectDayRecord(storeDailyRequest), executor);
//
//
//                    //查询团购卡单数和团购金额
//                    //todo ssmRechargeService.grouponTuan         recharge_list_202302
//                    CompletableFuture<StoreDaily> grouponTuanCompletableFuture = CompletableFuture.supplyAsync(() -> ssmRechargeService.grouponTuan(storeDailyRequest), executor);
//
//                    //查询疗程单数和疗程现付金额、卡付金额
//
//
//                    //todo ssmCourseService.selectDayRecord         course_list_202302
//                    CompletableFuture<StoreDaily> storeDailyCourseCompletableFuture = CompletableFuture.supplyAsync(() -> ssmCourseService.selectDayRecord(storeDailyRequest), executor);
//
//                    //查询加价转疗程金额和id
//                    //todo ssmCourseService.selectOutcourse          course_list_202302
//                    CompletableFuture<StoreDaily> storeDailyOutcourseCompletableFuture = CompletableFuture.supplyAsync(() -> ssmCourseService.selectOutcourse(storeDailyRequest), executor);
//
//                    //查询还款金额
//                    //todo  ssmRepayService.selectDayRecord           repay_list
//                    CompletableFuture<StoreDaily> storeDailyRepayCompletableFuture = CompletableFuture.supplyAsync(() -> ssmRepayService.selectDayRecord(storeDailyRequest), executor);
//
//                    //查询还款类型金额
//                    //todo  ssmRepayService.selectRepay               repay_list
//                    CompletableFuture<StoreDaily> selectRepayCompletableFuture = CompletableFuture.supplyAsync(() -> ssmRepayService.selectRepay(storeDailyRequest), executor);
//
//
//                    //查询促销金额
//                    //todo  ssmRepayService.selectCashRechargeMoney    repay_list
//                    CompletableFuture<StoreDaily> selectCashRechargeMoneyCompletableFuture = CompletableFuture.supplyAsync(() -> ssmRepayService.selectCashRechargeMoney(storeDailyRequest), executor);
//
//                    //还款类型单数
//                    //todo  ssmRepayService.selectRepayType            repay_list
//                    CompletableFuture<StoreDaily> selectRepayTypeCompletableFuture = CompletableFuture.supplyAsync(() -> ssmRepayService.selectRepayType(storeDailyRequest), executor);
//
//                    //查询已审核退款单 退款金额
//                    storeDailyRequest.setState(3);
////                StoreDaily storeDailyRefund = ssmRefundOrderService.selectDayRecord(storeDailyRequest);//没有调用
//                    //todo  ssmRefundOrderService.selectRefund         refund_order_list
//                    CompletableFuture<List<StoreDaily>> selectRefundCompletableFuture = CompletableFuture.supplyAsync(() -> ssmRefundOrderService.selectRefund(storeDailyRequest), executor);
//                    selectRefundCompletableFuture.get();
//                        //查询门店每日负卡金
//                    //todo  ssmNegativeCardService.selectDayList       account_trade_record_202302   negative_card_day_record
//                    CompletableFuture<List<NegativeDayStatistic>> listCompletableFuture = CompletableFuture.supplyAsync(() -> ssmNegativeCardService.selectDayList(storeDailyRequest.getStoreId(), date), executor);
//                    listCompletableFuture.get();
//                    //查询当天新添加的会员数
//                    storeDailyRequest.setState(1);
//                    //todo  ssmStoreMemberService.selectDayRecord      store_member_list
//                    CompletableFuture<StoreDaily> storeDailyNewMemCompletableFuture = CompletableFuture.supplyAsync(() -> ssmStoreMemberService.selectDayRecord(storeDailyRequest), executor);
//                    storeDailyNewMemCompletableFuture.get();
//                    //查询会员总数
//                    storeDailyRequest.setDateStr(null);
//                    storeDailyRequest.setState(null);
//                    //todo  ssmStoreMemberService.selectDayRecord        store_member_list
//                    CompletableFuture<StoreDaily> storeDailyMemCompletableFuture = CompletableFuture.supplyAsync(() -> ssmStoreMemberService.selectDayRecord(storeDailyRequest), executor);
//                    storeDailyMemCompletableFuture.get();
//                    //查询启用会员数
//                    storeDailyRequest.setDateStr(null);
//                    storeDailyRequest.setState(1);
//                    //todo  ssmStoreMemberService.selectDayRecord        store_member_list
//                    CompletableFuture<StoreDaily> storeDailyStartMemCompletableFuture = CompletableFuture.supplyAsync(() -> ssmStoreMemberService.selectDayRecord(storeDailyRequest), executor);
//                    storeDailyStartMemCompletableFuture.get();
//
//                    //查询促销单数和充值金额
//                    storeDailyRequest.setDateStr(df.format(date));
//                    //todo  ssmDocPromotionService.selectDayRecord        doc_promotion
//                    CompletableFuture<StoreDaily> storeDailyPromotionCompletableFuture = CompletableFuture.supplyAsync(() -> ssmDocPromotionService.selectDayRecord(storeDailyRequest), executor);
//
//
//                    storeDailyPromotionCompletableFuture.get();
//                            CompletableFuture.allOf(
//                            storeDailyCashListCompletableFuture, storeDailyRecCompletableFuture, grouponTuanCompletableFuture,
//                            storeDailyCourseCompletableFuture, storeDailyOutcourseCompletableFuture, storeDailyRepayCompletableFuture,
//                            selectCashRechargeMoneyCompletableFuture, selectRepayTypeCompletableFuture, selectRefundCompletableFuture,
//                            storeDailyNewMemCompletableFuture, storeDailyMemCompletableFuture, storeDailyStartMemCompletableFuture,
//                            storeDailyPromotionCompletableFuture).join();
//
//
//
//
//                    List<StoreDaily> storeDailyCashList = storeDailyCashListCompletableFuture.get();
//                    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 = storeDailyRecCompletableFuture.get();
//                    if (storeDailyRec.getIds() != null) {
//                        for (String id : storeDailyRec.getIds()) {
//                            ids.add(id);
//                        }
//                    }
//
//
//                    StoreDaily grouponTuan = grouponTuanCompletableFuture.get();
//
//
//                    StoreDaily storeDailyCourse = storeDailyCourseCompletableFuture.get();
//                    if (storeDailyCourse.getIds() != null) {
//                        for (String id : storeDailyCourse.getIds()) {
//                            ids.add(id);
//                        }
//                    }
//
//                    StoreDaily storeDailyOutcourse = storeDailyOutcourseCompletableFuture.get();
//                    if (storeDailyOutcourse != null && storeDailyOutcourse.getIds() != null) {
//                        for (String id : storeDailyOutcourse.getIds()) {
//                            ids.add(id);
//                        }
//                    }
//
//                    StoreDaily storeDailyRepay = storeDailyRepayCompletableFuture.get();
//
//                    StoreDaily selectRepay = selectRepayCompletableFuture.get();
//                    if (selectRepay.getIds() != null) {
//                        for (String id : selectRepay.getIds()) {
//                            ids.add(id);
//                        }
//                    }
//
//                    StoreDaily selectCashRechargeMoney = selectCashRechargeMoneyCompletableFuture.get();
//                    if (selectCashRechargeMoney != null && selectCashRechargeMoney.getIds() != null) {
//                        for (String id : selectCashRechargeMoney.getIds()) {
//                            ids.add(id);
//                        }
//                    }
//
//                    StoreDaily selectRepayType = selectRepayTypeCompletableFuture.get();
//
//                    List<StoreDaily> selectRefund = selectRefundCompletableFuture.get();
//
//                    list = listCompletableFuture.get();
//
//                    StoreDaily storeDailyNewMem = storeDailyNewMemCompletableFuture.get();
//
//                    StoreDaily storeDailyMem = storeDailyMemCompletableFuture.get();
//
//                    StoreDaily storeDailyStartMem = storeDailyStartMemCompletableFuture.get();
//
//                    StoreDaily storeDailyPromotion = storeDailyPromotionCompletableFuture.get();
//
//                    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==null || 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 != 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.getChangeInPayMoney() == null ? new BigDecimal("0") : storeDailyOutcourse.getChangeInPayMoney())
//                    );
//                    if(storeDailyOutcourse!=null && storeDailyOutcourse.getChangeInPayMoney()!=null){
//
//                        storeDailyResponse.setCoursexMoneys(storeDailyResponse.getCoursexMoneys().add(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;
//                }
//                time += oneDay;
//            }
//            //查询现付类型
//            if (ids.size() > 0) {
//                storeDailyRequest.setIds(ids);
//                //storeDailyRequest.setStoreId(storeId);
//                List<PayList> selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);
//                lists.setPayLists(selectTypeMoneys);
//
//            }
//            lists.setSumList(storeDailyResponses);
//            BaseStore baseStore=ssmBaseStoreService.selectByPrimaryKey(baseStores.getId());
//            lists.setStoreName(baseStore.getStoreName());
//            lists.setStoreNum(baseStore.getStoreNum());
//            storeDailyList.add(lists);
//        }
//        Collections.sort(storeDailyList,new StrComparatorUtils<>("storeNum",false));
//        storeDailyResponsesList.setList(storeDailyList);
//        genericResponse.setData(storeDailyResponsesList);
//        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) {
        GenericResponse genericResponse = new GenericResponse<>();

        StoreDailyResponsesList storeDailyResponsesList = new StoreDailyResponsesList();
        List<StoreDailyResponses> storeDailyList = new ArrayList<>();

        StoreRequest storeRequest = new StoreRequest();
        Long storeId = SessionHelp.getStoreId(session);
        storeRequest
                .setId(storeId);
        //.setId(Long.valueOf("310946554386628608"));
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
        List<BaseStores> pagesorganizations = ssmBaseStoreService.getBaseStoresByChildAndParent(storeId);//修改后
        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        String beginTime = sm.format(storeDailyRequest.getStartDate());
        String[] beginTime1 = beginTime.split("-");
        String jieshuTime = sm.format(storeDailyRequest.getEndDate());
        String[] endTime1 = jieshuTime.split("-");


        for (BaseStores baseStores : pagesorganizations) {
            StoreDailyResponses lists = new StoreDailyResponses();
            List<StoreDailyResponse> storeDailyResponses = new ArrayList<>();
            storeDailyRequest.setStoreId(baseStores.getId());

            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;

        //获取月
        int month = start.get(Calendar.MONTH) + 1;
        //天数

        int dayNum = Integer.valueOf(String.valueOf((endTime - startTIme) / oneDay)) + 1;

//        storeDailyResponsesList.setExportList(this.dayNum(month,dayNum));
            System.out.println((beginTime1[2]+"======="+ (beginTime1[2].startsWith("0") ? beginTime1[2].replace("0", "") : beginTime1[2])));
        storeDailyResponsesList.setExportList(this.dayNum1(month,
                Integer.valueOf(endTime1[2]),
                Integer.valueOf(beginTime1[2].startsWith("0")?beginTime1[2].replace("0",""):beginTime1[2])));
        Long time = startTIme;
        List<String> ids = new ArrayList<>();
            System.out.println(time+"======"+endTime);
        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 {
                //查询收银人头数和收银服务金额
                //todo   ssmCashService.selectDayRecord      cash_list_202302
                //获取到收银单号、店铺id、店铺名字
                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);
                    }
                //查询充值单数和充值金额
                //todo  ssmRechargeService.selectDayRecord    recharge_list_202302
                StoreDaily storeDailyRec = ssmRechargeService.selectDayRecord(storeDailyRequest);
                //查询团购卡单数和团购金额
                //todo ssmRechargeService.grouponTuan         recharge_list_202302
                StoreDaily grouponTuan = ssmRechargeService.grouponTuan(storeDailyRequest);
                if (storeDailyRec.getIds() != null) {
                    for (String id : storeDailyRec.getIds()) {
                        ids.add(id);
                    }
                }
                //查询疗程单数和疗程现付金额、卡付金额
                //todo ssmCourseService.selectDayRecord         course_list_202302
                StoreDaily storeDailyCourse = ssmCourseService.selectDayRecord(storeDailyRequest);
                if (storeDailyCourse.getIds() != null) {
                    for (String id : storeDailyCourse.getIds()) {
                        ids.add(id);
                    }
                }
                //查询加价转疗程金额和id
                //todo ssmCourseService.selectOutcourse          course_list_202302
                StoreDaily storeDailyOutcourse = ssmCourseService.selectOutcourse(storeDailyRequest);
                if (storeDailyOutcourse != null && storeDailyOutcourse.getIds() != null) {
                    for (String id : storeDailyOutcourse.getIds()) {
                        ids.add(id);
                    }
                }
                //查询还款金额
                //todo  ssmRepayService.selectDayRecord           repay_list
                StoreDaily storeDailyRepay = ssmRepayService.selectDayRecord(storeDailyRequest);

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

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


                //查询已审核退款单 退款金额
                storeDailyRequest.setState(3);
//                StoreDaily storeDailyRefund = ssmRefundOrderService.selectDayRecord(storeDailyRequest);//没有调用
                //todo  ssmRefundOrderService.selectRefund         refund_order_list
                List<StoreDaily> selectRefund = ssmRefundOrderService.selectRefund(storeDailyRequest);
                //查询门店每日负卡金
                //todo  ssmNegativeCardService.selectDayList       account_trade_record_202302   negative_card_day_record
                list = ssmNegativeCardService.selectDayList(storeDailyRequest.getStoreId(), date);
                //查询当天新添加的会员数
                storeDailyRequest.setState(1);
                //todo  ssmStoreMemberService.selectDayRecord      store_member_list
//                StoreDaily storeDailyNewMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
                //查询会员总数
                storeDailyRequest.setDateStr(null);
                storeDailyRequest.setState(null);
                //todo  ssmStoreMemberService.selectDayRecord        store_member_list
//                StoreDaily storeDailyMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);
                //查询启用会员数
                storeDailyRequest.setDateStr(null);
                storeDailyRequest.setState(1);
                //todo  ssmStoreMemberService.selectDayRecord        store_member_list
//                StoreDaily storeDailyStartMem = ssmStoreMemberService.selectDayRecord(storeDailyRequest);

                //查询促销单数和充值金额
                storeDailyRequest.setDateStr(df.format(date));
                //todo  ssmDocPromotionService.selectDayRecord        doc_promotion
                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 != 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.getChangeInPayMoney() == null ? new BigDecimal("0") : storeDailyOutcourse.getChangeInPayMoney())
                );
                if(storeDailyOutcourse!=null && storeDailyOutcourse.getChangeInPayMoney()!=null){

                    storeDailyResponse.setCoursexMoneys(storeDailyResponse.getCoursexMoneys().add(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;
            }
            time += oneDay;
        }
//        查询现付类型
        if (ids.size() > 0) {
            storeDailyRequest.setIds(ids);
            //storeDailyRequest.setStoreId(storeId);
            List<PayList> selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);
            lists.setPayLists(selectTypeMoneys);

        }
        lists.setSumList(storeDailyResponses);
            BaseStore baseStore=ssmBaseStoreService.selectByPrimaryKey(baseStores.getId());
            lists.setStoreName(baseStore.getStoreName());
            lists.setStoreNum(baseStore.getStoreNum());
            storeDailyList.add(lists);
    }
        Collections.sort(storeDailyList,new StrComparatorUtils<>("storeNum",false));
        storeDailyResponsesList.setList(storeDailyList);
        genericResponse.setData(storeDailyResponsesList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }
    @Autowired
    private SsmPayService ssmPayService;

    @Autowired
    private SsmSysRoleStoreService ssmSysRoleStoreService;

    @ApiOperation(value = "门店日报按月统计随日查询", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
    @RequestMapping(path="/storeDailysNew", method= RequestMethod.POST)
    public GenericResponse dayStatisticsNew(@RequestBody StoreDailyRequest storeDailyRequest,HttpSession session) {
        GenericResponse genericResponse = new GenericResponse<>();
        StoreDailyResponsesList storeDailyResponsesList = new StoreDailyResponsesList();
        List<StoreDailyResponses> storeDailyList = new ArrayList<>();
        StoreRequest storeRequest = new StoreRequest();
        Long storeId = SessionHelp.getStoreId(session);
        storeRequest
                .setId(storeId);
        //.setId(Long.valueOf("310946554386628608"));
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.pagesorganizations(storeRequest);
//        List<BaseStores> pagesorganizations = ssmBaseStoreService.getBaseStoresByChildAndParent(storeId);//修改后


        /**
         * 角色
         */
        List<BaseStores> collect1 = ssmBaseStoreService.getBaseStoresByChildAndParent(storeId);//修改后
        SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
        sysStoreRole.setRoleId(storeDailyRequest.getRoleId());
        PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
        List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
        List<BaseStores> pagesorganizations = collect1.stream().filter( item ->item.getState()==1 &&  collect.contains(item.getId())).collect(Collectors.toList());


        SimpleDateFormat sm = new SimpleDateFormat("yyyy-MM-dd");
        String beginTime = sm.format(storeDailyRequest.getStartDate());
        String[] beginTime1 = beginTime.split("-");
        String jieshuTime = sm.format(storeDailyRequest.getEndDate());
        String[] endTime1 = jieshuTime.split("-");


        for (BaseStores baseStores : pagesorganizations) {
            StoreDailyResponses lists = new StoreDailyResponses();
            List<StoreDailyResponse> storeDailyResponses = new ArrayList<>();
            storeDailyRequest.setStoreId(baseStores.getId());

            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;

            //获取月
            int month = start.get(Calendar.MONTH) + 1;
            //天数

            int dayNum = Integer.valueOf(String.valueOf((endTime - startTIme) / oneDay)) + 1;

//        storeDailyResponsesList.setExportList(this.dayNum(month,dayNum));
            System.out.println((beginTime1[2]+"======="+ (beginTime1[2].startsWith("0") ? beginTime1[2].replace("0", "") : beginTime1[2])));
            storeDailyResponsesList.setExportList(this.dayNum1(month,
                    Integer.valueOf(endTime1[2]),
                    Integer.valueOf(beginTime1[2].startsWith("0")?beginTime1[2].replace("0",""):beginTime1[2])));
            Long time = startTIme;
            List<String> ids = new ArrayList<>();
            System.out.println(time+"======"+endTime);
            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);
                //一次查询一个店铺的收入总额
                BigDecimal sum = ssmPayService.selectStoreDailys(storeDailyRequest);
                storeDailyResponse.setSum(sum);
                storeDailyResponse.setDate(df.format(date));
                storeDailyResponses.add(storeDailyResponse);
                time += oneDay;
            }
//        查询现付类型
            if (ids.size() > 0) {
                storeDailyRequest.setIds(ids);
                //storeDailyRequest.setStoreId(storeId);
                List<PayList> selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);
                lists.setPayLists(selectTypeMoneys);

            }
            lists.setSumList(storeDailyResponses);
            BaseStore baseStore=ssmBaseStoreService.selectByPrimaryKey(baseStores.getId());
            lists.setStoreName(baseStore.getStoreName());
            lists.setStoreNum(baseStore.getStoreNum());
            storeDailyList.add(lists);
        }
        Collections.sort(storeDailyList,new StrComparatorUtils<>("storeNum",false));
        storeDailyResponsesList.setList(storeDailyList);
        genericResponse.setData(storeDailyResponsesList);
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    public List<String> dayNum(int month,int dayNum){
        List<String> Numday=new ArrayList<>();
        for(int i=dayNum;i>0;i--){
            String monthDay=month+"-"+i;
            Numday.add(monthDay);
        }
        return Numday;
    }
    public List<String> dayNum1(int month,int endTime,int beginTime){
        List<String> Numday=new ArrayList<>();
        for(int i=beginTime;i<=endTime;i++){
            String monthDay=month+"-"+i;
            Numday.add(monthDay);
        }
        return Numday;
    }


    @Autowired
    private SsmBaseZengkaService ssmBaseZengkaService;

    @ApiOperation(value = "记录导出  门店端-门店日报", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/export")
    public void export(@RequestBody StoreDailyRequest storeDailyRequest,HttpSession session) {
        StoreDailyResponsesList storeDailyResponsesList=this.exportDate(storeDailyRequest,session);
        Collections.sort(storeDailyResponsesList.getList(),new StrComparatorUtils<>("storeNum",false));
        // 模板注意 用{} 来表示你要用的变量 如果本来就有"{","}" 特殊字符 用"\{","\}"代替
        // {} 代表普通变量 {.} 代表是list的变量 {前缀.} 前缀可以区分不同的list
        ExcelWriter excelWriter = null;
        OutputStream out = null;
        try {
            String fileName = "shoukuanhuizong";
            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/storeDailys.xlsx");
            excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
            WriteSheet writeSheet = EasyExcel.writerSheet().build();
            FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
            for(int i=0;i<storeDailyResponsesList.getList().size();i++){
                int num=i+1;
                excelWriter.fill(new FillWrapper("sumList"+num, storeDailyResponsesList.getList().get(i).getSumList()),fillConfig, writeSheet);
            }
            excelWriter.fill(new FillWrapper("list", storeDailyResponsesList.getList()), writeSheet);
            excelWriter.fill(storeDailyResponsesList, writeSheet);
            out.flush();
        } catch (Exception e) {
            log.error("SsmProjectTypeController.export:",e);
        } finally {
            //destFile.delete();
            // 别忘记关闭流
            excelWriter.finish();
        }
    }


    public StoreDailyResponsesList exportDate( StoreDailyRequest storeDailyRequest,HttpSession session) {

        StoreDailyResponsesList storeDailyResponsesList = new StoreDailyResponsesList();
        List<StoreDailyResponses> storeDailyList = new ArrayList<>();

        StoreRequest storeRequest = new StoreRequest();
        storeRequest.setId(SessionHelp.getStoreId(session));
        List<BaseStores> collect1 = ssmBaseStoreService.pagesorganizations(storeRequest);
        SysRoleStoreRequest sysStoreRole = new SysRoleStoreRequest();
        sysStoreRole.setRoleId(storeDailyRequest.getRoleId());
        PageInfo<SysRoleStore> sysRoleStorePageInfo = ssmSysRoleStoreService.selectPage(sysStoreRole);
        List<Long> collect = sysRoleStorePageInfo.getList().stream().map(item -> item.getStoreId()).collect(Collectors.toList());
        List<BaseStores> pagesorganizations = collect1.stream().filter(item -> item.getState()==1 && collect.contains(item.getId())).collect(Collectors.toList());

        for (BaseStores baseStores : pagesorganizations) {
            StoreDailyResponses lists = new StoreDailyResponses();
            List<StoreDailyResponse> storeDailyResponses = new ArrayList<>();
            storeDailyRequest.setStoreId(baseStores.getId());

            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;
            //获取年
            int year = start.get(Calendar.YEAR) ;
            storeDailyResponsesList.setYear(String.valueOf(year));
            //获取月
            int month = start.get(Calendar.MONTH) + 1;
            storeDailyResponsesList.setMonth(String.valueOf(month));
            //天数
            int dayNum = Integer.valueOf(String.valueOf((endTime - startTIme) / oneDay)) + 1;
            storeDailyResponsesList.setExportList(this.dayNum(month,dayNum));
            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 != 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);
                }
                time += oneDay;
            }
            //查询现付类型
            if (ids.size() > 0) {
                storeDailyRequest.setIds(ids);
                //storeDailyRequest.setStoreId(storeId);
                List<PayList> selectTypeMoneys = ssmRepayService.selectTypeMoneys(storeDailyRequest);
                lists.setPayLists(selectTypeMoneys);

            }
            lists.setSumList(storeDailyResponses);
            BaseStore baseStore=ssmBaseStoreService.selectByPrimaryKey(baseStores.getId());
            lists.setStoreName(baseStore.getStoreName());
            lists.setStoreNum(baseStore.getStoreNum());
            storeDailyList.add(lists);
        }

        storeDailyResponsesList.setList(storeDailyList);
        return storeDailyResponsesList;
    }

}
