package com.hzya.frame.grpU8.nxproof.glxmzl.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.glxmzl.dao.ISenderGlXmzlDao;
import com.hzya.frame.grpU8.nxproof.glxmzl.entity.SenderGlXmzlEntity;
import com.hzya.frame.grpU8.nxproof.glxmzl.service.ISenderGlXmzlService;
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;

/**
 * (GlXmzl)表服务实现类
 *
 * @author makejava
 * @since 2024-06-20 10:02:38
 */
@Service(value = "SenderGlXmzlServiceImpl")
public class SenderGlXmzlServiceImpl extends BaseService<SenderGlXmzlEntity, String> implements ISenderGlXmzlService {


     
    private ISenderGlXmzlDao senderGlXmzlDao;
    
    @Autowired
	public void setSenderGlXmzlDao(ISenderGlXmzlDao dao) {
		this.senderGlXmzlDao = dao;
		this.dao = dao;
	}

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

	private String sendXmzlToHealthBureau(List<SenderGlXmzlEntity> senderGlXmzlEntities){
			if (CollectionUtils.isEmpty(senderGlXmzlEntities)) {
				logger.info("GRPU8没有需要同步得项目资料信息");
				return "GRPU8没有需要同步得项目资料信息";
			} else {
				for (SenderGlXmzlEntity xmzlEntity : senderGlXmzlEntities) {
					//xmzlEntity.setKjnd("2021");
					//根据会计年度，公司代码，项目代码查询数据是否已经同步过
					SenderGlXmzlEntity receiverGlXmzlEntity = new SenderGlXmzlEntity();
					receiverGlXmzlEntity.setKjnd(xmzlEntity.getKjnd());
					receiverGlXmzlEntity.setGsdm(xmzlEntity.getGsdm());
					receiverGlXmzlEntity.setXmdm(xmzlEntity.getXmdm());
					try {
						List<SenderGlXmzlEntity> receiverGlXmzlEntities=queryEntityPageHealthBureau(receiverGlXmzlEntity);
						//设置id为会计年度，公司代码，项目代码拼接，测试时候这样，具体看情况
						xmzlEntity.setId(xmzlEntity.getGsdm() + '-' + xmzlEntity.getKjnd() + '-' + xmzlEntity.getXmdm());
						JsonResultEntity jsonResultEntity=null;
						if (CollectionUtils.isEmpty(receiverGlXmzlEntities)) {
							logger.info("===========开始将公司代码为:{}得GRPU8项目资料信息同步保存到卫生局GRPU8中=============",xmzlEntity.getGsdm());
							JSONObject jsonObjectStr = new JSONObject();
							jsonObjectStr.put("jsonStr", xmzlEntity);
							Object o = saveEntityHealthBureau(jsonObjectStr);
							JSONObject json = (JSONObject) JSON.toJSON(o);
							if(json.getString("status").equalsIgnoreCase("200")){
								//JSONObject json = (JSONObject) JSONObject.toJSON(jsonResultEntity.getAttribute());
								logger.info("======公司代码为：{}得GROU8项目资料信息同步保存到卫生局得返回结果为:{}=======",xmzlEntity.getGsdm(),json.getString("xmzl"));
							}
							/*if (jsonObjectAttribute != null) {
								logger.info("===========开始将发送方的GRPU8项目资料信息新增到项目资料日志中=============");
								//保存发送方得日志
								xmzlEntity.setDataSourceCode(grp_data_source_code);
								senderGlXmzlDao.saveSenderGlXmzlEntity(xmzlEntity);
							}*/
						} else {
							logger.info("===========开始将公司代码为：{}得GRPU8项目资料信息同步更新到卫生局GRPU8中=============",xmzlEntity.getGsdm());
							JSONObject jsonObjectStr = new JSONObject();
							jsonObjectStr.put("jsonStr", xmzlEntity);
							Object o = updateEntityHealthBureau(jsonObjectStr);
							JSONObject json = (JSONObject) JSON.toJSON(o);
							if(json.getString("status").equalsIgnoreCase("200")){
								//JSONObject json = (JSONObject) JSONObject.toJSON(jsonResultEntity.getAttribute());
								logger.info("======公司代码：{}得GROU8项目资料信息同步更新到卫生局得返回结果为:{}=======",xmzlEntity.getGsdm(),json.getString("xmzl"));
							}
							/*if (jsonObjectAttribute != null) {
								logger.info("===========开始将发送方的GRPU8项目资料信息更新到项目资料日志中=============");
								//保存发送方得日志
								xmzlEntity.setDataSourceCode(grp_data_source_code);
								senderGlXmzlDao.updateSenderGlXmzlEntity(xmzlEntity);
							}*/
						}
					} catch (Exception e) {
						logger.info("公司代码为:{}得GRPU8项目资料同步失败:{}", xmzlEntity.getGsdm(),e.getMessage());
						e.printStackTrace();
					}
				}
				return "同步GROU8项目资料成功";
			}
	}

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

