package com.huazheng.project.greenplum.service.impl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.huazheng.project.greenplum.mapper.GPMapper;
import com.huazheng.project.hana.mapper.SapMapper;
import com.huazheng.project.hana.model.Afvc;
import com.huazheng.project.hana.model.Knkk;
import com.huazheng.project.hana.model.Knvp;
import com.huazheng.project.hana.model.Mska;
import com.huazheng.project.hana.model.S066;
import com.huazheng.project.hana.model.S067;
import com.huazheng.project.hana.model.T001w;
import com.huazheng.project.hana.model.T023t;
import com.huazheng.project.hana.model.Tspat;
import com.huazheng.project.hana.model.Tvkbt;
import com.huazheng.project.mssql2.mapper.HzcrmMapper;
import com.huazheng.project.mssql2.model.TKeHu;
import com.huazheng.project.mssql2.model.TKeHuXiaoShouZZ;
import com.huazheng.project.mssql2.model.TKeHuXiaoShouZZYeWuY;
import com.huazheng.project.mssql2.model.TWuLiaoDB;
import com.huazheng.project.mssql3.mapper.Cinderellaw2Mapper;
import com.huazheng.project.mssql3.model.PersonComp;
import com.huazheng.project.mssql4.mapper.UltimusDBMapper;
import com.huazheng.project.mssql4.model.Tasks;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.log4j.Log4j2;

@Log4j2
@Service
public class DeleteUpdateJobServiceImpl {

	@Autowired
	private RedisTemplate<String, String> redis1Template;

	@Autowired
	private SapMapper sapMapper;
	@Autowired
	private GPMapper gpMapper;
	@Autowired
	private HzcrmMapper hzcrmMapper;
	@Autowired
	private UltimusDBMapper ultimusDBMapper;
	@Autowired
	private Cinderellaw2Mapper cinderellaw2Mapper;
	
	public String getErrorInfoFromException(Exception e) {
		try {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			return "\r\n" + sw.toString() + "\r\n";
		} catch (Exception e2) {
			return "bad getErrorInfoFromException";
		}
	}
	
