package com.hotwj.platform.workbench.controller;

import com.hotwj.platform.resourceManagement.company.service.ISysUserLoginPortService;
import com.hotwj.platform.workbench.domain.vo.HeadquartersTrainingStatsVo;
import com.hotwj.platform.workbench.domain.vo.MonthlyStudyTrendVo;
import com.hotwj.platform.workbench.domain.vo.RegionCompanyStatsVo;
import com.hotwj.platform.workbench.service.IHeadquartersWorkbenchService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 总部端首页
 *
 * @author shihongwei
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workbench/headquarters")
@Tag(name = "总部端首页", description = "总部端首页统计模块")
public class HeadquartersWorkbenchController extends BaseController {

    private final IHeadquartersWorkbenchService headquartersWorkbenchService;

    /**
     * 获取总部端培训计划统计
     */
    @GetMapping("/trainingStats")
    @Operation(summary = "获取总部端培训计划统计")
    public R<List<HeadquartersTrainingStatsVo>> getTrainingStats() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getTrainingStats());
    }

    /**
     * 获取事故年度对比统计
     */
    @GetMapping("/accidentYearlyComparison")
    @Operation(summary = "获取事故年度对比统计")
    public R<com.hotwj.platform.workbench.domain.vo.HeadquartersYearlyComparisonVo> getAccidentYearlyComparison() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getAccidentYearlyComparison());
    }

    /**
     * 获取隐患年度对比统计
     */
    @GetMapping("/hiddenDangerYearlyComparison")
    @Operation(summary = "获取隐患年度对比统计")
    public R<com.hotwj.platform.workbench.domain.vo.HeadquartersYearlyComparisonVo> getHiddenDangerYearlyComparison() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getHiddenDangerYearlyComparison());
    }

    /**
     * 获取企业列表（含管理人员数）
     */
    @GetMapping("/companyList")
    @Operation(summary = "获取企业列表（含管理人员数）")
    public R<com.hotwj.platform.workbench.domain.vo.HeadquartersCompanyListVo> getCompanyList() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getCompanyList());
    }

    /**
     * 月度学习折线图
     */
    @GetMapping("/monthlyStudyTrend")
    @Operation(summary = "获取月度学习折线图数据")
    public R<MonthlyStudyTrendVo> getMonthlyStudyTrend(@RequestParam int year, @RequestParam int month) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getMonthlyStudyTrend(year, month));
    }

    /**
     * 违规统计柱状图（按月，默认统计本年）
     */
    @GetMapping("/violationMonthlyStats")
    @Operation(summary = "获取本年违规统计柱状图（按月）")
    public R<com.hotwj.platform.workbench.domain.vo.ViolationMonthlyBarVo> getViolationMonthlyStats(
            @RequestParam(required = false) Integer year
    ) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getViolationMonthlyStats(year));
    }

    /**
     * 隐患统计概览（总部端）
     */
    @GetMapping("/hiddenDanger/summary")
    @Operation(summary = "获取总部端隐患统计概览")
    public R<com.hotwj.platform.workbench.domain.vo.HeadquartersHiddenDangerSummaryVo> getHiddenDangerSummary(
            @RequestParam(required = false) String regionCode
    ) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getHeadquartersHiddenDangerSummary(regionCode));
    }

    /**
     * 区域企业统计（按行业分类）
     */
    @GetMapping("/regionCompanyStats")
    @Operation(summary = "获取区域企业统计")
    public R<RegionCompanyStatsVo> getRegionCompanyStats(@RequestParam String regionCode) {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getRegionCompanyStats(regionCode));
    }

    /**
     * 获取企业发展趋势统计
     */
    @GetMapping("/enterpriseTrendStats")
    @Operation(summary = "获取企业发展趋势统计")
    public R<com.hotwj.platform.workbench.domain.vo.HeadquartersEnterpriseTrendVo> getEnterpriseTrendStats() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getEnterpriseTrendStats());
    }

    /**
     * 获取车辆发展趋势统计
     */
    @GetMapping("/vehicleTrendStats")
    @Operation(summary = "获取车辆发展趋势统计")
    public R<com.hotwj.platform.workbench.domain.vo.HeadquartersVehicleTrendVo> getVehicleTrendStats() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getVehicleTrendStats());
    }

    /**
     * 获取从业人员发展趋势统计
     */
    @GetMapping("/personnelTrendStats")
    @Operation(summary = "获取从业人员发展趋势统计")
    public R<com.hotwj.platform.workbench.domain.vo.HeadquartersPersonnelTrendVo> getPersonnelTrendStats() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getPersonnelTrendStats());
    }

    @GetMapping("/squareInfo/pendingAuditCount")
    @Operation(summary = "获取广场信息审核中数量")
    public R<Long> getSquareInfoPendingAuditCount() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getSquareInfoPendingAuditCount());
    }

    @GetMapping("/complaintAdvice/pendingReplyCount")
    @Operation(summary = "获取投诉建议未回复数量")
    public R<Long> getComplaintAdvicePendingReplyCount() {
        LoginUser loginUser = LoginHelper.getLoginUser();
        boolean isHeadquarters = loginUser != null && ISysUserLoginPortService.HEADQUARTERS_PORT.equals(loginUser.getLoginPort());
        boolean isAdmin = LoginHelper.isSuperAdmin() || LoginHelper.isTenantAdmin();
        if (!isHeadquarters && !isAdmin) {
            return R.fail("非总部端用户无权访问");
        }
        return R.ok(headquartersWorkbenchService.getComplaintAdvicePendingReplyCount());
    }
}
