package jnpf.cultivate.controller.web.gained;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.cultivate.service.FtbCourseGainedLikeService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.dto.gained.FtbLikePagination;
import jnpf.model.cultivate.po.gained.FtbCourseGainedLikeEntity;
import jnpf.model.cultivate.vo.gained.FtbCourseGainedLikeVO;
import jnpf.permission.vo.v2.user.UserBoundVO;
import jnpf.util.JsonUtil;
import jnpf.util.UploaderUtil;
import jnpf.util.UserProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * app岗位学习课程-心得点赞
 */
@RestController
@Tag(name = "心得点赞", description = "心得点赞")
@RequestMapping("/course_gained_like")
public class FtbCourseGainedLikeController {
    @Autowired
    private FtbCourseGainedLikeService ftbCourseGainedLikeService;

    @Autowired
    private UserApiV2Util userApiV2Util;
    @Autowired
    private UserProvider userProvider;

    @Operation(summary = "心得点赞获取列表（移动端）")
    @GetMapping("/list")
    public ActionResult<PageListVO<FtbCourseGainedLikeVO>> list(FtbLikePagination pagination) {
        List<FtbCourseGainedLikeEntity> entity = ftbCourseGainedLikeService.getList(pagination);
        List<FtbCourseGainedLikeVO> listVo = JsonUtil.getJsonToList(JsonUtil.getObjectToString(entity), FtbCourseGainedLikeVO.class);
        PaginationVO vo = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        List<String> userIds = listVo.stream().map(FtbCourseGainedLikeVO::getLikeUserId).collect(Collectors.toList());
        Map<String, UserBoundVO> map = userApiV2Util.getUserPrimaryBoundBatch(userIds,null);
        listVo.forEach(c -> {
            UserBoundVO userBoundVO = map.get(c.getLikeUserId());

            if (userBoundVO != null) {
                c.setUserName(userBoundVO.getUserName());
                c.setOrganizeName(userBoundVO.getOrganizeName());
                c.setPositionName(userBoundVO.getPositionName());
                c.setHeadIcon(UploaderUtil.uploaderImg(userBoundVO.getHeadIcon()));
            }
        });
        return ActionResult.page(listVo, vo);
    }

    @Operation(summary = "心得点赞获取详情")
    @GetMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<FtbCourseGainedLikeEntity> info(@PathVariable("id") String id) {
        FtbCourseGainedLikeEntity entity = ftbCourseGainedLikeService.getInfo(id);
        //ContractInfoVO vo = JsonUtil.getJsonToBean(entity, ContractInfoVO.class);
        return ActionResult.success(entity);
    }

    @Operation(summary = "心得点赞新建")
    @PostMapping
    public ActionResult<String> create(@RequestBody @Valid FtbCourseGainedLikeEntity entity) {
        //ContractEntity entity = JsonUtil.getJsonToBean(contractForm, ContractEntity.class);
        ftbCourseGainedLikeService.create(entity);
        return ActionResult.success("保存成功");
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<String> update(@PathVariable("id") String id, @RequestBody @Valid FtbCourseGainedLikeEntity entity) {
        entity.setId(id);
        ftbCourseGainedLikeService.updateById(entity);
        return ActionResult.success("修改成功");
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<String> delete(@PathVariable("id") String id) {
        ftbCourseGainedLikeService.removeById(id);
        return ActionResult.success("删除成功");
    }

    @Operation(summary = "取消点赞")
    @DeleteMapping("/{gainedId}/cancel")
    @Parameters({
            @Parameter(name = "gainedId", description = "心得ID", required = true),
    })
    public ActionResult<String> cancel(@PathVariable("gainedId") String gainedId) {
        ftbCourseGainedLikeService.remove(new LambdaQueryWrapper<FtbCourseGainedLikeEntity>()
                .eq(FtbCourseGainedLikeEntity::getGainedId, gainedId)
                .eq(FtbCourseGainedLikeEntity::getLikeUserId, userProvider.get().getUserId()));
        return ActionResult.success("取消成功");
    }

}