嗨森逛服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hai-server/hai-bweb/src/main/java/com/bweb/controller/HighCouponController.java

459 lines
22 KiB

package com.bweb.controller;
import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSONObject;
import com.bweb.excelListener.ImportCouponListener;
import com.bweb.model.ImportCouponModel;
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.generateCode;
import com.hai.common.security.SessionObject;
import com.hai.common.security.UserCenter;
import com.hai.common.utils.DateUtil;
import com.hai.common.utils.MemberValidateUtil;
import com.hai.common.utils.ResponseMsgUtil;
import com.hai.entity.*;
import com.hai.enum_type.ApproveType;
import com.hai.model.HighCouponHandselModel;
import com.hai.model.HighCouponModel;
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 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.util.*;
import java.util.stream.Collectors;
/**
* @Auther: 胡锐
* @Description:
* @Date: 2021/3/11 22:16
*/
@Controller
@RequestMapping(value = "/coupon")
@Api(value = "卡卷接口")
public class HighCouponController {
private static Logger log = LoggerFactory.getLogger(HighCouponController.class);
@Autowired
private UserCenter userCenter;
@Resource
private HighMerchantService highMerchantService;
@Resource
private HighCouponService highCouponService;
@Resource
private HighCouponHandselService highCouponHandselService;
@Resource
private HighCouponCodeService highCouponCodeService;
@Resource
private HighApproveService highApproveService;
@RequestMapping(value="/insertCoupon",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "增加卡卷")
public ResponseData insertCoupon(@RequestBody HighCouponModel highCoupon, HttpServletRequest request) {
try {
SessionObject sessionObject = userCenter.getSessionObject(request);
UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
if (highCoupon.getMerchantId() == null
|| StringUtils.isBlank(highCoupon.getCouponName())
|| StringUtils.isBlank(highCoupon.getCouponImg())
|| StringUtils.isBlank(highCoupon.getCouponCarouselImg())
|| StringUtils.isBlank(highCoupon.getCouponDesc())
|| highCoupon.getCouponPrice() == null
|| highCoupon.getCouponType() == null
|| highCoupon.getSalesEndTime() == null
|| highCoupon.getRecycleDay() == null
|| highCoupon.getLimitNumber()== null
|| highCoupon.getSalesPrice()== null
|| highCoupon.getIsPresent() == null) {
log.error("HighCouponController -> insertCoupon() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
// 是否赠送卡卷
if (highCoupon.getIsPresent() == true) {
if (highCoupon.getHandselCouponId() == null || highCoupon.getHandselCouponId().size() == 0) {
log.error("HighCouponController -> insertCoupon() error!","请选择赠送卡卷名单");
throw ErrorHelp.genException(SysCode.System, ErrorCode.SELECT_HANDSEL_COUPON_ERROR, "");
}
HighCouponHandselModel highCouponHandsel;
for (Long couponId : highCoupon.getHandselCouponId()) {
highCouponHandsel = new HighCouponHandselModel();
highCouponHandsel.setHandselCouponId(couponId);
highCouponHandsel.setCreateTime(new Date());
highCouponHandsel.setStatus(1); // 状态 0:删除 1:正常
highCouponHandsel.setOperatorId(userInfoModel.getSecUser().getId());
highCouponHandsel.setOperatorName(userInfoModel.getSecUser().getUserName());
highCoupon.getHandselCouponList().add(highCouponHandsel);
}
}
// 查询商户
HighMerchant merchant = highMerchantService.getMerchantById(highCoupon.getMerchantId());
if (merchant == null) {
log.error("HighCouponController -> insertCoupon() error!","未找到商户");
throw ErrorHelp.genException(SysCode.System, ErrorCode.MERCHANT_NOF_FOUND, "");
}
String setCouponKey = DateUtil.date2String(new Date(), "yyyyMMddhhmmss"); //订单号生成 年月日小时分秒 + 5位随机数
highCoupon.setCouponKey(setCouponKey);
highCoupon.setCompanyId(merchant.getCompanyId());
highCoupon.setOperatorId(userInfoModel.getSecUser().getId());
highCoupon.setOperatorName(userInfoModel.getSecUser().getUserName());
highCoupon.setCreateTime(new Date());
highCoupon.setUpdateTime(new Date());
highCoupon.setStatus(1); // 状态:0.删除 1.编辑中 2.已上架 3.已下架 101.上架审批中 102.上架审批驳回
highCouponService.insertCoupon(highCoupon);
return ResponseMsgUtil.success(highCoupon);
} catch (Exception e) {
log.error("HighCouponController -> insertCoupon() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/updateCoupon",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "修改卡卷")
public ResponseData updateCoupon(@RequestBody HighCouponModel highCoupon, HttpServletRequest request) {
try {
SessionObject sessionObject = userCenter.getSessionObject(request);
UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
if (highCoupon.getMerchantId() == null
|| StringUtils.isBlank(highCoupon.getCouponName())
|| StringUtils.isBlank(highCoupon.getCouponImg())
|| StringUtils.isBlank(highCoupon.getCouponCarouselImg())
|| StringUtils.isBlank(highCoupon.getCouponDesc())
|| highCoupon.getCouponPrice() == null
|| highCoupon.getCouponType() == null
|| highCoupon.getSalesEndTime() == null
|| highCoupon.getRecycleDay() == null
|| highCoupon.getLimitNumber()== null
|| highCoupon.getSalesPrice()== null
|| highCoupon.getIsPresent() == null) {
log.error("HighCouponController -> insertCoupon() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
// 查询卡券
HighCouponModel coupon = highCouponService.getCouponById(highCoupon.getId());
if (coupon == null) {
log.error("HighCouponController -> updateCoupon() error!","未找到卡卷信息");
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, "");
}
// 是否赠送卡卷
if (highCoupon.getIsPresent() == true) {
if (highCoupon.getHandselCouponId() == null || highCoupon.getHandselCouponId().size() == 0) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.SELECT_HANDSEL_COUPON_ERROR, "");
}
HighCouponHandselModel highCouponHandsel;
// 查询数据库中的赠送卡卷列表
List<HighCouponHandselModel> list = highCouponHandselService.getHandselListByCoupon(highCoupon.getId());
// 上传数据与数据库对比,需要新增的数据
for (Long couponId : highCoupon.getHandselCouponId()) {
List<HighCouponHandselModel> collect = list.stream().filter(o -> o.getHandselCouponId().equals(couponId)).collect(Collectors.toList());
if (collect == null || collect.size() == 0) {
highCouponHandsel = new HighCouponHandselModel();
highCouponHandsel.setHandselCouponId(couponId);
highCouponHandsel.setCreateTime(new Date());
highCouponHandsel.setStatus(1);
highCouponHandsel.setOperatorId(userInfoModel.getSecUser().getId());
highCouponHandsel.setOperatorName(userInfoModel.getSecUser().getUserName());
highCoupon.getHandselCouponList().add(highCouponHandsel);
}
}
// 上传数据与数据库对比,需要删除的数据
for (HighCouponHandsel handsel : list) {
List<Long> collect = highCoupon.getHandselCouponId().stream().filter(o -> o.equals(handsel.getHandselCouponId())).collect(Collectors.toList());
if (collect == null || collect.size() == 0) {
handsel.setStatus(0);
}
}
}
// 查询商户
HighMerchant merchant = highMerchantService.getMerchantById(highCoupon.getMerchantId());
if (merchant == null) {
log.error("HighCouponController -> updateCoupon() error!","未找到商户");
throw ErrorHelp.genException(SysCode.System, ErrorCode.MERCHANT_NOF_FOUND, "");
}
highCoupon.setCompanyId(merchant.getCompanyId());
highCoupon.setOperatorId(userInfoModel.getSecUser().getId());
highCoupon.setOperatorName(userInfoModel.getSecUser().getUserName());
highCoupon.setUpdateTime(new Date());
highCoupon.setStatus(3); // 状态:0.删除 1.编辑中 2.已上架 3.已下架 101.上架审批中 102.上架审批驳回
highCouponService.updateCoupon(highCoupon);
return ResponseMsgUtil.success(highCoupon);
} catch (Exception e) {
log.error("HighCouponController -> updateCoupon() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/upShelfApprove",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "卡卷上架审批")
public ResponseData upShelfApprove(@RequestParam(name = "id", required = true) Long id, HttpServletRequest request) {
try {
SessionObject sessionObject = userCenter.getSessionObject(request);
UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
// 查询卡券
HighCouponModel coupon = highCouponService.getCouponById(id);
if (coupon == null) {
log.error("HighCouponController -> upShelfApprove() error!","未找到卡卷信息");
throw ErrorHelp.genException(SysCode.System, ErrorCode.NOT_FOUND_COUPON, "");
}
// 如果卡卷状态不处于 编辑中、已下架、审上架批驳回
if (coupon.getStatus() != 1 && coupon.getStatus() != 3 && coupon.getStatus() != 102) {
log.error("HighCouponController -> upShelfApprove() error!","卡卷状态错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_UNABLE_UP_SHELF, "");
}
// 根据卡卷查询 销售码库存
if (highCouponCodeService.getStockCountByCoupon(coupon.getId()) == 0) {
log.error("HighCouponController -> upShelfApprove() error!","卡卷库存数量错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_STOCK_ERROR, "");
}
HighApprove approve = new HighApprove();
approve.setObjectType(ApproveType.UP_SHELF_APPROVE.getType());
approve.setObjectId(id);
approve.setObjectName(coupon.getCouponName());
approve.setApproveSerialNo(DateUtil.date2String(new Date(), "MMddhhmmss") + ((Math.random()*9+1)*10000));
approve.setStatus(1);
approve.setCreateTime(new Date());
approve.setUpdateTime(new Date());
approve.setSubmitOperatorId(userInfoModel.getSecUser().getId());
approve.setSubmitOperatorName(userInfoModel.getSecUser().getUserName());
// 增加审批
highApproveService.insertApprove(approve);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("HighCouponController -> upShelfApprove() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/ofShelfApprove",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "卡卷下架")
public ResponseData ofShelfApprove(@RequestParam(name = "id", required = true) Long id) {
try {
highCouponService.ofShelfCoupon(id);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("HighCouponController -> ofShelfApprove() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/deleteCoupon",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "删除卡卷")
public ResponseData deleteCoupon(@RequestParam(name = "id", required = true) Long id) {
try {
highCouponService.deleteCoupon(id);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("HighCouponController -> deleteCoupon() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/getCouponById",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "根据id 查询卡卷")
public ResponseData getCouponById(@RequestParam(name = "id", required = true) Long id) {
try {
return ResponseMsgUtil.success(highCouponService.getCouponById(id));
} catch (Exception e) {
log.error("HighCouponController -> getCouponById() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/getCouponList",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "卡卷列表")
public ResponseData getCouponList(
@RequestParam(name = "merchantId", required = false) Long merchantId,
@RequestParam(name = "couponName", required = false) String couponName,
@RequestParam(name = "couponType", required = false) Integer couponType,
@RequestParam(name = "status", required = false) Integer status,
@RequestParam(name = "pageNum", required = true) Integer pageNum,
@RequestParam(name = "pageSize", required = true) Integer pageSize, 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("merchantId", merchantId);
map.put("couponName", couponName);
map.put("couponType", couponType);
map.put("status", status);
PageHelper.startPage(pageNum, pageSize);
return ResponseMsgUtil.success(new PageInfo<>(highCouponService.getCouponList(map)));
} catch (Exception e) {
log.error("HighCouponController -> getCouponList() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/importStock",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "导入卡卷库存(外部卷)")
public ResponseData importStock(@RequestParam(name = "couponId", required = true) Long couponId,
@RequestParam(value = "files" , required = true) MultipartFile files,
HttpServletRequest request) {
try {
SessionObject sessionObject = userCenter.getSessionObject(request);
UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
// 查找商户
HighCouponModel coupon = highCouponService.getCouponById(couponId);
if (coupon == null) {
log.error("HighCouponController -> writeStock() error!","未找到商户");
throw ErrorHelp.genException(SysCode.System, ErrorCode.MERCHANT_NOF_FOUND, "");
}
if (coupon.getCouponType() != 2) {
log.error("HighCouponController -> writeStock() error!","卡卷类型错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_TYPE_ERROR, "");
}
if (coupon.getStatus() != 1) {
log.error("HighCouponController -> writeStock() error!","卡卷状态错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_STATUS_ERROR, "");
}
ImportCouponListener importCouponListener = new ImportCouponListener();
importCouponListener.initData(coupon,userInfoModel, highCouponCodeService);
EasyExcel.read(files.getInputStream(), ImportCouponModel.class, importCouponListener).sheet().doRead();
Map<String, Object> map = new HashMap<>();
map.put("errorData", importCouponListener.getErrorData());
map.put("errorTotal", importCouponListener.getErrorData().size());
return ResponseMsgUtil.success(map);
} catch (Exception e) {
log.error("HighCouponController -> importStock() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/writeStock",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "填写卡卷库存(内部劵)")
public ResponseData writeStock(@RequestBody String reqBody, HttpServletRequest request){
try {
SessionObject sessionObject = userCenter.getSessionObject(request);
UserInfoModel userInfoModel = (UserInfoModel) sessionObject.getObject();
JSONObject jsonObject = JSONObject.parseObject(reqBody);
Long couponId = jsonObject.getLong("couponId");
Long salesEndTime = jsonObject.getLong("salesEndTime");
Long useEndTime = jsonObject.getLong("useEndTime");
Integer generateNum = jsonObject.getInteger("generateNum");
if(couponId == null || salesEndTime == null || useEndTime == null || generateNum == null) {
log.error("HighCouponController -> writeStock() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
// 查找商户
HighCouponModel coupon = highCouponService.getCouponById(couponId);
if (coupon == null) {
log.error("HighCouponController -> writeStock() error!","未找到商户");
throw ErrorHelp.genException(SysCode.System, ErrorCode.MERCHANT_NOF_FOUND, "");
}
if (coupon.getCouponType() != 1) {
log.error("HighCouponController -> writeStock() error!","卡卷类型错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_TYPE_ERROR, "");
}
if (coupon.getStatus() != 1) {
log.error("HighCouponController -> writeStock() error!","卡卷状态错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COUPON_STATUS_ERROR, "");
}
// 销售截止时间
Calendar salesEndTimeC = Calendar.getInstance();
salesEndTimeC.setTime(new Date(salesEndTime));
salesEndTimeC.set(Calendar.HOUR_OF_DAY, 23);
salesEndTimeC.set(Calendar.MINUTE, 59);
salesEndTimeC.set(Calendar.SECOND, 59);
// 使用截止时间
Calendar useEndTimeC = Calendar.getInstance();
useEndTimeC.setTime(new Date(useEndTime));
useEndTimeC.set(Calendar.HOUR_OF_DAY, 23);
useEndTimeC.set(Calendar.MINUTE, 59);
useEndTimeC.set(Calendar.SECOND, 59);
HighCouponCode highCouponCode;
List<HighCouponCode> list = new ArrayList<>();
for (int i = 0; i < generateNum;i++) {
highCouponCode = new HighCouponCode();
highCouponCode.setCouponId(couponId);
highCouponCode.setMerchantId(coupon.getMerchantId());
highCouponCode.setSalesCode(generateCode.couponSalesCode(coupon.getMerchantId(), couponId));
highCouponCode.setSalesEndTime(salesEndTimeC.getTime());
highCouponCode.setUseEndTime(useEndTimeC.getTime());
highCouponCode.setStatus(1); // 状态:1.待销售 2.未使用 3.已使用
highCouponCode.setCreateTime(new Date());
highCouponCode.setOperatorId(userInfoModel.getSecUser().getId());
highCouponCode.setOperatorName(userInfoModel.getSecUser().getUserName());
list.add(highCouponCode);
}
highCouponCodeService.insertList(list);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("HighCouponController -> writeStock() error!",e);
return ResponseMsgUtil.exception(e);
}
}
}