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.cultivate.v2.service.V2CultivatePromotionService;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.v2.course.vo.app.AppCourseSimpleVo;
import jnpf.model.cultivate.v2.course.vo.app.AppCultivateCourseExamVo;
import jnpf.model.cultivate.v2.course.vo.app.AppCultivateCourseIdentityVo;
import jnpf.model.cultivate.v2.course.web.req.V2NextUserAllMapListReq;
import jnpf.model.cultivate.v2.course.web.req.V2NextUserCultivateCourseListReq;
import jnpf.model.cultivate.v2.position.vo.AppPracticeCountVo;
import jnpf.model.cultivate.v2.promotion.vo.MyCultivatePromotionListVo;
import jnpf.model.cultivate.v2.promotion.vo.NextUserCultivatePromotionVo;
import jnpf.model.cultivate.v2.promotion.vo.V2CultivatePostAndGradeVo;
import jnpf.permission.vo.v2.user.UserBoundVO;
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.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * app下属的岗位学习模块
 *
 * @author xgl
 */
@RestController
@RequestMapping("/v2/app/position/next-user")
public class V2NextUserCultivatePositionAppController {
    /**
     * 服务对象
     */
    @Autowired
    private V2CultivatePromotionService promotionService;

    @Autowired
    private V2CultivatePositionService v2CultivatePositionService;
    @Autowired
    private UserApiV2Util userApiV2Util;


    /**
     * 用户的地图列表
     * req 请求参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/all-map-list")
    public ActionResult<NextUserCultivatePromotionVo> allMapList(V2NextUserAllMapListReq req) {
        NextUserCultivatePromotionVo vo = new NextUserCultivatePromotionVo();
        UserBoundVO userPrimaryBoundOne = userApiV2Util.getUserPrimaryBoundOne(req.getUserId(), null);
        if (userPrimaryBoundOne == null) {
            return ActionResult.success("成功", vo);
        }
        vo.fillUserInfo(userPrimaryBoundOne);
        vo.setUserAvatar(UploaderUtil.uploaderImg(userPrimaryBoundOne.getHeadIcon()));
        List<MyCultivatePromotionListVo> myCultivatePromotionListVos = promotionService.queryMyAllPromotionList(req.getUserId());

        if (CollUtil.isNotEmpty(myCultivatePromotionListVos)) {
            List<String> promotionIds = myCultivatePromotionListVos.stream().map(MyCultivatePromotionListVo::getId).collect(Collectors.toList());
            Map<String, List<V2CultivatePostAndGradeVo>> map = promotionService.queryAllSelectPosition(req.getUserId(), promotionIds);
            for (MyCultivatePromotionListVo myCultivatePromotionListVo : myCultivatePromotionListVos) {
                myCultivatePromotionListVo.setSelectPositionList(map.get(myCultivatePromotionListVo.getId()));
            }
        }

        vo.setPromoteList(myCultivatePromotionListVos);
        return ActionResult.success("成功", vo);
    }

    /**
     * 课程列表
     * req 岗位学习课程列表参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/all-complete-course-list")
    public ActionResult<PageListVO<AppCourseSimpleVo>> allCompleteCourseLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(CultivatePage.coverPageList(v2CultivatePositionService.allCompleteCourseLists(req)));

    }


    /**
     * 考试列表
     * req 请求参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/all-complete-exam-list")
    public ActionResult<PageListVO<AppCultivateCourseExamVo>> allCompleteExamLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(CultivatePage.coverPageList(v2CultivatePositionService.allCompleteExamLists(req)));
    }


    /**
     * 鉴定列表
     * req 请求参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/all-complete-identity-list")
    public ActionResult<PageListVO<AppCultivateCourseIdentityVo>> allCompleteIdentityLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(CultivatePage.coverPageList(v2CultivatePositionService.allCompleteIdentityLists(req)));
    }


    /**
     * 练习列表
     * req 请求参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/all-complete-practice-list")
    public ActionResult<PageListVO<AppPracticeCountVo>> allCompletePracticeLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(v2CultivatePositionService.allCompletePracticeLists(req));
    }




    /**
     * 本岗位-完成课程列表
     * req 岗位学习课程列表参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/self-position-all-complete-course-list")
    public ActionResult<PageListVO<AppCourseSimpleVo>> selfPositionAllCompleteCourseLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(CultivatePage.coverPageList(v2CultivatePositionService.selfPositionAllCompleteCourseLists(req)));

    }


    /**
     * 本岗位-考试列表
     * req 请求参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/self-position-all-complete-exam-list")
    public ActionResult<PageListVO<AppCultivateCourseExamVo>> selfPositionAllCompleteExamLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(CultivatePage.coverPageList(v2CultivatePositionService.selfPositionAllCompleteExamLists(req)));
    }


    /**
     * 本岗位-鉴定列表
     * req 请求参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/self-position-all-complete-identity-list")
    public ActionResult<PageListVO<AppCultivateCourseIdentityVo>> selfPositionAllCompleteIdentityLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(CultivatePage.coverPageList(v2CultivatePositionService.selfPositionAllCompleteIdentityLists(req)));
    }


    /**
     * 本岗位-练习列表
     * req 请求参数
     *
     * @return {@link ActionResult}
     */
    @GetMapping("/self-position-all-complete-practice-list")
    public ActionResult<PageListVO<AppPracticeCountVo>> selfPositionAllCompletePracticeLists(V2NextUserCultivateCourseListReq req) {
        return ActionResult.success(v2CultivatePositionService.selfPositionAllCompletePracticeLists(req));
    }

}
