package com.bweb.controller; import com.alibaba.fastjson.JSONObject; 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.ResponseMsgUtil; import com.hai.entity.*; import com.hai.model.ResponseData; import com.hai.model.UserInfoModel; import com.hai.service.HighAgentService; import com.hai.service.HighDiscountPackageAgentService; import com.hai.service.HighDiscountPackageService; 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.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.util.*; @Controller @RequestMapping(value = "/discountPackageAgent") @Api(value = "优惠券包代理商接口") public class HighDiscountPackageAgentController { private static Logger log = LoggerFactory.getLogger(HighDiscountPackageAgentController.class); @Resource private HighDiscountPackageAgentService discountPackageAgentService; @Resource private HighDiscountPackageService discountPackageService; @Resource private HighAgentService highAgentService; @Autowired private UserCenter userCenter; @RequestMapping(value="/distribute",method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "分发优惠券包") public ResponseData distribute(@RequestBody JSONObject object, HttpServletRequest request) { try { SessionObject sessionObject = userCenter.getSessionObject(request); UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject(); if (object.getLong("agentId") == null || object.getInteger("discountPackageId") == null || object.getInteger("num") == null) { log.error("discountPackageAgent -> distribute() error!","参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } HighDiscountPackage discountPackage = discountPackageService.findDiscountPackageById(object.getInteger("discountPackageId")); HighAgent agent = highAgentService.findByAgentMsgId(object.getLong("agentId")); if (object.getLong("parentAgentId") != null) { Map mapParent = new HashMap<>(); mapParent.put("agentId" , object.getLong("parentAgentId")); mapParent.put("discountPackageId" , object.getLong("discountPackageId")); mapParent.put("type" , 1); HighDiscountPackageAgentRel discountPackageAgentRelParent = discountPackageAgentService.getDetailRelByMap(mapParent); if (discountPackageAgentRelParent != null && discountPackageAgentRelParent.getStockCount() < object.getInteger("num") ) { log.error("discountPackageAgent -> distribute() error!","参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "库存数量不足"); } discountPackageAgentRelParent.setStockCount(discountPackageAgentRelParent.getStockCount()-object.getInteger("num")); discountPackageAgentService.updateDiscountPackageAgentRel(discountPackageAgentRelParent); } if (discountPackage == null) { log.error("discountPackageAgent -> distribute() error!","参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "优惠券包不存在"); } if (agent == null) { log.error("discountPackageAgent -> distribute() error!","参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "代理商不存在"); } Map map = new HashMap<>(); map.put("agentId" , object.getLong("agentId")); map.put("discountPackageId" , object.getLong("discountPackageId")); map.put("type" , 1); HighDiscountPackageAgentRel discountPackageAgentRel = discountPackageAgentService.getDetailRelByMap(map); HighDiscountPackageAgentRecord discountPackageAgentRecord = new HighDiscountPackageAgentRecord(); if (discountPackageAgentRel == null) { discountPackageAgentRel = new HighDiscountPackageAgentRel(); discountPackageAgentRel.setStatus(1); discountPackageAgentRel.setType(1); discountPackageAgentRel.setOpId(userInfoModel.getSecUser().getId()); discountPackageAgentRel.setOpName(userInfoModel.getSecUser().getLoginName()); discountPackageAgentRel.setAgentId(object.getLong("agentId")); discountPackageAgentRel.setAgentName(agent.getAgentName()); discountPackageAgentRel.setDiscountPackageId(object.getLong("discountPackageId")); discountPackageAgentRel.setDiscountPackageTitle(discountPackage.getTitle()); discountPackageAgentRel.setSalesCount(0); discountPackageAgentRel.setStockCount(0); discountPackageAgentRel.setCreateTime(new Date()); discountPackageAgentRel.setUpdateTime(new Date()); } discountPackageAgentRel.setStockCount(object.getInteger("num") + discountPackageAgentRel.getStockCount()); if (discountPackageAgentRel.getId() == null) { discountPackageAgentService.insertDiscountPackageAgentRel(discountPackageAgentRel); } else { discountPackageAgentRel.setUpdateTime(new Date()); discountPackageAgentRel.setType(1); discountPackageAgentService.updateDiscountPackageAgentRel(discountPackageAgentRel); } discountPackageAgentRecord.setAgentId(object.getLong("agentId")); discountPackageAgentRecord.setAgentName(agent.getAgentName()); discountPackageAgentRecord.setDiscountPackageId(object.getLong("discountPackageId")); discountPackageAgentRecord.setDiscountPackageTitle(discountPackage.getTitle()); discountPackageAgentRecord.setCreateTime(new Date()); discountPackageAgentRecord.setUpdateTime(new Date()); discountPackageAgentRecord.setStatus(1); discountPackageAgentRecord.setType(1); discountPackageAgentRecord.setOpId(userInfoModel.getSecUser().getId()); discountPackageAgentRecord.setOpName(userInfoModel.getSecUser().getLoginName()); discountPackageAgentRecord.setCount(object.getInteger("num")); discountPackageAgentRecord.setAfterCount(discountPackageAgentRel.getStockCount()); discountPackageAgentRecord.setFrontCount(discountPackageAgentRel.getStockCount() - object.getInteger("num")); discountPackageAgentService.insertDiscountPackageAgentRecord(discountPackageAgentRecord); return ResponseMsgUtil.success("操作成功"); } catch (Exception e) { log.error("HighDiscountCouponRelController -> insertDiscountCoupon() error!",e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/getDiscountPackageAgentRelList", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "获取优惠券包代理商关联列表") public ResponseData getDiscountPackageAgentRelList(@RequestParam(name = "agentId", required = false) Long agentId, @RequestParam(name = "type", required = false) Long type) { try { Map map = new HashMap<>(); map.put("agentId",agentId); map.put("type",type); List list = discountPackageAgentService.getListRel(map); return ResponseMsgUtil.success(list); } catch (Exception e) { log.error("HighOrderController --> getUserOrderList() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/getDiscountPackageAgentRecord", method = RequestMethod.GET) @ResponseBody @ApiOperation(value = "获取优惠券包代理商分发记录列表") public ResponseData getDiscountPackageAgentRecord(@RequestParam(name = "agentId", required = false) Long agentId, @RequestParam(name = "type", required = false) Integer type, @RequestParam(name = "discountPackageId", required = false) Long discountPackageId) { try { Map map = new HashMap<>(); map.put("agentId",agentId); map.put("discountPackageId",discountPackageId); map.put("type",type); List list = discountPackageAgentService.getListRecord(map); return ResponseMsgUtil.success(list); } catch (Exception e) { log.error("HighOrderController --> getUserOrderList() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/generateDiscountPackageAgentCode", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "绑定优惠券包") public ResponseData generateDiscountPackageAgentCode(@RequestBody JSONObject object) { try { if (object.getLong("agentId") == null || object.getInteger("discountPackageId") == null || object.getInteger("num") == null) { log.error("discountPackageAgent -> distribute() error!","参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } HighDiscountPackage discountPackage = discountPackageService.findDiscountPackageById(object.getInteger("discountPackageId")); HighAgent agent = highAgentService.findByAgentMsgId(object.getLong("agentId")); if (discountPackage == null || agent == null) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "相关内容不存在!"); } JSONObject objectMap = new JSONObject(); objectMap.put("agentId",agent.getId()); objectMap.put("agentName",agent.getAgentName()); objectMap.put("discountPackageId",object.getInteger("discountPackageId")); objectMap.put("discountPackageName",discountPackage.getTitle()); objectMap.put("num" , object.getInteger("num")); objectMap.put("serialNumber" , object.getString("serialNumber")); discountPackageAgentService.generateDiscountPackageAgentCode(objectMap); return ResponseMsgUtil.success("生成成功"); } catch (Exception e) { log.error("HighOrderController --> getUserOrderList() error!", e); return ResponseMsgUtil.exception(e); } } @RequestMapping(value = "/recoveryDiscountPackage", method = RequestMethod.POST) @ResponseBody @ApiOperation(value = "回收优惠券包") public ResponseData recoveryDiscountPackage(@RequestBody JSONObject object) { try { if (object.getLong("agentId") == null || object.getInteger("discountPackageId") == null || object.getInteger("num") == null) { log.error("discountPackageAgent -> recoveryDiscountPackage() error!","参数错误"); throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, ""); } if (object.getInteger("num") < 1) { throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "请输入正确数量!"); } discountPackageAgentService.recoveryDiscountPackage(object); return ResponseMsgUtil.success("回收成功"); } catch (Exception e) { log.error("HighOrderController --> getUserOrderList() error!", e); return ResponseMsgUtil.exception(e); } } }