package com.bootdo.portal.controller;

import com.bootdo.common.utils.EhcacheUtil;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.data.domain.CaInfo;
import com.bootdo.data.entity.TfInfoEnterprise;
import com.bootdo.data.service.CaInfoService;
import com.bootdo.data.service.TfInfoEnterpriseService;
import com.bootdo.portal.common.CommonConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 企业证书申请api控制层
 *
 * @author Mr.xiBel
 * @date 2022/5/10
 **/
@CrossOrigin
@RestController
@RequestMapping("/portal/ca")
public class CaInfoApiController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 服务对象
     */
    @Resource
    private TfInfoEnterpriseService tfInfoEnterpriseService;


    /**
     * 证书信息 服务对象
     */
    @Resource
    private CaInfoService caInfoService;

    /**
     * 企业登陆
     *
     * @param parameter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getEnterpriseCa", method = RequestMethod.POST)
    public R getEnterpriseCa(@RequestBody Map<String, Object> parameter) {
        // 检查用户是否登陆
        R checkEnterpriseInfoCache = checkEnterpriseInfoCache(parameter);
        if (!Objects.isNull(checkEnterpriseInfoCache)) {
            return checkEnterpriseInfoCache;
        }
        // 已登陆
        TfInfoEnterprise infoEnterpriseCache = EhcacheUtil.getInstance().getUserByToken((String) parameter.get("uid"));

        // 获取企业ca 检查是否存在ca
        CaInfo caInfo = caInfoService.getEnterpriseCaByCompanyNameAndUniformSocialCreditCode(infoEnterpriseCache.getCompanyName(),
                infoEnterpriseCache.getUniformSocialCreditCode());

        // 假如还没有申请 生成一条模拟数据 供前端点击
        if (Objects.isNull(caInfo)) {
            caInfo = new CaInfo();
            caInfo.setCompanyName(infoEnterpriseCache.getCompanyName());
            caInfo.setUniformSocialCreditCode(infoEnterpriseCache.getUniformSocialCreditCode());
            // 未申请
            caInfo.setCaStatus(4);
        }

        // 过期的
        if (caInfo.getCaStatus() == CaInfo.CA_STATUS_PASS && caInfo.getEffectiveTimeEnd() != null) {
            if (new Date().after(caInfo.getEffectiveTimeEnd())) {
                // 已过期
                caInfo.setCaStatus(5);
            }
        }

        // 已经有申请记录的返回最新的一条
        return R.ok().put(CommonConstants.DATA_FIELD_DATA, Collections.singletonList(caInfo));
    }


    @ResponseBody
    @RequestMapping(value = "/apply", method = RequestMethod.POST)
    public R apply(@RequestBody Map<String, Object> parameter) {
        // 检查用户是否登陆
        R checkEnterpriseInfoCache = checkEnterpriseInfoCache(parameter);
        if (!Objects.isNull(checkEnterpriseInfoCache)) {
            return checkEnterpriseInfoCache;
        }

        // 已登陆
        TfInfoEnterprise infoEnterpriseCache = EhcacheUtil.getInstance().getUserByToken((String) parameter.get("uid"));

        // 创建证书申请记录
        CaInfo caInfoObject = createCaInfoObject(infoEnterpriseCache);
        caInfoService.insert(caInfoObject);

        return R.ok();
    }

    /**
     * cainfo 对象
     *
     * @param infoEnterpriseCache
     * @return
     */
    private CaInfo createCaInfoObject(TfInfoEnterprise infoEnterpriseCache) {
        CaInfo caInfo = new CaInfo();
        caInfo.setCaStatus(CaInfo.CA_STATUS_APPLY);
        caInfo.setUniformSocialCreditCode(infoEnterpriseCache.getUniformSocialCreditCode());
        caInfo.setCompanyName(infoEnterpriseCache.getCompanyName());
        caInfo.setTaxationNum(infoEnterpriseCache.getUniformSocialCreditCode());
        caInfo.setConnectAddress(infoEnterpriseCache.getAddress());
        caInfo.setConnectName(infoEnterpriseCache.getContacts());
        caInfo.setConnectPhone(infoEnterpriseCache.getMobile());
        caInfo.setConnectEmail(infoEnterpriseCache.getEmail());
        caInfo.setCrateTime(new Date());
        return caInfo;
    }

    /**
     * 检查用户是否能登陆
     *
     * @param parameter
     * @return
     */
    private R checkEnterpriseInfoCache(Map<String, Object> parameter) {
        Object uid = parameter.get("uid");
        if (Objects.isNull(uid)) {
            return R.error(499, "请先登陆系统");
        }

        // 这里需要企业登录后获取
        TfInfoEnterprise infoEnterprise = EhcacheUtil.getInstance().getUserByToken((String) uid);
        if (Objects.isNull(infoEnterprise)) {
            return R.error(499, "请先登陆系统");
        }

        return null;
    }
}
