package jnpf.cultivate.controller.web.offline;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.cultivate.service.FtbCultivateOfflineTrainService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.offline.*;
import jnpf.model.cultivate.po.course.FtbCultivateCourse;
import jnpf.model.cultivate.vo.offline.FtbCultivateOfflineTrainDetailsVO;
import jnpf.model.cultivate.vo.offline.FtbCultivateOfflineTrainPageVO;
import jnpf.model.cultivate.vo.offline.FtbCultivateOfflineTrainPeopleSigningInVO;
import jnpf.permission.entity.UserEntity;
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.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* web线下培训模块
*
 * @author wcx
*/
@RestController
@RequestMapping("/web/ftb-cultivate-offline-train")
public class FtbCultivateOfflineTrainController {

    @Resource
    private FtbCultivateOfflineTrainService ftbCultivateOfflineTrainService;
    @Autowired
    private UserApiV2Util userApiV2Util;

    /**
     * 添加线下培训
     *
     * @param ftbCultivateCourseDTO 线下培训参数
     * @return {@link ActionResult}
     */
    @PostMapping("/add")
    public ActionResult add(@Validated @RequestBody FtbCultivateOfflineTrainDTO ftbCultivateCourseDTO) {
        ftbCultivateOfflineTrainService.add(ftbCultivateCourseDTO);
        return ActionResult.success();
    }

    /**
     * 查看线下培训时-培训结果变更
     *
     * @param ftbCultivateCourseDTO 线下培训参数
     * @return {@link ActionResult}
     */
    @PostMapping("/update")
    public ActionResult update(@Validated @RequestBody FtbCultivateOfflineTrainUpdateDTO ftbCultivateCourseDTO) {
        ftbCultivateOfflineTrainService.updateTrainingResults(UserProvider.getUser(), ftbCultivateCourseDTO);
        return ActionResult.success();
    }

    /**
     * 线下培训分页查询
     *
     * @param cultivatePage 分页模型
     * @param keywords 关键字查询
     * @return {@link ActionResult}
     */
    @GetMapping("/query-list")
    public ActionResult<PageListVO<FtbCultivateOfflineTrainPageVO>> list(CultivatePage cultivatePage, String keywords) {
        Page<FtbCultivateOfflineTrainPageVO> page = cultivatePage.coverCultivatePage("a.F_Id");
        page = ftbCultivateOfflineTrainService.listPage(page, keywords);
        PageListVO<FtbCultivateOfflineTrainPageVO> ftbCultivateOfflineTrainPageVOPageListVO = CultivatePage.coverPageList(page);
        return ActionResult.success(ftbCultivateOfflineTrainPageVOPageListVO);
    }

    /**
     * 线下培训详情-编辑时回显调用
     *
     * @param id 线下培训主键id
     * @return {@link ActionResult}<{@link FtbCultivateCourse}>
     */
    @GetMapping("/details/{id}")
    public ActionResult<FtbCultivateOfflineTrainDetailsVO> courseDetails(@PathVariable("id") String id) {
        FtbCultivateOfflineTrainDetailsVO ftbCultivateOfflineTrainDetailsVO = ftbCultivateOfflineTrainService.courseDetails(id);
        return ActionResult.success(ftbCultivateOfflineTrainDetailsVO);
    }

    /**
     * 删除线下培训
     * @return {@link ActionResult}
     */
    @DeleteMapping("/delete/{id}")
    public ActionResult deleteOfflineTrain(@PathVariable("id") String id) {
        ftbCultivateOfflineTrainService.deleteOfflineTrain(id);
        return ActionResult.success();
    }

    /**
     * 成员管理-查询已有成员，返回用户主键id
     *
     * @param id 线下培训主键id(必传)
     * @return {@link ActionResult }
     */
    @GetMapping("/query-existing-members")
    public ActionResult queryExistingMembers(String id) {
        List<String> results = ftbCultivateOfflineTrainService.queryExistingMembers(id);
        return ActionResult.success(results);
    }

    /**
     * 成员管理-修改线下培训成员
     *
     * @return {@link ActionResult }
     */
    @PostMapping("/modify-offline-training-members")
    public ActionResult modifyOfflineTrainingMembers(@Validated @RequestBody FtbModifyOfflineTrainingMembersDTO data) {
        ftbCultivateOfflineTrainService.modifyOfflineTrainingMembers(data);
        return ActionResult.success();
    }

    /**
     * 撤回
     *
     * @param id 线下培训主键id(必传)
     * @return {@link ActionResult }
     */
    @PutMapping("/withdraw/{id}")
    public ActionResult withdraw(@PathVariable("id") String id) {
        ftbCultivateOfflineTrainService.withdraw(id);
        return ActionResult.success();
    }

    /**
     * 线下培训签到人数分页查询
     *
     * @param cultivatePage 分页模型
     * @return {@link ActionResult}
     */
    @GetMapping("/query-list-people-signing")
    public ActionResult<PageListVO<FtbCultivateOfflineTrainPeopleSigningInVO>> numberOfflineTraining(CultivatePage cultivatePage,
                                                                                                     FtbCultivateOfflineTrainPeopleSigningInDTO data) {
        Page<FtbCultivateOfflineTrainPeopleSigningInVO> page = cultivatePage.coverCultivatePage();
        page = ftbCultivateOfflineTrainService.numberOfflineTraining(page, data);
        if(CollUtil.isNotEmpty(page.getRecords())){
            List<String> userIds = page.getRecords().stream()
                    .map(FtbCultivateOfflineTrainPeopleSigningInVO::getUserId).collect(Collectors.toList());
            Map<String, UserEntity> userNameAndCopyForUserIds = userApiV2Util.getUserNameAndCopyForUserIds(userIds);
            page.getRecords().forEach(item -> {
                UserEntity userEntity = userNameAndCopyForUserIds.get(item.getUserId());
                if(userEntity != null){
                    item.setSignInPersonName(userEntity.getRealName());
                }
            });
        }
        PageListVO<FtbCultivateOfflineTrainPeopleSigningInVO> ftbCultivateOfflineTrainPageVOPageListVO = CultivatePage.coverPageList(page);
        return ActionResult.success(ftbCultivateOfflineTrainPageVOPageListVO);
    }

    /**
     * 编辑线下培训
     * @return {@link ActionResult}
     */
    @PostMapping("/edit-offline-training")
    public ActionResult editOfflineTraining(@Validated @RequestBody FtbCultivateOfflineTrainChangeDTO ftbCultivateOfflineTrainDTO) {
        ftbCultivateOfflineTrainService.editOfflineTraining(ftbCultivateOfflineTrainDTO);
        return ActionResult.success();
    }


}
