package com.matrix.system.shopXcx.action;
|
|
|
import com.matrix.core.anotations.RemoveRequestToken;
|
import com.matrix.core.anotations.SaveRequestToken;
|
import com.matrix.core.constance.MatrixConstance;
|
import com.matrix.core.constance.SystemErrorCode;
|
import com.matrix.core.constance.SystemMessageCode;
|
import com.matrix.core.exception.GlobleException;
|
import com.matrix.core.pojo.AjaxResult;
|
import com.matrix.core.pojo.PaginationVO;
|
import com.matrix.core.tools.ModelUtils;
|
import com.matrix.core.tools.StringUtils;
|
import com.matrix.core.tools.WebUtil;
|
import com.matrix.system.common.bean.SysUsers;
|
import com.matrix.system.constance.Dictionary;
|
import com.matrix.system.shopXcx.bean.ShopCoupon;
|
import com.matrix.system.shopXcx.bean.ShopProduct;
|
import com.matrix.system.shopXcx.dao.ShopCouponDao;
|
import com.matrix.system.shopXcx.dao.ShopProductDao;
|
import org.apache.commons.collections.CollectionUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Controller;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.servlet.ModelAndView;
|
|
import java.beans.Transient;
|
import java.util.*;
|
|
/**
|
* @author jyy
|
* @description 优惠券
|
* @date 2019-06-12 15:17
|
*/
|
@Controller
|
@RequestMapping(value = "admin/shopCoupon")
|
public class ShopCouponAction {
|
|
@Autowired
|
private ShopCouponDao shopCouponDao;
|
|
@Autowired
|
private ShopProductDao shopProductDao;
|
|
|
|
|
|
|
//记录编辑前的值Before_Edit_Value
|
public static final String BEV = "ShopCoupon_BEV";
|
|
|
/**
|
* 列表显示
|
*/
|
@RequestMapping(value = "/showList")
|
public @ResponseBody
|
AjaxResult showList(ShopCoupon shopCoupon, PaginationVO pageVo) {
|
|
pageVo.setSort("update_time");
|
pageVo.setOrder("desc");
|
SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
|
if (!user.getShopRole().equals(Dictionary.FLAG_YES_Y)) {
|
shopCoupon.setShopId(user.getShopId());
|
}
|
shopCoupon.setCompanyId(user.getCompanyId());
|
List<ShopCoupon> dataList = shopCouponDao.selectInPage(shopCoupon, pageVo);
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, dataList, shopCouponDao.selectTotalRecord(shopCoupon));
|
}
|
|
/**
|
* 新增
|
*/
|
@RemoveRequestToken
|
@RequestMapping(value = "/addShopCoupon")
|
@Transient
|
public @ResponseBody
|
AjaxResult addShopCoupon(ShopCoupon shopCoupon) {
|
//如果结束时间早于开始时间
|
if (shopCoupon.getBeginTime().after(shopCoupon.getEndTime())) {
|
return new AjaxResult(AjaxResult.STATUS_FAIL, "结束时间不得早于开始时间!");
|
}
|
//如果抵消金额大于最低消费金额
|
if (shopCoupon.getOffsetAmount().compareTo(shopCoupon.getMinAmount()) == 1) {
|
return new AjaxResult(AjaxResult.STATUS_FAIL, "抵消金额不能大于最低消费金额(满减)!");
|
}
|
SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
|
shopCoupon.setCreateBy(user.getSuName());
|
shopCoupon.setUpdateBy(user.getSuName());
|
shopCoupon.setQuantityReceive(0);
|
//设置为未开始状态
|
shopCoupon.setCouponStatus(1);
|
//如果是全部产品可以使用
|
if (shopCoupon.getIsAll() == 1) {
|
shopCoupon.setProductIds("");
|
shopCoupon.setAttrIds("");
|
}
|
// @TODO wzy增加
|
shopCoupon.setShopId(user.getShopId());
|
shopCoupon.setCompanyId(user.getCompanyId());
|
int i = shopCouponDao.insert(shopCoupon);
|
|
productLabeling(shopCoupon);
|
|
if (i > 0) {
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.ADD_SUCCES, "优惠券");
|
} else {
|
throw new GlobleException(SystemErrorCode.DATA_ADD_FAIL);
|
}
|
}
|
|
|
/**
|
* 修改
|
*/
|
@Transactional(rollbackFor = Exception.class)
|
@RequestMapping(value = "/modifyShopCoupon")
|
public @ResponseBody
|
AjaxResult modifyShopCoupon(ShopCoupon newShopCoupon) {
|
//如果结束时间早于开始时间
|
if (newShopCoupon.getBeginTime().after(newShopCoupon.getEndTime())) {
|
return new AjaxResult(AjaxResult.STATUS_FAIL, "结束时间不得早于开始时间!");
|
}
|
//如果抵消金额大于最低消费金额
|
if (newShopCoupon.getOffsetAmount().compareTo(newShopCoupon.getMinAmount()) == 1) {
|
return new AjaxResult(AjaxResult.STATUS_FAIL, "抵消金额不能大于最低消费金额(满减)!");
|
}
|
//如果所有产品都优惠或没有选择产品
|
if (newShopCoupon.getIsAll() == 1 || newShopCoupon.getProductIds() == null) {
|
newShopCoupon.setProductIds("");
|
}
|
//如果所有产品都优惠或没有选择属性
|
if (newShopCoupon.getIsAll() == 1 || newShopCoupon.getAttrIds() == null) {
|
newShopCoupon.setAttrIds("");
|
}
|
ShopCoupon oldShopCoupon = WebUtil.getSessionAttribute(BEV);
|
int i = 0;
|
Map<String, Object> modifyMap = null;
|
try {
|
if (!ModelUtils.isModified(oldShopCoupon, newShopCoupon)) {
|
i = MatrixConstance.DML_SUCCESSS;
|
}
|
modifyMap = ModelUtils.comparePojo2Map(oldShopCoupon, newShopCoupon);
|
} catch (Exception e) {
|
throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newShopCoupon);
|
}
|
if (modifyMap.size() > 0) {
|
modifyMap.put("id", oldShopCoupon.getId());
|
shopCouponDao.updateByMap(modifyMap);
|
}
|
i = MatrixConstance.DML_SUCCESSS;
|
WebUtil.removeSessionAttribute(BEV);
|
if (i > 0) {
|
shopCouponDao.updateCouponStatusById(newShopCoupon.getId());
|
if (newShopCoupon.getKind() == 2) {
|
// @TODO wzy增加
|
SysUsers users = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
|
newShopCoupon.setShopId(users.getShopId());
|
|
//更新产品标签
|
shopProductDao.removeProductCouponLable(newShopCoupon.getId());
|
productLabeling(newShopCoupon);
|
|
}
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, "优惠券");
|
} else {
|
throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
|
}
|
}
|
|
|
/**
|
* 进入修改界面
|
*/
|
@SaveRequestToken
|
@RequestMapping(value = "/editForm")
|
public ModelAndView editForm(Integer id) {
|
ShopCoupon shopCoupon = new ShopCoupon();
|
ModelAndView modelAndView = new ModelAndView("admin/shop/shopCoupon-form");
|
if (id != null) {
|
shopCoupon = shopCouponDao.selectById(id);
|
WebUtil.setSessionAttribute(BEV, shopCoupon);
|
}
|
modelAndView.addObject("obj", shopCoupon);
|
return modelAndView;
|
}
|
|
|
/**
|
* 删除
|
*/
|
@RequestMapping(value = "/del")
|
@Transactional(rollbackFor = Exception.class)
|
public @ResponseBody
|
AjaxResult del(String keys) {
|
List<String> ids = StringUtils.strToCollToString(keys, ",");
|
int i = shopCouponDao.deleteByIds(ids);
|
for (String couponId : ids) {
|
shopProductDao.removeProductCouponLable(Integer.valueOf(couponId));
|
}
|
if (i > 0) {
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.DELETE_SUCCES, i);
|
} else {
|
throw new GlobleException(SystemErrorCode.DATA_DELETE_FAIL);
|
}
|
}
|
|
/**
|
* 批量启用
|
*/
|
@RequestMapping(value = "/startUsing")
|
@Transactional(rollbackFor = Exception.class)
|
public @ResponseBody
|
AjaxResult startUsing(String keys) {
|
List<String> ids = StringUtils.strToCollToString(keys, ",");
|
List<String> newIds = new ArrayList<>();
|
Date now = new Date();
|
//如果结束已经晚于当前时间则跳过不开启
|
for (String couponId : ids) {
|
Integer id = Integer.valueOf(couponId);
|
ShopCoupon shopCoupon = shopCouponDao.selectById(id);
|
if (shopCoupon.getEndTime().after(now)) {
|
newIds.add(couponId);
|
//更新产品标签
|
if (shopCoupon.getKind() == 2) {
|
//更新产品标签
|
productLabeling(shopCoupon);
|
}
|
}
|
}
|
int i = 0;
|
if (CollectionUtils.isNotEmpty(newIds)) {
|
i = shopCouponDao.updateStateByStateAndIds(newIds, 1);
|
}
|
if (i > 0) {
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, i);
|
} else {
|
throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
|
}
|
}
|
|
/**
|
* 批量停用
|
*/
|
@Transactional(rollbackFor = Exception.class)
|
@RequestMapping(value = "/blockUp")
|
public @ResponseBody
|
AjaxResult blockUp(String keys) {
|
List<String> ids = StringUtils.strToCollToString(keys, ",");
|
int i = shopCouponDao.updateStateByStateAndIds(ids, 2);
|
for (String couponId : ids) {
|
shopProductDao.removeProductCouponLable(Integer.valueOf(couponId));
|
}
|
if (i > 0) {
|
return new AjaxResult(AjaxResult.STATUS_SUCCESS, SystemMessageCode.UPDATE_SUCCES, i);
|
} else {
|
throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL);
|
}
|
}
|
|
|
/**
|
* 给产品打上合适的标签
|
*
|
* @param shopCoupon
|
*/
|
private void productLabeling(ShopCoupon shopCoupon) {
|
SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
|
Date now = new Date();
|
Boolean needUpdate = false;
|
//优惠券处于可用状态
|
if (shopCoupon.getIsOpen() == 1 && shopCoupon.getBeginTime().before(now) &&
|
shopCoupon.getEndTime().after(now)) {
|
//查询优惠券适配的所有产品
|
List<ShopProduct> shopProductList = new ArrayList<>();
|
if (shopCoupon.getIsAll() == 1) {
|
ShopProduct productQuery = new ShopProduct();
|
productQuery.setDelFlag(2);
|
productQuery.setCompanyId(user.getCompanyId());
|
shopProductList = shopProductDao.selectByModel(productQuery);
|
} else {
|
if (StringUtils.isNotBlank(shopCoupon.getProductIds())) {
|
//根据产品id查询,合适的产品
|
shopProductList.addAll(shopProductDao.selectProductByIds(shopCoupon.getProductIds(), shopCoupon.getShopId()));
|
}
|
if (StringUtils.isNotBlank(shopCoupon.getAttrIds())) {
|
List<Integer> pIds = new ArrayList<>();
|
//根据属性id查询合适的属性
|
List<String> attrIds = StringUtils.strToColl(shopCoupon.getAttrIds(), ",");
|
for (String attrId : attrIds) {
|
List<ShopProduct> tempList = shopProductDao.selectProductByAttrid(attrId, shopCoupon.getShopId());
|
for (ShopProduct product : tempList) {
|
if (!pIds.contains(product.getId())) {
|
shopProductList.add(product);
|
}
|
}
|
}
|
}
|
}
|
//更新优惠券标签
|
if (CollectionUtils.isNotEmpty(shopProductList)) {
|
for (ShopProduct product : shopProductList) {
|
if (product.getCouponId() != null) {
|
ShopCoupon coupon = shopCouponDao.selectById(product.getCouponId());
|
//之前的活动存在,且正在进行中
|
if (coupon != null && coupon.getCouponStatus() == 2) {
|
//比较活动金额
|
if (coupon.getMinAmount().compareTo(shopCoupon.getMinAmount()) <= 0) {
|
//当前优惠活动力度大则更新
|
needUpdate = true;
|
}
|
} else {
|
needUpdate = true;
|
}
|
} else {
|
needUpdate = true;
|
}
|
if (needUpdate) {
|
Map<String, Object> map = new HashMap<>();
|
map.put("id", product.getId());
|
map.put("couponId", shopCoupon.getId());
|
shopProductDao.updateByMap(map);
|
}
|
}
|
}
|
} else {
|
//清除标签
|
shopProductDao.removeProductCouponLable(shopCoupon.getId());
|
}
|
}
|
}
|