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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.grpU8.nxproof.pubkjqj.dao.ISenderPubkjqjDao;
import com.hzya.frame.grpU8.nxproof.pubkjqj.entity.SenderPubkjqjEntity;
import com.hzya.frame.grpU8.nxproof.pubkjqj.service.ISenderPubkjqjService;
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;

/**
 * (Pubkjqj)表服务实现类
 *
 * @author makejava
 * @since 2024-06-20 09:49:34
 */
@Service(value = "SenderPubkjqjServiceImpl")
public class SenderPubkjqjServiceImpl extends BaseService<SenderPubkjqjEntity, String> implements ISenderPubkjqjService {


    private ISenderPubkjqjDao senderPubkjqjDao;
    
    @Autowired
	public void setSenderPubkjqjDao(ISenderPubkjqjDao dao) {
		this.senderPubkjqjDao = dao;
		this.dao = dao;
	}

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

	private String sendKjqjToHealthBureau(List<SenderPubkjqjEntity> senderPubkjqjEntities){
			if (CollectionUtils.isEmpty(senderPubkjqjEntities)) {
				logger.info("GRPU8没有需要同步得会计期间信息");
				return "GRPU8没有需要同步得会计期间信息";
			} else {
				for (SenderPubkjqjEntity kjqjEntity : senderPubkjqjEntities) {
					//kjqjEntity.setKjnd("2021");
					//kjqjEntity.setQsrq(kjqjEntity.getQsrq().replace("2024", "2021"));
					//kjqjEntity.setJsrq(kjqjEntity.getJsrq().replace("2024", "2021"));
					//根据会计年度，公司代码，账套号查询数据是否已经同步过
					SenderPubkjqjEntity receiverPubkjqjEntity = new SenderPubkjqjEntity();
					receiverPubkjqjEntity.setKjnd(kjqjEntity.getKjnd());
					receiverPubkjqjEntity.setGsdm(kjqjEntity.getGsdm());
					receiverPubkjqjEntity.setZth(kjqjEntity.getZth());
					receiverPubkjqjEntity.setKjqjxh(kjqjEntity.getKjqjxh());
					try {
						//查询接收方的数据是否存在
						List<SenderPubkjqjEntity> receiverPubkjqjEntities = queryEntityPageHealthBureau(receiverPubkjqjEntity);
						//设置id为会计年度，公司代码，账套号,会计期间序号拼接，测试时候这样，具体看情况
						kjqjEntity.setId(kjqjEntity.getGsdm() + '-' + kjqjEntity.getKjnd() + '-' + kjqjEntity.getZth() + '-' + kjqjEntity.getKjqjxh());
						JsonResultEntity jsonResultEntity=null;
						if (CollectionUtils.isEmpty(receiverPubkjqjEntities)) {
							logger.info("===========开始将账套号为：{}得GRPU8会计期间数据同步新增到卫生局GRPU8中=============",kjqjEntity.getZth());
							JSONObject jsonObjectStr = new JSONObject();
							jsonObjectStr.put("jsonStr", kjqjEntity);
							Object o = saveEntityHealthBureau(jsonObjectStr);
							JSONObject json = (JSONObject) JSON.toJSON(o);
							if(json.getString("status").equalsIgnoreCase("200")){
								//JSONObject jsonObjectAttribute = (JSONObject) JSONObject.toJSON(jsonResultEntity.getAttribute());
								logger.info("账套号{}得GROU8会计期间数据同步保存得返回结果为:{}",kjqjEntity.getZth(),json.getString("kjqj"));
							}
							/*if (jsonObjectAttribute != null) {
								logger.info("===========开始将发送方的GRPU8会计期间新增到会计期间日志中=============");
								//保存发送方得日志
								kjqjEntity.setDataSourceCode(grp_data_source_code);
								senderPubkjqjDao.saveSenderPubkjqjEntity(kjqjEntity);
							}*/
						} else {
							logger.info("===========开始将账套号为：{}得GRPU8会计期间数据同步更新到卫生局GRPU8中=============",kjqjEntity.getZth());
							JSONObject jsonObjectStr = new JSONObject();
							jsonObjectStr.put("jsonStr", kjqjEntity);
							Object o = updateEntityHealthBureau(jsonObjectStr);
							JSONObject json = (JSONObject) JSON.toJSON(o);
							if(json.getString("status").equalsIgnoreCase("200")){
								//JSONObject jsonObjectAttribute = (JSONObject) JSONObject.toJSON(jsonResultEntity.getAttribute());
								logger.info("账套号{}得GROU8会计期间数据同步更新得返回结果为:{}",kjqjEntity.getZth(),json.getString("kjqj"));
							}
							/*if (jsonObjectAttribute != null) {
								logger.info("===========开始将发送方的GRPU8会计期间更新到会计期间日志中=============");
								//保存发送方得日志
								kjqjEntity.setDataSourceCode(grp_data_source_code);
								senderPubkjqjDao.updateSenderPubkjqjEntity(kjqjEntity);
							}*/
						}
					} catch (Exception e) {
						logger.info("账套号{}得GRPU8会计期间信息同步失败:{}", kjqjEntity.getZth(),e.getMessage());
					}
				}
				return "同步GROU8会计期间信息成功";
			}
	}

