package jnpf.cultivate.controller.web.position;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.ActionResult;
import jnpf.base.UserInfo;
import jnpf.base.entity.ModuleEntity;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivatePositionService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.position.*;
import jnpf.model.cultivate.dto.position.web.FtbCultivatePositionJobLearnCourseCertificateDTO;
import jnpf.model.cultivate.dto.position.web.FtbCultivatePositionJobLearnCourseStateSwitchDTO;
import jnpf.model.cultivate.dto.position.web.FtbJobLearnCourseRetakeCertificateDTO;
import jnpf.model.cultivate.dto.statistics.CultivatePermissionModuleParam;
import jnpf.model.cultivate.vo.common.InnerPowerPositionVO;
import jnpf.model.cultivate.vo.position.*;
import jnpf.permission.eum.v2.OrganizeCategoryEnums;
import jnpf.permission.model.BaseListIdDTO;
import jnpf.permission.vo.v2.organzie.OrganizeGeneralDetailVO;
import jnpf.permission.vo.v2.user.UserBoundVO;
import jnpf.util.UserProvider;
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.ArrayList;
import java.util.List;

/**
 * web岗位学习模块
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/position")
public class FtbCultivatePositionController {

    @Resource
    private FtbCultivatePositionService ftbCultivatePositionService;

    @Autowired
    private UserApiV2Util userApiV2Util;

    /**
     * 添加岗位学习，返回岗位学习主键id，用于操作岗位
     *
     * @param ftbCultivatePositionJobLearnDTO
     * @return
     */
    @PostMapping("/add-job-learning")
    public ActionResult<String> addJobLearning(@RequestBody @Validated FtbCultivatePositionJobLearnDTO ftbCultivatePositionJobLearnDTO) {
        String id = ftbCultivatePositionService.addJobLearning(ftbCultivatePositionJobLearnDTO);
        return ActionResult.success(id);
    }

    /**
     * 岗位学习上下架
     *
     * @param ftbCultivateShelvesDTO
     * @return {@link ActionResult}
     */
    @PostMapping("/upper-lower-shelves")
    public ActionResult upperLower(@Validated @RequestBody FtbCultivatePositionShelvesDTO ftbCultivateShelvesDTO) {
        ftbCultivatePositionService.upperLower(ftbCultivateShelvesDTO);
        return ActionResult.success();
    }

    /**
     * 添加岗位学习考试
     *
     * @param ftbCultivatePositionJobLearnExamDTO
     * @return {@link ActionResult}
     */
    @PostMapping("/job-learning-exam")
    public ActionResult addJobLearningExam(@Validated @RequestBody FtbCultivatePositionJobLearnExamDTO ftbCultivatePositionJobLearnExamDTO) {
        ftbCultivatePositionService.addJobLearningExam(ftbCultivatePositionJobLearnExamDTO);
        return ActionResult.success("操作成功");
    }

    /**
     * 添加岗位学习实操鉴定
     *
     * @param ftbCultivatePositionLearnPracticalDTO
     * @return {@link ActionResult}
     */
    @PostMapping("/post-learning-practical-appraisal")
    public ActionResult addPostLearningPracticalAppraisal(@Validated @RequestBody FtbCultivatePositionLearnPracticalDTO ftbCultivatePositionLearnPracticalDTO) {
        ftbCultivatePositionService.addPostLearningPracticalAppraisal(ftbCultivatePositionLearnPracticalDTO);
        return ActionResult.success("操作成功");
    }

    /**
     * 添加岗位学习课程
     *
     * @param ftbCultivatePositionLearnPracticalDTO
     * @return {@link ActionResult}
     */
    @PostMapping("/job-learning-courses")
    public ActionResult addJobLearningCourses(@Validated @RequestBody FtbCultivatePositionLearingCourselDTO ftbCultivatePositionLearnPracticalDTO) {
        ftbCultivatePositionService.addJobLearningCourses(ftbCultivatePositionLearnPracticalDTO);
        return ActionResult.success("操作成功");
    }


    /**
     * 添加岗位学习课程时，课程列表选择
     *
     * @param ftbCultivatePositionCoursePageDTO
     * @return {@link ActionResult}
     */
    @GetMapping("/job-learn-course-list")
    public ActionResult<List<FtbCultivatePositionJobLearnCourseVO>> jobLearningCourseList(FtbCultivatePositionCoursePageDTO ftbCultivatePositionCoursePageDTO) {
        ftbCultivatePositionCoursePageDTO.setLabel(2);
        List<FtbCultivatePositionJobLearnCourseVO> result = ftbCultivatePositionService.jobLearningCourseList(ftbCultivatePositionCoursePageDTO);
        return ActionResult.success(result);
    }

    /**
     * 岗位学习中课程列表页
     *
     * @param ftbCultivatePositionCourseLevelPageDTO
     * @param cultivatePage
     * @return {@link ActionResult}<{@link List}<{@link FtbCultivatePositionJobLearnCourseVO}>>
     */
    @GetMapping("/job-level-course-list")
    public ActionResult<PageListVO<FtbCultivatePositionLearnLevelVO>> jobLevelCourseList(CultivatePage cultivatePage,
                                                                                         FtbCultivatePositionCourseLevelPageDTO ftbCultivatePositionCourseLevelPageDTO) {
        Page<FtbCultivatePositionLearnLevelVO> page = cultivatePage.coverCultivatePage("a.F_Id");
        page = ftbCultivatePositionService.jobLevelCourseList(page, ftbCultivatePositionCourseLevelPageDTO);
        PageListVO<FtbCultivatePositionLearnLevelVO> result = CultivatePage.coverPageList(page);
        return ActionResult.success(result);
    }

    /**
     * 岗位学习课程-下拉列表
     *
     * @param ftbCourseDropDownListDTO
     * @return {@link ActionResult}
     */
    @GetMapping("/course-drop-down-list")
    public ActionResult<FtbPositionCourseDropDownListVO> courseDropDownList(FtbCourseDropDownListDTO ftbCourseDropDownListDTO) {
        FtbPositionCourseDropDownListVO ftbPositionCourseDropDownListVO = ftbCultivatePositionService.courseDropDownList(ftbCourseDropDownListDTO);
        return ActionResult.success(ftbPositionCourseDropDownListVO);
    }

    /**
     * 岗位学习中课程列表-添加课程考试
     *
     * @param ftbStudyCourseExamAddDTO
     * @return {@link ActionResult}
     */
    @PostMapping("/study-course-exam-added")
    public ActionResult studyCourseExamAdded(@Validated @RequestBody FtbStudyCourseExamAddDTO ftbStudyCourseExamAddDTO) {
        ftbCultivatePositionService.studyCourseExamAdded(ftbStudyCourseExamAddDTO);
        return ActionResult.success();
    }

    /**
     * 岗位学习中课程列表-添加课程鉴定
     *
     * @param ftbStudyCourseIdentityAddDTO
     * @return {@link ActionResult}
     */
    @PostMapping("/study-course-identity-added")
    public ActionResult studyCourseIdentityAdded(@Validated @RequestBody FtbStudyCourseIdentityAddDTO ftbStudyCourseIdentityAddDTO) {
        ftbCultivatePositionService.studyCourseIdentityAdded(ftbStudyCourseIdentityAddDTO);
        return ActionResult.success();
    }

    /**
     * 岗位学习中课程列表-课程考试删除接口
     *
     * @param courseExamId 岗位学习课程考试关联主键id，必填
     * @return 删除结果
     */
    @DeleteMapping("/course-exam-deletion/{id}")
    public ActionResult courseExamDeletion(@PathVariable("id") String courseExamId) {
        ftbCultivatePositionService.courseExamDeletion(courseExamId);
        return ActionResult.success();
    }

    /**
     * 岗位学习中课程列表-课程鉴定删除接口
     *
     * @param courseIdentityId 岗位学习课程鉴定关联主键id
     * @return 删除结果
     */
    @DeleteMapping("/course-identity-deletion/{id}")
    public ActionResult courseIdentityDeletion(@PathVariable("id") String courseIdentityId) {
        ftbCultivatePositionService.courseIdentityDeletion(courseIdentityId);
        return ActionResult.success();
    }

    /**
     * 岗位学习中课程列表-删除岗位学习课程
     *
     * @param id 岗位学习课程主键id，必传
     * @return {@link ActionResult}
     */
    @DeleteMapping("/deletion-job-learning-courses")
    public ActionResult deletionOfOnTheJobLearningCourses(String id) {
        ftbCultivatePositionService.deletionOfOnTheJobLearningCourses(id);
        return ActionResult.success();
    }

    /**
     * 岗位学习课程列表-重选考试
     *
     * @return {@link ActionResult}
     */
    @PutMapping("/retake-exam")
    public ActionResult retakeExam(@Validated @RequestBody FtbRetakeExamDTO ftbRetakeExamDTO) {
        ftbCultivatePositionService.retakeExam(ftbRetakeExamDTO);
        return ActionResult.success();
    }

    /**
     * 岗位学习课程列表-重选鉴定
     *
     * @return {@link ActionResult}
     */
    @PutMapping("/reselection-appraisal")
    public ActionResult reselectionAppraisal(@Validated @RequestBody FtbReselectionAppraisalDTO ftbReselectionAppraisalDTO) {
        ftbCultivatePositionService.reselectionAppraisal(ftbReselectionAppraisalDTO);
        return ActionResult.success();
    }


    /**
     * 岗位学习管理-岗位学习分页列表
     *
     * @param ftbJobLearningPaginDTO
     * @return {@link ActionResult}<{@link PageListVO}<{@link FtbJobLearningPaginatedVO}>>
     */
    @GetMapping("/job-learning-paginated-list")
    public ActionResult<PageListVO<FtbJobLearningPaginatedVO>> jobLearningPaginatedList(CultivatePage cultivatePage, FtbJobLearningPaginDTO ftbJobLearningPaginDTO) {
        Page<FtbJobLearningPaginatedVO> page = cultivatePage.coverCultivatePage("F_Id");
        page = ftbCultivatePositionService.jobLearningPaginatedList(page, ftbJobLearningPaginDTO);
        PageListVO<FtbJobLearningPaginatedVO> result = CultivatePage.coverPageList(page);
        return ActionResult.success(result);
    }

    /**
     * 岗位学习管理-删除岗位学习
     *
     * @param id 岗位学习主键id(必传)，二次删除判断，通过列表中职等数、试卷数、鉴定数、课程数、鉴定数等数量任意是否大于0，弹出内容提示框
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete-job-learning/{id}/{flag}")
    public ActionResult deletionOfJobLearning(@PathVariable("id") String id) {
        if (StrUtil.isBlank(id)) {
            throw new IllegalArgumentException("参数错误");
        }
        return ftbCultivatePositionService.deletionOfJobLearning(id);
    }

    /**
     * 岗位学习配置总览
     *
     * @param postLearnId 岗位学习主键ID(必传)
     * @param postId      岗位主键ID(必传)
     * @return {@link ActionResult}
     */
    @GetMapping("/overview-job-learning-configuration")
    public ActionResult<FtbJobLearningConfigurationVO> overviewOfJobLearningConfiguration(String postLearnId, String postId) {
        FtbJobLearningConfigurationVO ftbJobLearningConfigurationVO = ftbCultivatePositionService.overviewOfJobLearningConfiguration(postLearnId, postId);
        return ActionResult.success(ftbJobLearningConfigurationVO);
    }

    /**
     * 岗位学习，根据岗位ID和岗位学习ID，获取实操鉴定和考试及证书
     *
     * @param postLearnId 岗位学习主键ID(必传)
     * @param postIdID    岗位ID(必传)
     * @return {@link ActionResult}
     */
    @GetMapping("/job-learning-exam-appraisal")
    public ActionResult<FtbJobLearningExamAppraisalVO> jobLearningExamAppraisal(String postLearnId, String postIdID) {
        FtbJobLearningExamAppraisalVO ftbJobLearningExamAppraisalVO = ftbCultivatePositionService.jobLearningExamAppraisal(postLearnId, postIdID);
        return ActionResult.success(ftbJobLearningExamAppraisalVO);
    }

    /**
     * 岗位学习-考试删除
     *
     * @param postLearnId 岗位学习主键ID(必传)
     * @param postRankId  岗位ID(必传)
     * @return {@link ActionResult}
     */
    @DeleteMapping("/post-study-exam-deleted")
    public ActionResult postStudyExamDeleted(String postLearnId, String postRankId) {
        ftbCultivatePositionService.postStudyExamDeleted(postLearnId, postRankId);
        return ActionResult.success();
    }

    /**
     * 岗位学习-实操鉴定删除
     *
     * @param postLearnId 岗位学习主键ID(必传)
     * @param postRankId  岗位ID(必传)
     * @return {@link ActionResult}
     */
    @DeleteMapping("/job-learning-appraisal-delete")
    public ActionResult jobLearningPracticalAppraisalDelete(String postLearnId, String postRankId) {
        ftbCultivatePositionService.jobLearningPracticalAppraisalDelete(postLearnId, postRankId);
        return ActionResult.success();
    }

    /**
     * 成员管理模块调用此接口-删除该员工绑定职等对应的岗位学习
     *
     * @param id     岗位id（必填）
     * @param userId 用户id(必填)
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete-member-grade/{id}")
    public ActionResult deleteMemberGrade(@PathVariable("id") String id, String userId) {
        ftbCultivatePositionService.deleteMemberGrade(id, userId);
        return ActionResult.success();
    }

    /**
     * 获取绑定岗位的人数
     *
     * @param postionId 岗位主键id(必传)
     * @return {@link ActionResult}
     */
    @GetMapping("/get-people-bound-to-the-position")
    public ActionResult<Integer> getTheNumberOfPeopleBoundToThePosition(String postionId) {
        // 获取该岗位下所有人
        List<UserBoundVO> userListForPositions = userApiV2Util.getUserListForPositions(List.of(postionId), null);
        return ActionResult.success(userListForPositions.size());
    }

    /**
     * 添加岗位学习证书
     *
     * @param certificateDTO DTO 证书
     * @return {@link ActionResult }
     */
    @PostMapping("/on-the-job-learning-certificate")
    public ActionResult onTheJobLearningCertificate(@Validated @RequestBody FtbCultivatePositionJobLearnCertificateDTO certificateDTO) {
        ftbCultivatePositionService.onTheJobLearningCertificate(certificateDTO);
        return ActionResult.success();
    }

    /**
     * 岗位学习-证书删除
     *
     * @param postLearnId 岗位学习主键ID(必传)
     * @param postRankId  岗位ID(必传)
     * @return {@link ActionResult}
     */
    @DeleteMapping("/certificate-deletion")
    public ActionResult certificateDeletion(String postLearnId, String postRankId) {
        ftbCultivatePositionService.certificateDeletion(postLearnId, postRankId);
        return ActionResult.success();
    }


    /**
     * 添加岗位学习课程证书
     *
     * @param certificateDTO DTO 证书
     * @return {@link ActionResult }
     */
    @PostMapping("/add-job-learning-course-certificate")
    public ActionResult addJobLearningCourseCertificate(@Validated @RequestBody FtbCultivatePositionJobLearnCourseCertificateDTO certificateDTO) {
        ftbCultivatePositionService.addJobLearningCourseCertificate(certificateDTO);
        return ActionResult.success();
    }

    /**
     * 岗位学习-课程证书删除
     *
     * @param postLearnId 岗位学习主键ID(必传)
     * @param courseId    课程ID(必传)
     * @return {@link ActionResult}
     */
    @DeleteMapping("/certificate-course-deletion/{postLearnId}/{courseId}")
    public ActionResult certificateCourseDeletion(@PathVariable("postLearnId") String postLearnId, @PathVariable("courseId") String courseId) {
        ftbCultivatePositionService.certificateCourseDeletion(postLearnId, courseId);
        return ActionResult.success();
    }

    /**
     * 更改课程选修和必修切换
     *
     * @return {@link ActionResult }
     */
    @PostMapping("/switch-compulsory-courses")
    public ActionResult changeCourseElectiveAndRequiredSwitching(@Validated @RequestBody FtbCultivatePositionJobLearnCourseStateSwitchDTO stateSwitchDTO) {
        ftbCultivatePositionService.changeCourseElectiveAndRequiredSwitching(stateSwitchDTO);
        return ActionResult.success();
    }

    /**
     * 重选课程证书
     *
     * @return {@link ActionResult }
     */
    @PutMapping("/course-retake-certificate")
    public ActionResult courseRetakeCertificate(@Validated @RequestBody FtbJobLearnCourseRetakeCertificateDTO certificateDTO) {
        ftbCultivatePositionService.courseRetakeCertificate(certificateDTO);
        return ActionResult.success();
    }


    /**
     * 根据用户ids获取用户信息,返回map
     *
     * @param dto
     * @return
     */
    @PostMapping("/get-userinfo-for-user-ids")
    public ActionResult<List<UserBoundVO>> getUserInfoForUserIds(@Validated @RequestBody BaseListIdDTO dto) {
        // 获取该岗位下所有人
        return ActionResult.success(userApiV2Util.getUserPrimaryBoundBatchReturnList(dto.getIds(), null));
    }


    /**
     * 查询所有的组织、门店、班组信息列表
     *
     * @return
     */
    @GetMapping("/get-all-org-store-team")
    public ActionResult<List<OrganizeGeneralDetailVO>> getAllOrg() {
        return ActionResult.success(userApiV2Util.getOrgListByWhere("", new ArrayList<>(), new ArrayList<>(), null));
    }


    /**
     * 获取当前登录的用户信息
     *
     * @return {@link ActionResult }
     */
    @GetMapping("/get-curr-login-user-info")
    public ActionResult<UserBoundVO> getCurrLoginUserInfo() {
        return ActionResult.success(userApiV2Util.getUserPrimaryBoundOne(userApiV2Util.getCurrentLoginUserId(), null));
    }

    /**
     * 获取当前登录的数据权限类型
     *
     * @return 0-全部  1-所在组织和下级组织员工   2-所在组织员工  3-仅下属  4-指定组织  -1-无效权限信息
     */
    @GetMapping("/get-curr-login-user-permission-type")
    public ActionResult<Integer> getCurrLoginUserPermissionType(CultivatePermissionModuleParam dto) {
        return ActionResult.success(userApiV2Util.getPermissionModuleType());
    }


    @GetMapping("/get-curr-login-user-org-list")
    public ActionResult<List<OrganizeGeneralDetailVO>> getCurrLoginUserOrgList(CultivatePermissionModuleParam dto) {
        List<OrganizeGeneralDetailVO> orgListForCurrUser = userApiV2Util.getOrgListForCurrUser(
                List.of(OrganizeCategoryEnums.COMPANY, OrganizeCategoryEnums.STORE, OrganizeCategoryEnums.DEPARTMENT, OrganizeCategoryEnums.TEAM),null);
        return ActionResult.success(orgListForCurrUser);
    }

    /**
     * 获取当前登录用户有权限的岗位ID列表
     *
     * @return
     */
    @GetMapping("/get-curr-login-user-has-permission-position-id")
    public InnerPowerPositionVO getCurrLoginUserHasPermissionPositionId(@RequestParam(name = "permissionModule", defaultValue = "") String permissionModule) {
        return userApiV2Util.getPositionListForCurrUser(null);
    }


}
