package com.saye.hrs.controller;

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.service.HospitalService;
import com.saye.hrs.service.OrderService;
import com.saye.hrs.service.PatientService;
import com.saye.hrs.service.system.LoggerService;
import com.saye.hrs.service.system.ServiceParamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("api")
@Controller
public class HttpController {

    @Resource
    private PatientService patientService;
    @Resource
    private HospitalService hospitalService;
    @Resource
    private OrderService orderService;
    @Resource
    private ServiceParamsService serviceParamsService;
    @Autowired
	private LoggerService loggerService;
    /**
     * 根据openid获取患者信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/getPatientByOpenid")
    @ResponseBody
    public List<HashMap<Object,Object>> getPatientByOpenid(@RequestBody  HashMap<Object,Object> map) throws Exception{

        List<HashMap<Object,Object>> list = this.patientService.findPatientByOpenid(map);
        return list;
    }
    /**
     * 通过主键查询单条数据
     * @param map
     * @return 单条数据
     */
    @RequestMapping("/findPatientById")
    @ResponseBody
    public HashMap<Object,Object> findPatientById(@RequestBody HashMap<Object,Object> map) {
        return this.patientService.findPatientById(map);
    }

    /**
     * 根据openid获取住院患者信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/getZyPatientByOpenid")
    @ResponseBody
    public List<HashMap<Object,Object>> getZyPatientByOpenid(@RequestBody  HashMap<Object,Object> map) throws Exception{
        return this.patientService.getZyPatientByOpenid(map);
    }

    /**
     * 微信用户绑定患者
     * @return
     * @throws Exception
     */
    @RequestMapping("/patientBindWXUser")
    @ResponseBody
    public HashMap<Object,Object> patientBindWXUser(@RequestBody  HashMap<Object,Object> map) throws Exception{
        String errCode = "0";
        String errMsg = "";
        try {
            this.patientService.patientBindWXUser(map);
        }catch(Exception e){
            errCode = "999";
            errMsg = e.toString();
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * 微信用户绑定患者
     * @return
     * @throws Exception
     */
    @RequestMapping("/zyPatientBindWXUser")
    @ResponseBody
    public HashMap<Object,Object> zyPatientBindWXUser(@RequestBody  HashMap<Object,Object> map) throws Exception{
        String errCode = "0";
        String errMsg = "";
        try {
            this.patientService.zyPatientBindWXUser(map);
        }catch(Exception e){
            errCode = "999";
            errMsg = e.toString();
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**设为默认 
     * @return
     * @throws Exception
     */
    @RequestMapping("/setPatientTypeByOpenid")
    @ResponseBody
    public HashMap<Object,Object> setPatientTypeByOpenid(@RequestBody HashMap<Object,Object> map) throws Exception{
        String errCode = "0";
        String errMsg = "";
        try {
            this.patientService.setPatientTypeByOpenid(map);
        }catch(Exception e){
            errCode = "999";
            errMsg = e.toString();
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * 微信用户解除绑定患者
     * @return
     * @throws Exception
     */
    @RequestMapping("/patientUnbindWXUser")
    @ResponseBody
    public HashMap<Object,Object> patientUnbindWXUser(@RequestBody HashMap<Object,Object> map) throws Exception{
        String errCode = "0";
        String errMsg = "";
        try {
            this.patientService.patientUnbindWXUser(map);
        }catch(Exception e){
            errCode = "999";
            errMsg = e.toString();
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }

    /**
     * 修改患者信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/updatePatientByOpenid")
    @ResponseBody
    public HashMap<Object,Object> updatePatientByOpenid(@RequestBody HashMap<Object,Object> map) throws Exception{
        String errCode = "0";
        String errMsg = "";
        try {
            this.patientService.updatePatientByOpenid(map);
        }catch(Exception e){
            errCode = "999";
            errMsg = e.toString();
        }
        HashMap<Object,Object> resultMap = new HashMap<Object,Object>();
        resultMap.put("errCode",errCode);
        resultMap.put("errMsg",errMsg);
        return resultMap;
    }
    
    /** 查询医院信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/findHospital")
    @ResponseBody
    public HashMap<Object,Object> findHospital() throws Exception{
        
    	return this.hospitalService.findHospitalList(new HashMap<Object,Object>()).get(0);
    }
    
    /** 查询医院通知列表
     * @param
     * @return
     */
    @RequestMapping("/findHospitalNotice")
    @ResponseBody
    public List<HashMap<Object, Object>> findHospitalNotice(@RequestBody HashMap<Object,Object> map) {
        return this.hospitalService.findHospitalNoticeList(map);
    }

    /**查询医院信息列表
     * @return 查询结果
     */
    @RequestMapping("/findHospitalAbout")
    @ResponseBody
    public List<HashMap<Object, Object>> findHospitalAbout() {

        return this.hospitalService.findHospitalList(new HashMap<Object,Object>());
    }
    
    /**查询预约须知
     * @return 查询结果
     */
    @RequestMapping("/findOrderNotice")
    @ResponseBody
    public HashMap<Object, Object> findOrderNotice() {

        return this.orderService.findOrderNotice();
    }
    
    /** 查询就医指导
     * @return
     * @throws Exception
     */
    @RequestMapping("/findMedicalGuide")
    @ResponseBody
    public List<HashMap<Object,Object>> findMedicalGuide() throws Exception{
    	return this.hospitalService.findMedicalGuide();
    }
    
    /**
     * 查询就医指导明细
     * @return
     * @throws Exception
     */
    @RequestMapping("/findMedicalGuideById")
    @ResponseBody
    public List<HashMap<Object,Object>> findMedicalGuideById(@RequestBody HashMap<Object,Object> map) throws Exception{
    	return this.hospitalService.findMedicalGuideById(map);
    }
    
    
    /**
     * 查询医院健康百科
     * @return
     * @throws Exception
     */
    @RequestMapping("/findHospitalArticle")
    @ResponseBody
    public List<HashMap<Object, Object>> findHospitalArticle(@RequestBody HashMap map) throws Exception{
    	return this.hospitalService.findHospitalArticleList(map);
    }

    /**问题反馈
     * @return 
     * @return
     * @throws Exception
     */
    @RequestMapping("/problemFeedback")
    @ResponseBody
    public HashMap<Object, Object> problemFeedback(@RequestBody Map map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	try {
			this.hospitalService.problemFeedback(map);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "系统异常！";
			LogUtil.error(this.getClass(), "问题反馈异常："+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	return resultMap;
    }
    
    /**
     * @description 查询消息通知
     * @author dqzhang       
     * @created 2021年11月23日 下午2:59:02 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/findMessageNotice")
    @ResponseBody
    public List<HashMap<Object, Object>> findMessageNotice(@RequestBody HashMap<Object, Object> map) throws Exception{
    	
    	map.put("status", "0");
		return	this.hospitalService.findMessageNotice(map);
    }
    @RequestMapping("/findAllMessageNotice")
    @ResponseBody
    public List<HashMap<Object, Object>> findMessageAllNotice(@RequestBody HashMap<Object, Object> map) throws Exception {
        return this.hospitalService.findMessageNotice(map);
    }
    /**
     * @description 设置消息已读
     * @author dqzhang       
     * @created 2021年11月23日 下午2:58:43 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/setMessageNoticeRead")
    @ResponseBody
    public HashMap<Object, Object> setMessageNoticeRead(@RequestBody Map map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	try {
			this.hospitalService.setMessageNoticeRead(map);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "系统异常！";
			LogUtil.error(this.getClass(), "设置消息已读失败："+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	return resultMap;
    }
    /**
     * @param map
     * @return
     * @throws Exception
     * @description 设置消息已读
     * @author dqzhang
     * @created 2021年11月23日 下午2:58:43
     */
    @RequestMapping("/setMessageNoticeAllRead")
    @ResponseBody
    public HashMap<Object, Object> setMessageNoticeAllRead(@RequestBody Map map) throws Exception {
        String errCode = "0";
        String errMsg = "";
        try {
            this.hospitalService.setMessageNoticeAllRead(map);
        } catch (Exception e) {
            errCode = "999";
            errMsg = "系统异常！";
            LogUtil.error(this.getClass(), "设置消息已读失败：" + e.toString());
        }
        HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
        resultMap.put("errCode", errCode);
        resultMap.put("errMsg", errMsg);
        return resultMap;
    }

    /**
     * @description 写入银商下单接口信息
     * @author dqzhang       
     * @created 2021年11月22日 下午3:43:14 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/addPayRequest")
    @ResponseBody
    public HashMap<Object, Object> addPayRequest(@RequestBody Map map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	try {
    		map.put("create_time", DateDUtil.DateToStr("yyyy-MM-dd HH:mm:ss", new Date()));
			this.hospitalService.addPayRequest(map);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "写入下单信息失败！";
			LogUtil.error(this.getClass(), "写入下单信息失败："+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	return resultMap;
    }
    
    /**
     * @description 支付成功写入his充值接口
     * @author dqzhang       
     * @created 2021年11月22日 下午3:43:14 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/addHisRecharge")
    @ResponseBody
    public HashMap<Object, Object> addHisRecharge(@RequestBody Map map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	String hisStatus = "";
    	try {
    		HashMap<Object, Object> respMap = this.hospitalService.addHisRecharge(map);
    		hisStatus = StringDUtil.changeNullToEmpty(respMap.get("hisStatus"));
		} catch (Exception e) {
			errCode = "999";
			errMsg = "支付成功，调用调用充值接口失败！";
			LogUtil.error(this.getClass(), "写入his充值接口失败："+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	resultMap.put("hisStatus", hisStatus);
    	return resultMap;
    }
    
    /**
     * @description 预约成功记录写入数据库
     * @author dqzhang       
     * @created 2021年11月23日 下午3:06:26 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/insertOrder")
    @ResponseBody
    public HashMap<Object, Object> insertOrder(@RequestBody HashMap<Object,Object> map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	try {
			this.orderService.insertOrder(map);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "预约失败！";
			LogUtil.error(this.getClass(), "预约成功记录写入数据库失败："+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	return resultMap;
    }
    
    /**
     * @description 取消预约
     * @author dqzhang       
     * @created 2021年11月23日 下午3:59:40 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/cancelOrder")
    @ResponseBody
    public HashMap<Object, Object> cancelOrder(@RequestBody HashMap<Object,Object> map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	try {
			this.orderService.cancelOrder(map);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "取消预约！";
			LogUtil.error(this.getClass(), "取消预约失败："+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	return resultMap;
    }
    
    /**
     * @description 设置微信小程序最后登录时间
     * @author dqzhang       
     * @created 2021年11月24日 上午10:04:10 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/setWXLoginTime")
    @ResponseBody
    public HashMap<Object, Object> setWXLoginTime(@RequestBody HashMap<Object,Object> map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	try {
			this.orderService.setWXLoginTime(map);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "更新最后登录时间！";
			LogUtil.error(this.getClass(), "更新最后登录时间失败："+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	return resultMap;
    }
    
    /**
     * @description 查询待支付订单
     * @author dqzhang       
     * @created 2021年11月30日 上午8:59:50 
     * @param
     * @return
     * @throws Exception
     */
    @RequestMapping("/findRechargeWait")
    @ResponseBody
    public List<HashMap<Object, Object>> findRechargeWait() throws Exception{
    	
		return	this.orderService.findRechargeWait();
    }
    
    /**
     * @description 新增退款记录
     * @author dqzhang       
     * @created 2021年11月24日 上午10:04:10 
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping("/addRefund")
    @ResponseBody
    public HashMap<Object, Object> addRefund(@RequestBody HashMap<Object,Object> map) throws Exception{
    	String errCode = "0";
    	String errMsg = "";
    	try {
    		map.put("create_time", DateDUtil.DateToStr("yyyy-MM-dd HH:mm:ss", new Date()));
			this.orderService.addRefund(map);
		} catch (Exception e) {
			errCode = "999";
			errMsg = "新增退款记录失败！";
			LogUtil.error(this.getClass(), errMsg+e.toString());
		}
    	HashMap<Object, Object> resultMap = new HashMap<Object, Object>();
    	resultMap.put("errCode", errCode);
    	resultMap.put("errMsg", errMsg);
    	return resultMap;
    }
    
    /**
     * @description 获取退款时间
     * @author dqzhang       
     * @created 2022年2月7日 下午2:33:39 
     * @return
     * @throws Exception
     */
    @RequestMapping("/getRefundTime")
    @ResponseBody
    public String getRefundTime() throws Exception{
    	List<HashMap<Object, Object>> list = serviceParamsService.findParamValByParamCode("REFUND_TIME");
    	if(list.isEmpty()) {
    		return "";
    	}
    	return StringDUtil.changeNullToEmpty(list.get(0).get("PARAM_VAL"));
    }
    
    /**
     * @description 写入充值不成功且退款失败的消息提醒
     * @author dqzhang       
     * @created 2022年2月7日 下午2:37:15 
     * @return
     * @throws Exception
     */
    @RequestMapping("/addRefundMessageNotice")
    @ResponseBody
    public void addRefundMessageNotice(@RequestBody HashMap<Object,Object> map) throws Exception{
    	this.orderService.addRefundMessageNotice(map);
    }
    
    /**
     * @description 添加his日志
     * @author dqzhang       
     * @created 2022年2月7日 下午2:37:15 
     * @return
     * @throws Exception
     */
    @RequestMapping("/addHisLog")
    @ResponseBody
    public void addHisLog(@RequestBody HashMap<Object,Object> map) throws Exception{
    	this.loggerService.addHisLog(map);
    }
}
