package com.xinelu.applet.controller.nursingorder;

import com.alibaba.fastjson2.JSONObject;
import com.xinelu.applet.dto.nurseorder.NurseOrderDTO;
import com.xinelu.applet.service.nursingorder.INursingOrderService;
import com.xinelu.applet.vo.nursingorder.AppletGoodsOrderVO;
import com.xinelu.applet.vo.nursingorder.NursingOrderInfoVO;
import com.xinelu.common.annotation.MobileRequestAuthorization;
import com.xinelu.common.core.controller.BaseController;
import com.xinelu.common.core.domain.AjaxResult;
import com.xinelu.common.core.domain.entity.SysDictData;
import com.xinelu.common.core.page.TableDataInfo;
import com.xinelu.common.enums.OrderTypeEnum;
import com.xinelu.manage.domain.goodsOrder.GoodsOrder;
import com.xinelu.manage.service.messagepush.MessagePushService;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * @Description 个人中心订单页面
 * @Author zhangheng
 * @Date 2022-09-06
 * @Version 1.0
 */
@RestController
@RequestMapping("/nurseApplet/nursingOrder")
public class NursingOrderController extends BaseController {

    @Resource
    private INursingOrderService nursingOrderService;

	@Resource
	private MessagePushService messagePushService;

    /**
     * 根据登录id查询订单列表(暂时不用)
     */
    @GetMapping("/userPage")
    public TableDataInfo page(Long parentId) {
        startPage();
        List<NursingOrderInfoVO> nursingOrderByPatientId = nursingOrderService.getNursingOrderByPatientId(parentId);
        return getDataTable(nursingOrderByPatientId);
    }

    /**
     * 根据护理人id以及订单状态查询订单信息
     */
    @MobileRequestAuthorization
    @PostMapping("/goodsOrder")
    public TableDataInfo goodsOrder(@RequestBody AppletGoodsOrderVO goodsOrderVO) {
        startPage();
        List<AppletGoodsOrderVO> goodsOrder = nursingOrderService.getGoodsOrder(goodsOrderVO);
        return getDataTable(goodsOrder);
    }

    /**
     * 根据订单编号查询专家咨询订单详情
     */
    @GetMapping("/getConsultationOrder")
    public AjaxResult getConsultationOrder(@RequestParam("orderNo") String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("订单编号不能为空！");
        }
        return AjaxResult.success(nursingOrderService.getConsultationOrder(orderNo));
    }

    /**
     * 查询退款原因
     */
    @MobileRequestAuthorization
    @GetMapping("/refundReason")
    public TableDataInfo refundReason(SysDictData sysDictData) {
        startPage();
        List<SysDictData> refundReason = nursingOrderService.getRefundReason(sysDictData);
        return getDataTable(refundReason);
    }

    /**
     * 修改商品订单(退款售后记录信息)
     */
    @MobileRequestAuthorization
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsOrder goodsOrder) {
        if (Objects.isNull(goodsOrder.getId())) {
            return AjaxResult.error("订单id不能为空！");
        }
        if (StringUtils.isNotBlank(goodsOrder.getOrderType()) && !OrderTypeEnum.HEALTH_CONSULTATION.getInfo().equals(goodsOrder.getOrderType())) {
            if (Objects.isNull(goodsOrder.getRefundReasonDictId())) {
                return AjaxResult.error("请选择退款原因！");
            }
        }
        return toAjax(nursingOrderService.updateGoodsOrder(goodsOrder));
    }

    /**
     * 根据预约订单编号查询对应得订单详情信息
     *
     * @param orderNo 订单编号
     * @return 详情信息
     */
    @MobileRequestAuthorization
    @GetMapping("/getAppointmentDetailsInfo")
    public AjaxResult getAppointmentDetailsInfo(String orderNo) {
        if (StringUtils.isBlank(orderNo)) {
            return AjaxResult.error("请选择要查询的预约订单信息！");
        }
        return nursingOrderService.getAppointmentDetailsInfo(orderNo);
    }

    /**
     * 根据parentId编号查询对应得订单
     */
    @MobileRequestAuthorization
    @GetMapping("/getAppletOrderList")
    public TableDataInfo getAppletOrderList(NurseOrderDTO nurseOrder) {
        if (Objects.isNull(nurseOrder) || Objects.isNull(nurseOrder.getParentId()) || StringUtils.isBlank(nurseOrder.getCardNo()) || StringUtils.isBlank(nurseOrder.getOrderStatus())) {
            return getDataTable(new ArrayList<>());
        }
        return nursingOrderService.getAppletOrderList(nurseOrder);
    }

	@GetMapping("/pushMeg")
	public AjaxResult pushMeg(Long orderId, Long patientId) {
		JSONObject result = nursingOrderService.pushMeg(orderId, patientId);
		if(result != null && result.containsKey("openid")) {
			messagePushService.fdApprovePush(result);
		}
		return AjaxResult.success();
	}
}
