package com.bweb.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.fastjson.JSONObject;
import com.bweb.config.SysConfig;
import com.bweb.config.SysConst;
import com.bweb.excelListener.ImportGiveAwayListener;
import com.bweb.excelListener.ImportStoreListener;
import com.bweb.model.ImportGiveAwayModel;
import com.bweb.model.ImportStoreModel;
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.MemberValidateUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.*;
import com.hai.model.ResponseData;
import com.hai.model.UserInfoModel;
import com.hai.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;

/**
 * @Author Sum1Dream
 * @ClassName HighDiscountPackageController
 * @Description // 优惠券包接口
 * @Date 2021/11/25 4:32 下午
 **/
@Controller
@RequestMapping(value = "/highDiscountPackage")
@Api(value = "优惠券包接口")
public class HighDiscountPackageController {

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

    @Autowired
    private UserCenter userCenter;

    @Resource
    private HighUserService userService;

    @Resource
    private HighDiscountPackageService highDiscountPackageService;

    @Resource
    private HighDiscountPackageDeliverService discountPackageDeliverService;

    @Resource
    private HighDiscountPackageDetailsService highDiscountPackageDetailsService;

    @Resource
    private HighDiscountService highDiscountService;

    @Resource
    private HighDiscountInventoryRecordService discountInventoryRecordService;

    @Resource
    private HighDiscountPackageRecordService discountPackageRecordService;

    @Resource
    private HighDiscountPackageActualService discountPackageActualService;

