package com.hzya.frame.grpU8.nxproof.glYeb.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.grpU8.nxproof.glYeb.dao.ISenderGlYebDao;
import com.hzya.frame.grpU8.nxproof.glYeb.entity.SenderGlYebEntity;
import com.hzya.frame.grpU8.nxproof.glYeb.service.ISenderGlYebService;
import com.hzya.frame.grpU8.nxproof.glxmzl.entity.SenderGlXmzlEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.commons.collections.CollectionUtils;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (GlYeb)表服务实现类
 *
 * @author makejava
 * @since 2024-07-08 11:12:24
 */
@Service(value = "SenderGlYebServiceImpl")
public class SenderGlYebServiceImpl extends BaseService<SenderGlYebEntity, String> implements ISenderGlYebService {


    private ISenderGlYebDao senderGlYebDao;


    
    @Autowired
	public void setSenderGlYebDao(ISenderGlYebDao dao) {
		this.senderGlYebDao = dao;
		this.dao = dao;
	}

	//同步余额数据
	@Override
	public Object glYebSynchronization(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderGlYebEntity senderGlYebEntity = jsonObject.toJavaObject(SenderGlYebEntity.class);
		List<SenderGlYebEntity> senderGlYebEntities=new ArrayList<>();
		String result=null;
		try {
			//如果没有传入年度，则查询当年得，避免数据查询太大
			if(senderGlYebEntity.getKjnd()==null){
				senderGlYebEntity.setKjnd(DateUtil.format(new Date(),"yyyy"));
			}
			/*try {
				//同步南浔区人民医院001余额数据
				senderGlYebEntity.setZth("001");
				senderGlYebEntity.setGsdm("0101010101");
				// senderGlYebEntity.setKmdm("1002010101");
				logger.info("================开始查询南浔区人民医院余额表数据=================");
				senderGlYebEntities= sqlSessionNxrm.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("南浔区人民医院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("南浔区人民医院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}*/
			try {
				//同步南浔区练市人民医院003余额数据
				senderGlYebEntity.setZth("003");
				senderGlYebEntity.setGsdm("0101010103");
				logger.info("================开始查询南浔区练市人民医院余额表数据=================");
				senderGlYebEntities= senderGlYebDao.querySenderGlYebEntity(senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("南浔区练市人民医院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("南浔区练市人民医院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}
			/*try {
				//同步练市镇中心卫生院005余额数据
				senderGlYebEntity.setZth("005");
				senderGlYebEntity.setGsdm("0101010202");
				logger.info("================开始查询南浔区练市镇中心卫生院余额表数据=================");
				senderGlYebEntities= sqlSessionNxrm.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("南浔区练市镇中心卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("南浔区练市镇中心卫生院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}*/
			/*try {
				//同步善琏镇卫生院007余额数据
				senderGlYebEntity.setZth("007");
				senderGlYebEntity.setGsdm("0101010204");
				logger.info("================开始查询南浔区善琏镇卫生院余额表数据=================");
				senderGlYebEntities= sqlSessionNxrm.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("南浔区善琏镇卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("南浔区善琏镇卫生院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}*/
			/*try {
				//同步双林镇中心卫生院004余额数据
				senderGlYebEntity.setZth("004");
				senderGlYebEntity.setGsdm("0101010201");
				senderGlYebEntities= sqlSessionSlz.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("双林镇中心卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("双林镇中心卫生院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}
			try {
				//同步菱湖镇中心卫生院009余额数据
				senderGlYebEntity.setZth("009");
				senderGlYebEntity.setGsdm("0101020201");
				senderGlYebEntities= sqlSessionLhz.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("菱湖镇中心卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("菱湖镇中心卫生院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}
			try {
				//同步和孚镇卫生院010余额数据
				senderGlYebEntity.setZth("010");
				senderGlYebEntity.setGsdm("0101020202");
				senderGlYebEntities= sqlSessionHfz.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("和孚镇卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("和孚镇卫生院GRPU8余额数据同步失败:{}",e.getMessage());
			}
			try {
				//同步千金镇卫生院011余额数据
				senderGlYebEntity.setZth("011");
				senderGlYebEntity.setGsdm("0101020203");
				senderGlYebEntities= sqlSessionQjz.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("千金镇卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("千金镇卫生院GRPU8余额数据同步失败:{}",e.getMessage());
			}
			try {
				//同步石崇镇卫生院012余额数据
				senderGlYebEntity.setZth("012");
				senderGlYebEntity.setGsdm("0101020204");
				senderGlYebEntities= sqlSessionScz.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("石崇镇卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("石崇镇卫生院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}
			try {
				//同步南浔镇中心卫生院014余额数据
				senderGlYebEntity.setZth("014");
				senderGlYebEntity.setGsdm("010202");
				senderGlYebEntities= sqlSessionNxz.selectList("SenderGlYebEntity_list_base",senderGlYebEntity);
				result =sendYebToHealthBureauTwo(senderGlYebEntities);
				logger.info("南浔镇中心卫生院GRPU8余额数据同步结果为:{}",result);
			}catch (Exception e){
				logger.info("南浔镇中心卫生院GRPU8余额数据同步失败:{}",e.getMessage());
				e.printStackTrace();
			}*/
		}catch (Exception e){
			logger.info("卫生院GRPU8余额数据同步失败:{}",e.getMessage());
		}
		JSONObject jsonObject1=new JSONObject();
		jsonObject1.put("status","200");
		jsonObject1.put("result",result);
		return jsonObject1;
	}


	private String sendYebToHealthBureauTwo(List<SenderGlYebEntity> senderGlYebEntities){
		if (CollectionUtils.isEmpty(senderGlYebEntities)) {
			logger.info("GRPU8没有需要同步得余额数据信息");
			return "GRPU8没有需要同步得余额数据信息";
		} else {
			//先删除再保存 根据会计年度，公司代码，账套号删除
			SenderGlYebEntity receiverGlYebEntity = new SenderGlYebEntity();
			receiverGlYebEntity.setKjnd(senderGlYebEntities.get(0).getKjnd());
			//receiverGlYebEntity.setKjnd("2021");
			receiverGlYebEntity.setGsdm(senderGlYebEntities.get(0).getGsdm());
			receiverGlYebEntity.setZth(senderGlYebEntities.get(0).getZth());
			try {
				JSONObject jsonObjectStr=new JSONObject();
				jsonObjectStr.put("jsonStr",receiverGlYebEntity);
				logger.info("========开始将卫生局得GRPU8账套号:{},公司代码:{},会计年度:{}的余额数据删除==========",senderGlYebEntities.get(0).getZth(),senderGlYebEntities.get(0).getGsdm(),senderGlYebEntities.get(0).getKjnd());
				deleteEntityHealthBureau(jsonObjectStr);
				logger.info("========卫生局得GRPU8账套号:{},公司代码:{},会计年度:{}的余额数据删除完毕==========",senderGlYebEntities.get(0).getZth(),senderGlYebEntities.get(0).getGsdm(),senderGlYebEntities.get(0).getKjnd());
			}catch (Exception e){
				logger.info("{}得GRPU8余额数据删除失败:{}", senderGlYebEntities.get(0).getZth(),e.getMessage());
				return "卫生院GRPU8余额数据删除失败";
			}
			for (SenderGlYebEntity senderGlYebEntity : senderGlYebEntities) {
				//senderGlYebEntity.setKjnd("2021");
				JSONObject jsonObjectStrSave = new JSONObject();
				jsonObjectStrSave.put("jsonStr", senderGlYebEntity);
				try {
					//保存数据
					logger.info("========开始将账套号为：{}得GRPU8余额数据同步新增到卫生局GRPU8余额数据中==========",senderGlYebEntity.getZth());
					saveEntityHealthBureau(jsonObjectStrSave);
				}catch (Exception e){
					logger.info("账套号：{}得GRPU8余额数据新增失败:{}",senderGlYebEntity.getZth(), e.getMessage());
					e.printStackTrace();
				}
			}
			return "同步卫生院GROU8余额数据成功";
		}
	}

	//查询余额数据
	@Override
	public Object queryEntityPage(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderGlYebEntity senderGlYebEntity = jsonObject.toJavaObject(SenderGlYebEntity.class);
		List<SenderGlYebEntity> senderGlYebEntities=new ArrayList<>();
		try {
			//如果没有传入年度，则查询当年得，避免数据查询太大
			if(senderGlYebEntity.getKjnd()==null){
				senderGlYebEntity.setKjnd(String.valueOf(DateUtil.format(new Date(),"yyyy")));
			}
			if(StrUtil.isEmpty(senderGlYebEntity.getZth())){
				logger.info("======根据账套号:{}查询余额数据为空",senderGlYebEntity.getZth());
				return BaseResult.getFailureMessageEntity("请传递账套号");
			}
		/*	switch (senderGlYebEntity.getGsdm()){
				case "0101010103"://练市人民医院
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101010201"://双林镇中心卫生院
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101010202"://练市镇中心卫生院
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101010204"://善琏镇卫生院
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101020201":
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101020202":
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101020203":
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101020204":
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				case "0101010101"://南浔区人民医院
					senderGlYebEntities = senderGlYebDao.queryByLike(senderGlYebEntity);
					break;
				default:
					break;
			}*/
			senderGlYebEntities = senderGlYebDao.querySenderGlYebEntity(senderGlYebEntity);
			PageInfo pageInfo=new PageInfo(senderGlYebEntities);
			JSONObject jsonObject1=new JSONObject();
			jsonObject1.put("status","200");
			jsonObject1.put("pageInfo",pageInfo);
			return jsonObject1;
		}catch (Exception e){
			logger.info("根据账套号：{}查询卫生院GROU8余额数据失败:",senderGlYebEntity.getZth(),e.getMessage());
			return BaseResult.getFailureMessageEntity("查询余额数据失败");
		}
	}


	//查询余额数据
	@Override
	public List<SenderGlYebEntity> queryEntityPageHealthBureau(SenderGlYebEntity senderGlYebEntity) {
		try {
			logger.info("====开始根据账套号:{}查询卫生局中得余额数据====",senderGlYebEntity.getZth());
			List<SenderGlYebEntity> receiverGlYebEntities = senderGlYebDao.queryGlYebHealthBureau(senderGlYebEntity);
			return receiverGlYebEntities;
		}catch (Exception e){
			logger.info("根据账套号:{}查询卫生局GRPU8余额数据错误：{}",senderGlYebEntity.getZth(),e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	//保存余额数据
	@Override
	public Object saveEntityHealthBureau(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderGlYebEntity receiverGlYebEntity = jsonObject.toJavaObject(SenderGlYebEntity.class);
		if (!checkStr(jsonObject.getString("kjnd"))) {
			logger.info("========保存余额数据时会计年度为空==========");
			return BaseResult.getFailureMessageEntity("会计年度为空");
		}
		if (!checkStr(jsonObject.getString("kmdm"))) {
			logger.info("========保存余额数据时科目代码为空==========");
			return BaseResult.getFailureMessageEntity("科目代码为空");
		}
		if (!checkStr(jsonObject.getString("gsdm"))) {
			logger.info("========保存余额数据时公司代码为空==========");
			return BaseResult.getFailureMessageEntity("公司代码为空");
		}
		if (!checkStr(jsonObject.getString("zth"))) {
			logger.info("========保存余额数据时账套号为空==========");
			return BaseResult.getFailureMessageEntity("账套号为空");
		}
		try {
			logger.info("==========根据账套号：{}开始保存余额数据=========",receiverGlYebEntity.getZth());
			SenderGlYebEntity glYebEntity = senderGlYebDao.saveGlYebHealthBureau(receiverGlYebEntity);
			logger.info("==========根据账套号：{}保存得余额数据id为:{}=========",receiverGlYebEntity.getZth(),glYebEntity.getId());
			/*logger.info("=========开始保存余额中间表========");
			receiverGlYebEntity.setJid(glYebEntity.getId());//卫生局的id
			receiverGlYebEntity.setYid(jsonObject.getString("id")==null?String.valueOf(UUIDLong.longUUID()):jsonObject.getString("id"));//卫生院的id
			receiverGlYebEntity.setId(String.valueOf(UUIDLong.longUUID()));//自己本身的id
			ReceiverGlYebEntity receiverGlYeb = receiverGlYebDao.saveReceiverGlYebRelevance(receiverGlYebEntity);
			logger.info("==========保存得余额中间表的数据id为:{}=========",receiverGlYeb.getId());*/
			JSONObject jsonObject1=new JSONObject();
			jsonObject1.put("status","200");
			jsonObject1.put("yeb",glYebEntity);
			return jsonObject1;
		}catch (Exception e){
			logger.info("根据账套号：{}保存卫生局GROU8余额数据失败,",receiverGlYebEntity.getZth(),e.getMessage());
			return BaseResult.getFailureMessageEntity("保存卫生局GROU8余额数据失败");
		}
	}



	@Override
	public Object deleteEntityHealthBureau(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderGlYebEntity receiverGlYebEntity = jsonObject.toJavaObject(SenderGlYebEntity.class);
		if (!checkStr(jsonObject.getString("kjnd"))) {
			logger.info("========保存余额数据时会计年度为空==========");
			return BaseResult.getFailureMessageEntity("会计年度为空");
		}
		if (!checkStr(jsonObject.getString("gsdm"))) {
			logger.info("========保存余额数据时公司代码为空==========");
			return BaseResult.getFailureMessageEntity("公司代码为空");
		}
		if (!checkStr(jsonObject.getString("zth"))) {
			logger.info("========保存余额数据时账套号为空==========");
			return BaseResult.getFailureMessageEntity("账套号为空");
		}
		try {
			logger.info("==========开始删除余额数据,会计年度为:{}，公司代码为：{}，账套号为：{}，科目代码为:{}=========",
					jsonObject.getString("kjnd"),jsonObject.getString("gsdm"),jsonObject.getString("zth"),receiverGlYebEntity.getKmdm());
			Integer integer = senderGlYebDao.deleteGlYebHealthBureau(receiverGlYebEntity);
			JSONObject jsonObject1=new JSONObject();
			jsonObject1.put("status","200");
			jsonObject1.put("yeb",integer);
			return jsonObject1;
		}catch (Exception e){
			logger.info("根据账套号：{}，会计年度：{}删除卫生局GROU8余额数据失败,",receiverGlYebEntity.getZth(),receiverGlYebEntity.getKjnd(),e.getMessage());
			return BaseResult.getFailureMessageEntity("删除卫生局GROU8余额数据失败");
		}
	}
}
