package com.cweb.controller.order;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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.BsGasOrder;
import com.hfkj.entity.BsGasOrderSpread;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.gas.BsGasOrderSpreadService;
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.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/gasOrder")
@Api(value = "加油订单业务")
public class GasOrderController {
    private static Logger log = LoggerFactory.getLogger(GasOrderController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsGasOrderService gasOrderService;
    @Resource
    private BsGasOrderSpreadService gasOrderSpreadService;

    @RequestMapping(value="/queryUserOilOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询用户加油订单")
    public ResponseData queryUserOilOrder(@RequestParam(name = "orderNo", required = false) String orderNo,
                                          @RequestParam(name = "receiptStatus", required = false) String receiptStatus,
                                          @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                          @RequestParam(name = "payTimeS", required = false) Long payTimeS,
                                          @RequestParam(name = "payTimeE", required = false) Long payTimeE,
                                          @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                          @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
            if (userSession == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("userId", userSession.getUser().getId());
            param.put("orderNo", orderNo);
            param.put("status", orderStatus);
            param.put("receiptStatus", receiptStatus);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(gasOrderService.getGasOrderList(param)));

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

    @RequestMapping(value="/queryOilOrderSpread",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询加油推广订单")
    public ResponseData queryOilOrderSpread(@RequestParam(name = "orderNo", required = false) String orderNo,
                                            @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                            @RequestParam(name = "payTimeS", required = false) Long payTimeS,
                                            @RequestParam(name = "payTimeE", required = false) Long payTimeE,
                                            @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                            @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            UserSessionObject userSession = userCenter.getSessionModel(UserSessionObject.class);
            if (userSession == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ACCOUNT_LOGIN_NOT, "");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("userId", userSession.getUser().getId());
            param.put("orderNo", orderNo);
            param.put("status", orderStatus);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(gasOrderService.getOilOrderSpread(param)));

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

    @RequestMapping(value="/queryOilOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询加油订单详情")
    public ResponseData queryOilOrder(@RequestParam(name = "orderNo", required = false) String orderNo,
                                      @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                      @RequestParam(name = "payChannelOrderNo", required = false) String payChannelOrderNo) {
        try {
            BsGasOrder gasOrder = null;
            if (StringUtils.isNotBlank(orderNo)) {
                gasOrder = gasOrderService.getDetailByOrderNo(orderNo);

            } else if (StringUtils.isNotBlank(paySerialNo)) {
                gasOrder = gasOrderService.getDetailByPaySerialNo(paySerialNo);

            } else if (StringUtils.isNotBlank(payChannelOrderNo)) {
                gasOrder = gasOrderService.getDetailByPayChannelOrderNo(payChannelOrderNo);
            }
            if (gasOrder != null) {
                JSONObject data = JSONObject.parseObject(JSONObject.toJSONString(gasOrder), JSONObject.class);
                // 推广分销
                BsGasOrderSpread spread = gasOrderSpreadService.getDetailByOrderNo(gasOrder.getOrderNo());
                if (spread != null) {
                    data.put("spread", spread);
                }
                return ResponseMsgUtil.success(data);
            }
            return ResponseMsgUtil.success(gasOrder);


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



}
