package jnpf.certificate.controller;

import io.seata.spring.annotation.GlobalTransactional;
import io.swagger.v3.oas.annotations.Operation;
import jnpf.base.ActionResult;
import jnpf.certificate.service.CertificateStoreService;
import jnpf.exception.HandleException;
import jnpf.model.certificate.req.CertificateStoreSaveReq;
import jnpf.model.certificate.vo.CertificateStoreAndCertificatesVO;
import jnpf.model.certificate.vo.CertificateStoreTabVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * web门店证照控制器。
 */
@RestController
@Validated
@RequestMapping("/web/certificate-store")
public class CertificateStoreController {

    /**
     * 门店证照服务。
     */
    @Autowired
    private CertificateStoreService certificateStoreService;

    /**
     * 保存门店及证照数据。
     * 核心参数包含门店信息、健康证、营业执照、食品经营许可证和门店自定义证照集合。
     *
     * @param req 保存参数
     * @return 操作结果
     */
    @PostMapping("/save-store-and-certificates")
    @GlobalTransactional
    public ActionResult<String> saveStoreAndCertificates(@Validated @RequestBody CertificateStoreSaveReq req) {
        return ActionResult.success("success",certificateStoreService.saveStoreAndCertificates(req));
    }

    @Operation(summary = "[删除]删除门店和证照")
    @DeleteMapping(value = "/{id}")
    @GlobalTransactional
    public ActionResult<Boolean> deleteStore(@PathVariable("id") String id) throws HandleException {
        return ActionResult.success(certificateStoreService.deleteStore(id));
    }

    /**
     * 根据门店ID查询门店证照详情。营业执照、食品许可证、门店自定义证照集合。
     *
     * @param storeId 门店ID
     * @return 门店证照详情
     */
    @GetMapping("/get-store-certificates")
    public ActionResult<CertificateStoreAndCertificatesVO> getStoreCertificates(@RequestParam("storeId")
                                                                                    @NotBlank(message = "门店ID不能为空")
                                                                                    String storeId) {
        return ActionResult.success(certificateStoreService.getStoreAndCertificates(storeId));
    }

    /**
     * 查询门店证照Tab列表。
     *
     * @return 门店证照Tab列表
     */
    @GetMapping("/query-store-certificate-tab-list")
    public ActionResult<List<CertificateStoreTabVO>> queryStoreCertificateTabList() {
        return ActionResult.success(certificateStoreService.queryStoreCertificateTabList());
    }
}
