package jnpf.personnels.controller.app.roster;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivatePositionForAppService;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.position.app.FtbCultivatePositionForAppNewDTO;
import jnpf.model.cultivate.vo.identify.IdentifyApplyInfoApiVo;
import jnpf.model.cultivate.vo.position.app.FtbSubordinateLearningCoursesVO;
import jnpf.model.cultivate.vo.promotion.FtbCultivatePromotionWithPersonelVO;
import jnpf.model.personnels.dto.staff.employment.FtbPersonnelsStaffEmploymentApplyDto;
import jnpf.model.personnels.dto.staff.growth.FtbPersonnelsStaffGrowthLogDto;
import jnpf.model.personnels.dto.staff.roster.CheckRosterDeleteVo;
import jnpf.model.personnels.dto.staff.roster.EditUserBaseInfoDto;
import jnpf.model.personnels.dto.staff.roster.FtbPersonnelsStaffRosterDto;
import jnpf.model.personnels.dto.staff.roster.StaffHomeDto;
import jnpf.model.personnels.dto.staff.roster.WorkerStatisticsDto;
import jnpf.model.personnels.dto.staff.salarylog.FtbPersonnelsStaffSalaryChangeLogDto;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.model.personnels.req.roster.AppStaffRosterListReq;
import jnpf.permission.dto.QueryUserListDTO;
import jnpf.permission.vo.v2.user.UserPageListVO;
import jnpf.personnels.service.FtbPersonnelsStaffGrowthLogService;
import jnpf.personnels.service.FtbPersonnelsStaffRosterService;
import jnpf.personnels.service.FtbPersonnelsStaffSalaryChangeLogService;
import jnpf.util.FtbUtil;
import jnpf.util.UserProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * app员工花名册模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/app/personnels-staff-roster")
public class FtbPersonnelsStaffRosterAppController {

    @Autowired
    private FtbPersonnelsStaffRosterService staffRosterService;

    @Autowired
    private FtbPersonnelsStaffGrowthLogService growthLogService;

    @Autowired
    private FtbPersonnelsStaffSalaryChangeLogService staffSalaryChangeLogService;

    @Resource
    FtbCultivatePositionForAppService positionForAppService;

    /**
     * 花名册查询列表
     *
     * @param req
     * @return {@link ActionResult}<{@link FtbPersonnelsStaffEmploymentApplyDto}>
     */
    @PostMapping("/query-list")
    public ActionResult<PageListVO<FtbPersonnelsStaffRosterDto>> pageLists(@RequestBody @Validated AppStaffRosterListReq req) {
        PageInfo<FtbPersonnelsStaffRosterDto> pageVo = staffRosterService.getAppPageList(req);
        return ActionResult.page(pageVo.getList(), FtbUtil.getPagination(pageVo));
    }


    /**
     * 花名册员工数量统计
     *
     * @return
     */
    @GetMapping("/worker-number/statistics")
    public ActionResult<WorkerStatisticsDto> statisticsWorkerNumber() {
        return ActionResult.success(staffRosterService.statisticsWorkerNumber());
    }


    /**
     * 把员工从花名册中删除
     *
     * @param id 主键id，必传
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult<CheckRosterDeleteVo> delete(@PathVariable("id") String id) {
        //检测花名册是否有蓄电任务 值班任务 审批流程
        staffRosterService.deleteBatchData(List.of(id));
        return ActionResult.success();
    }


    /**
     * 获取员工花名册详情
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public ActionResult<FtbPersonnelsStaffRosterDto> get(@PathVariable("id") String id) {
        FtbPersonnelsStaffRosterDto info = staffRosterService.getInfo(id);
        return ActionResult.success(info);
    }

    /**
     * 获取员工花名册详情（参数）
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/getInfo")
    public ActionResult<StaffHomeDto> getInfo(@RequestParam("userId") String userId) {
        StaffHomeDto info = staffRosterService.queryWorkerHomeDetail(userId);
        return ActionResult.success(info);
    }


    /**
     * 查询员工主页信息
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/worker-home-detail/{userId}")
    public ActionResult<StaffHomeDto> queryWorkerHomeDetail(@PathVariable("userId") String userId) {
        StaffHomeDto info = staffRosterService.queryWorkerHomeDetail(userId);
        return ActionResult.success(info);
    }
    /**
     * 查询员工主页信息
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/worker-home-detail")
    public ActionResult<StaffHomeDto> queryWorkerHomeDetailWithId(@RequestParam("userId") String userId) {
        StaffHomeDto info = staffRosterService.queryWorkerHomeDetail(userId);
        return ActionResult.success(info);
    }


    /**
     * 查询员工成长列表
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/query-growth-list/{userId}")
    public ActionResult<List<FtbPersonnelsStaffGrowthLogDto>> queryGrowthList(@PathVariable("userId") String userId) {
        List<FtbPersonnelsStaffGrowthLogDto> list = growthLogService.queryAll(userId);
        return ActionResult.success(list);
    }


    /**
     * 查询员工薪酬变化列表
     *
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/query-salary-change-list/{userId}")
    public ActionResult<List<FtbPersonnelsStaffSalaryChangeLogDto>> querySalaryChangeList(@PathVariable("userId") String userId) {
        List<FtbPersonnelsStaffSalaryChangeLogDto> list = staffSalaryChangeLogService.queryAll(userId);
        return ActionResult.success(list);
    }

    /**
     * 检测当前登录用户是不是档案管理员
     *
     * @return {@link ActionResult}<{@link String}>
     */
    @GetMapping("/check-my-is-archive-manager")
    public ActionResult<Boolean> checkMyIsArchiveManager() {
        Boolean isManager = staffRosterService.checkCurrLoginUserArchiveManager();
        return ActionResult.success("成功", isManager);
    }

