package com.rongwei.savecheck.controller;

import com.rongwei.rwcommon.base.R;
import com.rongwei.rwcommon.utils.SpringContextUtils;
import com.rongwei.rwcommon.utils.StringUtils;
import com.rongwei.sfcommon.sys.service.UrgingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * UrgingController class
 *
 * @author XH
 * @date 2024/09/13
 */
@RestController
@RequestMapping("/urging")
public class UrgingController {
    /**
     * 特种设备
     */
    public static final String SPECIAL_EQUIPMENT = "SpecialEquipment";
    /**
     * 特种证书
     */
    public static final String SPECIAL_CERTIFICATE = "PersonnelCertificates";
    /**
     * 安全附件
     */
    public static final String SECURITY_ATTACHMENTS = "safeAttachments";
    private final Logger log = LoggerFactory.getLogger(this.getClass().getName());

    @PostMapping("/send")
    public R complaintForm(@RequestBody Map<String, List<String>> params) {
        log.info("开发发送催办提醒：{}", params);

        List<String> dataIds;
        String beanName;
        if (params.containsKey(SPECIAL_EQUIPMENT)) {
            beanName = "specialEquipmentUrgingServiceImpl";
            dataIds = params.getOrDefault(SPECIAL_EQUIPMENT, new ArrayList<>());
        } else if (params.containsKey(SPECIAL_CERTIFICATE)) {
            beanName = "specialCertificateUrgingServiceImpl";
            dataIds = params.getOrDefault(SPECIAL_CERTIFICATE, new ArrayList<>());
        } else if (params.containsKey(SECURITY_ATTACHMENTS)) {
            beanName = "securityAttachmentsUrgingServiceImpl";
            dataIds = params.getOrDefault(SECURITY_ATTACHMENTS, new ArrayList<>());
        } else {
            beanName = "";
            dataIds = new ArrayList<>();
        }
        if (StringUtils.isBlank(beanName) || dataIds == null || dataIds.isEmpty()) {
            log.error("催办类型不正确：{}或者暂无需要催办的数据：{}", params.keySet(), dataIds);
            return R.error("当前类型暂不支持催办");
        }
        UrgingService urgingService = SpringContextUtils.getBean(beanName);
        log.error("当前催办类型为：{},需要被催办的数据为：{}", beanName, dataIds);
        return urgingService.emailAndNotifyUrging(dataIds);
    }
}
