package jnpf.cultivate.v2.controller.web.certificate;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.ActionResult;
import jnpf.cultivate.service.FtbCultivateCertificateImagesService;
import jnpf.cultivate.service.FtbCultivateCertificateService;
import jnpf.model.cultivate.po.certificate.FtbCertificateEntity;
import jnpf.model.cultivate.po.certificate.FtbCertificateImagesEntity;
import jnpf.model.cultivate.v2.certificate.req.DeleteCertificateImageReq;
import jnpf.model.cultivate.v2.certificate.req.V2SaveCertificateReq;
import jnpf.model.cultivate.v2.common.vo.V2BaseIdNameVo;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * web证书模块
 *
 * @author shangyi
 */
@RestController
@RequestMapping("/v2/web/certificate")
public class V2CertificateController {

    private final FtbCultivateCertificateImagesService imagesService;
    private final FtbCultivateCertificateService certificateService;

    public V2CertificateController(FtbCultivateCertificateImagesService imagesService, FtbCultivateCertificateService certificateService) {
        this.imagesService = imagesService;
        this.certificateService = certificateService;
    }


    /**
     * 查询所有证书图片列表
     *
     * @return ActionResult 包含所有证书图片实体列表的成功响应
     */
    @GetMapping("/style/list-all")
    public ActionResult<List<FtbCertificateImagesEntity>> listAll() {
        return ActionResult.success(imagesService.listAll());
    }

    /**
     * 添加新的证书样式
     *
     * @param req 证书样式保存请求对象，包含样式名称、图片路径等信息
     * @return ActionResult 包含操作结果布尔值的成功响应
     */
    @PostMapping("/style/save")
    public ActionResult<Boolean> save(@RequestBody @Valid V2SaveCertificateReq req) {
        return ActionResult.success(imagesService.add(req));
    }

    /**
     * 根据ID删除指定的证书样式
     *
     * @param id 要删除的证书样式唯一标识符
     * @return ActionResult 包含删除操作结果布尔值的成功响应
     */
    @PostMapping("/style/del/{id}")
    @Transactional(rollbackFor = Exception.class)
    public ActionResult<Boolean> del(@PathVariable("id") String id, @Valid @RequestBody DeleteCertificateImageReq req) {
        FtbCertificateImagesEntity imagesEntity = imagesService.getById(id);
        if (imagesEntity == null) {
            return ActionResult.fail("未找到该证书模版");
        }
        certificateService.update(Wrappers.<FtbCertificateEntity>lambdaUpdate()
                .eq(FtbCertificateEntity::getTemplate, imagesEntity.getPath())
                .set(FtbCertificateEntity::getTemplate, req.getPath())
                .set(FtbCertificateEntity::getPoints, req.getPoints()));
        return ActionResult.success(imagesService.removeById(id));
    }

    /**
     * 获取证书下拉选项列表
     *
     * @return ActionResult 包含证书ID和名称对应关系列表的成功响应
     */
    @GetMapping("/select-options")
    public ActionResult<List<V2BaseIdNameVo>> selectOptions() {
        return ActionResult.success(certificateService.getOption());
    }

}
