package com.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.ssm.core.bean.GenericResponse;
import com.ssm.core.bean.PageResponse;
import com.ssm.core.util.SessionHelp;
import com.ssm.core.utils.AESUtils;
import com.ssm.core.utils.ChuangLanBeanSmsUtil;
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.CommonUtils;
import com.ssm.manage.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Api(value = "APP员工controller",tags = {"门店员工相关接口"})
@RequestMapping("/demay/ssm/staff")
@RestController
@Slf4j
public class SsmStaffController {
    @Autowired
    private SsmStoreStatisticService ssmStoreStatisticService;

    @Autowired
    private ChuangLanBeanSmsUtil chuangLanBeanSmsUtil;

    @Autowired
    private SsmCashService ssmCashService;

    @Autowired
    private SsmBaseStaffService ssmBaseStaffService;

    @Autowired
    private SsmProjectProgressService ssmProjectProgressService;

    @Autowired
    private SsmProjectProgressFileService ssmProjectProgressFileService;

    @Autowired
    private SsmStaffSalaryService ssmStaffSalaryService;

    @Autowired
    private SsmStoreMonthTaskService ssmStoreMonthTaskService;

    @Autowired
    private SsmNegativeCardService ssmNegativeCardService;

    @Autowired
    private SsmRechargeService ssmRechargeService;

    @Autowired
    private SsmRepayService ssmRepayService;

    @Autowired
    private  SsmRefundOrderService ssmRefundOrderService;

    @Autowired
    private SsmStoreMemberService ssmStoreMemberService;

    @Autowired
    private SsmCourseService ssmCourseService;

    @Autowired
    private SsmDocPromotionService ssmDocPromotionService;

    @Autowired
    private SsmDocPromotionProjectService ssmDocPromotionProjectService;

    @Autowired
    private SsmRefundRechargeService ssmRefundRechargeService;

    @Autowired
    private SsmRefundCourseService ssmRefundCourseService;

    @Autowired
    private SsmRefundCashService ssmRefundCashService;

    @Autowired
    private SsmRechargeAccountService ssmRechargeAccountService;

    @Autowired
    private SsmCourseAccountService ssmCourseAccountService;

    @Autowired
    private SsmBaseStoreService ssmBaseStoreService;

