package com.qmth.sop.server.api;

import com.qmth.boot.api.constant.ApiConstant;
import com.qmth.sop.business.bean.result.DingSubmitResult;
import com.qmth.sop.business.service.*;
import com.qmth.sop.common.annotation.OperationLog;
import com.qmth.sop.common.contant.SystemConstant;
import com.qmth.sop.common.enums.DingStatisticTypeEnum;
import com.qmth.sop.common.enums.LogTypeEnum;
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.io.IOException;

/**
 * @Description: 考勤统计Controller
 * @Author: CaoZixuan
 * @Date: 2024-05-22
 */
@Api(tags = "考勤统计Controller")
@RestController
@RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + SystemConstant.PREFIX_URL_DING_STATISTIC)
public class TBDingStatisticController {

    @Resource
    private TBDingStatisticService tbDingStatisticService;

    @Resource
    private TBDingStatisticByPersonService tbdingStatisticByPersonService;

    @Resource
    private TBDingStatisticByCrmService tbdingStatisticByCrmService;

    @Resource
    private TBDingStatisticBySopService tbdingStatisticBySopService;

    @Resource
    private TBDingStatisticByCustomService tbdingStatisticByCustomService;

    @Resource
    private TBDingStatisticBySupplierService tbdingStatisticBySupplierService;

    @ApiOperation(value = "工时统计-分页查询")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "查询成功", response = DingSubmitResult.class) })
    public Result findDingSubmitPage(@ApiParam(value = "服务单元id", required = true) @RequestParam Long serviceId,
            @ApiParam(value = "统计维度", required = true) @RequestParam DingStatisticTypeEnum 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) {
        switch (type) {
        case BY_PERSON:
            return ResultUtil.ok(
                    tbdingStatisticByPersonService.findStatisticByPersonPage(serviceId, pageNumber, pageSize));
        case BY_CRM:
            return ResultUtil.ok(tbdingStatisticByCrmService.findStatisticByCrmPage(serviceId, pageNumber, pageSize));
        case BY_SOP:
            return ResultUtil.ok(tbdingStatisticBySopService.findStatisticBySopPage(serviceId, pageNumber, pageSize));
        case BY_CUSTOM:
            return ResultUtil.ok(
                    tbdingStatisticByCustomService.findStatisticByCustomPage(serviceId, pageNumber, pageSize));
        case BY_SUPPLIER:
            return ResultUtil.ok(tbdingStatisticBySupplierService.findStatisticBySupplierPage(serviceId, pageNumber, pageSize));
        }
        return ResultUtil.ok();
    }

    @ApiOperation(value = "工时统计-批量导出")
    @RequestMapping(value = "/export", method = RequestMethod.POST)
    @ApiResponses({ @ApiResponse(code = 200, message = "返回信息", response = Object.class) })
    @OperationLog(logType = LogTypeEnum.EXPORT)
    public Result exportDingSubmit(@ApiParam(value = "服务单元id", required = true) @RequestParam Long serviceId,
            @ApiParam(value = "统计维度", required = true) @RequestParam DingStatisticTypeEnum type) throws IOException {
        tbDingStatisticService.exportStatisticData(serviceId, type);
        return ResultUtil.ok();
    }
}
