package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bweb.config.SysConst;
import com.bweb.model.ExportHighOrderModel;
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.pay.util.XmlUtil;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.common.utils.WxUtils;
import com.hai.config.HuiLianTongUnionCardConfig;
import com.hai.entity.HighOrder;
import com.hai.entity.HighTyAgent;
import com.hai.entity.HighTySalesman;
import com.hai.entity.OutRechargeOrder;
import com.hai.model.HighOrderModel;
import com.hai.model.OrderRefundModel;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.HighCouponCodeService;
import com.hai.service.HighOrderService;
import com.hai.service.HighTyAgentService;
import com.hai.service.HighTySalesmanService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.io.File;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Auther: 袁野
 * @Description:
 * @Date: 2021/3/26 23:08
 */
@Controller
@RequestMapping(value = "/highOrder")
@Api(value = "订单接口")
public class HighOrderController {

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

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighOrderService highOrderService;

    @Resource
    private HighCouponCodeService highCouponCodeService;

    @Resource
    private HighTyAgentService highTyAgentService;

    @Resource
    private HighTySalesmanService highTySalesmanService;


    @RequestMapping(value = "/print", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "打印")
    public ResponseData print(@RequestParam(name = "orderId", required = true) Long orderId,
                              @RequestParam(name = "printType", required = true) Integer printType) {
        try {

            HighOrder order = highOrderService.getOrderById(orderId);
            if (order == null) {
                log.error("HighCouponController -> getCouponList() error!","权限不足");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }
            order.setPrintStatus(true);
            order.setPrintNum(order.getPrintNum() + 1);
            highOrderService.updateOrderDetail(order);

            // 1. 热敏打印机  2. 云打印机
            if (printType == 2) {
                highOrderService.printGasOrder(order.getHighChildOrderList().get(0).getGoodsId(), order, true);
            }

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

        } catch (Exception e) {
            log.error("HighOrderController --> print() 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 {

            return ResponseMsgUtil.success(highOrderService.getOrderById(orderId));

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


    @RequestMapping(value = "/getOrderCouponList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取卡券订单列表")
    public ResponseData getUserOrderList(@RequestParam(name = "pageNum", required = true) Integer pageNum,
                                         @RequestParam(name = "pageSize", required = true) Integer pageSize,
                                         @RequestParam(name = "status", required = false) Integer status,
                                         @RequestParam(name = "couponId", required = false) Integer couponId,
                                         @RequestParam(name = "salesCode", required = false) Integer salesCode,
                                         HttpServletRequest request) {
        try {

            Map<String,Object> map = new HashMap<>();

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            if (userInfoModel.getBsCompany() == null) {
                log.error("HighCouponController -> getCouponList() error!","权限不足");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }
//            map.put("companyId", userInfoModel.getBsCompany().getId());
            map.put("couponId", couponId);
            map.put("salesCode", salesCode);
            map.put("status", status);

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

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

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

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();


            Map<String,Object> map = new HashMap<>();

            if (userInfoModel.getSecRole().getRoleType() == 5) {
                map.put("companyId" , userInfoModel.getBsCompany().getId());
            }
            if (userInfoModel.getSecRole().getRoleType() == 2) {
                map.put("merchantId" , userInfoModel.getMerchant().getId());
            }
            if (userInfoModel.getSecRole().getRoleType() == 3) {
                map.put("storeId" , userInfoModel.getMerchantStore().getId());
            }
            map.put("orderStatus", orderStatus);
            map.put("orderNo", orderNo);
            map.put("memPhone", memPhone);

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

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


    @RequestMapping(value = "/getGoodsOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询商品订单列表")
    public ResponseData getGoodsOrderList(@RequestParam(name = "companyId", required = false) Long companyId,
                                          @RequestParam(name = "merchantId", required = false) Long merchantId,
                                          @RequestParam(name = "orderNo", required = false) String orderNo,
                                          @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                          @RequestParam(name = "memDiscountName", required = false) String memDiscountName,
                                          @RequestParam(name = "memPhone", required = false) String memPhone,
                                          @RequestParam(name = "payModel", required = false) Integer payModel,
                                          @RequestParam(name = "payType", required = false) Integer payType,
                                          @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                          @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,
                                          HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {

                if (userInfoModel.getBsCompany().getId() == 3) {
                    map.put("companyId", 23);
                } else {
                    map.put("companyId", userInfoModel.getBsCompany().getId());
                }

                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());

            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("memDiscountName", memDiscountName);
            map.put("payType", payType);
            map.put("memPhone", memPhone);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

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

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

    @RequestMapping(value = "/exportGoodsOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出商品订单列表")
    public ResponseData exportGoodsOrderList(@RequestParam(name = "companyId", required = false) Long companyId,
                                             @RequestParam(name = "merchantId", required = false) Long merchantId,
                                             @RequestParam(name = "orderNo", required = false) String orderNo,
                                             @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                             @RequestParam(name = "payModel", required = false) Integer payModel,
                                             @RequestParam(name = "payType", required = false) Integer payType,
                                             @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                             @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,
                                             HttpServletRequest request) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);
                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {
                if (userInfoModel.getBsCompany().getId() == 3) {
                    map.put("companyId", 23);
                } else {
                    map.put("companyId", userInfoModel.getBsCompany().getId());
                }

                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

            String name = "/goodsOrder/商品订单" + System.currentTimeMillis() + ".xlsx";
            List<HighOrderModel> list = highOrderService.getGoodsOrderModelList(map);
            String fileName = SysConst.getSysConfig().getFileUrl() + name;
            EasyExcel.write(fileName, ExportHighOrderModel.class).sheet("商品订单").doWrite(list);
            return ResponseMsgUtil.success(name);

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


    @RequestMapping(value = "/getGasOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询加油订单列表")
    public ResponseData getGasOrderList(@RequestParam(name = "companyId", required = false) Long companyId,
                                        @RequestParam(name = "merchantId", required = false) Long merchantId,
                                        @RequestParam(name = "orderNo", required = false) String orderNo,
                                        @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                        @RequestParam(name = "payModel", required = false) Integer payModel,
                                        @RequestParam(name = "payType", required = false) Integer payType,
                                        @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                        @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,
                                        HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {
                map.put("companyId", userInfoModel.getBsCompany().getId());
                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());
                
            } else {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMPETENCE_INSUFFICIENT, "");
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

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

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

    @RequestMapping(value = "/getKfcOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询KFC订单列表")
    public ResponseData getKfcOrderList(@RequestParam(name = "companyId", required = false) Long companyId,
                                        @RequestParam(name = "merchantId", required = false) Long merchantId,
                                        @RequestParam(name = "orderNo", required = false) String orderNo,
                                        @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                        @RequestParam(name = "payModel", required = false) Integer payModel,
                                        @RequestParam(name = "payType", required = false) Integer payType,
                                        @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                        @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,
                                        HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {
                map.put("companyId", userInfoModel.getBsCompany().getId());
                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

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

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

    @RequestMapping(value = "/getCinemaOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询电影订单列表")
    public ResponseData getCinemaOrderList(@RequestParam(name = "companyId", required = false) Long companyId,
                                           @RequestParam(name = "merchantId", required = false) Long merchantId,
                                           @RequestParam(name = "orderNo", required = false) String orderNo,
                                           @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                           @RequestParam(name = "payModel", required = false) Integer payModel,
                                           @RequestParam(name = "payType", required = false) Integer payType,
                                           @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                           @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,
                                           HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {
                map.put("companyId", userInfoModel.getBsCompany().getId());
                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

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

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

    @RequestMapping(value = "/getOrderListByIdCode", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据表示查询订单")
    public ResponseData getOrderListByIdCode(@RequestParam(name = "merchantId", required = false) Long merchantId,
                                             @RequestParam(name = "identificationCode", required = true) String identificationCode,
                                             @RequestParam(name = "orderNo", required = false) String orderNo,
                                             @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                             @RequestParam(name = "memPhone", required = false) String memPhone,
                                             @RequestParam(name = "memDiscountName", required = false) String memDiscountName,
                                             @RequestParam(name = "payModel", required = false) Integer payModel,
                                             @RequestParam(name = "payType", required = false) Integer payType,
                                             @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                             @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,
                                             HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            map.put("identificationCode", identificationCode);
            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("memPhone", memPhone);
            map.put("memDiscountName", memDiscountName);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(highOrderService.getOrderListByIdCode(identificationCode, map)));

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

    @RequestMapping(value = "/exportOrderListByIdCode", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出商品订单列表")
    public ResponseData exportOrderListByIdCode(@RequestParam(name = "companyId", required = false) Long companyId,
                                                @RequestParam(name = "merchantId", required = false) Long merchantId,
                                                @RequestParam(name = "identificationCode", required = true) String identificationCode,
                                                @RequestParam(name = "orderNo", required = false) String orderNo,
                                                @RequestParam(name = "memPhone", required = false) String memPhone,
                                                @RequestParam(name = "memDiscountName", required = false) String memDiscountName,
                                                @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                                @RequestParam(name = "payModel", required = false) Integer payModel,
                                                @RequestParam(name = "payType", required = false) Integer payType,
                                                @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                                @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,
                                                HttpServletRequest request) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            Map<String,Object> map = new HashMap<>();
            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("memPhone", memPhone);
            map.put("memDiscountName", memDiscountName);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

            String name = "/goodsOrder/订单列表" + System.currentTimeMillis() + ".xlsx";
            List<HighOrderModel> list = highOrderService.getOrderListByIdCode(identificationCode,map);
            String fileName = SysConst.getSysConfig().getFileUrl() + name;
            EasyExcel.write(fileName, ExportHighOrderModel.class).sheet("商品订单").doWrite(list);
            return ResponseMsgUtil.success(name);

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

    @RequestMapping(value = "/getMobileOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询话费充值订单列表")
    public ResponseData getMobileOrderList(@RequestParam(name = "companyId", required = false) Long companyId,
                                           @RequestParam(name = "merchantId", required = false) Long merchantId,
                                           @RequestParam(name = "orderNo", required = false) String orderNo,
                                           @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                           @RequestParam(name = "payModel", required = false) Integer payModel,
                                           @RequestParam(name = "payType", required = false) Integer payType,
                                           @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                           @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,
                                           HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {
                map.put("companyId", userInfoModel.getBsCompany().getId());
                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

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

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


    @RequestMapping(value = "/orderToRefundHlt", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "汇联通订单退款")
    public ResponseData orderToRefundHlt(@RequestParam(name = "orderId", required = false) Long orderId,
                                         HttpServletRequest request) {
        try {

            HighOrder order = highOrderService.getOrderById(orderId);
            if (order == null) {
                log.error("orderToPay error!", "未找到充值订单");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到充值订单");
            }

            // 订单状态 ： 1.待支付 2.已支付 3.已完成 4.已取消 5.已退款
            if (order.getOrderStatus() != 2) {
                log.error("orderToPay error!", "无法退款，订单不处于已支付状态");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法退款，订单不处于已支付状态");
            }


            JSONObject jsonObject = HuiLianTongUnionCardConfig.refund( "HFR"+new Date().getTime() , order.getOrderNo());
            if (jsonObject == null) {
                log.error("orderToPay error!", "请求超时");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请求超时，请重新点击");
            }

            JSONObject dataObject = HuiLianTongUnionCardConfig.resolveResponse(jsonObject.getString("data"));

            if (dataObject.getBoolean("success") || Objects.equals(dataObject.getString("message"), "原交易已撤销,不可再次操作")) {
                order.setOrderStatus(4);
                order.setRefundTime(new Date());
                order.setRefundContent(dataObject.getString("orderId"));
                order.setRefusalRefundContent("HFR"+new Date().getTime() );
                order.setRefundPrice(order.getPayRealPrice());
                highOrderService.updateOrder(order);
                return ResponseMsgUtil.success("退款成功");
            } else {
                log.error("orderToPay error!", dataObject.getString("message"));
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, dataObject.getString("message"));
            }

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

    @RequestMapping(value = "/getDiscountPackageList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "查询优惠券包订单列表")
    public ResponseData getDiscountPackageList(@RequestParam(name = "companyId", required = false) Long companyId,
                                          @RequestParam(name = "merchantId", required = false) Long merchantId,
                                          @RequestParam(name = "orderNo", required = false) String orderNo,
                                          @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                          @RequestParam(name = "memDiscountName", required = false) String memDiscountName,
                                          @RequestParam(name = "memPhone", required = false) String memPhone,
                                          @RequestParam(name = "payModel", required = false) Integer payModel,
                                          @RequestParam(name = "payType", required = false) Integer payType,
                                          @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                          @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,
                                          HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {

                if (userInfoModel.getBsCompany().getId() == 3) {
                    map.put("companyId", 23);
                } else {
                    map.put("companyId", userInfoModel.getBsCompany().getId());
                }

                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("memDiscountName", memDiscountName);
            map.put("payType", payType);
            map.put("memPhone", memPhone);
            map.put("goodsType", 7);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

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

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

    @RequestMapping(value = "/exportDiscountPackageList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出优惠券包订单列表")
    public ResponseData exportDiscountPackageList(@RequestParam(name = "companyId", required = false) Long companyId,
                                             @RequestParam(name = "merchantId", required = false) Long merchantId,
                                             @RequestParam(name = "orderNo", required = false) String orderNo,
                                             @RequestParam(name = "paySerialNo", required = false) String paySerialNo,
                                             @RequestParam(name = "payModel", required = false) Integer payModel,
                                             @RequestParam(name = "payType", required = false) Integer payType,
                                             @RequestParam(name = "orderStatus", required = false) Integer orderStatus,
                                             @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,
                                             HttpServletRequest request) {
        try {
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
            Map<String,Object> map = new HashMap<>();
            // 用户来源 0：超级管理员  1：公司  2：商户  3：门店 4. 代理商
            if(userInfoModel.getSecUser().getObjectType() == 0) {
                map.put("companyId", companyId);
                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 1) {
                if (userInfoModel.getBsCompany().getId() == 3) {
                    map.put("companyId", 23);
                } else {
                    map.put("companyId", userInfoModel.getBsCompany().getId());
                }

                map.put("merchantId", merchantId);

            } else if (userInfoModel.getSecUser().getObjectType() == 2) {
                map.put("merchantId", userInfoModel.getMerchant().getId());
            }

            map.put("orderNo", orderNo);
            map.put("paySerialNo", paySerialNo);
            map.put("payModel", payModel);
            map.put("payType", payType);
            map.put("orderStatus", orderStatus);
            map.put("createTimeS", createTimeS);
            map.put("createTimeE", createTimeE);
            map.put("goodsType", 7);
            map.put("payTimeS", payTimeS);
            map.put("payTimeE", payTimeE);

            String name = "/goodsOrder/商品订单" + System.currentTimeMillis() + ".xlsx";
            List<HighOrderModel> list = highOrderService.selectDiscountPackageOrderList(map);
            String fileName = SysConst.getSysConfig().getFileUrl() + name;
            EasyExcel.write(fileName, ExportHighOrderModel.class).sheet("商品订单").doWrite(list);
            return ResponseMsgUtil.success(name);

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


    @RequestMapping(value = "/orderByIndex", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "订单首页统计")
    public ResponseData orderByIndex(
            @RequestParam(name = "code", required = true) Integer code) {
        try {

            return ResponseMsgUtil.success(highOrderService.orderByIndex(code));

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

    @RequestMapping(value = "/HLTOrderByIndex", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "工会卡预支付首页统计")
    public ResponseData HLTOrderByIndex(
            @RequestParam(name = "code", required = true) Integer code) {
        try {

            return ResponseMsgUtil.success(highOrderService.HLTOrderByIndex(code));

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


    @RequestMapping(value = "/HLTOrderByList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "工会卡预支付查询列表")
    public ResponseData HLTOrderByList(
            @RequestParam(name = "code", required = true) Integer code) {
        try {

            return ResponseMsgUtil.success(highOrderService.HLTOrderByList(code));

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

    @RequestMapping(value = "/exportHLTOrderByList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "导出预支付订单列表")
    public ResponseData exportHLTOrderByList(@RequestParam(name = "code", required = true) Integer code) {
        try {

            String name = "/goodsOrder/商品订单" + System.currentTimeMillis() + ".xlsx";
            List<HighOrderModel> list = highOrderService.HLTOrderByList(code);
            String fileName = SysConst.getSysConfig().getFileUrl() + name;
            EasyExcel.write(fileName, ExportHighOrderModel.class).sheet("商品订单").doWrite(list);
            return ResponseMsgUtil.success(name);

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

    @RequestMapping(value = "/TyOrderStatistics", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "团油订单统计")
    public ResponseData TyOrderStatistics(@RequestBody JSONObject object,HttpServletRequest request) {
        try {

            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            Map<String, Object> map = new HashMap<>();

            if (userInfoModel.getBsCompany() != null && userInfoModel.getSecUser().getAdminFlag().equals(1)) {
                map.put("companyId", userInfoModel.getBsCompany().getId());
            }

            if (object.getInteger("organizationId") != null) {
                map.put("orgId" , object.getInteger("organizationId"));
            }

            if (object.getInteger("agentId") != null) {
                map.put("agentId" , object.getInteger("agentId"));
            }

            if (object.getInteger("gasSalesmanId") != null) {
                map.put("gasSalesmanId" , object.getInteger("gasSalesmanId"));
            }


            map.put("payType" , object.getInteger("payType"));
            map.put("gasOilNo" , object.getString("gasOilNo"));
            map.put("gasOilType" , object.getInteger("gasOilType"));
            map.put("goodsId" , object.getLong("goodsId"));
            map.put("createTimeS" , object.getLong("createTimeS"));
            map.put("createTimeE" , object.getLong("createTimeE"));

            return ResponseMsgUtil.success(highOrderService.TyOrderStatistics(map));

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


}

