package jnpf.cultivate.controller.web.certificate;


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.Pagination;
import jnpf.base.vo.PageListVO;
import jnpf.base.vo.PaginationVO;
import jnpf.cultivate.service.FtbCultivateCertificateService;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.cultivate.dto.certificate.FtbCertificateForm;
import jnpf.model.cultivate.po.certificate.FtbCertificateEntity;
import jnpf.model.cultivate.vo.certificate.FtbCertificateInfoVO;
import jnpf.model.cultivate.vo.certificate.FtbCertificateListVO;
import jnpf.permission.UserApi;
import jnpf.permission.entity.UserEntity;
import jnpf.util.JsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * web证书模板模块
 *
 * @author hao.peng
 */
@RestController
@Tag(name = "证书", description = "证书")
@RequestMapping("/certificate/new")
public class FtbCultivateCertificateController {

    /**
     * 服务对象
     */
    @Resource
    private FtbCultivateCertificateService service;

    @Autowired
    private UserApi userApi;

    /**
     * 获取列表
     * @param pagination
     * @return listVo
     */
    @Operation(summary = "获取列表")
    @GetMapping("/list")
    public ActionResult<PageListVO<FtbCertificateListVO>> list(Pagination pagination) {
        List<FtbCertificateEntity> entity = service.getList(pagination);
        List<String> userIds = entity.stream().map(FtbCertificateEntity::getCreatorUserId).filter(Objects::nonNull).distinct().collect(Collectors.toList());
        List<UserEntity> userNameList = userApi.getUserName(userIds);
        Map<String, String> userIdList = userNameList.stream().collect(Collectors.toMap(UserEntity::getId,UserEntity::getRealName,(a,b)->a));
        List<FtbCertificateListVO> listVo = JsonUtil.getJsonToList(JsonUtil.getObjectToString(entity), FtbCertificateListVO.class);
        listVo.forEach(item->{
            String name = userIdList.get(item.getCreatorUserId());
            item.setCreatorUserName(name);
        });
        PaginationVO vo = JsonUtil.getJsonToBean(pagination, PaginationVO.class);
        return ActionResult.page(listVo, vo);
    }

    /**
     * 获取详情
     * @param id
     * @return vo
     */
    @Operation(summary = "获取详情")
    @GetMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<FtbCertificateInfoVO> info(@PathVariable("id") String id) {
        FtbCertificateEntity entity = service.getInfo(id);
        FtbCertificateInfoVO vo = JsonUtil.getJsonToBean(entity, FtbCertificateInfoVO.class);
        return ActionResult.success(vo);
    }

    /**
     * 新建
     * @param FtbCertificateForm
     * @return
     */
    @Operation(summary = "新建")
    @PostMapping
    public ActionResult<String> create(@RequestBody @Valid FtbCertificateForm FtbCertificateForm) {
        FtbCertificateEntity entity = JsonUtil.getJsonToBean(FtbCertificateForm, FtbCertificateEntity.class);
        service.create(entity);
        return ActionResult.success("保存成功");
    }
    /**
     * 修改
     * @param FtbCertificateForm
     * @return
     */
    @Operation(summary = "修改")
    @PutMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<String>  update(@PathVariable("id") String id, @RequestBody @Validated FtbCertificateForm FtbCertificateForm) {
        FtbCertificateEntity entity = JsonUtil.getJsonToBean(FtbCertificateForm, FtbCertificateEntity.class);
        String s = service.updateEntity(id, entity);
        if (!StringUtils.isEmpty(s)) {
            return ActionResult.fail(s);
        }
        return ActionResult.success("修改成功");
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    @Parameters({
            @Parameter(name = "id", description = "主键", required = true),
    })
    public ActionResult<String> delete(@PathVariable("id") String id) {
        service.deleteCertificate(id);
        return ActionResult.success("删除成功");
    }

    /**
     * 岗位学习查所有证书
     * @return
     */
    @GetMapping("/queryCertificateListByNotChoose")
    public ActionResult<PageListVO<FtbCertificateListVO>> queryCertificateListByNotChoose(@RequestParam(value = "keyWords",required = false)
                                                                                        String keyWords, CultivatePage page){

        return ActionResult.success(service.queryCertificateListByNotChoose(keyWords,page));

    }
}
