package com.yihu.wlyy.web.patient.booking;

import com.yihu.wlyy.entity.patient.Patient;
import com.yihu.wlyy.entity.patient.PatientReservation;
import com.yihu.wlyy.service.app.reservation.PatientReservationService;
import com.yihu.wlyy.service.common.account.PatientService;
import com.yihu.wlyy.service.third.guahao.GuahaoXMService;
import com.yihu.wlyy.service.third.guahao.GuahaoYihuService;
import com.yihu.wlyy.task.PushMsgTask;
import com.yihu.wlyy.web.WeixinBaseController;
import com.yihu.wlyy.wechat.util.WeiXinOpenIdUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 预约挂号
 * Created by yeshijie on 2017/2/4.
 */
@Controller
@RequestMapping(value = "/patient/guahao")
@Api(description = "预约挂号接口-居民端")
public class PatientBookingController extends WeixinBaseController{
    @Autowired
    private GuahaoXMService guahaoXM;
    @Autowired
    private GuahaoYihuService guahaoYihu;
    @Autowired
    private PatientReservationService patientReservationService;
    @Autowired
    private PatientService patientService;
    @Autowired
    WeiXinOpenIdUtils weiXinOpenIdUtils;


    @RequestMapping(value = "CancelOrder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("取消挂号单")
    public String CancelOrder(@ApiParam(name = "orderId", value = "订单id", defaultValue = "48")
                              @RequestParam(value = "orderId", required = true) String orderId) {
        try {
            //获取订单信息
            PatientReservation obj = patientReservationService.findByCode(orderId);
            boolean re = false;
            if (obj != null) {
                String type = obj.getType();
                String code = obj.getCode();
                if (type.equals("0")) {  //医护网接口
                    re = guahaoYihu.CancelOrder(code);
                } else if (type.equals("1"))   //厦门市民健康预约接口
                {
                    re = guahaoXM.CancelOrder(code, obj.getSsc());
                }
            }

            if (re) {
                //更新状态
                patientReservationService.patientCancelOrder(orderId,getRepUID());//"9aa5c557e06a4324911487a035195545"
//                patientReservationService.patientCancelOrder(orderId,getUID());//"9aa5c557e06a4324911487a035195545"
                //微信消息
                Patient p = patientService.findByCode(obj.getPatient());
                if (StringUtils.isNotEmpty(p.getOpenid())) {
                    JSONObject json = new JSONObject();
                    json.put("first", "");
                    json.put("toUser", p.getCode());
                    json.put("represented",p.getCode());//被代理人
                    json.put("name", obj.getName());
                    json.put("date", obj.getStartTime());
                    json.put("doctorName", obj.getDoctorName());
                    json.put("orgName", obj.getOrgName());
                    json.put("remark", obj.getName() + "，您好！\n您已取消了" + obj.getStartTime() + "的挂号！");

                    //判断是否判定openId,有没有发则查找家人发送
                    if(StringUtils.isNotBlank(p.getOpenid())){
                        // 添加到发送队列
                        PushMsgTask.getInstance().putWxMsg(getAccessToken(), 7, p.getOpenid(), obj.getName(), json);
                    }
//                    else{
//                        JSONObject j  = weiXinOpenIdUtils.getFamilyOpenId(p.getCode());
//                        Patient member = (Patient) j.get("member");
//                        if(StringUtils.isNotBlank(member.getOpenid())){
//                            String first = (String) json.get("first");
//                            json.remove("first");
//                            json.put("first",weiXinOpenIdUtils.getTitleMes(p,j.getInt("relation"),member.getName())+first);
//                            PushMsgTask.getInstance().putWxMsg(getAccessToken(), 7, member.getOpenid(), member.getName(), json);
//                        }
//                    }
                    //发送代理人
                    JSONArray jsonArray = weiXinOpenIdUtils.getAgentOpenId(p.getCode(),p.getOpenid());
                    if(jsonArray!=null&&jsonArray.length()>0){
                        String first = (String) json.get("first");
                        for (int i = 0;i<jsonArray.length();i++){
                            JSONObject j  = jsonArray.getJSONObject(i);
                            Patient member = (Patient) j.get("member");
                            JSONObject data = json;
                            data.remove("toUser");
                            data.put("toUser",member.getCode());
                            data.remove("first");
                            data.put("first",weiXinOpenIdUtils.getTitleMes(p,j.getInt("relation"),p.getName())+first);
                            PushMsgTask.getInstance().putWxMsg(getAccessToken(), 7, member.getOpenid(), p.getName(), data);
                        }
                    }
                }
                return write(200, "取消挂号单成功！");
            } else {
                return error(-1, "取消挂号单失败！");
            }
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }
}
