package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.QRCodeUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.InspectInfor;
import com.ruoyi.system.domain.QueryConfig;
import com.ruoyi.system.mapper.InspectInforMapper;
import com.ruoyi.system.mapper.QueryConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ReportDetailMapper;
import com.ruoyi.system.domain.ReportDetail;
import com.ruoyi.system.service.IReportDetailService;

/**
 * 报告信息Service业务层处理
 * 
 * @author boman
 * @date 2021-12-27
 */
@Service
public class ReportDetailServiceImpl implements IReportDetailService 
{
    @Autowired
    private ReportDetailMapper reportDetailMapper;

    @Autowired
    private QueryConfigMapper queryConfigMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private InspectInforMapper inspectInforMapper;

    /**
     * 查询报告信息
     * 
     * @param reportId 报告信息主键
     * @return 报告信息
     */
    @Override
    public ReportDetail selectReportDetailByReportId(Long reportId)
    {
        return reportDetailMapper.selectReportDetailByReportId(reportId);
    }

    /**
     * 查询报告信息列表
     * 
     * @param reportDetail 报告信息
     * @return 报告信息
     */
    @Override
    public List<ReportDetail> selectReportDetailList(ReportDetail reportDetail)
    {
        return reportDetailMapper.selectReportDetailList(reportDetail);
    }

    /**
     * 新增报告信息
     * 
     * @param reportDetail 报告信息
     * @return 结果
     */
    @Override
    public int insertReportDetail(ReportDetail reportDetail)
    {
        //生成二维码
        try {
            //查询二维码的有效期
            QueryConfig queryConfig = new QueryConfig();
            queryConfig.setQueryMode("0");
            QueryConfig queryConfigQr = queryConfigMapper.selectQueryConfig(queryConfig);
            String qrTime = "30";
            String qrUnit = "3";
            if (queryConfigQr != null){
                 qrTime = queryConfigQr.getQrTime();
                 qrUnit = queryConfigQr.getQrUnit();
            }
            //返回二维码地址
            String imagePath= QRCodeUtils.encode(reportDetail.getReportNumber(), null, RuoYiConfig.getUploadQrPath(), true);
            String verifyKey = UserConstants.QR_IMAGE + reportDetail.getReportNumber();
            redisCache.setCacheObject(verifyKey,imagePath,Integer.parseInt(qrTime),changeUnit(qrUnit));
            reportDetail.setQrPath(imagePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
        reportDetail.setCreateTime(DateUtils.getNowDate());
        //把对应送检信息该为已经生成报告
        InspectInfor inspectInfor = new InspectInfor();
        inspectInfor.setInspectId(reportDetail.getInspectId());
        inspectInfor.setIsReport("N");
        inspectInforMapper.updateInspectInfor(inspectInfor);
        return reportDetailMapper.insertReportDetail(reportDetail);
    }

    /**
     * 修改报告信息
     * 
     * @param reportDetail 报告信息
     * @return 结果
     */
    @Override
    public int updateReportDetail(ReportDetail reportDetail)
    {
        if (UserConstants.NOT_UNIQUE.equals(checkReportNumberUpdate(reportDetail))){
            //生成新的二维码覆盖
            try {
                //查询二维码的有效期
                QueryConfig queryConfig = new QueryConfig();
                queryConfig.setQueryMode("0");
                QueryConfig queryConfigQr = queryConfigMapper.selectQueryConfig(queryConfig);
                String qrTime = "30";
                String qrUnit = "3";
                if (queryConfigQr != null){
                    qrTime = queryConfigQr.getQrTime();
                    qrUnit = queryConfigQr.getQrUnit();
                }
                //返回二维码地址
                String imagePath= QRCodeUtils.encode(reportDetail.getReportNumber(), null, RuoYiConfig.getUploadQrPath(), true);
                String verifyKey = UserConstants.QR_IMAGE + reportDetail.getReportNumber();
                redisCache.setCacheObject(verifyKey,imagePath,Integer.parseInt(qrTime),changeUnit(qrUnit));
                reportDetail.setQrPath(imagePath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        reportDetail.setUpdateTime(DateUtils.getNowDate());
        return reportDetailMapper.updateReportDetail(reportDetail);
    }

    /**
     * 批量删除报告信息
     * 
     * @param reportIds 需要删除的报告信息主键
     * @return 结果
     */
    @Override
    public int deleteReportDetailByReportIds(Long[] reportIds)
    {
        return reportDetailMapper.deleteReportDetailByReportIds(reportIds);
    }

    /**
     * 删除报告信息信息
     * 
     * @param reportId 报告信息主键
     * @return 结果
     */
    @Override
    public int deleteReportDetailByReportId(Long reportId)
    {
        return reportDetailMapper.deleteReportDetailByReportId(reportId);
    }

    /**
     * 校验报告编号唯一性
     * @param reportDetail
     * @return
     */
    @Override
    public String checkReportNumberUnique(ReportDetail reportDetail) {
        Long reportId = StringUtils.isNull(reportDetail.getReportId())? -1L : reportDetail.getReportId();
        ReportDetail info = reportDetailMapper.checkReportNumberUnique(reportDetail.getReportNumber());
        if (StringUtils.isNotNull(info) && info.getReportId().longValue() != reportId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验报告编号是否修改
     * @param reportDetail
     * @return
     */
    public String checkReportNumberUpdate(ReportDetail reportDetail) {
        Long reportId = StringUtils.isNull(reportDetail.getReportId())? -1L : reportDetail.getReportId();
        ReportDetail infoOld = reportDetailMapper.selectReportDetailByReportId(reportId);
        if (!infoOld.getReportNumber().equals(reportDetail.getReportNumber()))
        {
            //修改了报告编号,删除原先旧的key
            String verifyKey = UserConstants.QR_IMAGE + infoOld.getReportNumber();
            redisCache.deleteObject(verifyKey);
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 转换单位
     * @param unit
     * @return
     */
    private TimeUnit changeUnit(String unit){
        if ("0".equals(unit)){
            return TimeUnit.SECONDS;
        }
        if ("1".equals(unit)){
            return TimeUnit.MINUTES;
        }
        if ("2".equals(unit)){
            return TimeUnit.HOURS;
        }
        if ("3".equals(unit)){
            return TimeUnit.DAYS;
        }
        return TimeUnit.DAYS;
    }
}
