package com.nantian.cloud.dbcapp.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nantian.cloud.dbcapp.mapper.record.UpdateConfigMapper;
import com.nantian.cloud.dbcapp.entity.UpdateConfig;
import com.nantian.cloud.dbcapp.service.IUpdateConfigService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * <p>
 * 企业数据修改时间限制配置表 服务实现类
 * </p>
 *
 * @author qqm
 * @since 2024-02-08
 */
@Service
public class UpdateConfigServiceImpl extends ServiceImpl<UpdateConfigMapper, UpdateConfig> implements IUpdateConfigService {


    @Override
    public boolean isCheckUpdateConfig(String enterpriseCode,String statDate) {
        //计算一下两个时间之差，是否允许修改。
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = sdf.parse(statDate);
            LocalDate lastDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
            LocalDate nowDate = LocalDate.now();
            QueryWrapper<UpdateConfig> param = new QueryWrapper<>();
            param.eq("enterprise_code", enterpriseCode);
            param.eq("is_use","1");
            UpdateConfig configDataOne = baseMapper.selectOne(param);
            if (null != configDataOne){
                long daysBetween = ChronoUnit.DAYS.between(lastDate, nowDate);
                if (daysBetween > configDataOne.getDay()){
                    return true;
                }
            }else{
                //没有单独配置，就查询全局配置
                QueryWrapper<UpdateConfig> paramAll = new QueryWrapper<>();
                paramAll.eq("enterprise_code", "00");
                paramAll.eq("is_use","1");
                UpdateConfig configDataAll = baseMapper.selectOne(paramAll);
                if (null != configDataAll){
                    long daysBetween = ChronoUnit.DAYS.between(lastDate, nowDate);
                    if (daysBetween > configDataAll.getDay()){
                        return true;
                    }
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }
}