    /**
     * 检测当前花名册处于流程中【调岗、转正、离职】
     *
     * @param id 花名册ID
     * @return 0、无流程  ,1转正 2,调岗, 3离职, 4晋升5调店 6降职
     * 于
     */
    @GetMapping("/check-roster-run-task/{id}")
    public ActionResult<Integer> checksItInTheReviewProcess(@PathVariable("id") String id) {
        String loginUserId = UserProvider.getLoginUserId();
        Integer ty = staffRosterService.checksItInTheReviewProcess(id);
        return ActionResult.success("成功", ty);
    }
    /**
     * 检测当前花名册处于流程中【调岗、转正、离职】
     *
     * @param id 花名册ID
     * @return 0、无流程  ,1转正 2,调岗, 3离职, 4晋升5调店 6降职
     * 于
     */
    @GetMapping("/check-roster-run-task")
    public ActionResult<Integer> checksItInTheReviewProcessWithId(@RequestParam("id") String id) {
        Integer ty = staffRosterService.checksItInTheReviewProcess(id);
        return ActionResult.success("成功", ty);
    }


    /**
     * 查询直属主管
     *
     * @param dto
     * @return
     */
    @GetMapping("/queryAllUserForOrgAndStatus")
    public ActionResult<List<UserPageListVO>> getAllUser(QueryUserListDTO dto) {
        List<UserPageListVO> list = staffRosterService.queryAllUserForOrgAndStatus(dto);
        return ActionResult.success("成功", list);
    }

    /**
     * 查看培训数据
     * 培训v1.1
     *
     * @param postId 岗位Id
     * @param userId 用户id
     */
    @GetMapping("/queryTrainData")
    public ActionResult<FtbCultivatePromotionWithPersonelVO> queryTrainData(@RequestParam("userId") String userId,
                                                                            @RequestParam("postId") String postId) {
        FtbCultivatePromotionWithPersonelVO info = staffRosterService.queryTrainData(userId, postId);
        return ActionResult.success("成功", info);
    }

    /**
     * 查看课程培训数据(培训v1.1)
     */
    @GetMapping("/queryCourseTrainDataDetail")
    public ActionResult<PageListVO<FtbSubordinateLearningCoursesVO>> queryCourseTrainDataDetail(FtbCultivatePositionForAppNewDTO dto,
                                                                                                CultivatePage page) {
        Page<FtbSubordinateLearningCoursesVO> result = page.coverCultivatePage();
        result = positionForAppService.subordinateLearningCourses(dto, result);
        return ActionResult.success(CultivatePage.coverPageList(result));
    }

    /**
     * 查看鉴定培训数据(培训v1.1)
     */
    @GetMapping("/queryIdentityTrainDataDetail")
    public ActionResult<PageListVO<IdentifyApplyInfoApiVo>> queryIdentityTrainDataDetail(FtbCultivatePositionForAppNewDTO dto,
                                                                                         CultivatePage page) {
        PageListVO<IdentifyApplyInfoApiVo> result = positionForAppService.queryIdentityTrainDataDetail(dto, page);
        return ActionResult.success(result);
    }

    /**
     * 修改用户基本信息[头像 姓名 花名]
     *
     * @return
     */

    @PutMapping("/updateBaseUserInfo")
    public ActionResult<Boolean> updateBaseUserInfo(@RequestBody EditUserBaseInfoDto req) {
        String loginUserId = UserProvider.getLoginUserId();
        if (StringUtils.isEmpty(req.getName()) && StringUtils.isEmpty(req.getHeadLogo()) && StringUtils.isEmpty(req.getFlowerName())) {
            return ActionResult.fail("用户信息不能为空");
        }
        if(StringUtils.isNotEmpty(req.getName())){
            Boolean aBoolean = staffRosterService.canUpdateRealName();
            if(aBoolean==false){
                throw new RuntimeException("对不起,当前企业不支持修改用户姓名");
            }
        }

        staffRosterService.updateBaseUserInfo(loginUserId, req);
        return ActionResult.success("操作成功", true);
    }


    /**
     * 修改用户离职证明是否签署
     * @param userId 用户ID
     * @param status 签署状态 0-未签署 1-已签署
     * @return
     */
    @PutMapping("/updateSignSeparation/{userId}/{status}")
    public ActionResult updateSignSeparation(@PathVariable("userId") String userId,@PathVariable("status") Integer status) {
        staffRosterService.updateSignSeparation(userId, status);
        return ActionResult.success();
    }

    /**
     * 查询用户离职证明是否签署
     * @param userId 用户ID
     * @return
     */
    @GetMapping("/querySignSeparation/{userId}")
    public ActionResult<Integer> querySignSeparation(@PathVariable("userId") String userId) {
        FtbPersonnelsStaffRoster roster = staffRosterService.queryRosterInfoByUserId(userId);
        if(null==roster){
            throw new RuntimeException("用户信息不存在");
        }
        return ActionResult.success(roster.getIsSignSeparation());
    }


    /**
     * 查询是否能够修改用户的真实姓名(true-可以修改,false-不可以修改)
     * @return  true 可以修改,false 不可以修改
     */
    @GetMapping("/queryCanUpdateRealName")
    public ActionResult<Boolean> queryCanUpdateRealName() {
        Boolean check = staffRosterService.canUpdateRealName();
        return ActionResult.success("查询成功", check);
    }
}