	//查询会计期间
	@Override
	public Object queryEntityPage(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderPubkjqjEntity senderPubkjqjEntity = jsonObject.toJavaObject(SenderPubkjqjEntity.class);
		List<SenderPubkjqjEntity> senderPubkjqjEntities=new ArrayList<>();
		//如果没有传入年度，则查询当年得，避免数据查询太大
		if(senderPubkjqjEntity.getKjnd()==null){
			senderPubkjqjEntity.setKjnd(DateUtil.format(new Date(),"yyyy"));
		}
		if(StrUtil.isEmpty(senderPubkjqjEntity.getZth())){
			logger.info("======根据账套号:{}查询会计期间为空",senderPubkjqjEntity.getZth());
			return BaseResult.getFailureMessageEntity("请传递账套号");
		}
		try {
		/*	switch (senderPubkjqjEntity.getZth()){
				case "003"://练市人民医院
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "004"://双林镇中心卫生院
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "005"://练市镇中心卫生院
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "007"://善琏镇卫生院
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "009":
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "010":
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "011":
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "012":
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				case "001"://南浔区人民医院
					senderPubkjqjEntities = senderPubkjqjDao.queryByLike(senderPubkjqjEntity);
					break;
				default:
					break;
			}*/
			senderPubkjqjEntities = senderPubkjqjDao.querySenderPubkjqjEntity(senderPubkjqjEntity);
			PageInfo pageInfo=new PageInfo(senderPubkjqjEntities);
			JSONObject jsonObject1=new JSONObject();
			jsonObject1.put("status","200");
			jsonObject1.put("pageInfo",pageInfo);
			return jsonObject1;
		}catch (Exception e){
			logger.info("查询账套号为：{}得GROU8会计期间失败:{}",senderPubkjqjEntity.getZth(),e.getMessage());
			return BaseResult.getFailureMessageEntity("查询GROU8会计期间失败");
		}
	}


	//查询会计期间
	@Override
	public List<SenderPubkjqjEntity> queryEntityPageHealthBureau(SenderPubkjqjEntity senderPubkjqjEntity) {
		try {
			logger.info("====开始根据账套号为:{}查询卫生局中得会计期间数据======",senderPubkjqjEntity.getZth());
			List<SenderPubkjqjEntity> receiverPubkjqjEntities = senderPubkjqjDao.queryPubkjqjHealthBureau(senderPubkjqjEntity);
			return	receiverPubkjqjEntities;
		}catch (Exception e){
			logger.info("查询账套号为:{}得GROU8会计期间失败:{}",senderPubkjqjEntity.getZth(),e.getMessage());
			e.printStackTrace();
		}
		return null;
	}