	public void checkJob2() {
		selectTKeHuCheck(); // 6
		selectTKeHuXiaoShouZZCheck(); // 7
		selectTKeHuXiaoShouZZYeWuYCheck(); // 8
		selectTWuLiaoDBCheck(); // 9
	}
	private void selectTKeHuCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:TKeHu:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:TKeHu:rowNum");
			TKeHu build = TKeHu.builder().rowNum(rowNum).build();
			List<TKeHu> list = gpMapper.selectTKeHuCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:TKeHu:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				TKeHu source = hzcrmMapper.selectTKeHuById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteTKeHu(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateTKeHu(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:TKeHu:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectTKeHuCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:TKeHu:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectTKeHuXiaoShouZZCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:TKeHuXiaoShouZZ:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:TKeHuXiaoShouZZ:rowNum");
			TKeHuXiaoShouZZ build = TKeHuXiaoShouZZ.builder().rowNum(rowNum).build();
			List<TKeHuXiaoShouZZ> list = gpMapper.selectTKeHuXiaoShouZZCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:TKeHuXiaoShouZZ:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				TKeHuXiaoShouZZ source = hzcrmMapper.selectTKeHuXiaoShouZZById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteTKeHuXiaoShouZZ(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateTKeHuXiaoShouZZ(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:TKeHuXiaoShouZZ:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectTKeHuXiaoShouZZCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:TKeHuXiaoShouZZ:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectTKeHuXiaoShouZZYeWuYCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:TKeHuXiaoShouZZYeWuY:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:TKeHuXiaoShouZZYeWuY:rowNum");
			TKeHuXiaoShouZZYeWuY build = TKeHuXiaoShouZZYeWuY.builder().rowNum(rowNum).build();
			List<TKeHuXiaoShouZZYeWuY> list = gpMapper.selectTKeHuXiaoShouZZYeWuYCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:TKeHuXiaoShouZZYeWuY:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				TKeHuXiaoShouZZYeWuY source = hzcrmMapper.selectTKeHuXiaoShouZZYeWuYById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteTKeHuXiaoShouZZYeWuY(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateTKeHuXiaoShouZZYeWuY(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:TKeHuXiaoShouZZYeWuY:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectTKeHuXiaoShouZZYeWuYCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:TKeHuXiaoShouZZYeWuY:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectTWuLiaoDBCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:TWuLiaoDB:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:TWuLiaoDB:rowNum");
			TWuLiaoDB build = TWuLiaoDB.builder().rowNum(rowNum).build();
			List<TWuLiaoDB> list = gpMapper.selectTWuLiaoDBCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:TWuLiaoDB:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				TWuLiaoDB source = hzcrmMapper.selectTWuLiaoDBById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteTWuLiaoDB(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateTWuLiaoDB(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:TWuLiaoDB:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectTWuLiaoDBCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:TWuLiaoDB:rowNum", getErrorInfoFromException(e));
		}
	}
	
	public void checkJob4() {
		selectKnkkCheck(); // 19
		selectMskaCheck(); // 26
		selectS066Check(); // 28
		selectS067Check(); // 29
		selectT001wCheck(); // 30
		selectT023tCheck(); // 31
		selectTvkbtCheck(); //
		selectAfvcCheck();
		selectTspatCheck();
		selectKnvpCheck();
	}
	
	// 计数器复位删除1
	private void selectKnkkCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:Knkk:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:Knkk:rowNum");
			Knkk build = Knkk.builder().rowNum(rowNum).build();

			List<Knkk> list = gpMapper.selectKnkkCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) { // rowid已完成，则转为由更新时间字段开始同步
				redis1Template.opsForValue().set("huazheng:check:Knkk:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			list.forEach(target -> { // 遍历要检查的数据
				Knkk source = sapMapper.selectKnkkById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteKnkk(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateKnkk(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}

				redis1Template.opsForValue().set("huazheng:check:Knkk:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectKnkkCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:Knkk:rowNum", getErrorInfoFromException(e));
		}
	}
	public void selectKnkkCheckUpdate() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:checkUpdate:Knkk:rowids", "0");
			Long rowids = Long.parseLong(opsForValue.get("huazheng:checkUpdate:Knkk:rowids"));
			Knkk build = Knkk.builder().rowids(rowids).build();

			List<Knkk> slist = sapMapper.selectKnkkCheckByUpdate(build); // 从源库中按更新时间查询
			if (slist.size() == 0) { // rowid已完成，则转为由更新时间字段开始同步
				redis1Template.opsForValue().set("huazheng:checkUpdate:Knkk:rowids", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			
			slist.forEach(source -> { // 遍历源库中的数据，去目标库中查询数据
				Knkk target = gpMapper.selectKnkk(source);
				String operator = "none";
				Long srowids = source.getRowids();
				if (target != null) {
					source.setRowids(null);
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						// ===============================
						// ===============================
						while (true) {
							try {
								gpMapper.updateKnkk(source); // 更新数据到数仓中
								operator = "update";
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				
				redis1Template.opsForValue().set("huazheng:checkUpdate:Knkk:rowids", srowids.toString());
				if (!operator.equals("none")) {
					log.info(String.format("selectKnkkCheckUpdate --> rowids:%s, operator:%s", srowids.toString(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkUpdateError:Knkk:rowids", getErrorInfoFromException(e));
		}
	}
	private void selectAfvcCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:Afvc:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:Afvc:rowNum");
			Afvc build = Afvc.builder().rowNum(rowNum).build();
			List<Afvc> list = gpMapper.selectAfvcCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:Afvc:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				Afvc source = sapMapper.selectAfvcById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteAfvc(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateAfvc(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:Afvc:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectAfvcCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:Afvc:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectKnvpCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:Knvp:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:Knvp:rowNum");
			Knvp build = Knvp.builder().rowNum(rowNum).build();
			List<Knvp> list = gpMapper.selectKnvpCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:Knvp:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				Knvp source = sapMapper.selectKnvpById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteKnvp(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateKnvp(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:Knvp:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectKnvpCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:Knvp:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectTspatCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:Tspat:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:Tspat:rowNum");
			Tspat build = Tspat.builder().rowNum(rowNum).build();
			List<Tspat> list = gpMapper.selectTspatCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:Tspat:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				Tspat source = sapMapper.selectTspatById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteTspat(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateTspat(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:Tspat:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectTspatCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:Tspat:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectMskaCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:Mska:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:Mska:rowNum");
			Mska build = Mska.builder().rowNum(rowNum).build();
			List<Mska> list = gpMapper.selectMskaCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:Mska:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				Mska source = sapMapper.selectMskaById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteMska(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						// ===============================
						// ===============================
						while (true) {
							try {
								gpMapper.updateMska(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:Mska:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectMskaCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:Mska:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectS066Check() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:S066:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:S066:rowNum");
			S066 build = S066.builder().rowNum(rowNum).build();
			List<S066> list = gpMapper.selectS066Check(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:S066:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				S066 source = sapMapper.selectS066ById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteS066(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						// ===============================
						String sptags = source.getSptags(); // 日期转义
						if (sptags == null || sptags.equals("00000000") || StrUtil.isBlank(sptags)) {
							Date date = DateUtil.parse("1970-01-01 00:00:00").toJdkDate();
							source.setSptag(date);
						} else {
							String erdat2 = new StringBuffer(sptags).insert(4, "-").insert(7, "-").toString();
							Date date = DateUtil.parse(erdat2);
							source.setSptag(date);
						}
						if (source.getSpwocs() == null) {
							source.setSpwocs("000000");
						}
						if (source.getSpbups() == null) {
							source.setSpbups("000000");
						}
						// ===============================
						while (true) {
							try {
								gpMapper.updateS066(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:S066:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectS066Check --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:S066:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectS067Check() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:S067:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:S067:rowNum");
			S067 build = S067.builder().rowNum(rowNum).build();
			List<S067> list = gpMapper.selectS067Check(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:S067:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				S067 source = sapMapper.selectS067ById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteS067(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						// ===============================
						String sptags = source.getSptags(); // 日期转义
						if (sptags == null || sptags.equals("00000000") || StrUtil.isBlank(sptags)) {
							Date date = DateUtil.parse("1970-01-01 00:00:00").toJdkDate();
							source.setSptag(date);
						} else {
							String erdat2 = new StringBuffer(sptags).insert(4, "-").insert(7, "-").toString();
							Date date = DateUtil.parse(erdat2);
							source.setSptag(date);
						}
						if (source.getSpwocs() == null) {
							source.setSpwocs("000000");
						}
						if (source.getSpbups() == null) {
							source.setSpbups("000000");
						}
						// ===============================
						while (true) {
							try {
								gpMapper.updateS067(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:S067:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectS067Check --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:S067:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectT001wCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:T001w:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:T001w:rowNum");
			T001w build = T001w.builder().rowNum(rowNum).build();
			List<T001w> list = gpMapper.selectT001wCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:T001w:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				T001w source = sapMapper.selectT001wById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteT001w(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						// ===============================
						// ===============================
						while (true) {
							try {
								gpMapper.updateT001w(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:T001w:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectT001wCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:T001w:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectT023tCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:T023t:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:T023t:rowNum");
			T023t build = T023t.builder().rowNum(rowNum).build();
			List<T023t> list = gpMapper.selectT023tCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:T023t:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				T023t source = sapMapper.selectT023tById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteT023t(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						// ===============================
						// ===============================
						while (true) {
							try {
								gpMapper.updateT023t(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:T023t:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectT023tCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:T023t:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectTvkbtCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:Tvkbt:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:Tvkbt:rowNum");
			Tvkbt build = Tvkbt.builder().rowNum(rowNum).build();
			List<Tvkbt> list = gpMapper.selectTvkbtCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:Tvkbt:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				Tvkbt source = sapMapper.selectTvkbtById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteTvkbt(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						// ===============================
						// ===============================
						while (true) {
							try {
								gpMapper.updateTvkbt(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:Tvkbt:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectTvkbtCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:Tvkbt:rowNum", getErrorInfoFromException(e));
		}
	}
	
	public void checkJob8() {
		selectTasksCheck();
		selectPersonCompCheck();
	}
	private void selectTasksCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:Tasks:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:Tasks:rowNum");
			Tasks build = Tasks.builder().rowNum(rowNum).build();
			List<Tasks> list = gpMapper.selectTasksCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:Tasks:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				Tasks source = ultimusDBMapper.selectTasksById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deleteTasks(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updateTasks(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:Tasks:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectTasksCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:Tasks:rowNum", getErrorInfoFromException(e));
		}
	}
	private void selectPersonCompCheck() {
		try {
			ValueOperations<String, String> opsForValue = redis1Template.opsForValue();
			opsForValue.setIfAbsent("huazheng:check:PersonComp:rowNum", "0");
			String rowNum = opsForValue.get("huazheng:check:PersonComp:rowNum");
			PersonComp build = PersonComp.builder().rowNum(rowNum).build();
			List<PersonComp> list = gpMapper.selectPersonCompCheck(build); // 从数仓中查询一组数据
			if (list.size() == 0) {
				redis1Template.opsForValue().set("huazheng:check:PersonComp:rowNum", "0"); // 计数器复位
				ThreadUtil.sleep(1000); // 没有数据了，休眠一下
			}
			list.forEach(target -> { // 遍历要检查的数据
				PersonComp source = cinderellaw2Mapper.selectPersonCompById(target); // 根据主键查询源库中的数据
				String operator = "none";
				if (source == null) { // 如果源库中没有数据
					gpMapper.deletePersonComp(target); // 删除数仓中的数据
					operator = "delete";
				} else { // 源库中有数据
					String shash = SecureUtil.md5(JSONUtil.toJsonStr(source)); // 源库中数据的hash结果
					String thash = target.getHashResult(); // 数仓中数据的hash结果
					if (!shash.equals(thash)) { // 如果hash结果不一致
						source.setHashResult(shash);
						while (true) {
							try {
								gpMapper.updatePersonComp(source); // 更新数据到数仓中
								break;
							} catch (RuntimeException e) {
								log.error(e.getMessage());ThreadUtil.safeSleep(500);
							}
						}
						ThreadUtil.safeSleep(500);
					}
				}
				redis1Template.opsForValue().set("huazheng:check:PersonComp:rowNum", target.getRowNum());
				if (!operator.equals("none")) {
					log.info(String.format("selectPersonCompCheck --> rowNum:%s, operator:%s", target.getRowNum(), operator));
				}
			});
		} catch (Exception e) {
			redis1Template.opsForValue().set("huazheng:checkError:PersonComp:rowNum", getErrorInfoFromException(e));
		}
	}
	
}
