package com.ruoyi.postCheck.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.Arith;
import com.ruoyi.postCheck.domain.PostAssessmentRating;
import com.ruoyi.postCheck.domain.homePage.*;
import com.ruoyi.postCheck.service.IPostAssessmentRatingService;
import com.ruoyi.postCheck.service.IPostCheckedProblemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;










/**
 * 岗检问题表Controller
 *
 * @author csg
 * @date 2023-12-23
 */
@Api(tags = "1-首页")
@RestController
@RequestMapping("/postCheck/homePage")
public class PostCheckedHomePageController extends BaseController {
    @Autowired
    private IPostCheckedProblemService postCheckedProblemService;
    @Autowired
    private IPostAssessmentRatingService postAssessmentRatingService;
    /**
     * 1、责任制清单：post_list表
     * 2、检查出的问题：post_checked_problem表
     * 3、岗检数量-已查：post_plan t1 inner join post_plan_info t2 两张表连接，t2.status(岗检状态：1-未检；2-已检；)
     * 4、岗检数量-未查：post_plan t1 inner join post_plan_info t2 两张表连接，t2.status(岗检状态：1-未检；2-已检；)
     */
    @ApiOperation("1-总览")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "listCount", value = "责任制清单数量", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemCount", value = "检查出的问题数量", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "checkedPostCount", value = "岗检数量-已查", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "uncheckedPostCount", value = "岗检数量-未查", dataType = "Integer", dataTypeClass = Integer.class),
    })
    @GetMapping("/totalView")
    public AjaxResult totalView() {
        return success(postCheckedProblemService.totalView());
    }


    @ApiOperation("2-检查出的问题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "problemTotal", value = "总数量", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemComplete", value = "完整性数量", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemSuitable", value = "适宜性数量", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "problemEffective", value = "有效性数量", dataType = "Integer", dataTypeClass = Integer.class),

    })
    @GetMapping("/checkedProblem")
    public AjaxResult checkedProblem() {
//        检查出的问题：post_checked_problem表 按 problem_attribute-问题属性（1-完整问题；2-有效性问题；3-适宜性问题；） 分组 count(id)
        return success(postCheckedProblemService.checkedProblem());
    }


    @ApiOperation("3-人员数量")
    @GetMapping("/userTotal")
    public AjaxResult userTotal() {
        return success(postCheckedProblemService.userTotal());
    }


    @ApiOperation("4-岗检问题整改率（中间饼状图 统计的 岗检问题整改率 和 岗检部门整改率 tab 切换时前端自己计算，不需要提供接口）")
    @GetMapping("/problemRectification")
    public AjaxResult problemRectification() {
        return success(postCheckedProblemService.problemRectification());
    }


    @ApiOperation("5-岗检部门整改率（中间饼状图 统计的 岗检问题整改率 和 岗检部门整改率 tab 切换时前端自己计算，不需要提供接口）")
    @GetMapping("/deptProblemRectification")
    public AjaxResult deptProblemRectification() {
//        查询问题表：post_checked_problem
//        group by checked_dept_id 整改责任部门ID（被检查部门ID）
//        最好给前端定义一个对象，用Map也可以
        return success(postCheckedProblemService.deptProblemRectification());
    }


    @ApiOperation("6-岗检已查")
    @GetMapping("/postChecked")
    public AjaxResult postChecked() {
//        SELECT
//           t2."checked_dept_id" ,
//           t2.checked_post_id,
//           MIN(t2."status")  AS "status" ,
//           MAX (t2."status")  AS aa
//        FROM "post_plan" t1
//        INNER JOIN "post_plan_info" t2 ON t2."post_plan_id" = t1."id"
//        WHEN t1.status = 3 -- 状态：1-待审核；2-审核中；3-通过；4-驳回；
//        AND t2."status" = ? -- 岗检状态：1-未检；2-已检；
//        GROUP BY t2."checked_dept_id", t2."checked_post_id"
//        ORDER BY t2."checked_dept_id"

//        最后一列人数 left join “用户表”+“用户岗位中间表” on t2."checked_post_id" =  sys_user_post.post_id 进行 count(sys_users.id) as 人数
//        select * from "public"."sys_users";
//        select * from "public"."sys_user_post";

        return success(postCheckedProblemService.postChecked(1));
    }


    @ApiOperation("7-岗检未查")
    @GetMapping("/postUnChecked")
    public AjaxResult postUnChecked() {
//        SELECT
//           t2."checked_dept_id" ,
//           t2.checked_post_id,
//           MIN(t2."status")  AS "status" ,
//           MAX (t2."status")  AS aa
//        FROM "post_plan" t1
//        INNER JOIN "post_plan_info" t2 ON t2."post_plan_id" = t1."id"
//        WHEN t1.status = 3 -- 状态：1-待审核；2-审核中；3-通过；4-驳回；
//        AND t2."status" = ? -- 岗检状态：1-未检；2-已检；
//        GROUP BY t2."checked_dept_id", t2."checked_post_id"
//        ORDER BY t2."checked_dept_id"

//        最后一列人数 left join “用户表”+“用户岗位中间表” on t2."checked_post_id" =  sys_user_post.post_id 进行 count(sys_users.id) as 人数
//        select * from "public"."sys_users";
//        select * from "public"."sys_user_post";

        return success(postCheckedProblemService.postChecked(2));
    }


    @ApiOperation("8-评比排名")
    @GetMapping("/assessmentRating")
    public AjaxResult assessmentRating() {
//        岗位问题表结合岗检规则实时计算岗检扣分情况
//        select * from "public"."post_checked_problem";

//        互评表（职能互评得分/生产日常得分）
//        select * from "public"."post_assessment_rating_other";
//        select * from "public"."post_assessment_rating_other_info";

//      部门得分 = （岗检分数 * 0.5 + 互评分数 * 0.5  ）
//      根据部门得分进行排名计算
        PostAssessmentRating postAssessmentRating1 = new PostAssessmentRating();
        postAssessmentRating1.setType(1);
        List<PostAssessmentRating> postAssessmentRatings1 = postAssessmentRatingService.selectPostAssessmentRatingList(postAssessmentRating1);
        PostAssessmentRating postAssessmentRating2 = new PostAssessmentRating();
        postAssessmentRating2.setType(2);
        List<PostAssessmentRating> postAssessmentRatings2 = postAssessmentRatingService.selectPostAssessmentRatingList(postAssessmentRating2);
        postAssessmentRatings1.addAll(postAssessmentRatings2);
        List<AssessmentRatingView> dataList = new ArrayList<>();
        postAssessmentRatings1.forEach(x->{
            AssessmentRatingView data1 = new AssessmentRatingView();
            data1.setDeptId(x.getDeptId());
            data1.setDeptName(x.getDeptIdStr());
            data1.setPostCount(postCheckedProblemService.getPostCountByDeptId(x.getDeptId()));
            data1.setScore(x.getDepartmentScore());
            dataList.add(data1);
        });
        List<AssessmentRatingView> collect = dataList.stream().sorted(Comparator.comparing(AssessmentRatingView::getScore).reversed()).collect(Collectors.toList());
        Integer i = 1;
        for (AssessmentRatingView x : collect) {
            x.setRating(i);
            i++;
        }
        return success(collect);
    }
}
