package com.saye.hrs.service.impl;

import com.saye.hrs.commons.date.DateDUtil;
import com.saye.hrs.commons.log.LogUtil;
import com.saye.hrs.commons.string.StringDUtil;
import com.saye.hrs.commons.webservice.WSUtil;
import com.saye.hrs.mapper.HospitalMapper;
import com.saye.hrs.mapper.PatientMapper;
import com.saye.hrs.service.HospitalService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * (Hospital)表服务实现类
 *
 * @author makejava
 * @since 2021-09-15 09:38:59
 */
@Service
@Transactional
public class HospitalServiceImpl implements HospitalService {
    @Resource
    private HospitalMapper hospitalMapper;
    @Resource
    private PatientMapper patientMapper;

    @Override
    public HashMap<Object, Object> findHospitalById(HashMap<Object,Object> map) {
        return this.hospitalMapper.findHospitalById(map);
    }

    @Override
    public  List<HashMap<Object, Object>> findHospitalList(HashMap<Object,Object> map) {
    	List<HashMap<Object, Object>> list = this.hospitalMapper.findHospitalList(map);
    	for(HashMap<Object, Object> hosMap : list) {
    		hosMap.put("floorList", this.hospitalMapper.findHospitalFloorList(hosMap));
    	}
        return list;
    }

    @Override
    public void insertHospital(HashMap<Object,Object> map) {
        this.hospitalMapper.insertHospital(map);
    }

    @Override
    public void updateHospital(HashMap<Object,Object> map) {
        this.hospitalMapper.updateHospital(map);
    }

    @Override
    public void deleteHospitalById(HashMap<Object,Object> map) {
        this.hospitalMapper.deleteHospitalById(map);
    }

    @Override
    public void updateHospitalAbout(HashMap<Object, Object> map) {
        this.hospitalMapper.updateHospitalAbout(map);
    }

    @Override
    public List<HashMap<Object, Object>> findHospitalNoticeList(HashMap<Object, Object> map) {
        return this.hospitalMapper.findHospitalNoticeList(map);
    }

    @Override
    public void addHospitalNotice(HashMap<Object, Object> map) {
        this.hospitalMapper.addHospitalNotice(map);
    }

    @Override
    public void updateHospitalNotice(HashMap<Object, Object> map) {
        this.hospitalMapper.updateHospitalNotice(map);
    }

    @Override
    public void deleteHospitalNotice(HashMap<Object, Object> map) {
        this.hospitalMapper.deleteHospitalNotice(map);
    }

    @Override
    public List<HashMap<Object, Object>> findHospitalArticleList(HashMap<Object, Object> map) {
        return this.hospitalMapper.findHospitalArticleList(map);
    }

    @Override
    public void addHospitalArticle(HashMap<Object, Object> map) {
        this.hospitalMapper.addHospitalArticle(map);
    }

    @Override
    public void updateHospitalArticle(HashMap<Object, Object> map) {
        this.hospitalMapper.updateHospitalArticle(map);
    }

    @Override
    public void deleteHospitalArticle(HashMap<Object, Object> map) {
        this.hospitalMapper.deleteHospitalArticle(map);
    }

	@Override
	public void addMedicalGuide(HashMap<Object, Object> map) {
		this.hospitalMapper.addMedicalGuide(map);
	}

	@Override
	public void updateMedicalGuide(HashMap<Object, Object> map) {
		this.hospitalMapper.updateMedicalGuide(map);
	}

	@Override
	public void deleteMedicalGuide(HashMap<Object, Object> map) {
		this.hospitalMapper.deleteMedicalGuide(map);
	}

	@Override
	public List<HashMap<Object, Object>> findMedicalGuideList(HashMap<Object, Object> map) {
		return this.hospitalMapper.findMedicalGuideList(map);
	}

	@Override
	public List<HashMap<Object, Object>> findMedicalGuide() {
		List<HashMap<Object, Object>> typeList = this.hospitalMapper.findMedicalGuideType();
		for(HashMap<Object, Object> map : typeList) {
			map.put("childList", this.hospitalMapper.findMedicalGuideList(map));
		}
		return typeList;
	}

	@Override
	public List<HashMap<Object, Object>> findHospitalFloorList(HashMap<Object, Object> map) {
		
		return this.hospitalMapper.findHospitalFloorList(map);
	}

	@Override
	public void updateHospitalFloor(HashMap<Object, Object> map) {
		// TODO Auto-generated method stub
		this.hospitalMapper.updateHospitalFloor(map);
	}

	@Override
	public void deleteHospitalFloor(HashMap<Object, Object> map) {
		// TODO Auto-generated method stub
		this.hospitalMapper.deleteHospitalFloor(map);
	}

	@Override
	public void addHospitalFloor(HashMap<Object, Object> map) {
		// TODO Auto-generated method stub
		this.hospitalMapper.addHospitalFloor(map);
	}

	@Override
	public void problemFeedback(Map map) {
		// TODO Auto-generated method stub
		this.hospitalMapper.problemFeedback(map);
	}

	@Override
	public List<HashMap<Object, Object>> findMessageNotice(HashMap<Object, Object> map) {
		// TODO Auto-generated method stub
		return this.hospitalMapper.findMessageNotice(map);
	}