    @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("SsmStaffController.achiCommList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工业绩提成分类统计列表  APP端-员工-门店员工业绩提成分类统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/achiCommClassifyList")
    public GenericResponse achiCommClassifyList(@RequestBody StaffStatistic staffStatistic){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();
        List<StaffStatistic> totalList = new ArrayList<>();
        List<StaffStatistic> list = new ArrayList<>();
        try {
            staffStatistic.setClassify("0");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("1");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("2");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("3");
            list = ssmStoreStatisticService.achiCommListRepay(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("4");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("5");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("6");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("7");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("8");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("9");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("10");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
            staffStatistic.setClassify("11");
            list = ssmStoreStatisticService.achiCommList(staffStatistic);
            totalList.addAll(list);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStaffController.achiCommList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(totalList);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工业绩提成明细统计列表  APP端-员工-门店员工业绩提成明细统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/achiCommDetailList")
    public GenericResponse achiCommDetailList(@RequestBody StaffStatistic staffStatistic){
        GenericResponse<AchiCommEntity> genericResponse = new GenericResponse();

        List<AchiCommEntity> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.achiCommDetailList(staffStatistic);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e){
            log.error("SsmStaffController.achiCommDetailList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "查询员工明细  门店端-员工-查询员工明细", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectOne")
    public GenericResponse selectOne(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        BaseStaff baseStaff = new BaseStaff();
        try {
            baseStaff = ssmBaseStaffService.selectOne(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.selectOne:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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



    @ApiOperation(value = "分页获取门店员工疗程服务日志列表  APP端-员工-分页获取门店员工疗程服务日志列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/getProjectProgress")
    public PageResponse getProjectProgress(@RequestBody ProjectProgressRequest progressRequest){
        PageResponse<ProjectProgress> pageResponse = new PageResponse();
        PageInfo<ProjectProgress> pageInfo = new PageInfo<>();
        try {
            pageInfo = ssmProjectProgressService.selectPage(progressRequest);
        }catch (Exception e){
            log.error("SsmStaffController.getProjectProgress",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "获取门店员工疗程服务日志明细 主要是文件查询  APP端-员工-获取门店员工疗程服务日志明细",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/getProjectProgressDetail")
    public GenericResponse getProjectProgressDetail(@RequestBody ProjectProgressFile projectProgressFile){
        GenericResponse<ProjectProgressFile> genericResponse = new GenericResponse();
        List<ProjectProgressFile> list = new ArrayList<>();
        try {
            list = ssmProjectProgressFileService.select(projectProgressFile);
        }catch (Exception e){
            log.error("SsmStaffController.getProjectProgressDetail",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();
        Long storeId = staffStatistic.getStoreId();
        List<StaffStatistic> list = new ArrayList<>();
        try {
            list = ssmStoreStatisticService.selectDayStaffList(storeId, new Date());
        }catch (Exception e){
            log.error("SsmStaffController.staffDayList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "分页查询员工信息  员工端-分页查询员工信息",httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping("/page")
    public PageResponse selectPage(@RequestBody StaffRequest staffRequest){
        PageResponse<StaffResponse> pageResponse = new PageResponse<>();
        PageInfo<StaffResponse> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        staffRequest.setStoreId(storeId);
        try {
            pageInfo = ssmBaseStaffService.selectPage(staffRequest);
        }catch (Exception e){
            log.error("SsmStaffController.selectPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }


    @ApiOperation(value = "员工服务人相关收银单据查询 员工反馈使用  APP端-员工-员工服务人相关收银单据查询 员工反馈使用",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/staffProgress")
    public PageResponse staffProgress(@RequestBody StaffProgress staffProgress){
        PageResponse<StaffProgress> pageResponse = new PageResponse();
        PageInfo<StaffProgress> pageInfo = new PageInfo<>();

        try {
            pageInfo = ssmCashService.selectStaffProgress(staffProgress);
        }catch (SmsException e){
            log.error("SsmStaffController.staffProgress",e);
            pageResponse.setCodeAndMsg(e);
            return pageResponse;
        }catch (Exception e){
            log.error("SsmStaffController.staffProgress",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "门店员工跟踪反馈添加  员工微信小程序-门店员工跟踪反馈添加",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/addProjectProgress")
    public GenericResponse addProjectProgress(@RequestBody ProjectProgressRequest progressRequest){
        GenericResponse<ProjectProgress> genericResponse = new GenericResponse<>();
        if (null == progressRequest.getUploadFiles() || progressRequest.getUploadFiles().isEmpty()) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "文件");
            return genericResponse;
        }
        int i = 0;
        try {
            i = ssmProjectProgressService.add(progressRequest);
        }catch (Exception e){
            log.error("SsmMemberController.addProjectProgress:",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        return genericResponse;
    }

    @ApiOperation(value = "查询员工工资  APP-统计报表-查询员工工资", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/selectSalary")
    public GenericResponse selectSalary(@RequestBody StaffSalaryRequest staffSalaryRequest) {
        GenericResponse<StaffSalary> genericResponse = new GenericResponse();
        List<StaffSalary> list = new ArrayList<>();
//        staffSalaryRequest.setStoreId(SessionHelp.getStoreId(session));
//        staffSalaryRequest.setStoreName(SessionHelp.getStoreName(session));
        try {
            list = ssmStaffSalaryService.selectSalary(staffSalaryRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.selectSalary:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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


    @ApiOperation(value = "员工个人查询工资  APP-员工个人查询工资", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/salaryByStaff")
    public GenericResponse selectSalaryByStaff(@RequestBody StaffSalaryRequest staffSalaryRequest) {
        GenericResponse<StaffSalaryResponse> genericResponse = new GenericResponse();
        StaffSalaryResponse staffSalaryResponse = new StaffSalaryResponse();
        try {
            staffSalaryResponse = ssmStaffSalaryService.selectSalaryByStaff(staffSalaryRequest);
        }catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        } catch (Exception e) {
            log.error("SsmStaffController.selectSalaryByStaff:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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


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

    @ApiOperation(value = "门店项目统计列表  APP-门店统计-门店项目统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/projectList")
    public GenericResponse projectList(@RequestParam Long storeId, @RequestParam int type){
        GenericResponse<ProjectStatistic> genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
        List<ProjectStatistic> list = new ArrayList<>();
        String dateStr = DateUtils.ymdFormat(new Date());
        try {
            list = ssmStoreStatisticService.selectProjectList(storeId, dateStr ,type);
        }catch (Exception e){
            log.error("SsmStaffController.projectList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店员工统计列表  APP-门店统计-门店员工统计列表",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/staffMonthList")
    public GenericResponse staffMonthList(@RequestBody StaffStatistic staffStatistic){
        GenericResponse<StaffStatistic> genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
        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("SsmStaffController.staffMonthList",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "门店月度任务统计  APP-门店月度任务-门店月度任务统计", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/statistic")
    public GenericResponse statistic(@RequestBody StoreMonthTaskRequest storeMonthTaskRequest) {
        GenericResponse<StoreMonthTaskResponse> genericResponse = new GenericResponse<>();
//        storeMonthTaskRequest.setStoreId(SessionHelp.getStoreId(session));
        StoreMonthTaskResponse response = new StoreMonthTaskResponse();

        try {
            response = ssmStoreMonthTaskService.statistic(storeMonthTaskRequest);
        }catch (Exception e) {
            log.error("SsmStaffController.statistic", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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


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

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

    @ApiOperation(value = "门店日报", notes = "APP-门店统计报表-门店日报", response = GenericResponse.class)
    @RequestMapping(path="/storeDaily", method= RequestMethod.POST)
    public GenericResponse dayStatistic(@RequestBody StoreDailyRequest storeDailyRequest) {
        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);


//        CashListRequest cashListRequest = new CashListRequest();
//        BeanUtils.copyProperties(storeDailyRequest, cashListRequest);
//        RechargeListRequest rechargeListRequest = new RechargeListRequest();
//        BeanUtils.copyProperties(storeDailyRequest, rechargeListRequest);
//        Example repayExample = new Example(RepayList.class);
//        Example.Criteria repayCriteria = repayExample.createCriteria();
//        repayCriteria.andLike("repayDate",DateUtils.ymdFormat(date) + "%");
//        repayCriteria.andEqualTo("isCancel",storeDailyRequest.getIsCancel());
//        repayCriteria.andEqualTo("storeId",storeDailyRequest.getStoreId());
//        Example refundExample = new Example(RefundOrderList.class);
//        Example.Criteria refundCriteria = refundExample.createCriteria();
//        refundCriteria.andLike("refundDate",DateUtils.ymdFormat(date) + "%");
//        refundCriteria.andEqualTo("isCancel",storeDailyRequest.getIsCancel());
//        refundCriteria.andEqualTo("storeId",storeDailyRequest.getStoreId());
        try{
            //查询收银人头数和收银服务金额
            List<StoreDaily> storeDailyCashList = ssmCashService.selectDayRecord(storeDailyRequest);
            StoreDaily storeDailyCash = new StoreDaily();
            if (null != storeDailyCashList && !storeDailyCashList.isEmpty()){
                storeDailyCash = storeDailyCashList.get(0);
            }
            //查询疗程单数和疗程金额
            StoreDaily storeDailyCourse = ssmCourseService.selectDayRecord(storeDailyRequest);
            //查询充值单数和充值金额
            StoreDaily storeDailyRec = ssmRechargeService.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);
            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());
        }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 = "查询当前门店所有在职员工  门店端-员工作品-查询当前门店所有在职员工", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/selectList")
    public GenericResponse selectList(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
//        staffRequest.setStoreId(SessionHelp.getStoreId(session));
        List<BaseStaff> list = new ArrayList<>();
        try {
            list = ssmBaseStaffService.selectList(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.selectList:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }

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

    @ApiOperation(value = "查询人员占比信息", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/selectRatioInfo")
    public GenericResponse selectPersonZB(@RequestBody CourseAccountRequest request) {
        GenericResponse<PersonZBResponse> genericResponse = new GenericResponse();

        List<PersonZBResponse> list = new ArrayList<>();
        try {
            list = ssmBaseStaffService.selectPersonZB(request);
        } catch (Exception e) {
            log.error("SsmStaffController.selectRatioInfo:", 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){
        GenericResponse<ProjectStatistic> genericResponse = new GenericResponse();
        List<ProjectStatistic> list = new ArrayList<>();

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

    @ApiOperation(value = "查询收银服务单收款列表  (app-查询收银服务单收款列表)",httpMethod = "POST",response = GenericResponse.class)
    @RequestMapping("/cashList")
    public PageResponse selectCashList(@RequestBody CashListRequest cashListRequest){
        PageResponse<CashListResponse> pageResponse = new PageResponse<>();
        PageInfo<CashListResponse> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        cashListRequest.setStoreId(storeId);
        try {
            pageInfo = ssmCashService.selectCashList(cashListRequest);
        }catch (Exception e){
            log.error("SsmStaffController.selectCashList:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询疗程单  (app-列表分页查询疗程单)",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/coursePage")
    public PageResponse selectPage(@RequestBody CourseListRequest courseListRequest){
        PageResponse<CourseListResponse> pageResponse = new PageResponse<>();
        PageInfo<CourseListResponse> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        courseListRequest.setStoreId(storeId);
        try {
            pageInfo = ssmCourseService.selectPage(courseListRequest);
        }catch (SmsException e){
            pageResponse.setCodeAndMsg(e);
            return pageResponse;
        }catch (Exception e){
            log.error("SsmStaffController.coursePage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询充值单  (app-列表分页查询充值单)",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/rechargePage")
    public PageResponse selectPage(@RequestBody RechargeListRequest rechargeListRequest){
        PageResponse<RechargeListResponse> pageResponse = new PageResponse<>();
        PageInfo<RechargeListResponse> pageInfo = new PageInfo<>();
//        Long storeId = SessionHelp.getStoreId(session);
//        rechargeListRequest.setStoreId(storeId);
        try {
            pageInfo = ssmRechargeService.selectPage(rechargeListRequest);
        }catch (SmsException e){
            pageResponse.setCodeAndMsg(e);
            return pageResponse;
        }catch (Exception e){
            log.error("SsmStaffController.rechargePage:",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询  app-分页查询退款单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/refundPage")
    public PageResponse selectPage(@RequestBody RefundOrderResquest refundOrderResquest) {
        PageResponse<RefundOrderList> pageResponse = new PageResponse<>();
        PageInfo<RefundOrderList> pageInfo = new PageInfo<>();
//        refundOrderResquest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmRefundOrderService.selectPage(refundOrderResquest);
        } catch (Exception e) {
            log.error("SsmStaffController.refundPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询  app-分页查询还款单",httpMethod = "POST",response = PageResponse.class)
    @RequestMapping("/repayPage")
    public PageResponse selectPage(@RequestBody RepayListRequest repayListRequest){
        PageResponse<RepayListResponse> pageResponse = new PageResponse<>();
        PageInfo<RepayListResponse> pageInfo = new PageInfo<>();
//        repayListRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmRepayService.selectPage(repayListRequest);
        }catch (Exception e){
            log.error("SsmStaffController.repayPage",e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询  app-分页查询促销活动单", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/promotionPage")
    public PageResponse promotionPage(@RequestBody DocPromotionRequest docPromotionRequest) {
        PageResponse<DocPromotionResponse> pageResponse = new PageResponse<>();
        PageInfo<DocPromotionResponse> pageInfo = new PageInfo<>();
//        docPromotionRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmDocPromotionService.selectPage(docPromotionRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.promotionPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "查询促销活动项目  门店端-促销活动-查询促销活动项目", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/promotionProList")
    public GenericResponse promotionProList(@RequestBody DocPromotionRequest docPromotionRequest) {
        GenericResponse pageResponse = new GenericResponse();
//        docPromotionRequest.setStoreId(SessionHelp.getStoreId(session));
        List<DocPromotionProject> list = new ArrayList<>();
        try {
            list = ssmDocPromotionProjectService.selectList(docPromotionRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.promotionProList", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setRows(list);
        return pageResponse;
    }

    @ApiOperation(value = "分页查询  app-分页查询充值退款单明细", httpMethod = "POST", response = PageResponse.class)
    @RequestMapping("/refundRechargePage")
    public PageResponse refundRechargePage(@RequestBody RefundRechargeRequest refundRechargeRequest) {
        PageResponse<RefundRechargeResponse> pageResponse = new PageResponse<>();
        PageInfo<RefundRechargeResponse> pageInfo = new PageInfo<>();
//        refundRechargeRequest.setStoreId(SessionHelp.getStoreId(session));
        try {
            pageInfo = ssmRefundRechargeService.selectPage(refundRechargeRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.refundRechargePage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        pageResponse.setPageInfo(pageInfo);
        return pageResponse;
    }

    @ApiOperation(value = "疗程退明细查询  app-疗程退明细查询", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/refundCourseList")
    public GenericResponse refundCourseList(@RequestBody RefundCourseRequest refundCourseRequest) {
        GenericResponse<RefundCourseResponse> genericResponse = new GenericResponse<>();
//        refundCourseRequest.setStoreId(SessionHelp.getStoreId(session));
        List<RefundCourseResponse> list = new ArrayList<>();
        try {
            list = ssmRefundCourseService.selectList(refundCourseRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.refundCourseList", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "消费退明细查询  门店端-疗程退-消费退明细查询", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/refundCashList")
    public GenericResponse refundCashList(@RequestBody RefundCashReqest refundCashReqest) {
        GenericResponse<RefundCashResponse> genericResponse = new GenericResponse<>();
//        refundCashReqest.setStoreId(SessionHelp.getStoreId(session));
        List<RefundCashResponse> list = new ArrayList<>();
        try {
            list = ssmRefundCashService.selectList(refundCashReqest);
        } catch (Exception e) {
            log.error("SsmStaffController.refundCashList", 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 Long storeId){
        GenericResponse<RechargeAccountResponse> genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
        RechargeAccountRequest rechargeAccountRequest = new RechargeAccountRequest();
        rechargeAccountRequest.setStoreId(storeId);
        rechargeAccountRequest.setState(1);
        List<RechargeAccountResponse> list = new ArrayList<>();
        try {
            list = ssmRechargeAccountService.memberRecharge(rechargeAccountRequest);
        }catch (Exception e){
            log.error("SsmStaffController.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 Long storeId){
        GenericResponse<CourseAccountResponse> genericResponse = new GenericResponse();
//        Long storeId = SessionHelp.getStoreId(session);
        CourseAccountRequest courseAccountRequest = new CourseAccountRequest();
        courseAccountRequest.setStoreId(storeId);
        courseAccountRequest.setState(1);
        List<CourseAccountResponse> list = new ArrayList<>();
        try {
            list = ssmCourseAccountService.memberCourse(courseAccountRequest);
        }catch (Exception e){
            log.error("SsmStaffController.memberCourse",e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        genericResponse.setCodeAndMsg(ResponseCode.OK);
        genericResponse.setRows(list);
        return genericResponse;
    }

    @ApiOperation(value = "添加员工  门店端-员工-添加员工", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/add")
    public GenericResponse add(@RequestBody StaffRequest staffRequest, HttpSession session) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
            return genericResponse;
        }
        if (!CommonUtils.isMobile(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S, "手机号码");
            return genericResponse;
        }
        if (StringUtils.isBlank(staffRequest.getIdentityCard())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "身份证号");
            return genericResponse;
        }
        if (!CommonUtils.isIdentityCard(staffRequest.getIdentityCard())) {
            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S, "身份证号");
            return genericResponse;
        }
        String password = ObjectUtils.toString((int)((Math.random()*9+1)*1000));
        staffRequest.setPassword(AESUtils.encrypt(password));
//        Long storeId = SessionHelp.getStoreId(session);
//        String storeName = SessionHelp.getStoreName(session);
//        staffRequest.setStoreName(storeName);
//        staffRequest.setStoreId(storeId);
        int i = 0;
        try {
            i = ssmBaseStaffService.addStaff(staffRequest,SessionHelp.getLoginAccount(session));
        } catch (SmsException e){
            log.error("SsmStaffController.add:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
            BaseStore baseStore = ssmBaseStoreService.selectByPrimaryKey(staffRequest.getStoreId());
            //设置短信账户 发送 否则不发送
            if (StringUtils.isNotBlank(baseStore.getSmsAccount())) {
                try {
                    chuangLanBeanSmsUtil.sendStaffInitPwdSms(baseStore, staffRequest.getMobilePhone(), Integer.parseInt(password));
                } catch (IOException e) {
                    log.error("SsmStaffController.add:", e);
                } catch (SmsException e) {
                    log.error("SsmStaffController.add:", e);
                }
            }
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "员工修改   门店端-员工-员工修改", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/update")
    public GenericResponse update(@RequestBody StaffRequest staffRequest, HttpSession session) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
//        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
//            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
//            return genericResponse;
//        }
//        if (!CommonUtils.isMobile(staffRequest.getMobilePhone())) {
//            genericResponse.setCodeAndMsg(ResponseCode.NOT_MEET_REQUIREMENTS_S, "手机号码");
//            return genericResponse;
//        }
//        staffRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmBaseStaffService.updateStaff(staffRequest,SessionHelp.getLoginAccount(session));
        } catch (SmsException e){
            log.error("SsmStaffController.update:",e);
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.update:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }


    @ApiOperation(value = "员工月度任务   门店端-员工-员工月度任务", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/monthJob")
    public GenericResponse monthJob(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
//        staffRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmBaseStaffService.setMonthjob(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.monthJob:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "员工离职  门店端-员工-员工离职", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/leave")
    public GenericResponse leave(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
//        staffRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmBaseStaffService.staffLeave(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.add:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "员工复职  门店端-员工-员工复职", httpMethod = "POST", response = GenericResponse.class, notes = "")
    @RequestMapping("/reinstated")
    public GenericResponse reinstated(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
            return genericResponse;
        }
//        staffRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmBaseStaffService.staffReinstated(staffRequest);
        } catch (SmsException e){
            genericResponse.setCodeAndMsg(e);
            return genericResponse;
        }catch (Exception e) {
            log.error("SsmStaffController.reinstated:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        } else {
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
        }
        return genericResponse;
    }

    @ApiOperation(value = "员工跟进会员统计信息分页查询  门店端-员工会员统计表-员工跟进会员统计信息分页查询" ,httpMethod = "POST",notes = "",response = PageResponse.class)
    @RequestMapping("/staffMemberPage")
    public PageResponse staffMemberPage(@RequestBody StaffRequest staffRequest, HttpSession session){
        PageResponse<StaffResponse> pageResponse = new PageResponse<>();
        PageInfo<StaffResponse> pageInfo = new PageInfo<>();
        staffRequest.setMobilePhone(SessionHelp.getLoginAccount(session));
        try {
            pageInfo = ssmBaseStaffService.staffMemberPage(staffRequest);
        }catch (Exception e){
            log.error("SsmStaffController.staffMemberPage", e);
            pageResponse.setCodeAndMsg(ResponseCode.FAILED);
            return pageResponse;
        }
        pageResponse.setPageInfo(pageInfo);
        pageResponse.setCodeAndMsg(ResponseCode.OK);
        return pageResponse;
    }

    @ApiOperation(value = "门店员工启用  门店端-员工列表-门店员工启用", httpMethod = "POST", response = GenericResponse.class)
    @RequestMapping("/start")
    public GenericResponse start(@RequestBody StaffRequest staffRequest) {
        GenericResponse<BaseStaff> genericResponse = new GenericResponse();
        if (StringUtils.isBlank(staffRequest.getMobilePhone())) {
            genericResponse.setCodeAndMsg(ResponseCode.PARAMETER_NOT_EXIST_S, "手机号码");
            return genericResponse;
        }
//        staffRequest.setStoreId(SessionHelp.getStoreId(session));
        int i = 0;
        try {
            i = ssmBaseStaffService.start(staffRequest);
        } catch (Exception e) {
            log.error("SsmStaffController.start:", e);
            genericResponse.setCodeAndMsg(ResponseCode.FAILED);
            return genericResponse;
        }
        if (i > 0) {
            genericResponse.setCodeAndMsg(ResponseCode.OK);
        }
        return genericResponse;
    }
}
