package com.bootdo.data.service.impl;

import com.bootdo.common.utils.Query;
import com.bootdo.data.domain.CaDetail;
import com.bootdo.data.service.CaDetailService;
import com.bootdo.data.service.TfInfoEnterpriseService;
import com.bootdo.data.util.ApplyCertUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.bootdo.data.domain.CaInfo;
import com.bootdo.data.domain.CaInfoExample;
import com.bootdo.data.dao.CaInfoMapper;
import com.bootdo.data.service.CaInfoService;

@Service
public class CaInfoServiceImpl implements CaInfoService {

    private static Logger log = LoggerFactory.getLogger(CaInfoServiceImpl.class);

    /**
     * 证书生成 返回结果状态码
     */
    private static final String RESULT_CODE_SUCCESS = "000000";


    @Resource
    private CaInfoMapper caInfoMapper;

    @Resource
    private CaDetailService caDetailService;

    @Resource
    ApplyCertUtils applyCertUtils;

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


    @Override
    public long countByExample(CaInfoExample example) {
        return caInfoMapper.countByExample(example);
    }

    @Override
    public int deleteByExample(CaInfoExample example) {
        return caInfoMapper.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return caInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(CaInfo record) {
        return caInfoMapper.insert(record);
    }

    @Override
    public int insertSelective(CaInfo record) {
        return caInfoMapper.insertSelective(record);
    }

    @Override
    public List<CaInfo> selectByExample(CaInfoExample example) {
        return caInfoMapper.selectByExample(example);
    }

    @Override
    public CaInfo selectByPrimaryKey(Integer id) {
        return caInfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(CaInfo record, CaInfoExample example) {
        return caInfoMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int updateByExample(CaInfo record, CaInfoExample example) {
        return caInfoMapper.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(CaInfo record) {
        return caInfoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CaInfo record) {
        return caInfoMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<CaInfo> queryPage(Query query) {
        return caInfoMapper.queryPage(query);
    }

    @Override
    public int queryPageCount(Query query) {
        return caInfoMapper.queryPageCount(query);
    }

    @Override
    public CaInfo queryById(Integer id) {
        return this.selectByPrimaryKey(id);
    }

    @Override
    public void caStatusUpdate(Integer id, Integer caStatus) throws Exception {
        CaInfo caInfo = this.selectByPrimaryKey(id);

        // 审核通过时 需要生成证书
        if (caStatus == CaInfo.CA_STATUS_PASS) {
            generateCertificateAndRecording(caInfo);
        }

        // 更新企业证书状态
        caInfo.setCaStatus(caStatus);
        caInfo.setUpdateTime(new Date());
        updateByPrimaryKey(caInfo);
    }

    @Override
    public CaInfo getEnterpriseCaByCompanyNameAndUniformSocialCreditCode(String companyName, String uniformSocialCreditCode) {
        return caInfoMapper.getByCompanyNameAndUniformSocialCreditCode(companyName, uniformSocialCreditCode);
    }

    /**
     * 获取增加后的年份
     *
     * @param date
     * @return
     */
    private Date getYearAfterAddition(Date date) {
        Calendar rightNowDate = Calendar.getInstance();
        rightNowDate.setTime(date);
        rightNowDate.add(Calendar.YEAR, 1);
        return rightNowDate.getTime();
    }

    /**
     * 生成证书 并记录 证书详情
     *
     * @param caInfo 证书详情
     * @return 签名证书 Base64 编码字符串
     */
    private void generateCertificateAndRecording(CaInfo caInfo) throws Exception {
        // 调用第三方ca证书生成
        CaDetail caDetail = applyCertUtils.applyCertByCsr(caInfo);
        // 记录 证书详情 入库
        caDetailService.insertSelective(caDetail);
        // 更新 企业ca 证书文件路径
        caInfo.setFilePath(caDetail.getRemark());
        caInfo.setCaDetailId(caDetail.getId());
        // 设置证书 生效开始时间
        Date date = new Date();
        caInfo.setEffectiveTimeBeige(date);
        // 证书失效时间 这里默认为一年有效期 根据情况后可以修改
        caInfo.setEffectiveTimeEnd(getYearAfterAddition(date));
    }
}
