/*
 * Copyright [2022] [https://www.baiduc.com]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.baiduc.com
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.baiduc.com
 */
package vip.xiaonuo.hr.modular.baseemployeeinfo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.common.pojo.CommonValidList;
import vip.xiaonuo.hr.modular.baseemployeeinfo.entity.HrBaseEmployeeInfo;
import vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.CountPerson;
import vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.EoucationVo;
import vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.CountNum;
import vip.xiaonuo.hr.modular.baseemployeeinfo.entity.vo.PerNum;
import vip.xiaonuo.hr.modular.baseemployeeinfo.param.*;
import vip.xiaonuo.hr.modular.baseemployeeinfo.service.HrBaseEmployeeInfoService;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * 人员明细控制器
 *
 * @author 余
 * @date  2024/11/14 15:02
 */
@Api(tags = "人员明细控制器")
@ApiSupport(author = "SNOWY_TEAM", order = 1)
@RestController
@Validated
public class HrBaseEmployeeInfoController {

    @Resource
    private HrBaseEmployeeInfoService hrBaseEmployeeInfoService;

    @ApiOperationSupport(order = 1)
    @ApiOperation("获取人员明细分页")
    @GetMapping("/hr/baseemployeeinfo/page")
    public CommonResult<Page<HrBaseEmployeeInfo>> page(@RequestBody HrBaseEmployeeInfoPageParam hrBaseEmployeeInfoPageParam) {
        return CommonResult.data(hrBaseEmployeeInfoService.page(hrBaseEmployeeInfoPageParam));
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation("添加人员明细")
    @CommonLog("添加人员明细")
    @PostMapping("/hr/baseemployeeinfo/add")
    public CommonResult<String> add(@RequestBody @Valid HrBaseEmployeeInfoAddParam hrBaseEmployeeInfoAddParam) {
        hrBaseEmployeeInfoService.add(hrBaseEmployeeInfoAddParam);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation("编辑人员明细")
    @CommonLog("编辑人员明细")
    @PostMapping("/hr/baseemployeeinfo/edit")
    public CommonResult<String> edit(@RequestBody @Valid HrBaseEmployeeInfoEditParam hrBaseEmployeeInfoEditParam) {
        hrBaseEmployeeInfoService.edit(hrBaseEmployeeInfoEditParam);
        return CommonResult.ok();
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation("删除人员明细")
    @CommonLog("删除人员明细")
    @PostMapping("/hr/baseemployeeinfo/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   CommonValidList<HrBaseEmployeeInfoIdParam> hrBaseEmployeeInfoIdParamList) {
        hrBaseEmployeeInfoService.delete(hrBaseEmployeeInfoIdParamList);
        return CommonResult.ok();
    }


    @ApiOperation("获取人员明细详情")
    @PostMapping("/hr/baseemployeeinfo/detail")
    public CommonResult<HrBaseEmployeeInfo> detail(@Valid @RequestBody HrBaseEmployeeInfoIdParam hrBaseEmployeeInfoIdParam) {
        return CommonResult.data(hrBaseEmployeeInfoService.detail(hrBaseEmployeeInfoIdParam));
    }


    @ApiOperation("部门本科及以上学历")
    @PostMapping("/hr/baseemployeeinfo/listByEoucation")
    public CommonResult<List<EoucationVo>> listByEoucation(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.listByEoucation(employeeInfoEoucation));
    }

    @ApiOperation("数量统计及退休人员")
    @PostMapping("/hr/baseemployeeinfo/countPerson")
    public CommonResult<CountPerson> countPerson(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.countPerson(employeeInfoEoucation));
    }

    @ApiOperation("历史总人数等")
    @PostMapping("/hr/baseemployeeinfo/countNum")
    public CommonResult<CountNum> countNum(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        QueryWrapper<HrBaseEmployeeInfo> mapper = new QueryWrapper<>();
        CountNum countNum = new CountNum();

        long historyNum = hrBaseEmployeeInfoService.count(mapper.eq(StringUtils.isNotBlank(employeeInfoEoucation.getOrgId()),"ORG_ID", employeeInfoEoucation.getOrgId()));
        long jobNum = hrBaseEmployeeInfoService.count(mapper.eq("USER_TYPE",0));
        long maleNum = hrBaseEmployeeInfoService.count(mapper.eq("GENDER",0));

        countNum.setHistoryNum(historyNum);
        countNum.setMaleNum(maleNum);
        countNum.setFemaleNum(jobNum -maleNum);
        countNum.setJobNum(jobNum);
        return CommonResult.data(countNum);
    }

    @ApiOperation("部门人数统计")
    @PostMapping("/hr/baseemployeeinfo/listByDepart")
    public CommonResult<List<EoucationVo>> listByDepart(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.listByDepart(employeeInfoEoucation));
    }
    @ApiOperation("部门人数性别统计")
    @PostMapping("/hr/baseemployeeinfo/listByDepartGender")
    public CommonResult<List<EoucationVo>> listByDepartGender(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.listByDepartGender(employeeInfoEoucation));
    }

    @ApiOperation("年龄分布")
    @PostMapping("/hr/baseemployeeinfo/ageDis")
    public CommonResult<List<PerNum>> ageDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        String orgId = employeeInfoEoucation.getOrgId();
        long age22 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).between("AGE", 18,22));
        long age30 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).between("AGE", 23,30));
        long age40 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).between("AGE", 31,40));
        long age45 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).between("AGE", 41,45));
        long age60 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).between("AGE", 46,60));
        long age61 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).ge("AGE", 60));
        ArrayList<PerNum> perNums = new ArrayList<>();
        perNums.add(new PerNum("18-22岁",age22));
        perNums.add(new PerNum("23-30岁",age30));
        perNums.add(new PerNum("31-40岁",age40));
        perNums.add(new PerNum("41-45岁",age45));
        perNums.add(new PerNum("46-60岁",age60));
        perNums.add(new PerNum("60岁以上",age61));
        return CommonResult.data(perNums);
    }

    @ApiOperation("性别分布")
    @PostMapping("/hr/baseemployeeinfo/genderDis")
    public CommonResult<List<PerNum>> genderDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        String orgId = employeeInfoEoucation.getOrgId();
        long age22 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("GENDER", 0));
        long age30 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("GENDER", 1));
        ArrayList<PerNum> perNums = new ArrayList<>();
        perNums.add(new PerNum("男性",age22));
        perNums.add(new PerNum("女性",age30));
        return CommonResult.data(perNums);
    }

    @ApiOperation("婚姻状况")
    @PostMapping("/hr/baseemployeeinfo/marrDis")
    public CommonResult<List<PerNum>> marrDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        String orgId = employeeInfoEoucation.getOrgId();
        long age22 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("MARRIAGE", 0));
        long age30 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("MARRIAGE", 1));
        ArrayList<PerNum> perNums = new ArrayList<>();
        perNums.add(new PerNum("未婚",age22));
        perNums.add(new PerNum("已婚",age30));
        return CommonResult.data(perNums);
    }
    @ApiOperation("民族分布")
    @PostMapping("/hr/baseemployeeinfo/nationDis")
    public CommonResult<List<PerNum>> nationDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        String orgId = employeeInfoEoucation.getOrgId();
        long age22 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("NATION", "汉族"));
        long age30 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).ne("NATION", "汉族"));
        ArrayList<PerNum> perNums = new ArrayList<>();
        perNums.add(new PerNum("汉族",age22));
        perNums.add(new PerNum("其他",age30));
        return CommonResult.data(perNums);
    }

    @ApiOperation("学历分布")
    @PostMapping("/hr/baseemployeeinfo/educationDis")
    public CommonResult<List<PerNum>> educationDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        String orgId = employeeInfoEoucation.getOrgId();
        long age22 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("EDUCATION", 0));
        long age2 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("EDUCATION", 1));
        long age3 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("EDUCATION", 2));
        long age4 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("EDUCATION", 3));
        long age5 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("EDUCATION", 4));
        long age6 = hrBaseEmployeeInfoService.count(new QueryWrapper<HrBaseEmployeeInfo>().eq(StringUtils.isNotBlank(orgId),"ORG_ID", orgId).eq("USER_TYPE",0).eq("EDUCATION", 5));

        ArrayList<PerNum> perNums = new ArrayList<>();
        perNums.add(new PerNum("小学",age22));
        perNums.add(new PerNum("初中",age2));
        perNums.add(new PerNum("高中",age3));
        perNums.add(new PerNum("本科",age4));
        perNums.add(new PerNum("研究生",age5));
        perNums.add(new PerNum("博士研究生",age6));
        return CommonResult.data(perNums);
    }

    @ApiOperation("部门学历分布")
    @PostMapping("/hr/baseemployeeinfo/listByDepartGenderEducationDis")
    public CommonResult<List<EoucationVo>> listByDepartGenderEducationDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.listByDepartGenderEducationDis(employeeInfoEoucation));
    }

    @ApiOperation("生日分布")
    @PostMapping("/hr/baseemployeeinfo/birthDis")
    public CommonResult<List<PerNum>> birthDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.birthDis(employeeInfoEoucation));
    }

    @ApiOperation("司龄分布")
    @PostMapping("/hr/baseemployeeinfo/siLingDis")
    public CommonResult<List<PerNum>> siLingDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.siLingDis(employeeInfoEoucation));
    }

    @ApiOperation("工龄分布")
    @PostMapping("/hr/baseemployeeinfo/gongLingDis")
    public CommonResult<List<PerNum>> gongLingDis(@Valid @RequestBody EmployeeInfoEoucation employeeInfoEoucation) {
        return CommonResult.data(hrBaseEmployeeInfoService.gongLingDis(employeeInfoEoucation));
    }
}