	//保存项目资料
	@Override
	public Object saveEntityHealthBureau(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderGlXmzlEntity receiverGlXmzlEntity = jsonObject.toJavaObject(SenderGlXmzlEntity.class);
		if (StrUtil.isEmpty(jsonObject.getString("kjnd"))) {
			logger.info("保存项目资料时会计年度为空");
			return BaseResult.getFailureMessageEntity("会计年度为空");
		}
		if (StrUtil.isEmpty(jsonObject.getString("gsdm"))) {
			logger.info("保存项目资料时公司代码为空");
			return BaseResult.getFailureMessageEntity("公司代码为空");
		}
		if (StrUtil.isEmpty(jsonObject.getString("xmdm"))) {
			logger.info("保存项目资料时项目资料代码为空");
			return BaseResult.getFailureMessageEntity("项目资料代码为空");
		}
		if (StrUtil.isEmpty(jsonObject.getString("xmmc"))) {
			logger.info("保存项目资料时项目资料名称为空");
			return BaseResult.getFailureMessageEntity("项目资料名称为空");
		}
		try {
			SenderGlXmzlEntity senderGlXmzlEntity=new SenderGlXmzlEntity();
			senderGlXmzlEntity.setGsdm(receiverGlXmzlEntity.getGsdm());
			senderGlXmzlEntity.setKjnd(receiverGlXmzlEntity.getKjnd());
			senderGlXmzlEntity.setXmdm(receiverGlXmzlEntity.getXmdm());
			List<SenderGlXmzlEntity> senderGlXmzlEntities = queryEntityPageHealthBureau(senderGlXmzlEntity);
			if(CollectionUtils.isEmpty(senderGlXmzlEntities)){
				receiverGlXmzlEntity.setId(receiverGlXmzlEntity.getGsdm()+'-'+receiverGlXmzlEntity.getKjnd()+'-'+receiverGlXmzlEntity.getXmdm());
				logger.info("=======开始根据公司代码:{}保存项目资料，id为:{}",receiverGlXmzlEntity.getGsdm(),receiverGlXmzlEntity.getId());
				SenderGlXmzlEntity receiverGlXmzlEntitySave = senderGlXmzlDao.saveGlXmzlHealthBureau(receiverGlXmzlEntity);
				JSONObject jsonObject1=new JSONObject();
				jsonObject1.put("status","200");
				jsonObject1.put("xmzl",receiverGlXmzlEntitySave);
				return jsonObject1;
			}else{
				logger.info("=======开始根据公司代码:{}更新项目资料，id为:{}",receiverGlXmzlEntity.getGsdm(),receiverGlXmzlEntity.getId());
				SenderGlXmzlEntity receiverGlXmzlEntity1 = senderGlXmzlDao.updateGlXmzlHealthBureau(receiverGlXmzlEntity);
				JSONObject jsonObject1=new JSONObject();
				jsonObject1.put("status","200");
				jsonObject1.put("xmzl",receiverGlXmzlEntity1);
				return jsonObject1;
			}
		}catch (Exception e){
			logger.info("根据公司代码:{}保存GROU8项目资料失败:{}",receiverGlXmzlEntity.getGsdm(),e.getMessage());
			return  BaseResult.getFailureMessageEntity("保存GROU8项目资料失败");
		}
	}

