package com.cweb.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsMerchant;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.model.order.OrderChildModel;
import com.hfkj.model.order.OrderModel;
import com.hfkj.service.merchant.BsMerchantService;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.sysenum.order.OrderChildProductTypeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@Controller
@RequestMapping(value = "/order")
@Api(value = "订单业务")
public class BsOrderController {
    private static Logger log = LoggerFactory.getLogger(BsOrderController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsOrderService orderService;
    @Resource
    private BsMerchantService merchantService;

    @RequestMapping(value="/create",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "创建订单")
    public ResponseData create(@RequestBody OrderModel body) {
        try {
            // 用户信息
            try {
                UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
                if (userSession != null) {
                    body.setUserId(userSession.getUser().getId());
                    body.setUserName(userSession.getUser().getUserName());
                    body.setUserPhone(userSession.getUser().getPhone());
                }
            } catch (Exception e) {
                // 不做任何处理
            }

            if (body == null || body.getOrderChildList().isEmpty()) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 子订单必填项校验
            for (OrderChildModel orderChild : body.getOrderChildList()) {
                if (orderChild.getProductType() == null) {
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
                if (orderChild.getProductType().equals(OrderChildProductTypeEnum.type1.getCode())) {
                    if (orderChild.getProductPrice() == null
                            || StringUtils.isBlank(orderChild.getGasOilNo())
                            || StringUtils.isBlank(orderChild.getGasGunNo())
                            || StringUtils.isBlank(orderChild.getMerNo())
                    ) {
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                    }
                    // 查询油站
                    BsMerchant merchant = merchantService.getMerchant(orderChild.getMerNo());
                    if (merchant == null) {
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的油站");
                    }
                    // 除了自建站 其他需要登录
                    if (!merchant.getSourceType().equals(1) && StringUtils.isBlank(body.getUserPhone())) {
                        throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
                    }
                    // todo 目前只有加油业务，设置加油金额
                    body.setTotalPrice(orderChild.getProductPrice());
                }
            }

            return ResponseMsgUtil.success(orderService.create(body));

        } catch (Exception e) {
            log.error("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, "未找到订单");
            }
            return ResponseMsgUtil.success(orderService.cancel(body.getString("orderNo")));

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

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

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

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


}
