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

import com.yihu.wlyy.aop.ObserverRequired;
import com.yihu.wlyy.entity.patient.PatientReservation;
import com.yihu.wlyy.repository.patient.PatientReservationDao;
import com.yihu.wlyy.service.app.reservation.PatientReservationService;
import com.yihu.wlyy.service.third.guahao.GuahaoXMService;
import com.yihu.wlyy.service.third.guahao.GuahaoYihuService;
import com.yihu.wlyy.util.DateUtil;
import com.yihu.wlyy.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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 = "/doctor/guahao")
@Api(description = "预约挂号接口-医生端")
public class DoctorBookingController extends BaseController{

    @Autowired
    private GuahaoXMService guahaoXM;

    @Autowired
    private GuahaoYihuService guahaoYihu;

    @Autowired
    private PatientReservationService patientReservationService;
    @Autowired
    private PatientReservationDao patientReservationDao;


    @RequestMapping(value = "isCancelOrder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("医生是否可取消代预约")
    public String cancelOrderDt(@ApiParam(name = "orderId", value = "订单id", defaultValue = "48")
                                @RequestParam(value = "orderId", required = true) Long orderId){
        try{
            PatientReservation obj = patientReservationService.findById(orderId);
            if(obj != null){
                PatientReservation pr = patientReservationDao.findByPatientAndCancelerAndCancelTime(obj.getPatient(),getUID(), DateUtil.getCurMonthFirstDayShort());//"6c0cfe5065e011e69f7c005056850d66"
                if(pr == null){
                    return write(200,"查询成功","status","1");//取消预约次数将计入居民取消次数，居民每月取消超过3次将纳入黑名单。是否继续撤销？
                }else{
                    return write(200,"查询成功","status","0");//对不起，您本月已取消过该居民预约，如需继续撤销，请让居民自行处理
                }
            }else{
                return error(-1, "挂号单不存在！");
            }
        }catch (Exception e){
            return error(-1, e.getMessage());
        }
    }

    @RequestMapping(value = "CancelOrder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("取消挂号单")
    @ObserverRequired
    public String CancelOrder(@ApiParam(name = "orderId", value = "订单id", defaultValue = "48")
                              @RequestParam(value = "orderId", required = true) Long orderId) {
        try {
            //获取订单信息
            PatientReservation obj = patientReservationService.findById(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.doctorCancelOrder(orderId,getUID());//"6c0cfe5065e011e69f7c005056850d66"

                return write(200, "取消挂号单成功！");
            } else {
                return error(-1, "取消挂号单失败！");
            }
        } catch (Exception e) {
            return error(-1, e.getMessage());
        }
    }

}
