package com.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.model.HighUserModel;
import com.hai.model.ResponseData;
import com.hai.order.model.CreateOrderChildModel;
import com.hai.order.model.CreateOrderModel;
import com.hai.order.service.OrderPaySuccessService;
import com.hai.order.service.OrderService;
import com.hai.order.type.OrderChildGoodsType;
import com.hai.order.utils.OrderUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "")
@Api(value = "订单业务")
public class OrderController {

    private static Logger log = LoggerFactory.getLogger(OrderController.class);

    @Resource
    private OrderService orderService;

    @Resource
    private OrderPaySuccessService orderPaySuccessService;

    @Resource
    private UserCenter userCenter;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping(value="/create",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建订单")
    public ResponseData create(@RequestBody CreateOrderModel body) {
        try {
            // 用户信息
            HighUserModel userModel = userCenter.getSessionModel(HighUserModel.class);
            if (userModel == null) {
                log.error("OrderController -> create() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.USE_VISIT_ILLEGAL, "");
            }
            if (StringUtils.isBlank(userModel.getHighUser().getPhone())) {
                log.error("OrderController -> create() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.NO_BIND_PHONE, "");
            }
            body.setMemId(userModel.getHighUser().getId());
            body.setMemName(userModel.getHighUser().getName());
            body.setMemPhone(userModel.getHighUser().getPhone());

            if (body == null
                    || body.getChildOrderList() == null
                    || body.getChildOrderList().size() == 0) {
                log.error("OrderController -> create() error!","");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            for (CreateOrderChildModel childModel : body.getChildOrderList()) {
                childModel.setMemId(body.getMemId());
                childModel.setMemName(body.getMemName());
                childModel.setMemPhone(body.getMemPhone());

                // 校验子订单参数
                if (childModel.getGoodsType() == null
                        || OrderChildGoodsType.getDataByType(childModel.getGoodsType()) == null
                        || childModel.getGoodsId() == null
                        || childModel.getSaleCount() == null) {
                    log.error("OrderController -> create() error!","");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }

                if (childModel.getGoodsType().equals(OrderChildGoodsType.TYPE1.getNumber())
                        || childModel.getGoodsType().equals(OrderChildGoodsType.TYPE8.getNumber())) {
                    if (childModel.getGoodsPrice() == null) {
                        log.error("OrderController -> create() error!","");
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未填写充值金额");
                    }

                } else if (childModel.getGoodsType().equals(OrderChildGoodsType.TYPE3.getNumber())) {
                    if (childModel.getGoodsPrice() == null || childModel.getGasOilNo() == null || childModel.getGasGunNo() == null) {
                        log.error("OrderController -> create() error!","");
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未填写加油信息；加油金额、油号、抢号");
                    }
                }
            }

            body.setOrderNo(OrderUtil.generateOrderNo());
            return ResponseMsgUtil.success(orderService.createOrder(body));

        } catch (Exception e) {
            log.error("OrderController -> create() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/cancel",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单取消")
    public ResponseData cancel(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("orderNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单");
            }

            String orderNo = body.getString("orderNo");
            String remarks = body.getString("remarks");
            return ResponseMsgUtil.success(orderService.cancelOrder(orderNo, remarks));

        } catch (Exception e) {
            log.error("OrderController -> cancel() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/refund",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "订单退款")
    public ResponseData refund(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("childOrderNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单");
            }

            return ResponseMsgUtil.success(null);

        } catch (Exception e) {
            log.error("OrderController -> cancel() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/paySuccess",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "支付成功")
    public ResponseData paySuccess(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("orderNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到订单");
            }

            return ResponseMsgUtil.success(orderService.orderPaySuccessBus(orderService.getOrderDetailByNo(body.getString("orderNo"))));

        } catch (Exception e) {
            log.error("OrderController -> paySuccess() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value="/getDetailByOrderNo",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据订单号查询")
    public ResponseData getDetailByOrderNo(@RequestParam(name = "orderNo", required = true) String orderNo) {
        try {

            return ResponseMsgUtil.success(orderService.getOrderDetailByNo(orderNo));

        } catch (Exception e) {
            log.error("OrderController -> getDetailByOrderNo() error!",e);
            return ResponseMsgUtil.exception(e);
        }
    }

}
