package jnpf.cultivate.v2.controller.app.position;

import cn.hutool.core.collection.CollUtil;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.cultivate.v2.service.V2CultivatePositionService;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.v2.course.vo.app.AppCourseSimpleVo;
import jnpf.model.cultivate.v2.course.vo.app.OtherPositionCourseCountVo;
import jnpf.model.cultivate.v2.course.vo.app.PositionLearningCourseVo;
import jnpf.model.cultivate.v2.position.req.V2CultivatePositionCourseForAppReq;
import jnpf.model.cultivate.v2.position.req.V2OtherCultivatePositionCourseForAppReq;
import jnpf.model.cultivate.v2.position.vo.AppCultivatePositionDetailVo;
import jnpf.model.cultivate.v2.position.vo.CultivatePositionSimpleVo;
import jnpf.model.cultivate.v2.position.vo.CultivateSimpleUserInfoVo;
import jnpf.model.cultivate.v2.position.vo.V2CultivatePositionDetailForApp;
import jnpf.permission.vo.v2.user.UserPageListVO;
import jnpf.util.UploaderUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.ArrayList;
import java.util.List;

/**
 * app岗位学习模块
 *
 * @author xgl
 */
@RestController
@RequestMapping("/v2/app/position")
public class V2CultivatePositionAppController {

    @Autowired
    private V2CultivatePositionService v2CultivatePositionService;

    @Autowired
    private UserApiV2Util userApiV2Util;


    /**
     * 岗位学习本岗课程列表
     *
     * @param req 请求参数
     * @return 岗位学习课程列表
     */
    @GetMapping("/position-course-list")
    public ActionResult<PositionLearningCourseVo> positionCourseLists(V2CultivatePositionCourseForAppReq req) {
        return ActionResult.success(v2CultivatePositionService.positionCourseLists(req));
    }

    /**
     * 他岗课程岗位列表
     */
    @GetMapping("/other-position-list")
    public ActionResult<List<CultivatePositionSimpleVo>> otherPositionList(V2OtherCultivatePositionCourseForAppReq req) {
        return ActionResult.success("成功", v2CultivatePositionService.otherPositionList(req));
    }

    /**
     * 他岗课程课程列表
     */
    @GetMapping("/other-position-course-list")
    public ActionResult<PageListVO<AppCourseSimpleVo>> otherPositionCourseList(V2OtherCultivatePositionCourseForAppReq dto, CultivatePage page) {
        return ActionResult.success("成功", UserApiV2Util.buildPage(v2CultivatePositionService.otherPositionCourseList(dto, page), page.getPageSize(), page.getCurrentPage()));
    }

    /**
     * 他岗课程课程（总课程/已经完成课程）
     */
    @GetMapping("/other-position-course-count")
    public ActionResult<OtherPositionCourseCountVo> otherPositionCourseCount(V2OtherCultivatePositionCourseForAppReq dto) {
        return ActionResult.success("成功", v2CultivatePositionService.otherPositionCourseCount(dto));
    }


    /**
     * 查询岗位学习详情（userid + positionId）
     */
    @GetMapping("/position-study-detail")
    public ActionResult<AppCultivatePositionDetailVo> positionStudyDetail(V2CultivatePositionDetailForApp dto) {
        return ActionResult.success("成功", v2CultivatePositionService.positionStudyDetail(dto));
    }


    /**
     * 根据用户id查询用户下属的成员列表
     *
     * @param userId 用户id
     * @return 成员列表
     */
    @GetMapping("/queryUnderlingUserList")
    public ActionResult<List<CultivateSimpleUserInfoVo>> queryUnderlingByUserId(@RequestParam("userId") String userId) {
        List<UserPageListVO> userPageList = userApiV2Util.listUnderlingTargetUser(userId, null);
        List<CultivateSimpleUserInfoVo> list = new ArrayList<>();
        if (CollUtil.isEmpty(userPageList)) {
            return ActionResult.success(new ArrayList<>());
        }
        for (UserPageListVO vo : userPageList) {
            CultivateSimpleUserInfoVo newSub = new CultivateSimpleUserInfoVo();
            newSub.convert(vo);
            newSub.setHeadIcon(UploaderUtil.uploaderImg(vo.getHeadIcon()));
            list.add(newSub);
        }
        return ActionResult.success(list);
    }
}
