package com.qmth.jkserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.jkserver.constant.SystemConstant;
import com.qmth.jkserver.core.exception.JkServerException;
import com.qmth.jkserver.dao.ExamThirdOrgDao;
import com.qmth.jkserver.model.ExamThirdOrg;
import com.qmth.jkserver.service.ExamThirdOrgService;
import com.qmth.jkserver.util.RandomGUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.Optional;

/**
 * @Description: 数据中心第三方机构 impl
 * @Param:
 * @return:
 * @Author: wangliang
 * @Date: 2021/10/20
 */
@Service
@Scope(proxyMode = ScopedProxyMode.INTERFACES)
public class ExamThirdOrgServiceImpl extends ServiceImpl<ExamThirdOrgDao, ExamThirdOrg> implements ExamThirdOrgService {
    private static final Logger log = LoggerFactory.getLogger(ExamThirdOrgServiceImpl.class);

//    @Resource
//    private MongoTemplate mongoTemplate;

    @Resource
    @Lazy
    ExamThirdOrgService examThirdOrgService;

    /**
     * 根据code校验信息
     *
     * @param code
     * @return
     */
    @Override
    public ExamThirdOrg validByCode(String code) {
        ExamThirdOrg examThirdOrg = this.findByCode(code);
        Optional.ofNullable(examThirdOrg).orElseThrow(() -> new JkServerException("未录入编码为:[" + code + "]系统信息"));
        if (!examThirdOrg.getEnable()) {
            throw new JkServerException("该机构未启用");
        }
        Optional.ofNullable(examThirdOrg.getAccessKey()).orElseThrow(() -> new JkServerException("该机构access key为空"));
        Optional.ofNullable(examThirdOrg.getAccessSecret()).orElseThrow(() -> new JkServerException("该机构access secret为空"));
        long currentTimeMillis = System.currentTimeMillis();
        if ((Objects.nonNull(examThirdOrg.getStartTime()) && examThirdOrg.getStartTime() > currentTimeMillis)) {
            throw new JkServerException("该机构有效期还未开始");
        }
        if ((Objects.nonNull(examThirdOrg.getStartTime()) && examThirdOrg.getStartTime() <= currentTimeMillis) &&
                (Objects.nonNull(examThirdOrg.getEndTime()) && examThirdOrg.getEndTime() < currentTimeMillis)) {
            throw new JkServerException("该机构已过有效期");
        }
        return examThirdOrg;
    }

    /**
     * 根据code查询信息
     *
     * @param code
     * @return
     */
    @Override
    public ExamThirdOrg findByCode(String code) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("code").is(code));
//        return mongoTemplate.findOne(query, ExamThirdOrg.class);
        QueryWrapper<ExamThirdOrg> examThirdOrgQueryWrapper = new QueryWrapper<>();
        examThirdOrgQueryWrapper.lambda().eq(ExamThirdOrg::getCode, code);
        return examThirdOrgService.getOne(examThirdOrgQueryWrapper);
    }

    /**
     * 保存数据
     *
     * @param name
     * @param code
     * @param year
     */
    @Override
    public void save(String name, String code, long year) {
//        LocalDateTime localDateTime = LocalDateTime.now();
//        localDateTime = localDateTime.plusYears(year);
//        Date date = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
        ExamThirdOrg examThirdOrg = new ExamThirdOrg(name, code, new RandomGUID().toString(), new RandomGUID().toString(), System.currentTimeMillis());
//        mongoTemplate.save(examThirdOrg);
        examThirdOrgService.saveOrUpdate(examThirdOrg);
    }

    /**
     * 校验并保存
     *
     * @param code
     */
    @Override
    public void validAndSave(String code) {
        ExamThirdOrg examThirdOrg = this.findByCode(code);
        if (Objects.isNull(examThirdOrg) && code.contains(SystemConstant.THIRD_ORG_CODE_HMJL)) {
            this.save(SystemConstant.THIRD_ORG_NAME_HMJL, SystemConstant.THIRD_ORG_CODE_HMJL, SystemConstant.THIRD_ORG_YEAR_HMJL);
        }
    }
}
