package com.goafanti.admin.controller;

import com.goafanti.admin.bo.AdminPublicDuration;
import com.goafanti.admin.bo.AdminPublicDurationCount;
import com.goafanti.admin.bo.selectAllUserDataBo;
import com.goafanti.admin.service.AdminStatisticsService;
import com.goafanti.common.bo.Result;
import com.goafanti.common.constant.AFTConstants;
import com.goafanti.common.controller.CertifyApiController;
import com.goafanti.common.utils.excel.NewExcelUtil;
import com.goafanti.core.shiro.token.TokenManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping(value = "/api/admin/statistics")
public class AdminStatisticsController extends CertifyApiController {

    @Autowired
    private AdminStatisticsService adminStatisticsService;

    @Value(value = "${upload.path}")
    private final String	uploadPath			= null;

    /**
     * 营销首页信息
     * @param startTime  开始时间
     * @param endTime  结束时间
     */
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public Result info(String startTime,String endTime) {
        Result res =new Result();
        res.data(adminStatisticsService.info(startTime, endTime));

        return res;

    }

    /**
     * 营销首页折线图
     */
    @RequestMapping(value = "/lists", method = RequestMethod.GET)
    public Result lists() {
        Result res =new Result();
        res.data(adminStatisticsService.lists(TokenManager.getAdminId()));
        return res;
    }


    /**
     * 咨询师首页信息
     * @param startTime  开始时间
     * @param endTime  结束时间
     */
    @RequestMapping(value = "/consultant/info", method = RequestMethod.GET)
    public Result consultantInfo(String startTime,String endTime) {
        Result res =new Result();
        res.data(adminStatisticsService.consultantInfo(startTime, endTime));
        return res;

    }

    /**
     * 咨询首页折线图
     */
    @RequestMapping(value = "/consultant/lists", method = RequestMethod.GET)
    public Result consultantLists() {
        Result res =new Result();
        res.data(adminStatisticsService.consultantLists(TokenManager.getAdminId()));
        return res;
    }


    /**
     * 财务首页信息
     * @param startTime  开始时间
     * @param endTime  结束时间
     */
    @RequestMapping(value = "/finance/info", method = RequestMethod.GET)
    public Result financeInfo(String startTime,String endTime) {
        Result res =new Result();
        res.data(adminStatisticsService.financeInfo(startTime, endTime));
        return res;

    }

    /**
     * 咨询首页折线图
     */
    @RequestMapping(value = "/finance/lists", method = RequestMethod.GET)
    public Result financeLists() {
        Result res =new Result();
        res.data(adminStatisticsService.financeLists(TokenManager.getAdminId()));
        return res;
    }


    /**
     * 营销团队外呼统计
     */
    @RequestMapping(value = "/salesTeam", method = RequestMethod.GET)
    public Result salesTeam(String startTime,String endTime) {
        Result res =new Result();
        String aid = TokenManager.getAdminId();
        res.data(adminStatisticsService.salesTeam(aid,startTime, endTime));
        return res;
    }

    /**
     * 营销团队外呼统计缓存清理
     */
    @RequestMapping(value = "/salesTeam/clean", method = RequestMethod.GET)
    public Result salesTeamClean() {
        Result res =new Result();
        res.data(adminStatisticsService.salesTeamClean());
        return res;
    }

    /**
     * 统计月份打卡数据
     */
    @RequestMapping(value = "/month/data", method = RequestMethod.GET)
    public Result monthData(String date) {
        Result res =new Result();
        Map<String, Object> map = adminStatisticsService.monthData(date);
        res.setData(map);
        return res;
    }


    /**
     * 统计月份打卡数据
     */
    @RequestMapping(value = "/month/data/export", method = RequestMethod.GET)
    public Result monthDataExport(String date,Integer type) {

        Map<String, Object> map = adminStatisticsService.monthData(date);
        String sheetName;
        if (type==null||type==1){
            NewExcelUtil<AdminPublicDuration> newExcelUtil=new NewExcelUtil<>(AdminPublicDuration.class);
            sheetName="统计月份打卡数据（详情）";
            List<AdminPublicDuration> o = (List<AdminPublicDuration>) map.get("date");
            return newExcelUtil.exportExcel(o,sheetName,uploadPath);
        }else {
            sheetName="统计月份打卡数据（总计）";
            List<AdminPublicDuration>  o = (List<AdminPublicDuration>) map.get("count");
            List<AdminPublicDurationCount>  list = new ArrayList<>();
            o.forEach(e->{
                AdminPublicDurationCount adminPublicDurationCount = new AdminPublicDurationCount();
                adminPublicDurationCount.setId(e.getId());
                adminPublicDurationCount.setName(e.getName());
                adminPublicDurationCount.setDuration(e.getDuration());
                adminPublicDurationCount.setCountNumber(e.getCountNumber());
                adminPublicDurationCount.setConversion(e.getConversion());
                adminPublicDurationCount.setDays(e.getDays());
                list.add(adminPublicDurationCount);
            });
            NewExcelUtil<AdminPublicDurationCount> newExcelUtil=new NewExcelUtil<>(AdminPublicDurationCount.class);
            return newExcelUtil.exportExcel(list, sheetName, uploadPath);
        }
    }


    /**
     * 统计日周月年客户与面谈
     */
    @RequestMapping(value = "/AllDataExport", method = RequestMethod.GET)
    public Result AllDataExport( HttpServletResponse response) {
        //管理员与曾妹涵账号有权限导出
        if (TokenManager.hasRole(AFTConstants.SUPERADMIN)|| Objects.equals(TokenManager.getAdminId(), "a7661bcf-bdca-484e-baef-6f52b305aeae")){
            List<selectAllUserDataBo> list = adminStatisticsService.AllDataExport();
            NewExcelUtil<selectAllUserDataBo> newExcelUtil=new NewExcelUtil<>(selectAllUserDataBo.class);
            return newExcelUtil.exportExcel(list,"统计所有用户数据",response);
        }
        return new Result<>("账号权限不足");

    }

}
