package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
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.config.CommonSysConst;
import com.hfkj.entity.BsAgent;
import com.hfkj.entity.BsGasOrder;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.order.ExportGasOrderModel;
import com.hfkj.service.BsDeviceService;
import com.hfkj.service.agent.BsAgentService;
import com.hfkj.service.gas.BsGasOrderService;
import com.hfkj.service.order.BsOrderService;
import com.hfkj.sysenum.gas.GasOrderCreateType;
import com.hfkj.sysenum.merchant.MerchantSourceTypeEnum;
import com.hfkj.sysenum.SecUserObjectTypeEnum;
import com.hfkj.sysenum.gas.OrderOilStatus;
import com.hfkj.sysenum.order.OrderPayTypeEnum;
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.io.File;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

@Controller
@RequestMapping(value = "/gasOrder")
@Api(value = "加油订单业务")
public class BsGasOrderController {
    private static Logger log = LoggerFactory.getLogger(BsGasOrderController.class);
    @Resource
    private UserCenter userCenter;
    @Resource
    private BsGasOrderService gasOrderService;
    @Resource
    private BsAgentService agentService;
    @Resource
    private BsOrderService orderService;
    @Resource
    private BsDeviceService deviceService;

    @RequestMapping(value="/countOilData",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "统计加油数据")
    public ResponseData countOilData(@RequestParam(name = "merId", required = false) Long merId,
                                          @RequestParam(name = "merName", required = false) String merName,
                                          @RequestParam(name = "userPhone", required = false) String userPhone,
                                          @RequestParam(name = "gasStaffName", required = false) String gasStaffName,
                                          @RequestParam(name = "orderNo", required = false) String orderNo,
                                          @RequestParam(name = "payType", required = false) String payType,
                                          @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                          @RequestParam(name = "channelType", required = false) Integer channelType,
                                          @RequestParam(name = "channelOrderNo", required = false) String channelOrderNo,
                                          @RequestParam(name = "abnormal", required = false) String abnormal,
                                          @RequestParam(name = "status", required = false) Integer 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,
                                          @RequestParam(name = "refundTimeS", required = false) Long refundTimeS,
                                          @RequestParam(name = "refundTimeE", required = false) Long refundTimeE,
                                          @RequestParam(name = "gasClassGroupId", required = false) Long gasClassGroupId,
                                          @RequestParam(name = "gasClassGroupTaskId", required = false) Long gasClassGroupTaskId,
                                          @RequestParam(name = "gasOilNo", required = false) String gasOilNo,
                                          @RequestParam(name = "agentId", required = false) Long agentId,
                                          @RequestParam(name = "agentStaffId", required = false) Long agentStaffId) {
        try {
            SecUserSessionObject sessionObject = userCenter.getSessionModel(SecUserSessionObject.class);
            Map<String,Object> param = new HashMap<>();
            param.put("orderNo", orderNo);
            param.put("payType", payType);
            param.put("paySerialNo", paySerialNo);
            param.put("channelType", channelType);
            param.put("channelOrderNo", channelOrderNo);
            param.put("userPhone", userPhone);
            param.put("status", status);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);
            param.put("refundTimeS", refundTimeS);
            param.put("refundTimeE", refundTimeE);
            param.put("gasClassGroupId", gasClassGroupId);
            param.put("gasClassGroupTaskId", gasClassGroupTaskId);
            param.put("gasStaffName", gasStaffName);
            param.put("gasOilNo", gasOilNo);
            param.put("abnormal", abnormal);

            if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("agentId", agentId);
                param.put("agentStaffId", agentStaffId);
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                param.put("merId", sessionObject.getMerchant().getId());
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type3.getCode())) {
                param.put("gasStaffId", sessionObject.getAccount().getObjectId());
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode())) {
                param.put("agentId", sessionObject.getAccount().getObjectId());
                param.put("agentStaffId", agentStaffId);
                param.put("merName", merName);
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type5.getCode())) {
                param.put("agentStaffId", sessionObject.getAccount().getObjectId());
                param.put("merName", merName);

            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type6.getCode())) {
                param.put("merChainBrandId", sessionObject.getAccount().getObjectId());
                param.put("merName", merName);
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }

            Map<String, Object> deductionCouponParam = new HashMap<>(param);
            Map<String, Object> map = gasOrderService.getGasOrderCount(param);

            // 优惠卷订单
            deductionCouponParam.put("deductionCouponNotNull", "deductionCouponNotNull");
            Map<String, Object> deductionCouponNotNull = gasOrderService.getGasOrderCount(deductionCouponParam);
            map.put("deductionCouponCount", deductionCouponNotNull.get("totalCount"));

            if (Integer.parseInt(map.get("totalCount").toString()) > 0) {
                map.put("deductionCouponRate", new BigDecimal(map.get("deductionCouponCount").toString())
                        .divide(new BigDecimal(map.get("totalCount").toString()),2, BigDecimal.ROUND_DOWN)
                        .multiply(new BigDecimal("100")));
            } else {
                map.put("deductionCouponRate", 0);
            }

            return ResponseMsgUtil.success(map);

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

    @RequestMapping(value="/queryOilOrderList",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询加油订单")
    public ResponseData queryOilOrderList(@RequestParam(name = "merId", required = false) Long merId,
                                          @RequestParam(name = "merName", required = false) String merName,
                                          @RequestParam(name = "userPhone", required = false) String userPhone,
                                          @RequestParam(name = "gasStaffName", required = false) String gasStaffName,
                                          @RequestParam(name = "orderNo", required = false) String orderNo,
                                          @RequestParam(name = "payType", required = false) String payType,
                                          @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                          @RequestParam(name = "channelType", required = false) Integer channelType,
                                          @RequestParam(name = "channelOrderNo", required = false) String channelOrderNo,
                                          @RequestParam(name = "status", required = false) Integer status,
                                          @RequestParam(name = "abnormal", required = false) Integer abnormal,
                                          @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 = "refundTimeS", required = false) Long refundTimeS,
                                          @RequestParam(name = "refundTimeE", required = false) Long refundTimeE,
                                          @RequestParam(name = "gasOilNo", required = false) String gasOilNo,
                                          @RequestParam(name = "gasClassGroupId", required = false) Long gasClassGroupId,
                                          @RequestParam(name = "gasClassGroupTaskId", required = false) Long gasClassGroupTaskId,
                                          @RequestParam(name = "agentId", required = false) Long agentId,
                                          @RequestParam(name = "agentStaffId", required = false) Long agentStaffId,
                                          @RequestParam(name = "pageNum", required = true) Integer pageNum,
                                          @RequestParam(name = "pageSize", required = true) Integer pageSize) {
        try {
            SecUserSessionObject sessionObject = userCenter.getSessionModel(SecUserSessionObject.class);
            Map<String,Object> param = new HashMap<>();
            param.put("orderNo", orderNo);
            param.put("payType", payType);
            param.put("paySerialNo", paySerialNo);
            param.put("channelType", channelType);
            param.put("channelOrderNo", channelOrderNo);
            param.put("userPhone", userPhone);
            param.put("status", status);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);
            param.put("refundTimeS", refundTimeS);
            param.put("refundTimeE", refundTimeE);
            param.put("gasClassGroupId", gasClassGroupId);
            param.put("gasClassGroupTaskId", gasClassGroupTaskId);
            param.put("gasStaffName", gasStaffName);
            param.put("gasOilNo", gasOilNo);
            param.put("abnormal", abnormal);

            if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("agentId", agentId);
                param.put("agentStaffId", agentStaffId);
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                param.put("merId", sessionObject.getMerchant().getId());
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type3.getCode())) {
                param.put("gasStaffId", sessionObject.getAccount().getObjectId());
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode())) {
                param.put("agentId", sessionObject.getAccount().getObjectId());
                param.put("agentStaffId", agentStaffId);
                param.put("merName", merName);
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type5.getCode())) {
                param.put("agentStaffId", sessionObject.getAccount().getObjectId());
                param.put("merName", merName);
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type6.getCode())) {
                param.put("merChainBrandId", sessionObject.getAccount().getObjectId());
                param.put("merName", merName);
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }

            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="/exportOilOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出加油订单")
    public ResponseData exportOilOrder(@RequestParam(name = "merId", required = false) Long merId,
                                          @RequestParam(name = "merName", required = false) String merName,
                                          @RequestParam(name = "userPhone", required = false) String userPhone,
                                          @RequestParam(name = "gasStaffName", required = false) String gasStaffName,
                                          @RequestParam(name = "orderNo", required = false) String orderNo,
                                          @RequestParam(name = "payType", required = false) String payType,
                                          @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                          @RequestParam(name = "channelType", required = false) Integer channelType,
                                          @RequestParam(name = "channelOrderNo", required = false) String channelOrderNo,
                                          @RequestParam(name = "status", required = false) Integer status,
                                          @RequestParam(name = "abnormal", required = false) Integer abnormal,
                                          @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 = "refundTimeS", required = false) Long refundTimeS,
                                          @RequestParam(name = "refundTimeE", required = false) Long refundTimeE,
                                          @RequestParam(name = "gasOilNo", required = false) String gasOilNo,
                                          @RequestParam(name = "gasClassGroupId", required = false) Long gasClassGroupId,
                                          @RequestParam(name = "gasClassGroupTaskId", required = false) Long gasClassGroupTaskId,
                                          @RequestParam(name = "agentId", required = false) Long agentId,
                                          @RequestParam(name = "agentStaffId", required = false) Long agentStaffId) {
        try {
            SecUserSessionObject sessionObject = userCenter.getSessionModel(SecUserSessionObject.class);
            Map<String,Object> param = new HashMap<>();
            param.put("orderNo", orderNo);
            param.put("payType", payType);
            param.put("paySerialNo", paySerialNo);
            param.put("channelType", channelType);
            param.put("channelOrderNo", channelOrderNo);
            param.put("userPhone", userPhone);
            param.put("status", status);
            param.put("createTimeS", createTimeS);
            param.put("createTimeE", createTimeE);
            param.put("payTimeS", payTimeS);
            param.put("payTimeE", payTimeE);
            param.put("refundTimeS", refundTimeS);
            param.put("refundTimeE", refundTimeE);
            param.put("gasClassGroupId", gasClassGroupId);
            param.put("gasClassGroupTaskId", gasClassGroupTaskId);
            param.put("gasStaffName", gasStaffName);
            param.put("gasOilNo", gasOilNo);
            param.put("abnormal", abnormal);

            BsAgent agent = null;
            if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                param.put("merId", merId);
                param.put("merName", merName);
                param.put("agentId", agentId);
                param.put("agentStaffId", agentStaffId);
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode())) {
                param.put("merId", sessionObject.getMerchant().getId());
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type3.getCode())) {
                param.put("gasStaffId", sessionObject.getAccount().getObjectId());
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode())) {
                param.put("agentId", sessionObject.getAccount().getObjectId());
                param.put("agentStaffId", agentStaffId);
                param.put("merName", merName);
                // 查询代理商
                agent = agentService.getAgentById(sessionObject.getAccount().getObjectId());
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type5.getCode())) {
                param.put("agentStaffId", sessionObject.getAccount().getObjectId());
                param.put("merName", merName);
            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type6.getCode())) {
                param.put("merChainBrandId", sessionObject.getAccount().getObjectId());
                param.put("merName", merName);
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.ROLE_NOT_PERMISSIONS, "");
            }

            // 数据
            List<BsGasOrder> gasOrderList = gasOrderService.getGasOrderList(param);

            List<ExportGasOrderModel> dataList = new ArrayList<>();
            ExportGasOrderModel model;

            for (BsGasOrder gasOrder : gasOrderList) {
                model = new ExportGasOrderModel();
                model.setAgentName(StringUtils.isBlank(gasOrder.getAgentName())?"无":gasOrder.getAgentName());
                model.setAgentStaffName(StringUtils.isBlank(gasOrder.getAgentStaffName())?"无":gasOrder.getAgentStaffName());
                model.setChannelType(MerchantSourceTypeEnum.getNameByType(gasOrder.getChannelType()));
                model.setChannelOrderNo(gasOrder.getChannelOrderNo());
                model.setOrderNo(gasOrder.getOrderNo());
                model.setUserPhone(StringUtils.isBlank(gasOrder.getUserPhone())?"无":gasOrder.getUserPhone());
                model.setMerName(gasOrder.getMerName());
                model.setGasStaffName(gasOrder.getGasStaffName());
                model.setGasRefuelPrice(gasOrder.getGasRefuelPrice());
                model.setGasOilLiters(gasOrder.getGasOilLiters());
                model.setGasDiscount(gasOrder.getGasDiscount().compareTo(new BigDecimal("1")) != 0?gasOrder.getGasDiscount().multiply(new BigDecimal("100")).setScale(2).toString():"无");
                model.setGasOilNo(gasOrder.getGasOilNo() + "#");
                model.setGasGunNo(gasOrder.getGasGunNo() + "号枪");
                model.setGasPricePlatform(gasOrder.getGasPricePlatform().toString());
                model.setGasPriceVip(gasOrder.getGasPriceVip().toString());
                model.setGasPriceGun(gasOrder.getGasPriceGun());
                model.setGasPriceOfficial(gasOrder.getGasPriceOfficial());
                model.setGasSettlePrice(gasOrder.getGasSettlePrice());
                model.setGasAgentSettlePrice(gasOrder.getGasAgentSettlePrice());
                model.setPayTypeName(gasOrder.getPayType()!=null? OrderPayTypeEnum.getDataByType(gasOrder.getPayType()).getName():"未支付");
                model.setTotalDeductionPrice(gasOrder.getTotalDeductionPrice());
                model.setDeductionCouponPrice(gasOrder.getDeductionCouponPrice());
                model.setDeductionOilPrice(gasOrder.getDeductionOilPrice());
                model.setServiceFeePrice(gasOrder.getGasServiceFeePrice());
                model.setPayablePrice(gasOrder.getPayablePrice());
                model.setPayIntegral(gasOrder.getPayIntegral()==null?new BigDecimal("0"):new BigDecimal(""+gasOrder.getPayIntegral()).divide(new BigDecimal("100")));
                model.setPayPrice(gasOrder.getActualPayPrice()!=null?gasOrder.getActualPayPrice().toString():"未支付");
                model.setStatus(OrderOilStatus.getNameByType(gasOrder.getStatus()));
                model.setCreateTime(gasOrder.getCreateTime());
                model.setPayTime(gasOrder.getPayTime());
                model.setRefundTime(gasOrder.getRefundTime());
                model.setCancelTime(gasOrder.getCancelTime());
                model.setAbnormal(gasOrder.getAbnormal()?"异常":"正常");
                model.setAbnormalContent(gasOrder.getAbnormalContent());
                dataList.add(model);
            }

            String fileUrl = CommonSysConst.getSysConfig().getFilesystem() + "/temporary/";
            String pathName = System.currentTimeMillis()+".xlsx";
            File file = new File(fileUrl);
            if (!file.exists()) {
                file.mkdirs();
            }

            Set<String> includeColumnFiledNames = new HashSet<String>();
            includeColumnFiledNames.add("orderNo");
            includeColumnFiledNames.add("gasRefuelPrice");
            includeColumnFiledNames.add("gasOilLiters");
            includeColumnFiledNames.add("gasOilNo");
            includeColumnFiledNames.add("gasGunNo");
            includeColumnFiledNames.add("gasDiscount");
            includeColumnFiledNames.add("gasPricePlatform");
            includeColumnFiledNames.add("gasPriceVip");
            includeColumnFiledNames.add("gasPriceGun");
            includeColumnFiledNames.add("gasPriceOfficial");
            includeColumnFiledNames.add("payTypeName");
            includeColumnFiledNames.add("totalDeductionPrice");
            includeColumnFiledNames.add("deductionCouponPrice");
            includeColumnFiledNames.add("deductionOilPrice");
            includeColumnFiledNames.add("serviceFeePrice");
            includeColumnFiledNames.add("payablePrice");
            includeColumnFiledNames.add("payIntegral");
            includeColumnFiledNames.add("payPrice");
            includeColumnFiledNames.add("status");
            includeColumnFiledNames.add("createTime");
            includeColumnFiledNames.add("payTime");
            includeColumnFiledNames.add("refundTime");
            includeColumnFiledNames.add("cancelTime");
            includeColumnFiledNames.add("abnormal");
            includeColumnFiledNames.add("abnormalContent");

            if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type1.getCode())) {
                includeColumnFiledNames.add("agentName");
                includeColumnFiledNames.add("agentStaffName");
                includeColumnFiledNames.add("merName");
                includeColumnFiledNames.add("gasStaffName");
                includeColumnFiledNames.add("channelType");
                includeColumnFiledNames.add("channelOrderNo");
                includeColumnFiledNames.add("userPhone");
                includeColumnFiledNames.add("gasSettlePrice");
                includeColumnFiledNames.add("gasAgentSettlePrice");

            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type2.getCode()) ) {
                includeColumnFiledNames.add("userPhone");
                includeColumnFiledNames.add("gasStaffName");


            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type3.getCode()) ) {
                includeColumnFiledNames.add("userPhone");


            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type4.getCode()) ) {


                includeColumnFiledNames.add("agentStaffName");
                includeColumnFiledNames.add("merName");
                includeColumnFiledNames.add("channelType");
                includeColumnFiledNames.add("channelOrderNo");
                includeColumnFiledNames.add("gasSettlePrice");
                includeColumnFiledNames.add("gasAgentSettlePrice");

            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type5.getCode()) ) {
                includeColumnFiledNames.add("merName");
                includeColumnFiledNames.add("channelType");
                includeColumnFiledNames.add("channelOrderNo");
                includeColumnFiledNames.add("gasSettlePrice");
                includeColumnFiledNames.add("gasAgentSettlePrice");

            } else if (sessionObject.getAccount().getObjectType().equals(SecUserObjectTypeEnum.type6.getCode()) ) {
                includeColumnFiledNames.add("merName");
                includeColumnFiledNames.add("userPhone");
                includeColumnFiledNames.add("gasStaffName");
                includeColumnFiledNames.add("gasSettlePrice");
                includeColumnFiledNames.add("gasAgentSettlePrice");
            }

            EasyExcel.write(fileUrl+pathName)
                    .head(ExportGasOrderModel.class)
                    .includeColumnFiledNames(includeColumnFiledNames)
                    .sheet("加油订单")
                    .doWrite(dataList);
            return ResponseMsgUtil.success(pathName);

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


    @RequestMapping(value="/repairOrder",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "补单")
    public ResponseData repairOrder(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("orderNo"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            // 查询加油订单
            BsGasOrder gasOrder = gasOrderService.getDetailByOrderNo(body.getString("orderNo"));
            if (gasOrder == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油的订单");
            }
            if (!gasOrder.getAbnormal().equals(true)) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "加油订单未处于异常状态，无需补单！");
            }
            // 补推
            gasOrderService.repairOrder(gasOrder.getOrderNo());

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

        } 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 = true) String orderNo) {
        try {
            // 查询加油订单
            BsGasOrder gasOrder = gasOrderService.getDetailByOrderNo(orderNo);
            if (gasOrder == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油的订单");
            }
            Map<String,Object> param = new HashMap<>();
            param.put("gasOrder", gasOrder);
            param.put("tradeOrder", orderService.getOrder(gasOrder.getOrderNo()));
            return ResponseMsgUtil.success(param);

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


    @RequestMapping(value="/printOrder",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "打印订单")
    public ResponseData printOrder(@RequestParam(name = "orderNo", required = true) String orderNo) {
        try {
            // 查询加油订单
            BsGasOrder gasOrder = gasOrderService.getDetailByOrderNo(orderNo);
            if (gasOrder == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油的订单");
            }
            deviceService.printGasOrder(gasOrder.getMerId(), gasOrder, true);
            return ResponseMsgUtil.success("操作成功");

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



}