	//保存会计期间
	@Override
	public Object saveEntityHealthBureau(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderPubkjqjEntity receiverPubkjqjEntity = jsonObject.toJavaObject(SenderPubkjqjEntity.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("账套编码为空");
		}
		if (!checkStr(jsonObject.getString("kjqjxh"))) {
			logger.info("==========保存会计期间时会计期间序号为空============");
			return BaseResult.getFailureMessageEntity("会计期间序号为空");
		}
		try {
			SenderPubkjqjEntity senderPubkjqjEntity=new SenderPubkjqjEntity();
			senderPubkjqjEntity.setZth(receiverPubkjqjEntity.getZth());
			senderPubkjqjEntity.setGsdm(receiverPubkjqjEntity.getGsdm());
			senderPubkjqjEntity.setKjnd(receiverPubkjqjEntity.getKjnd());
			senderPubkjqjEntity.setKjqjxh(receiverPubkjqjEntity.getKjqjxh());
			List<SenderPubkjqjEntity> senderPubkjqjEntities = queryEntityPageHealthBureau(senderPubkjqjEntity);
			if(CollectionUtils.isEmpty(senderPubkjqjEntities)){
				receiverPubkjqjEntity.setId(receiverPubkjqjEntity.getGsdm()+'-'+receiverPubkjqjEntity.getKjnd()+'-'+receiverPubkjqjEntity.getZth()+'-'+receiverPubkjqjEntity.getKjqjxh());
				logger.info("==========开始保存账套号为：{}得会计期间，id为:{}=========",receiverPubkjqjEntity.getZth(),receiverPubkjqjEntity.getId());
				SenderPubkjqjEntity pubkjqjEntity = senderPubkjqjDao.savePubkjqjHealthBureau(receiverPubkjqjEntity);
				JSONObject jsonObject1=new JSONObject();
				jsonObject1.put("status","200");
				jsonObject1.put("kjqj",pubkjqjEntity);
				return jsonObject1;
			}else{
				logger.info("==========开始更新账套号为：{}得会计期间，id为:{}=========",receiverPubkjqjEntity.getZth(),receiverPubkjqjEntity.getId());
				SenderPubkjqjEntity pubkjqjEntity = senderPubkjqjDao.updatePubkjqjHealthBureau(receiverPubkjqjEntity);
				JSONObject jsonObject1=new JSONObject();
				jsonObject1.put("status","200");
				jsonObject1.put("kjqj",pubkjqjEntity);
				return jsonObject1;
			}
		}catch (Exception e){
			logger.info("保存账套号为：{}得GROU8会计期间失败:{}",receiverPubkjqjEntity.getZth(),e.getMessage());
			return BaseResult.getFailureMessageEntity("保存GROU8会计期间失败");
		}
	}

	//更新会计期间
	@Override
	public Object updateEntityHealthBureau(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderPubkjqjEntity receiverPubkjqjEntity = jsonObject.toJavaObject(SenderPubkjqjEntity.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("账套编码为空");
		}
		if (!checkStr(jsonObject.getString("kjqjxh"))) {
			logger.info("==========更新会计期间时会计期间序号为空============");
			return BaseResult.getFailureMessageEntity("会计期间序号为空");
		}
		try {
			receiverPubkjqjEntity.setId(receiverPubkjqjEntity.getGsdm()+'-'+receiverPubkjqjEntity.getKjnd()+'-'+receiverPubkjqjEntity.getZth()+'-'+receiverPubkjqjEntity.getKjqjxh());
			logger.info("==========开始更新账套号为：{}得会计期间，id为:{}=========",receiverPubkjqjEntity.getZth(),receiverPubkjqjEntity.getId());
			SenderPubkjqjEntity pubkjqjEntity = senderPubkjqjDao.updatePubkjqjHealthBureau(receiverPubkjqjEntity);
			JSONObject jsonObject1=new JSONObject();
			jsonObject1.put("status","200");
			jsonObject1.put("kjqj",pubkjqjEntity);
			return jsonObject1;
		}catch (Exception e){
			logger.info("更新账套号为：{}得GROU8会计期间失败:{}",receiverPubkjqjEntity.getZth(),e.getMessage());
			return BaseResult.getFailureMessageEntity("更新GROU8会计期间失败");
		}
	}

	//删除会计期间
	@Override
	public Object deleteEntityHealthBureau(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderPubkjqjEntity receiverPubkjqjEntity = jsonObject.toJavaObject(SenderPubkjqjEntity.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("账套编码为空");
		}
		if (!checkStr(jsonObject.getString("kjqjxh"))) {
			logger.info("==========删除会计期间时会计期间序号为空============");
			return BaseResult.getFailureMessageEntity("会计期间序号为空");
		}
		try {
			receiverPubkjqjEntity.setId(receiverPubkjqjEntity.getGsdm()+'-'+receiverPubkjqjEntity.getKjnd()+'-'+receiverPubkjqjEntity.getZth()+'-'+receiverPubkjqjEntity.getKjqjxh());
			logger.info("==========开始删除账套号为：{}得会计期间，id为:{}=========",receiverPubkjqjEntity.getZth(),receiverPubkjqjEntity.getId());
			Integer integer = senderPubkjqjDao.deletePubkjqjHealthBureau(receiverPubkjqjEntity);
			JSONObject jsonObject1=new JSONObject();
			jsonObject1.put("status","200");
			jsonObject1.put("kjqj",integer);
			return jsonObject1;
		}catch (Exception e){
			logger.info("删除账套号为：{}得GROU8会计期间失败:{}",receiverPubkjqjEntity.getZth(),e.getMessage());
			return BaseResult.getFailureMessageEntity("删除GROU8会计期间失败");
		}
	}


}
