package jnpf.certificate.controller;

import com.github.pagehelper.PageInfo;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.certificate.service.CertificateManageService;
import jnpf.certificate.service.CertificateInstanceService;
import jnpf.model.certificate.req.CertificateHealthManageQueryReq;
import jnpf.model.certificate.req.CertificateInstanceQueryReq;
import jnpf.model.certificate.req.CertificateStoreManageQueryReq;
import jnpf.model.certificate.req.CertificateStoreDashboardReq;
import jnpf.model.certificate.req.CertificateSyncHealthReq;
import jnpf.model.certificate.req.app.CertificateAppBusinessLicenseUpdateReq;
import jnpf.model.certificate.req.app.CertificateAppHealthCertificateUpdateReq;
import jnpf.model.certificate.req.app.CertificateAppHygieneLicenseUpdateReq;
import jnpf.model.certificate.req.app.CertificateAppStoreCustomUpdateReq;
import jnpf.model.certificate.vo.CertificateHealthManageVO;
import jnpf.model.certificate.vo.CertificateInstanceVO;
import jnpf.model.certificate.vo.CertificateStoreManageVO;
import jnpf.model.certificate.vo.CertificateStoreDashboardVO;
import jnpf.model.certificate.vo.CertificateStoreCustomStatusTableVO;
import jnpf.model.certificate.vo.CertificateTypeOptionVO;
import jnpf.model.certificate.vo.app.HealthCertificateDetailVO;
import jnpf.util.FtbUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

/**
 * web证照实例控制器。
 */
@RestController
@Validated
@RequestMapping("/web/certificate-instance")
public class CertificateInstanceController {

    @Autowired
    private CertificateInstanceService certificateInstanceService;
    @Autowired
    private CertificateManageService certificateManageService;

    /**
     * 更新健康证。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-health")
    public ActionResult<Void> updateHealth(@Validated @RequestBody CertificateAppHealthCertificateUpdateReq req) {
        certificateManageService.updateHealthCertificate(req);
        return ActionResult.success();
    }

    /**
     * 更新营业执照。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-business-license")
    public ActionResult<Void> updateBusinessLicense(@Validated @RequestBody CertificateAppBusinessLicenseUpdateReq req) {
        certificateManageService.updateBusinessLicense(req);
        return ActionResult.success();
    }

    /**
     * 更新食品经营许可证。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-hygiene-license")
    public ActionResult<Void> updateHygieneLicense(@Validated @RequestBody CertificateAppHygieneLicenseUpdateReq req) {
        certificateManageService.updateHygieneLicense(req);
        return ActionResult.success();
    }

    /**
     * 更新门店自定义证照。
     *
     * @param req 更新参数
     * @return 操作结果
     */
    @PutMapping("/update-store-custom")
    public ActionResult<Void> updateStoreCustom(@Valid @RequestBody CertificateAppStoreCustomUpdateReq req) {
        certificateManageService.updateStoreCustomCertificate(req);
        return ActionResult.success();
    }

    /**
     * 按ID查询详情。
     *
     * @param id 实例ID
     * @return 详情
     */
    @GetMapping("/query-info/{id}")
    public ActionResult<CertificateInstanceVO> queryInfo(@PathVariable("id") @NotBlank(message = "实例ID不能为空") String id) {
        return ActionResult.success(certificateInstanceService.queryInfo(id));
    }

    /**
     * 分页查询列表。
     *
     * @param req 查询参数
     * @return 分页结果
     */
    @GetMapping("/query-page")
    @Deprecated
    public ActionResult<PageListVO<CertificateInstanceVO>> queryPage(@Valid CertificateInstanceQueryReq req) {
        PageInfo<CertificateInstanceVO> pageInfo = certificateInstanceService.queryPage(req);
        return ActionResult.page(pageInfo.getList(), FtbUtil.getPagination(pageInfo));
    }

    /**
     * 健康证管理分页查询。
     *
     * @param req 查询参数
     * @return 分页结果
     */
    @PostMapping("/query-health-page")
    public ActionResult<PageListVO<CertificateHealthManageVO>> queryHealthPage(@RequestBody @Valid CertificateHealthManageQueryReq req) {
        PageInfo<CertificateHealthManageVO> pageInfo = certificateInstanceService.queryHealthPage(req);
        return ActionResult.page(pageInfo.getList(), FtbUtil.getPagination(pageInfo));
    }

    /**
     * 门店证照分页查询。
     *
     * @param req 查询参数
     * @return 分页结果
     */
    @PostMapping("/query-store-page")
    public ActionResult<PageListVO<CertificateStoreManageVO>> queryStorePage(@RequestBody @Valid CertificateStoreManageQueryReq req) {
        PageInfo<CertificateStoreManageVO> pageInfo = certificateInstanceService.queryStorePage(req);
        return ActionResult.page(pageInfo.getList(), FtbUtil.getPagination(pageInfo));
    }

    /**
     * 门店证照看板统计。
     *
     * @param req 查询参数
     * @return 看板统计结果
     */
    @PostMapping("/store-dashboard")
    public ActionResult<CertificateStoreDashboardVO> storeDashboard(@Validated @RequestBody CertificateStoreDashboardReq req) {
        return ActionResult.success(certificateInstanceService.storeCertificateDashboard(req));
    }

    /**
     * 门店证照看板表格分页查询。
     *
     * @param req 查询参数
     * @return 分页结果
     */
    @PostMapping("/store-dashboard-table-page")
    public ActionResult<PageListVO<CertificateStoreCustomStatusTableVO>> storeDashboardTablePage(@Validated @RequestBody CertificateStoreDashboardReq req) {
        PageInfo<CertificateStoreCustomStatusTableVO> pageInfo = certificateInstanceService.storeCertificateDashboardTablePage(req);
        return ActionResult.page(pageInfo.getList(), FtbUtil.getPagination(pageInfo));
    }

    /**
     * 查询证照类型选项。
     *
     * @return 证照类型选项
     */
    @GetMapping("/query-certificate-type-list")
    public ActionResult<List<CertificateTypeOptionVO>> queryCertificateTypeList() {
        return ActionResult.success(certificateInstanceService.queryCertificateTypeList());
    }
}