    @RequestMapping(value = "/getDiscountPackageList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券包列表")
    public ResponseData getDiscountPackageList(@RequestParam(name = "usingAttribution", required = false) Integer usingAttribution,
                                             @RequestParam(name = "title", required = false) String title,
                                             @RequestParam(name = "salesType", required = false) Integer salesType,
                                             @RequestParam(name = "status", required = false) Integer status,
                                             @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("companyId", userInfoModel.getBsCompany().getId());
            map.put("usingAttribution", usingAttribution);
            map.put("title" , title);
            map.put("salesType" , salesType);
            map.put("status" , status);
            PageHelper.startPage(pageNum,pageSize);

            List<HighDiscountPackage> list = highDiscountPackageService.getDiscountPackageList(map);

            for (HighDiscountPackage data : list) {
                data.setSurplusStock(discountPackageActualService.getDiscountPackageNum(data.getId()).intValue());
            }

            return ResponseMsgUtil.success(new PageInfo<>(list));

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

    @RequestMapping(value = "/getDiscountPackageRecordList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券包记录列表")
    public ResponseData getDiscountPackageRecordList(@RequestParam(name = "usingAttribution", required = false) Integer usingAttribution,
                                               @RequestParam(name = "discountPackageTitle", required = false) String discountPackageTitle,
                                               @RequestParam(name = "recordNo", required = false) String recordNo,
                                               @RequestParam(name = "phone", required = false) String phone,
                                               @RequestParam(name = "salesType", required = false) Integer salesType,
                                               @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("companyId", userInfoModel.getBsCompany().getId());
            map.put("usingAttribution", usingAttribution);
            map.put("discountPackageTitle" , discountPackageTitle);
            map.put("recordNo" , recordNo);
            map.put("phone" , phone);
            map.put("salesType" , salesType);

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

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

    @RequestMapping(value = "/insertCouponPackageInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增优惠券包基本信息")
    public ResponseData insertCouponPackageInfo(@RequestBody HighDiscountPackage highDiscountPackage, HttpServletRequest request) {
        try {

            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (StringUtils.isBlank(highDiscountPackage.getTitle())
                    || highDiscountPackage.getSalesType() == null
//                    || StringUtils.isBlank(highDiscountPackage.getBannerImg())
//                    || StringUtils.isBlank(highDiscountPackage.getListImg())
//                    || StringUtils.isBlank(highDiscountPackage.getDetailsImg())
            ) {
                log.error("HighAgentController -> insertCouponPackageInfo() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> insertCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            if (highDiscountPackage.getSalesType() == 1) {
                if (highDiscountPackage.getPurchaseNum() == null
                        || highDiscountPackage.getPrice() == null
                ) {
                    log.error("HighAgentController -> insertCouponPackageInfo() error!","参数错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }

            if (highDiscountPackage.getUsingAttribution() != null) {
                if (highDiscountPackageService.getCallExclusive(highDiscountPackage.getUsingAttribution() , userInfoModel.getBsCompany().getId().intValue()) != null) {
                    log.error("highCouponPackage -> insertCouponPackageInfo() error!","当前优惠券已存在");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前优惠券已存在");
                }
            }

            highDiscountPackage.setCompanyId(userInfoModel.getBsCompany().getId().intValue());
            highDiscountPackage.setStatus(3);
            highDiscountPackage.setCreatedTime(new Date());
            highDiscountPackage.setCreatedUserId(userInfoModel.getSecUser().getId().intValue());
            highDiscountPackage.setUpdatedTime(new Date());
            highDiscountPackage.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());
            highDiscountPackage.setTotalStock(0);
            highDiscountPackage.setSurplusStock(0);

            highDiscountPackageService.insertDiscountPackage(highDiscountPackage);

            return ResponseMsgUtil.success(highDiscountPackage);

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

    @RequestMapping(value = "/updateCouponPackageInfo", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "修改优惠券包基本信息")
    public ResponseData updateCouponPackageInfo(@RequestBody HighDiscountPackage highDiscountPackage, HttpServletRequest request) {
        try {

            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (StringUtils.isBlank(highDiscountPackage.getTitle())
                    || highDiscountPackage.getSalesType() == null
                    || highDiscountPackage.getId() == null
//                    || StringUtils.isBlank(highDiscountPackage.getBannerImg())
//                    || StringUtils.isBlank(highDiscountPackage.getListImg())
//                    || StringUtils.isBlank(highDiscountPackage.getDetailsImg())
            ) {
                log.error("HighAgentController -> updateCouponPackageInfo() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> updateCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            HighDiscountPackage highDiscountPackages = highDiscountPackageService.findDiscountPackageById(highDiscountPackage.getId());

            if (highDiscountPackages == null || highDiscountPackages.getStatus() == 1) {
                log.error("highCouponPackage -> updateCouponPackageInfo() error!","状态错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }

            if (highDiscountPackage.getSalesType() == 1) {
                if (highDiscountPackage.getPurchaseNum() == null
                        || highDiscountPackage.getPrice() == null
                ) {
                    log.error("HighAgentController -> updateCouponPackageInfo() error!","参数错误");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
                }
            }

            if (highDiscountPackage.getUsingAttribution() != null) {
                HighDiscountPackage highDiscountPackage1 = highDiscountPackageService.getCallExclusive(highDiscountPackage.getUsingAttribution() , userInfoModel.getBsCompany().getId().intValue());
                if (highDiscountPackage1 != null && highDiscountPackage1.getId() != highDiscountPackage.getId()) {
                    log.error("highCouponPackage -> updateCouponPackageInfo() error!","专属优惠券已存在");
                    throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "专属优惠券已存在");
                }
            }

            highDiscountPackage.setStatus(3);
            highDiscountPackage.setUpdatedTime(new Date());
            highDiscountPackage.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());
            highDiscountPackage.setCompanyId(highDiscountPackages.getCompanyId());
            highDiscountPackage.setCreatedUserId(highDiscountPackages.getCreatedUserId());
            highDiscountPackage.setCreatedTime(highDiscountPackages.getCreatedTime());
            highDiscountPackage.setTotalStock(highDiscountPackages.getTotalStock());

            highDiscountPackageService.updateDiscountPackage(highDiscountPackage);

            return ResponseMsgUtil.success(highDiscountPackage);

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

    @RequestMapping(value = "/insertCouponPackageRule", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "新增优惠券包规则信息")
    public ResponseData insertCouponPackageRule(@RequestBody HighDiscountPackageDetails highDiscountPackageDetails, HttpServletRequest request) {
        try {
            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if ( highDiscountPackageDetails.getDiscountPackageId() == null
                    || highDiscountPackageDetails.getDiscountId() == null
                    || highDiscountPackageDetails.getNum() == null
            ) {
                log.error("HighAgentController -> insertCouponPackageRule() error!","参数错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> insertCouponPackageRule() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            HighDiscount discount = highDiscountService.getDiscountById(highDiscountPackageDetails.getDiscountId().longValue());

            if (discount == null || highDiscountPackageService.findDiscountPackageById(highDiscountPackageDetails.getDiscountPackageId()) == null) {
                log.error("highCouponPackage -> insertCouponPackageRule() error!","优惠券 或者 优惠券包不存在");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "优惠券 或者 优惠券包不存在");
            }

            Map<String , Object> map = new HashMap<>();
            map.put("discountPackageId", highDiscountPackageDetails.getDiscountPackageId());
            map.put("discountId" , highDiscountPackageDetails.getDiscountId());

            if (highDiscountPackageDetailsService.getDiscountPackageDetailsList(map).size() > 0) {
                log.error("highCouponPackage -> insertCouponPackageRule() error!","当前优惠券已存在");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前优惠券已存在");
            }

            highDiscountPackageDetails.setStatus(1);
            highDiscountPackageDetails.setDiscountName(discount.getDiscountName());
            highDiscountPackageDetails.setCreatedTime(new Date());
            highDiscountPackageDetails.setCreatedUserId(userInfoModel.getSecUser().getId().intValue());
            highDiscountPackageDetails.setUpdatedTime(new Date());
            highDiscountPackageDetails.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());

            highDiscountPackageDetailsService.insertDiscountPackageDetails(highDiscountPackageDetails);

            return ResponseMsgUtil.success("新增成功");

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

    @RequestMapping(value = "/getDiscountPackageRuleList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券包规则列表")
    public ResponseData getDiscountPackageRuleList(@RequestParam(name = "id", required = true) Integer id,
                                             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("highCouponPackage -> highCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            map.put("discountPackageId", id);

            return ResponseMsgUtil.success(highDiscountPackageDetailsService.getDiscountPackageDetailsList(map));

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

    @RequestMapping(value = "/deleteDiscountPackageRule", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除优惠券包规则")
    public ResponseData deleteDiscountPackageRule(@RequestBody JSONObject object, HttpServletRequest request) {
        try {

            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            HighDiscountPackageDetails highDiscountPackageDetails = highDiscountPackageDetailsService.findDetailsById(object.getInteger("discountPackageId"));

            if (highDiscountPackageDetails == null || highDiscountPackageDetails.getStatus() == 2) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","状态错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }

            highDiscountPackageDetails.setStatus(2);
            highDiscountPackageDetails.setUpdatedTime(new Date());
            highDiscountPackageDetails.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());

            highDiscountPackageDetailsService.updateDiscountPackageDetails(highDiscountPackageDetails);

            return ResponseMsgUtil.success("删除成功");

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

    @RequestMapping(value = "/deleteDiscountPackage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "删除优惠券包")
    public ResponseData deleteDiscountPackage(@RequestBody JSONObject object, HttpServletRequest request) {
        try {

            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            HighDiscountPackage highDiscountPackage = highDiscountPackageService.findDiscountPackageById(object.getInteger("discountPackageId"));

            if (highDiscountPackage == null || highDiscountPackage.getStatus() == 1) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","状态错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }

            highDiscountPackage.setStatus(4);
            highDiscountPackage.setUpdatedTime(new Date());
            highDiscountPackage.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());

            highDiscountPackageService.updateDiscountPackage(highDiscountPackage);

            return ResponseMsgUtil.success("删除成功");

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

    @RequestMapping(value = "upDiscountPackage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "上架优惠券包")
    public ResponseData upDiscountPackage(@RequestBody JSONObject object, HttpServletRequest request) {
        try {

            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            HighDiscountPackage highDiscountPackage = highDiscountPackageService.findDiscountPackageById(object.getInteger("discountPackageId"));

            if (highDiscountPackage == null || highDiscountPackage.getStatus() == 1 ) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","状态错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }

            if (discountPackageActualService.getDiscountPackageNum(object.getInteger("discountPackageId")) == 0 ) {
                log.error("highCouponPackage -> upDiscountPackage() error!","当前库存不足");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "当前库存不足");
            }

            highDiscountPackage.setStatus(1);
            highDiscountPackage.setUpdatedTime(new Date());
            highDiscountPackage.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());

            highDiscountPackageService.updateDiscountPackage(highDiscountPackage);

            return ResponseMsgUtil.success("上架成功");

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

    @RequestMapping(value = "downDiscountPackage", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "下架优惠券包")
    public ResponseData downDiscountPackage(@RequestBody JSONObject object, HttpServletRequest request) {
        try {

            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            HighDiscountPackage highDiscountPackage = highDiscountPackageService.findDiscountPackageById(object.getInteger("discountPackageId"));

            if (highDiscountPackage == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","状态错误");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.STATUS_ERROR, "");
            }

            highDiscountPackage.setStatus(2);
            highDiscountPackage.setUpdatedTime(new Date());
            highDiscountPackage.setUpdatedUserId(userInfoModel.getSecUser().getId().intValue());

            highDiscountPackageService.updateDiscountPackage(highDiscountPackage);

            return ResponseMsgUtil.success("上架成功");

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

    @RequestMapping(value="/getDiscountPackageById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询")
    public ResponseData getDiscountPackageById(@RequestParam(name = "id", required = true) Integer id) {
        try {

            return ResponseMsgUtil.success(highDiscountPackageService.findDiscountPackageById(id));

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

    @RequestMapping(value="/getDiscountPackageRuleById",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据id查询优惠券规则")
    public ResponseData getDiscountPackageRuleById(@RequestParam(name = "id", required = true) Integer id) {
        try {

            Map<String,Object> map = new HashMap<>();
            
            map.put("discountPackageId" , id);

            return ResponseMsgUtil.success(highDiscountPackageDetailsService.getDiscountPackageDetailsList(map));

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

    @RequestMapping(value="/addDiscountPackageStock",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加优惠券包库存")
    public ResponseData addDiscountPackageStock(@RequestBody JSONObject object, HttpServletRequest request) {
        try {
            //发布人员
            SessionObject sessionObject = userCenter.getSessionObject(request);
            UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();

            if (userInfoModel.getBsCompany() == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }

            HighDiscountPackage highDiscountPackage = highDiscountPackageService.findDiscountPackageById(object.getInteger("discountPackageId"));

            if (highDiscountPackage == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","当前优惠券包不存在");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "");
            }

            if (object.getInteger("num") == null) {
                log.error("highCouponPackage -> highCouponPackageInfo() error!","请传入库存数量");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "");
            }

            highDiscountPackageService.addDiscountPackageStock(highDiscountPackage , userInfoModel , object.getInteger("num"));

            return ResponseMsgUtil.success("增加库存成功");

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


    @RequestMapping(value = "/getDiscountInventoryRecordServList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券包记录列表")
    public ResponseData getDiscountInventoryRecordServList(
                                                     @RequestParam(name = "title", required = false) String title,
                                                     @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("companyId", userInfoModel.getBsCompany().getId());
            map.put("title", title);
            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(discountInventoryRecordService.getDiscountInventoryRecordServList(map)));

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

    @RequestMapping(value = "/getDiscountPackageOrderList", method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取优惠券包订单记录列表")
    public ResponseData getDiscountPackageOrderList(
            @RequestParam(name = "title", required = false) String title,
            @RequestParam(name = "phone", required = false) String phone,
            @RequestParam(name = "recordNo", required = false) String recordNo,
            @RequestParam(name = "saleType", required = false) Integer saleType,
            @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("companyId", userInfoModel.getBsCompany().getId());
            map.put("title", title);
            map.put("phone", phone);
            map.put("recordNo", recordNo);
            map.put("saleType", saleType);
            PageHelper.startPage(pageNum,pageSize);
            return ResponseMsgUtil.success(new PageInfo<>(discountPackageRecordService.getDiscountPackageRecord(map)));

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

    @RequestMapping(value = "/getPhoneListByFileUrl", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "解析Excel文件，获取手机号列表")
    public ResponseData getPhoneListByFileUrl(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("fileUrl"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            String fileUrl = SysConst.getSysConfig().getFileUrl() + File.separator + body.getString("fileUrl");

            Map<String, String> param = new HashMap<>();
            param.put("status", "1");

            ImportGiveAwayListener giveAwayListener = new ImportGiveAwayListener();
            giveAwayListener.initData(userService.getListUser(param));
            EasyExcel.read(fileUrl, ImportGiveAwayModel.class, giveAwayListener).sheet().doRead();

            Map<String, Object> map = new HashMap<>();
            map.put("data", giveAwayListener.dataList);
            return ResponseMsgUtil.success(map);

        } catch (ExcelAnalysisException e) {
            log.error("HighDiscountPackageController --> getPhoneListByParseExcel() error!", e);
            return ResponseMsgUtil.builderResponse(ErrorCode.COMMON_ERROR.getCode(), "文件地址错误", null);
        } catch (Exception e) {
            log.error("HighDiscountPackageController --> getPhoneListByParseExcel() error!", e);
            return ResponseMsgUtil.exception(e);
        }
    }

    @RequestMapping(value = "/getPhoneRegisterStatus", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询手机号注册状态")
    public ResponseData getPhoneRegisterStatus(@RequestBody JSONObject body) {
        try {
            if (body == null || StringUtils.isBlank(body.getString("phone"))) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            return ResponseMsgUtil.success(userService.findByPhone(body.getString("phone")) != null?true:false);

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

    @RequestMapping(value = "/getHandleByPackageId", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "查询进行中的赠送任务")
    public ResponseData getHandleByPackageId(@RequestBody JSONObject body) {
        try {
            if (body == null || body.getLong("packageId") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }

            return ResponseMsgUtil.success(discountPackageDeliverService.getHandleByPackageId(body.getLong("packageId")));

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

    @RequestMapping(value = "/giveAway", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "赠送")
    public ResponseData giveAway(@RequestBody JSONObject body) {
        try {
            //发布人员
            UserInfoModel userInfoModel = userCenter.getSessionModel(UserInfoModel.class);
            if (userInfoModel == null || userInfoModel.getBsCompany() == null) {
                log.error("HighDiscountPackageController -> giveAway() error!","该主角色没有权限");
                throw ErrorHelp.genException(SysCode.System, ErrorCode.MENU_TREE_HAS_NOT_ERROR, "");
            }
            if (body.getLong("packageId") == null || body.getJSONArray("phoneArray") == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
            }
            if (body.getJSONArray("phoneArray").size() == 0) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "没有需要赠送的用户");
            }

            List<String> phoneList = new ArrayList<>();

            for (Object o : body.getJSONArray("phoneArray")) {
                JSONObject phone = (JSONObject) o;
                if (StringUtils.isNotBlank(phone.getString("phone")) && MemberValidateUtil.validatePhone(phone.getString("phone"))) {
                    if (phoneList.stream().filter(p -> p.equals(phone)).count() == 0) {
                        phoneList.add(phone.getString("phone"));
                    }
                }
            }

            return ResponseMsgUtil.success(highDiscountPackageService.giveAway(body.getLong("packageId"), phoneList));

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

}
