package com.web.controller.business;

import com.alibaba.excel.EasyExcel;
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.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.HighGasOrder;
import com.hai.enum_type.UserObjectTypeEnum;
import com.hai.enum_type.UserRoleTypeEnum;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.order.model.ExportGasOrderModel;
import com.hai.order.model.OrderCouponModel;
import com.hai.order.model.OrderIntegralRechargeModel;
import com.hai.order.service.OrderService;
import com.hai.order.service.OrderServiceExt;
import com.hai.order.type.OrderOilStatus;
import com.hai.service.HighGasOrderService;
import com.web.config.SysConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;

@Controller
@RequestMapping(value = "integralRecharge")
@Api(value = "积分充值业务")
public class OrderIntegralRechargeController {

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

    @Resource
    private UserCenter userCenter;

    @Resource
    private OrderService orderService;

    @Resource
    private OrderServiceExt orderServiceExt;

    @RequestMapping(value="/getOrderList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询订单列表")
    public ResponseData getOrderList(@RequestParam(name = "memPhone", required = false) String memPhone,
                                     @RequestParam(name = "orderNo", required = false) String orderNo,
                                     @RequestParam(name = "orderStatus", required = false) Long orderStatus,
                                     @RequestParam(name = "payType", required = false) Long payType,
                                     @RequestParam(name = "createTimeS", required = false) Long createTimeS,
                                     @RequestParam(name = "createTimeE", required = false) Long createTimeE,
                                     @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 {
            Map<String, Object> param = new HashMap<>();
            param.put("orderNo", orderNo);
            param.put("memPhone", memPhone);
            param.put("orderStatus", orderStatus);
            param.put("payType", payType);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);

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

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


    @RequestMapping(value="/getOrderDetail",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询油站详情")
    public ResponseData getOrderDetail(@RequestParam(name = "orderNo", required = true) String orderNo) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("tradeOrder", orderService.getOrderDetailByNo(orderNo));

            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value="/exportOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出订单")
    public ResponseData exportOrder(@RequestParam(name = "memPhone", required = false) String memPhone,
                                     @RequestParam(name = "orderNo", required = false) String orderNo,
                                     @RequestParam(name = "status", required = false) Long status,
                                     @RequestParam(name = "createTimeS", required = false) Long createTimeS,
                                     @RequestParam(name = "createTimeE", required = false) Long createTimeE,
                                     @RequestParam(name = "payTimeS", required = false) Long payTimeS,
                                     @RequestParam(name = "payTimeE", required = false) Long payTimeE
    ) {
        try {
            Map<String, Object> param = new HashMap<>();
            param.put("orderNo", orderNo);
            param.put("memPhone", memPhone);
            param.put("status", status);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);

            List<OrderIntegralRechargeModel> list = orderServiceExt.getOrderIntegralRechargeList(param);

            String fileName = "/temporary/" + System.currentTimeMillis() + ".xlsx";
            EasyExcel.write(SysConst.getSysConfig().getFileUrl() + fileName, OrderIntegralRechargeModel.class)
                    .sheet("积分充值订单")
                    .doWrite(list);

            return ResponseMsgUtil.success(fileName);

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

}
