package com.qmth.sop.server.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.CrmAnalyseResult;
import com.qmth.sop.business.bean.result.TBCrmResult;
import com.qmth.sop.business.service.CrmAnalyseService;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.ProductTypeEnum;
import com.qmth.sop.common.util.Result;
import com.qmth.sop.common.util.ResultUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.Map;

/**
 * 派单分析 控制器
 *
 * @author: shudonghui
 * @date: 2023-08-14 15:55:18
 * @version: 1.0
 * @email: shudonghui@qmth.com.cn
 * @Company: www.qmth.com.cn
 */
@Api(tags = "派单分析 Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_CRM + "/analyse")
public class CrmAnalyseController {

    @Resource
    CrmAnalyseService crmAnalyseService;

    @ApiOperation(value = "客户类型分布接口")
    @RequestMapping(value = "/custom/type", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "客户类型分布", response = Map.class)})
    public Result customType(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                             @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime) {
        return ResultUtil.ok(crmAnalyseService.countCustomType(startTime, endTime));
    }


    @ApiOperation(value = "客户类型分布下钻接口")
    @RequestMapping(value = "/custom/type/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "客户类型分布下钻", response = TBCrmResult.class)})
    public Result customTypeDetail(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                                   @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime,
                                   @ApiParam(value = "客户类型", required = true) @RequestParam ProductTypeEnum type,
                                   @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                   @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(crmAnalyseService.customTypeDetail(new Page<>(pageNumber, pageSize), startTime, endTime, type));
    }

    @ApiOperation(value = "月度派单分布及对比接口")
    @RequestMapping(value = "/monthly", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "月度派单分布及对比接口", response = Map.class)})
    public Result monthly(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                          @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime,
                          @ApiParam(value = "客户类型", required = true) @RequestParam ProductTypeEnum type) {
        return ResultUtil.ok(crmAnalyseService.monthly(startTime, endTime, type));
    }

    @ApiOperation(value = "月度派单分布下钻接口")
    @RequestMapping(value = "/monthly/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "月度派单分布下钻", response = TBCrmResult.class)})
    public Result monthlyDetail(@ApiParam(value = "年度", required = true) @RequestParam String year,
                                @ApiParam(value = "月份", required = true) @RequestParam String month,
                                @ApiParam(value = "客户类型", required = true) @RequestParam ProductTypeEnum type,
                                @ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                                @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime,
                                @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(crmAnalyseService.monthlyDetail(new Page<>(pageNumber, pageSize), year, month, type,startTime,endTime));
    }

    //供应商派单分布
    @ApiOperation(value = "供应商派单分布接口")
    @RequestMapping(value = "/supplier", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "供应商派单分布", response = CrmAnalyseResult.class)})
    public Result supplier(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                           @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime) {
        return ResultUtil.ok(crmAnalyseService.supplier(startTime, endTime));
    }

    @ApiOperation(value = "供应商派单分布下钻接口")
    @RequestMapping(value = "/supplier/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "供应商派单分布下钻", response = TBCrmResult.class)})
    public Result supplierDetail(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                                 @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime,
                                 @ApiParam(value = "供应商", required = false) @RequestParam(required = false) Long supplierId,
                                 @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                 @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(crmAnalyseService.supplierDetail(new Page<>(pageNumber, pageSize), startTime, endTime, supplierId));
    }

    //大区在执行派单排名
    @ApiOperation(value = "大区在执行派单排名接口")
    @RequestMapping(value = "/region", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "大区在执行派单排名", response = CrmAnalyseResult.class)})
    public Result region(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                         @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime,
                         @ApiParam(value = "客户类型", required = true) @RequestParam ProductTypeEnum type) {
        return ResultUtil.ok(crmAnalyseService.region(startTime, endTime, type));
    }


    //大区在执行派单排名
    @ApiOperation(value = "大区在执行派单排名下钻接口")
    @RequestMapping(value = "/region/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "大区在执行派单排名下钻", response = CrmAnalyseResult.class)})
    public Result regionDetail(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                               @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime,
                               @ApiParam(value = "城市", required = true) @RequestParam String province,
                               @ApiParam(value = "客户类型", required = true) @RequestParam ProductTypeEnum type,
                               @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                               @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(crmAnalyseService.regionDetail(new Page<>(pageNumber, pageSize), startTime, endTime, province, type));
    }

    //项目派单完成率
    @ApiOperation(value = "项目派单完成率接口")
    @RequestMapping(value = "/project", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "项目派单完成率", response = CrmAnalyseResult.class)})
    public Result project(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                          @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime) {
        return ResultUtil.ok(crmAnalyseService.project(startTime, endTime));
    }

    //项目派单完成率详细
    @ApiOperation(value = "项目派单完成率详细接口")
    @RequestMapping(value = "/project/detail", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "项目派单完成率详细", response = CrmAnalyseResult.class)})
    public Result projectDetail(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                                @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime,
                                @ApiParam(value = "服务单元", required = true) @RequestParam Long serviceId,
                                @ApiParam(value = "分页页码", required = true) @RequestParam @Min(SystemConstant.PAGE_NUMBER_MIN) Integer pageNumber,
                                @ApiParam(value = "分页数", required = true) @RequestParam @Min(SystemConstant.PAGE_SIZE_MIN) @Max(SystemConstant.PAGE_SIZE_MAX) Integer pageSize) {
        return ResultUtil.ok(crmAnalyseService.projectDetail(new Page<>(pageNumber, pageSize), startTime, endTime, serviceId));
    }

    //派单累计 服务人员累计
    @ApiOperation(value = "派单累计 服务人员累计")
    @RequestMapping(value = "/project/count", method = RequestMethod.POST)
    @ApiResponses({@ApiResponse(code = 200, message = "项目派单完成率详细", response = CrmAnalyseResult.class)})
    public Result projectCount(@ApiParam(value = "开始时间", required = true) @RequestParam Long startTime,
                               @ApiParam(value = "结束时间", required = true) @RequestParam Long endTime) {
        return ResultUtil.ok(crmAnalyseService.projectCount(startTime, endTime));
    }
}