package com.ssm.controller;

import com.ssm.core.bean.GenericResponse;
import com.ssm.manage.entity.BaseStore;
import com.ssm.manage.entity.SmsException;
import com.ssm.manage.extendEntity.*;
import com.ssm.manage.query.CourseAccountRequest;
import com.ssm.manage.query.ProjectStatisticRequest;
import com.ssm.manage.query.RechargeAccountRequest;
import com.ssm.manage.query.StoreDailyRequest;
import com.ssm.manage.response.CourseAccountResponse;
import com.ssm.manage.response.RechargeAccountResponse;
import com.ssm.manage.response.ResponseCode;
import com.ssm.manage.response.StoreDailyResponse;
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 javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @Autowired
    private SsmCashService ssmCashService;


    @ApiOperation(value = "门店员工业绩按天分组统计  APP端-员工-门店员工业绩按天分组统计",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/achiCommList")
    public GenericResponse achiCommList(@RequestBody StaffStatistic staffStatistic){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();

        List<StaffStatistic> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStoreStatisticController.achiCommList",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){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();

        List<StaffStatistic> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectMonthStaffList(staffStatistic.getStoreId(), 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(@RequestBody StaffStatistic staffStatistic){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();
        List<StaffStatistic> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectDayStaffList(staffStatistic.getStoreId(), staffStatistic.getDate());
        }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) {
        GenericResponse<StoreDaily> genericResponse = new GenericResponse<>();
        //根据时间类别转换时间
        int type = storeDailyRequest.getType();
        String dateStr = "";
        Date date = new Date();
        if (0 == type) {
            //上月
            date = DateUtils.diffMonth(new Date(), -1);
            dateStr = DateUtils.ymFormat(date);
        } else if (1 == type) {
            //昨日
            date = DateUtils.diffDay(new Date(), -1);
            dateStr = DateUtils.ymdFormat(date);
        } else if (2 == type) {
            //今日
            dateStr = DateUtils.ymdFormat(date);
        } else {
            //当月
            dateStr = DateUtils.ymFormat(date);
        }
        storeDailyRequest.setIsCancel(0);
        storeDailyRequest.setDateStr(dateStr);
        storeDailyRequest.setDate(date);

        //判断是否总店 查询所有分店信息
        if (storeDailyRequest.getIsHead() == 1) {
            List<Long> storeIds = new ArrayList<>();
            storeIds.add(storeDailyRequest.getStoreId());
            BaseStore store = new BaseStore();
            store.setParentId(storeDailyRequest.getStoreId());
            List<BaseStore> storeList = ssmBaseStoreService.select(store);
            //将分店id放入列表
            storeList.stream().forEach(baseStore -> storeIds.add(baseStore.getId()));
            storeDailyRequest.setStoreIds(storeIds);
            //删除storeDailyRequest中的storeId
            storeDailyRequest.setStoreId(null);
        }
        List<StoreDaily> storeDailyCashList = new ArrayList<>();
        try{
            //查询收银人头数和收银服务金额
            storeDailyCashList = ssmCashService.selectDayRecord(storeDailyRequest);

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

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

    @ApiOperation(value = "美发版业绩月统计折线图", notes = "APP-门店统计报表", response = GenericResponse.class)
    @RequestMapping(path="/storeMonthStatistic", method= RequestMethod.POST)
    public GenericResponse storeMonthStatistic(@RequestBody StoreDailyRequest storeDailyRequest) {
        GenericResponse genericResponse = new GenericResponse<>();
        StoreDailyResponse storeDailyResponse = new StoreDailyResponse();

        String dateStr = "";
        dateStr = DateUtils.ymFormat(storeDailyRequest.getDate());
        storeDailyRequest.setIsCancel(0);
        storeDailyRequest.setDateStr(dateStr);
        List<StoreDaily> storeDailyList = new ArrayList<>();
        try{
            //查询收银人头数和收银服务金额
            storeDailyList = ssmCashService.selectRecordByMonth(storeDailyRequest);
        }catch (Exception e){
            log.error("SsmStaffController.storeDaily:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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