/*
 *    Copyright (c) 2018-2025, hnqz All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: hnqz
 */
package com.qunzhixinxi.hnqz.daemon.quartz.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qunzhixinxi.hnqz.daemon.quartz.entity.WmScorePackage;
import com.qunzhixinxi.hnqz.daemon.quartz.entity.WmScorePackageStatus;
import com.qunzhixinxi.hnqz.daemon.quartz.mapper.WmScorePackageStatusMapper;
import com.qunzhixinxi.hnqz.daemon.quartz.service.WmScorePackageService;
import com.qunzhixinxi.hnqz.daemon.quartz.service.WmScorePackageStatusService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * 积分包状态
 *
 * @author pigx code generator
 * @date 2020-07-31 20:07:00
 */
@Service
@AllArgsConstructor
public class WmScorePackageStatusServiceImpl extends ServiceImpl<WmScorePackageStatusMapper, WmScorePackageStatus> implements WmScorePackageStatusService {

	private final WmScorePackageService wmScorePackageService;

	@Override
	public void changePackageStatus() {
		WmScorePackageStatus wmScorePackageStatus=new WmScorePackageStatus();
		wmScorePackageStatus.setStatus("1");
		wmScorePackageStatus.setDelFlag("0");
		wmScorePackageStatus.setEnableFlag("0");
		wmScorePackageStatus.setUpdateTime(LocalDateTime.now());
		List<WmScorePackageStatus> list=this.baseMapper.selectList(Wrappers.query(wmScorePackageStatus));

		for (WmScorePackageStatus wms:list) {
			if(judgmentDate(wms.getCreateTime(),48))
			{
				wms.setStatus("2");
				if(this.save(wms))
				{
					WmScorePackage wmScorePackage=wmScorePackageService.getById(wms.getPackageId());
					if(null!=wmScorePackage) {
						if ("2".equals(wmScorePackageStatus.getStatus())) {
							wmScorePackage.setZbId(wmScorePackageStatus.getUserId());
							wmScorePackage.setPackageStatus("2");
						} else {
							wmScorePackage.setPackageStatus(null);
						}
						wmScorePackageService.updateById(wmScorePackage);
					}
				}
			}
		}

	}


	public  boolean judgmentDate(LocalDateTime tableTime, Integer hour) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-d HH:mm:ss");

		ZoneId zoneId = ZoneId.systemDefault();
		ZonedDateTime zdt = tableTime.atZone(zoneId);
		Date date = Date.from(zdt.toInstant());
		Date start = date;

		Date end =new Date();

		long cha = end.getTime() - start.getTime();

		if (cha < 0) {

			return false;
		}

		double result = cha * 1.0 / (1000 * 60 * 60);

		if (result <= hour) {

			return false;//是小于等于 hour 小时

		} else {

			return true;

		}

	}
}
