package com.cf.imes.module.executor.controller.admin.order;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.module.executor.controller.admin.order.vo.order.OrderStatisticsReqVO;
import com.cf.imes.module.executor.service.order.OrderSupStatisticsService;
import com.cf.imes.module.system.api.organ.OrganApi;
import com.cf.imes.module.system.api.organ.dto.OrgStatisticsReqDTO;
import com.cf.imes.module.system.api.user.AdminUserApi;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 超管-生产单统计管理")
@RestController
@RequestMapping("/executor/order/supStatistics")
@Validated
@Slf4j
public class OrderSupStatisticsController {

    @Resource
    private OrderSupStatisticsService orderSupStatisticsService;

    @Resource
    private OrganApi organApi;

    @Resource
    private AdminUserApi adminUserApi;

    @GetMapping("/total/org")
    @Operation(summary = "组织总数")
    @PreAuthorize("@ss.hasPermission('homePage:analysis:org-statistic')")
    public CommonResult<Map<String, Integer>> getOrgTotal() {
        return success(organApi.getOrgTotal().getData());
    }

    @GetMapping("/total/user")
    @Operation(summary = "用户总数")
    @PreAuthorize("@ss.hasPermission('homePage:analysis:org-statistic')")
    public CommonResult<Map<String, Integer>> getUserTotal() {
        return success(adminUserApi.getUserTotal().getData());
    }

    @GetMapping("/total/userAct")
    @Operation(summary = "用户活跃数")
    @PreAuthorize("@ss.hasPermission('homePage:analysis:org-statistic')")
    public CommonResult<Map<String, Integer>> getUserActTotal() {
        return success(adminUserApi.getUserActTotal().getData());
    }

    @GetMapping("/total/order")
    @Operation(summary = "生产单总数")
    @PreAuthorize("@ss.hasPermission('homePage:analysis:org-statistic')")
    public CommonResult<Map<String, Integer>> getOrderTotal() {
        return success(orderSupStatisticsService.orderTotal());
    }

    @GetMapping("/total/plateArea")
    @Operation(summary = "拆单板件平方数")
    public CommonResult<Map<String, Double>> getPlateAreaTotal() {
        return success(orderSupStatisticsService.plateAreaTotal());
    }

    @GetMapping("/separate/order")
    @Operation(summary = "有效、无效生产单数量统计")
    @PreAuthorize("@ss.hasPermission('homePage:analysis:order-count')")
    public CommonResult<Map<String, Object>> getOrderSeparate(@Valid OrderStatisticsReqVO reqVO) {
        return success(orderSupStatisticsService.orderSeparate(reqVO));
    }

    @GetMapping("/separate/org")
    @Operation(summary = "新增、注销组织数量统计")
    @PreAuthorize("@ss.hasPermission('homePage:analysis:org-count')")
    public CommonResult<Map<String, Object>> getOrgSeparate(@Valid OrgStatisticsReqDTO reqVO) {
        return organApi.getOrgSeparate(reqVO);
    }

    @GetMapping("/separate/plateArea")
    @Operation(summary = "有效、无效拆单板件平方统计")
    public CommonResult<Map<String, Object>> getPlateAreaSeparate(@Valid OrderStatisticsReqVO reqVO) {
        return success(orderSupStatisticsService.plateAreaSeparate(reqVO));
    }

    @GetMapping("/warn/org")
    @Operation(summary = "组织过期日期统计")
    public CommonResult<Map<String, List<Object>>> getWarnToOrg() {
        return organApi.getWarnToOrg();
    }
}
