package jnpf.storecertificatephoto.controller;

import jnpf.base.ActionResult;
import jnpf.model.certificate.vo.CertificateTypeOptionVO;
import jnpf.model.storecertificatephoto.vo.StoreCertificatePhotoIdNameVO;
import jnpf.model.warningnotice.enums.CertificateTypeEnum;
import jnpf.model.warningnotice.req.WarningNoticeSaveReq;
import jnpf.model.warningnotice.vo.WarningNoticeVO;
import jnpf.storecertificatephoto.service.StoreCertificatePhotoService;
import jnpf.storecertificatephoto.service.WarningNoticeService;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;

/**
 * web预警通知控制器
 */
@RestController
@Validated
@RequestMapping("/web/warning-notice")
public class WarningNoticeController {

    @Autowired
    private WarningNoticeService warningNoticeService;
    @Autowired
    private StoreCertificatePhotoService storeCertificatePhotoService;

    /**
     * 保存预警通知配置
     *
     * @param req 保存请求参数
     * @return 预警通知配置
     */
    @PostMapping("/save")
    public ActionResult<WarningNoticeVO> save(@Validated @RequestBody WarningNoticeSaveReq req) {
        return ActionResult.success("保存成功", warningNoticeService.save(req));
    }

    /**
     * 批量保存预警通知配置
     *
     * @param reqList 批量保存请求参数
     * @return 预警通知配置列表
     */
    @PostMapping("/saveList")
    public ActionResult<List<WarningNoticeVO>> saveList(
            @RequestBody @NotEmpty(message = "保存参数不能为空") List<@Valid WarningNoticeSaveReq> reqList) {
        return ActionResult.success("保存成功", warningNoticeService.saveList(reqList));
    }

    /**
     * 根据证照类型查询预警通知配置
     *
     * @param type 证照类型
     * @return 预警通知配置
     */
    @GetMapping("/query-by-type")
    public ActionResult<WarningNoticeVO> queryByType(@RequestParam("type") @NotBlank(message = "证照类型，或者模板id不能为空!") String type) {
        return ActionResult.success("获取成功", warningNoticeService.queryByType(type));
    }

    /**
     * 查询所有预警通知配置
     *
     * @return 预警通知配置列表
     */
    @GetMapping("/query-all")
    public ActionResult<List<WarningNoticeVO>> queryAll() {
        return ActionResult.success("获取成功", warningNoticeService.queryAll());
    }

    /**
     * 查询证照类型选项。
     *
     * @return 证照类型选项
     */
    @GetMapping("/query-certificate-type-list")
    public ActionResult<List<CertificateTypeOptionVO>> queryCertificateTypeList() {
        List<StoreCertificatePhotoIdNameVO> customerCertificateList = storeCertificatePhotoService.queryIdNameList();
        List<CertificateTypeOptionVO> result = new ArrayList<>(CertificateTypeEnum.values().length + customerCertificateList.size());
        for (CertificateTypeEnum type : CertificateTypeEnum.values()){
            CertificateTypeOptionVO option = new CertificateTypeOptionVO();
            option.setLabel(type.getLabel());
            option.setKey(type.getType());
            option.setType(type.getType());
            result.add(option);
        }
        for (StoreCertificatePhotoIdNameVO customerCertificate : customerCertificateList){
            CertificateTypeOptionVO option = new CertificateTypeOptionVO();
            option.setLabel(customerCertificate.getCertificateName());
            option.setKey(customerCertificate.getId());
            option.setType(CertificateTypeEnum.STORE_CUSTOM_CERTIFICATE.getType());
            result.add(option);
        }
        return ActionResult.success(result);
    }

}
