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-oil-server/bweb/src/main/java/com/bweb/controller/BsDiscountController.java

253 lines
11 KiB

package com.bweb.controller;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.config.SpPrinterConfig;
import com.hfkj.entity.BsCompany;
import com.hfkj.entity.BsDevice;
import com.hfkj.entity.BsDiscount;
import com.hfkj.entity.BsMerchant;
import com.hfkj.model.ResponseData;
import com.hfkj.model.UserInfoModel;
import com.hfkj.service.BsCompanyService;
import com.hfkj.service.BsDeviceService;
import com.hfkj.service.BsDiscountService;
import com.hfkj.service.BsMerchantService;
import com.hfkj.sysenum.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping(value = "/discount")
@Api(value = "优惠券管理")
public class BsDiscountController {
private static Logger log = LoggerFactory.getLogger(BsDiscountController.class);
@Resource
private BsDiscountService discountService;
@Resource
private BsMerchantService merchantService;
@RequestMapping(value="/editDiscount",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "编辑优惠券")
public ResponseData editDiscount(@RequestBody BsDiscount body) {
try {
if (StringUtils.isBlank(body.getMerNo())
|| StringUtils.isBlank(body.getDiscountName())
|| body.getDiscountType() == null
|| body.getDiscountPrice() == null
|| StringUtils.isBlank(body.getUseScope())
|| body.getStartTime() == null
|| body.getEndTime() == null
) {
log.error("BsDiscountController -> editDiscount() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
if (DiscountTypeEnum.getNameByType(body.getDiscountType()) == null) {
log.error("BsDiscountController -> editDiscount() error!","未知优惠券类型");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知优惠券类型");
}
// 满减条件
if (DiscountTypeEnum.type1.getCode().equals(body.getDiscountType()) && body.getDiscountCondition() == null) {
log.error("BsDiscountController -> editDiscount() error!","未设置满减条件");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未设置满减条件");
}
if (DiscountUseScopeEnum.type1.getCode().equals(body.getDiscountType()) && body.getDiscountCondition() == null) {
log.error("BsDiscountController -> editDiscount() error!","未设置满减条件");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未设置满减条件");
}
BsDiscount discount;
if (StringUtils.isNotBlank(body.getDiscountNo())) {
// 查询优惠券
discount = discountService.getDetail(body.getDiscountNo());
if (discount == null) {
log.error("BsDiscountController -> editDiscount() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
} else {
discount = new BsDiscount();
}
// 查询商户
BsMerchant merchant = merchantService.getMerchant(body.getMerNo());
if (merchant == null) {
log.error("BsDiscountController -> editDiscount() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的商户");
}
discount.setMerId(merchant.getId());
discount.setMerNo(merchant.getMerNo());
discount.setMerName(merchant.getMerName());
discount.setDiscountType(body.getDiscountType());
discount.setDiscountName(body.getDiscountName());
discount.setDiscountCondition(body.getDiscountCondition());
discount.setDiscountPrice(body.getDiscountPrice());
discount.setUseScope(body.getUseScope());
discount.setStartTime(body.getStartTime());
discount.setEndTime(body.getEndTime());
discount.setStatus(DiscountStatusEnum.status1.getCode());
discountService.editDiscount(discount);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("BsDiscountController -> editDiscount() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/updateEndTime",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "修改结束时间")
public ResponseData updateEndTime(@RequestBody JSONObject body) {
try {
if (body == null
|| StringUtils.isBlank(body.getString("discountNo"))
|| body.getLong("endTime") == null) {
log.error("BsDiscountController -> updateEndTime() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
// 查询详情
BsDiscount discount = discountService.getDetail(body.getString("discountNo"));
if (discount == null) {
log.error("BsDiscountController -> updateEndTime() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的优惠券");
}
if (!discount.getStatus().equals(DiscountStatusEnum.status2.getCode())) {
log.error("BsDiscountController -> updateEndTime() error!","无法修改,优惠不处于上线状态");
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "无法修改,优惠不处于上线状态");
}
discount.setEndTime(new Date(body.getLong("endTime")));
discountService.editDiscount(discount);
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("BsDiscountController -> updateEndTime() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/online",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "上线优惠券")
public ResponseData online(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("discountNo"))) {
log.error("BsDiscountController -> online() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
discountService.online(body.getString("discountNo"));
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("BsDiscountController -> online() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/done",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "结束优惠券")
public ResponseData done(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("discountNo"))) {
log.error("BsDiscountController -> done() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
discountService.done(body.getString("discountNo"));
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("BsDiscountController -> done() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/delete",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "删除优惠券")
public ResponseData delete(@RequestBody JSONObject body) {
try {
if (body == null || StringUtils.isBlank(body.getString("discountNo"))) {
log.error("BsDiscountController -> delDiscount() error!","参数错误");
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
discountService.delete(body.getString("discountNo"));
return ResponseMsgUtil.success("操作成功");
} catch (Exception e) {
log.error("BsDiscountController -> delDiscount() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/queryDetail",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "查询详情")
public ResponseData queryDetail(@RequestParam(name = "discountNo", required = true) String discountNo) {
try {
return ResponseMsgUtil.success(discountService.getDetail(discountNo));
} catch (Exception e) {
log.error("BsDiscountController -> delDiscount() error!",e);
return ResponseMsgUtil.exception(e);
}
}
@RequestMapping(value="/queryList",method = RequestMethod.GET)
@ResponseBody
@ApiOperation(value = "查询列表")
public ResponseData queryList(@RequestParam(name = "merNo", required = false) String merNo,
@RequestParam(name = "discountName", required = false) String discountName,
@RequestParam(name = "discountType", required = false) Integer discountType,
@RequestParam(name = "status", required = false) Integer status,
@RequestParam(name = "pageNum", required = true) Integer pageNum,
@RequestParam(name = "pageSize", required = true) Integer pageSize) {
try {
Map<String,Object> param = new HashMap<>();
param.put("merNo", merNo);
param.put("discountName", discountName);
param.put("discountType", discountType);
param.put("status", status);
PageHelper.startPage(pageNum,pageSize);
return ResponseMsgUtil.success(new PageInfo<>(discountService.getList(param)));
} catch (Exception e) {
log.error("BsDiscountController -> queryList() error!",e);
return ResponseMsgUtil.exception(e);
}
}
}