package com.cweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.*;
import com.hai.entity.*;
import com.hai.model.*;
import com.hai.service.*;
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.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Controller
@RequestMapping(value = "/outRechargeOrder")
@Api(value = "充值订单接口")
public class OutRechargeOrderController {
    private static Logger log = LoggerFactory.getLogger(HighMerchantStoreController.class);

    @Autowired
    private UserCenter userCenter;

    @Resource
    private OutRechargeOrderService outRechargeOrderService;

    @Resource
    private OutRechargePriceService outRechargePriceService;


    @RequestMapping(value = "/addOrder", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加订单")
    public ResponseData addOrder(@RequestBody JSONObject object, HttpServletRequest request) {
        try {

            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();



            if (StringUtils.isBlank(object.getString("rechargeContent")) ||
                    object.getLong("goodsId") == null
            ) {
                log.error("addOrder error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            object.put("mchId", request.getHeader("mchId"));
            object.put("userId", userInfoModel.getHighUser().getId());

            return ResponseMsgUtil.success(outRechargeOrderService.insertOrder(object));

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


    @RequestMapping(value = "/getOrderById", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询订单详情")
    public ResponseData getOrderById(@RequestParam(name = "orderId", required = true) Long orderId) {
        try {

            OutRechargeOrderDetailModel rechargeOrderDetailModel = new OutRechargeOrderDetailModel();

            OutRechargeOrder order = outRechargeOrderService.findByOrderId(orderId);

            BeanUtils.copyProperties(order, rechargeOrderDetailModel);

            rechargeOrderDetailModel.setOutRechargePrice(outRechargePriceService.findByGoodsId(order.getGoodsId()));

            return ResponseMsgUtil.success(rechargeOrderDetailModel);

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

    @RequestMapping(value = "/cancelOrder", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "取消订单")
    public ResponseData cancelOrder(@RequestParam(name = "orderId", required = true) Long orderId) {
        try {

            OutRechargeOrder outRechargeOrder = outRechargeOrderService.findByOrderId(orderId);

            if (outRechargeOrder.getPayStatus() == 101 && (outRechargeOrder.getRechargeStatus() != 201 || outRechargeOrder.getRechargeStatus() != 202)) {
                outRechargeOrderService.cancelOrder(orderId);
            } else {
                log.error("orderToPay error!");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }

            return ResponseMsgUtil.success("操作成功");

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

    @RequestMapping(value = "/getUserOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取用户订单")
    public ResponseData getUserOrderList(
            @RequestParam(name = "status", required = false) Integer status,
            @RequestParam(name = "orderNo", required = false) String orderNo,
            @RequestParam(name = "pageNum", required = true) Integer pageNum,
            @RequestParam(name = "pageSize", required = true) Integer pageSize,
            HttpServletRequest request) {
        try {

            // 用户
            SessionObject sessionObject = userCenter.getSessionObject(request);
            HighUserModel userInfoModel = (HighUserModel) sessionObject.getObject();

            Map<String, Object> map = new HashMap<>();
            map.put("userId", userInfoModel.getHighUser().getId().toString());
            if (status != null) {
                map.put("payStatus", status.toString());
            }

            map.put("orderNo", orderNo);

            PageHelper.startPage(pageNum, pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(outRechargeOrderService.getListRechargeOrder(map)));

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


}