	@Override
	public void setMessageNoticeRead(Map map) {
		// TODO Auto-generated method stub
		this.hospitalMapper.setMessageNoticeRead(map);
	}

	@Override
	public List<HashMap<Object, Object>> findMedicalGuideById(HashMap<Object,Object> map) {
		// TODO Auto-generated method stub
		return this.hospitalMapper.findMedicalGuideById(map);
	}

	@Override
	public void addPayRequest(Map map) {

		this.hospitalMapper.addPayRequest(map);
	}

	@Override
	public HashMap<Object, Object> addHisRecharge(Map map) throws Exception {

		String searchMerOrderId = StringDUtil.changeNullToEmpty(map.get("merOrderId"));

		LogUtil.debug(this.getClass(),searchMerOrderId+"充值结果信息写入数据库开始");

		String hisStatus = "";
		String czType = StringDUtil.changeNullToEmpty(map.get("czType"));
		//将支付结果信息存入
		this.hospitalMapper.updatePayRequestResultText(map);
		LogUtil.debug(this.getClass(),searchMerOrderId+"支付结果信息存入结束");
		String status = StringDUtil.changeNullToEmpty(map.get("status"));
		HashMap<Object,Object> patMap = this.patientMapper.findPatientByMerOrderId(map);
		LogUtil.debug(this.getClass(),"根据订单id查询订单信息结束");
		if("TRADE_SUCCESS".equals(status)) {
			String merOrderId = StringDUtil.changeNullToEmpty(patMap.get("MERORDERID"));
			String card_type = StringDUtil.changeNullToEmpty(patMap.get("CARD_TYPE"));
			String medical_card = StringDUtil.changeNullToEmpty(patMap.get("MEDICAL_CARD"));
			String targetOrderId = StringDUtil.changeNullToEmpty(patMap.get("TARGETORDERID"));
			String patient_id = StringDUtil.changeNullToEmpty(patMap.get("PATIENT_ID"));
			String totalAmount = StringDUtil.changeNullToEmpty(patMap.get("TOTALAMOUNT"));
			BigDecimal ta = new BigDecimal(totalAmount);
			ta = ta.divide(new BigDecimal(100));
			totalAmount = ta.toString();
			String czlx = "4";
			String zzjjylsh = "";
			String paytype = "12";
			String[] czyhs = {"0421","0422","0423","0424","0425","0426","0427","0428","0429","0430","0431","0432","0433","0434","0435","0436","0437","0438","0439","0440"};
			String czyh = czyhs[(int) (Math.random()*20)];
			
			//先更新充值状态
			map.put("hisStatus", "1");
			int num = this.patientMapper.updatePayRequestHisStatus(map);
			LogUtil.debug(this.getClass(),searchMerOrderId+"更新充值状态结束");
			if(num==1) {

				HashMap<Object, Object> resultMap= null;

				//门诊充值和住院充值
				if ("1".equals(czType)){
					resultMap = WSUtil.mzRecharge(patient_id, totalAmount, czlx, medical_card, card_type, czyh, zzjjylsh, merOrderId, merOrderId, merOrderId, merOrderId, paytype);
				}else{
					resultMap = WSUtil.zyRecharge(patient_id, totalAmount, czlx, medical_card, card_type, czyh, zzjjylsh, merOrderId, merOrderId, merOrderId, merOrderId, paytype);
				}
				LogUtil.debug(this.getClass(),searchMerOrderId+"调用his门诊和住院充值接口结束");

				if("0".equals(resultMap.get("errCode"))) {
					hisStatus = "1";
				}else {
					hisStatus = "2";
				}
				map.put("hisStatus", hisStatus);
				map.put("hisMsg", resultMap.get("errMsg"));
				//更新his充值状态
				this.patientMapper.updatePayRequestHisStatus1(map);
				LogUtil.debug(this.getClass(),searchMerOrderId+"再次调用his门诊和住院充值接口结束");

			}
		}else {
			map.put("status", "未支付");
			String create_time = StringDUtil.changeNullToEmpty(patMap.get("CREATE_TIME"));
			if(!"".equals(create_time)) {
				long startTime = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd_HH_mm_ss, create_time).getTime();
				long endTime = new Date().getTime();
				long betweenMinutes = (long) ( ( endTime - startTime ) / ( 1000 * 60 ) );
				if(betweenMinutes>10) { //10分钟未完成支付 修改状态为未支付
					this.hospitalMapper.updatePayRequestResultText(map);
					LogUtil.debug(this.getClass(),searchMerOrderId+"未支付，状态更新为未支付");

				}
			}
		}
		HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
		resultMap.put("hisStatus", hisStatus);
		return resultMap;
	}

	@Override
	public void setMessageNoticeAllRead(Map map) {
		this.hospitalMapper.setMessageNoticeAllRead(map);
	}


	public static void main(String[] args) {
		String create_time = "2022-12-16 15:30:00";
		if(!"".equals(create_time)) {
			long startTime = DateDUtil.strToDate(DateDUtil.yyyy_MM_dd_HH_mm_ss, create_time).getTime();
			long endTime = new Date().getTime();
			long betweenMinutes = (long) ( ( endTime - startTime ) / ( 1000 * 60 ) );
			if(betweenMinutes>10) { //5分钟未完成支付 修改状态为未支付
				System.out.println(betweenMinutes);
			}
			System.out.println(betweenMinutes);
		}
	}
}