	//更新项目资料
	@Override
	public Object updateEntityHealthBureau(JSONObject json) {
		JSONObject jsonObject = json.getJSONObject("jsonStr");
		SenderGlXmzlEntity receiverGlXmzlEntity = jsonObject.toJavaObject(SenderGlXmzlEntity.class);
		if (StrUtil.isEmpty(jsonObject.getString("kjnd"))) {
			logger.info("更新项目资料时会计年度为空");
			return BaseResult.getFailureMessageEntity("会计年度为空");
		}
		if (StrUtil.isEmpty(jsonObject.getString("gsdm"))) {
			logger.info("更新项目资料时公司代码为空");
			return BaseResult.getFailureMessageEntity("公司代码为空");
		}
		if (StrUtil.isEmpty(jsonObject.getString("xmdm"))) {
			logger.info("更新项目资料时项目资料代码为空");
			return BaseResult.getFailureMessageEntity("项目资料代码为空");
		}
		try {
			receiverGlXmzlEntity.setId(receiverGlXmzlEntity.getGsdm()+'-'+receiverGlXmzlEntity.getKjnd()+'-'+receiverGlXmzlEntity.getXmdm());
			logger.info("=======开始根据公司代码:{}更新项目资料，id为:{}",receiverGlXmzlEntity.getGsdm(),receiverGlXmzlEntity.getId());
			SenderGlXmzlEntity receiverGlXmzlEntity1 = senderGlXmzlDao.updateGlXmzlHealthBureau(receiverGlXmzlEntity);
			JSONObject jsonObject1=new JSONObject();
			jsonObject1.put("status","200");
			jsonObject1.put("xmzl",receiverGlXmzlEntity1);
			return jsonObject1;
		}catch (Exception e){
			logger.info("根据公司代码:{}更新GROU8项目资料失败:{}",receiverGlXmzlEntity.getGsdm(),e.getMessage());
			return BaseResult.getFailureMessageEntity("更新GROU8项目资料失败");
		}
	}

	//删除项目资料
	@Override
	public Object deleteEntityHealthBureau(JSONObject json) {
			JSONObject jsonObject = json.getJSONObject("jsonStr");
			SenderGlXmzlEntity receiverGlXmzlEntity = jsonObject.toJavaObject(SenderGlXmzlEntity.class);
			if (StrUtil.isEmpty(jsonObject.getString("kjnd"))) {
				logger.info("删除项目资料时会计年度为空");
				return BaseResult.getFailureMessageEntity("会计年度为空");
			}
			if (StrUtil.isEmpty(jsonObject.getString("gsdm"))) {
				logger.info("删除项目资料时公司代码为空");
				return BaseResult.getFailureMessageEntity("公司代码为空");
			}
			try {
				receiverGlXmzlEntity.setId(receiverGlXmzlEntity.getGsdm()+'-'+receiverGlXmzlEntity.getKjnd()+'-'+receiverGlXmzlEntity.getXmdm());
				logger.info("=======开始根据公司代码:{}删除项目资料为:{}",receiverGlXmzlEntity.getGsdm(),receiverGlXmzlEntity.getXmdm());
				Integer integer = senderGlXmzlDao.deleteGlXmzlHealthBureau(receiverGlXmzlEntity);
				JSONObject jsonObject1=new JSONObject();
				jsonObject1.put("status","200");
				jsonObject1.put("xmzl",integer);
				return jsonObject1;
			}catch (Exception e){
				logger.info("根据公司代码:{}删除GROU8项目资料：{}失败:{}",receiverGlXmzlEntity.getGsdm(),receiverGlXmzlEntity.getXmdm(),e.getMessage());
				return BaseResult.getFailureMessageEntity("删除GROU8项目资料失败");
			}
	}

	//查询项目资料
	@Override
	public List<SenderGlXmzlEntity> queryEntityPageHealthBureau(SenderGlXmzlEntity senderGlXmzlEntity) {
		try {
			logger.info("====根据公司代码:{}查询卫生局中得项目资料数据====",senderGlXmzlEntity.getGsdm());
			List<SenderGlXmzlEntity> receiverGlXmzlEntities = senderGlXmzlDao.queryGlXmzlHealthBureau(senderGlXmzlEntity);
			return receiverGlXmzlEntities;
		}catch (Exception e){
			logger.info("根据公司代码:{}查询卫生局的GROU8项目资料失败:{}",senderGlXmzlEntity.getGsdm(),e.getMessage());
			e.printStackTrace();
		}
		return null;
	}
}
