package org.jeecg.modules.app.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.app.service.IAppIsinService;
import org.jeecg.modules.app.vo.AppIsinVerifyVO;
import org.jeecg.modules.system.app.entity.AppIsin;
import org.jeecg.modules.system.app.mapper.AppIsinMapper;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;

@Service
@Log4j2
public class AppIsinServiceImpl extends ServiceImpl<AppIsinMapper, AppIsin> implements IAppIsinService {
    @Override
    public Page<AppIsinVerifyVO> pageAppIsinVerifyRecords(Page<AppIsinVerifyVO> page, String orgCode) {
        return baseMapper.pageAppIsinVerifyRecords(page,orgCode);
    }

    @Override
    public String repealVerifyRecord(String appIsinId) {

        AppIsin appIsin = this.getById(appIsinId);
        //校验核验时间
        if(ObjectUtil.isNotEmpty(appIsin.getUseTime())){
            Date expTime = getDate(appIsin.getUseTime());
            if(expTime.before(new Date())){
                throw new JeecgBootException("已超过可撤销时间,无法撤销！");
            }
        }
        appIsin.setIsinStatus(0);
        Date expireTime = appIsin.getExpireTime();
        //判断当前时间是否过期
        if(expireTime.before(new Date())) {
            appIsin.setIsinStatus(2);
        }
        this.updateById(appIsin);
        return "撤销成功！";
    }

    //时间加1小时
    public static Date getDate(Date date){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 24小时制
        cal.add(Calendar.HOUR, 1);
        return cal.getTime();
    }
}
