package com.doumee.service.business.impl;
|
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.doumee.core.constants.ResponseStatus;
|
import com.doumee.core.exception.BusinessException;
|
import com.doumee.core.model.LoginUserInfo;
|
import com.doumee.core.model.PageData;
|
import com.doumee.core.model.PageWrap;
|
import com.doumee.core.utils.Constants;
|
import com.doumee.core.utils.Utils;
|
import com.doumee.dao.business.CouponJoinMapper;
|
import com.doumee.dao.business.CouponMapper;
|
import com.doumee.dao.business.MemberCouponMapper;
|
import com.doumee.dao.business.ShopMapper;
|
import com.doumee.dao.business.model.*;
|
|
import com.doumee.dao.web.dto.CouponDTO;
|
import com.doumee.service.business.CouponService;
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
import org.apache.shiro.SecurityUtils;
|
import org.springframework.beans.BeanUtils;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.util.CollectionUtils;
|
|
import java.util.Date;
|
import java.util.List;
|
import java.util.Objects;
|
|
/**
|
* 优惠券信息表Service实现
|
* @author 江蹄蹄
|
* @date 2023/03/21 15:48
|
*/
|
@Service
|
public class CouponServiceImpl implements CouponService {
|
|
@Autowired
|
private CouponMapper couponMapper;
|
|
@Autowired
|
private CouponJoinMapper couponJoinMapper;
|
|
|
@Autowired
|
private MemberCouponMapper memberCouponMapper;
|
|
@Autowired
|
private ShopMapper shopMapper;
|
|
@Override
|
public Integer create(Coupon coupon) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
isCreateParamValid(coupon);
|
|
/**平台优惠券是兑换优惠券,商家优惠券是领取优惠券*/
|
if(Constants.equalsInteger(coupon.getType(),Constants.ZERO)){
|
coupon.setGetMethod(Constants.ZERO);
|
}else if(Constants.equalsInteger(coupon.getType(),Constants.ONE)){
|
coupon.setGetMethod(Constants.ONE);
|
}
|
coupon.setIsdeleted(Constants.ZERO);
|
coupon.setCreator(user.getId());
|
coupon.setCreateDate(new Date());
|
coupon.setStatus(Constants.ONE);
|
couponMapper.insert(coupon);
|
return coupon.getId();
|
}
|
|
public void isCreateParamValid(Coupon coupon){
|
|
if(StringUtils.isBlank(coupon.getName())
|
|| coupon.getType()==null
|
||coupon.getLimitPrice()==null
|
||coupon.getPrice()==null
|
||coupon.getNum()==null
|
/* ||coupon.getIntegral()==null*/
|
||coupon.getStartDate()==null
|
||coupon.getEndDate()==null
|
||coupon.getValidDays()==null
|
|
){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), ResponseStatus.BAD_REQUEST.getMessage());
|
}
|
if(Constants.equalsInteger(coupon.getType(),Constants.ZERO)){
|
if(coupon.getShopId()==null){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), ResponseStatus.BAD_REQUEST.getMessage());
|
}
|
QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
|
queryWrapper.lambda().eq(Shop::getIsdeleted,Constants.ZERO);
|
queryWrapper.lambda().eq(Shop::getId,coupon.getShopId());
|
Shop shop=shopMapper.selectOne(queryWrapper);
|
if(Objects.isNull(shop)){
|
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "关联的店铺不存在!");
|
}
|
}
|
}
|
|
@Override
|
public void deleteById(Integer id) {
|
|
Coupon query= couponMapper.selectById(id);
|
initCouponStatus(query);
|
if(!Constants.equalsInteger(query.getCouponStatus(),Constants.ZERO)){
|
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "活动已开始/已结束状态,不支持删除操作");
|
}
|
Coupon coupon=new Coupon();
|
coupon.setId(id);
|
coupon.setIsdeleted(Constants.ONE);
|
couponMapper.updateById(coupon);
|
}
|
|
@Override
|
public void delete(Coupon coupon) {
|
UpdateWrapper<Coupon> deleteWrapper = new UpdateWrapper<>(coupon);
|
couponMapper.delete(deleteWrapper);
|
}
|
|
@Override
|
public void deleteByIdInBatch(List<Integer> ids) {
|
if (CollectionUtils.isEmpty(ids)) {
|
return;
|
}
|
couponMapper.deleteBatchIds(ids);
|
}
|
|
@Override
|
public void updateById(Coupon coupon) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
isCreateParamValid(coupon);
|
Coupon query= couponMapper.selectById(coupon.getId());
|
initCouponStatus(query);
|
if(!Constants.equalsInteger(query.getCouponStatus(),Constants.ZERO)){
|
throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(), "活动已开始/已结束状态,不支持编辑操作");
|
}
|
coupon.setEditor(user.getId());
|
coupon.setEditDate(new Date());
|
couponMapper.updateById(coupon);
|
}
|
|
|
@Override
|
public void updateStatus(Coupon coupon) {
|
LoginUserInfo user = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
|
if(coupon.getId()==null||coupon.getStatus()==null){
|
throw new BusinessException(ResponseStatus.BAD_REQUEST.getCode(), ResponseStatus.BAD_REQUEST.getMessage());
|
}
|
coupon.setEditor(user.getId());
|
coupon.setEditDate(new Date());
|
couponMapper.updateById(coupon);
|
}
|
|
@Override
|
public void updateByIdInBatch(List<Coupon> coupons) {
|
if (CollectionUtils.isEmpty(coupons)) {
|
return;
|
}
|
for (Coupon coupon: coupons) {
|
this.updateById(coupon);
|
}
|
}
|
|
@Override
|
public Coupon findById(Integer id) {
|
MPJLambdaWrapper<Coupon> queryWrapper = new MPJLambdaWrapper<>();
|
|
queryWrapper.selectAs(Shop::getName,Coupon::getShopName);
|
queryWrapper.eq(Coupon::getId,id);
|
queryWrapper.leftJoin(Shop.class,Shop::getId,Coupon::getShopId);
|
Coupon coupon=couponJoinMapper.selectJoinOne(Coupon.class,queryWrapper);
|
return coupon;
|
}
|
|
@Override
|
public Coupon findOne(Coupon coupon) {
|
QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
|
return couponMapper.selectOne(wrapper);
|
}
|
|
@Override
|
public List<Coupon> findList(Coupon coupon) {
|
QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
|
return couponMapper.selectList(wrapper);
|
}
|
|
@Override
|
public PageData<Coupon> findPage(PageWrap<Coupon> pageWrap) {
|
IPage<Coupon> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
MPJLambdaWrapper<Coupon> queryWrapper = new MPJLambdaWrapper<>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
queryWrapper.selectAll(Coupon.class);
|
queryWrapper.select("(select count(id) from member_coupon where ISDELETED=0 and COUPON_ID=t.id) as received");
|
queryWrapper.select("(select count(id) from member_coupon where ISDELETED=0 and COUPON_ID=t.id and STATUS=0) as unused");
|
queryWrapper.select("(select count(id) from member_coupon where ISDELETED=0 and COUPON_ID=t.id and STATUS=1) as used");
|
|
queryWrapper.like(StringUtils.isNotBlank(pageWrap.getModel().getName()),Coupon::getName,pageWrap.getModel().getName());
|
queryWrapper.eq(Coupon::getIsdeleted,Constants.ZERO);
|
queryWrapper.orderByDesc(Coupon::getCreateDate);
|
IPage<Coupon> result = couponJoinMapper.selectJoinPage(page,Coupon.class, queryWrapper);
|
for(Coupon model:result.getRecords()){
|
initCouponStatus(model);
|
}
|
|
return PageData.from(result);
|
}
|
|
|
public void initCouponStatus(Coupon coupon){
|
if(coupon==null){
|
return ;
|
}
|
long now =System.currentTimeMillis();
|
coupon.setCouponStatus(Constants.ZERO);
|
if(coupon.getStartDate()!=null&&coupon.getEndDate()!=null&&coupon.getStartDate().getTime()<=now && coupon.getEndDate().getTime()>=now){
|
coupon.setCouponStatus(Constants.ONE);
|
}else if(coupon.getEndDate()!=null && coupon.getEndDate().getTime()<now){
|
coupon.setCouponStatus(Constants.TWO);
|
}
|
|
}
|
@Override
|
public long count(Coupon coupon) {
|
QueryWrapper<Coupon> wrapper = new QueryWrapper<>(coupon);
|
return couponMapper.selectCount(wrapper);
|
}
|
|
@Override
|
public CouponDTO findCouponDTO(Integer shopId, Integer memberId) {
|
|
QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
|
wrapper.lambda().eq(Coupon::getShopId,shopId)
|
.orderByDesc(Coupon::getCreateDate).last("limit 1");
|
Coupon coupon = couponMapper.selectOne(wrapper);
|
QueryWrapper<MemberCoupon> memberCouponWrapper = new QueryWrapper<>();
|
memberCouponWrapper.lambda()
|
.eq(MemberCoupon::getMemberId,memberId)
|
.eq(MemberCoupon::getCouponId,coupon.getId());
|
MemberCoupon memberCoupon = memberCouponMapper.selectById(memberCouponWrapper);
|
|
memberCouponWrapper.clear();
|
memberCouponWrapper.lambda().eq(MemberCoupon::getCouponId,coupon.getId());
|
// Integer integer = memberCouponMapper.selectCount(memberCouponWrapper);
|
|
CouponDTO couponDTO = new CouponDTO();
|
BeanUtils.copyProperties(coupon,couponDTO);
|
couponDTO.setDrawStatus(Objects.isNull(memberCoupon) ? "un_draw":"has_draw");
|
return couponDTO;
|
}
|
|
|
@Override
|
public PageData<CouponDTO> findCouponDTO(PageWrap<CouponDTO> pageWrap) {
|
|
MPJLambdaWrapper<Coupon> wrapper = new MPJLambdaWrapper<>();
|
IPage<Coupon> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
|
MPJLambdaWrapper<Coupon> queryWrapper = new MPJLambdaWrapper<>();
|
Utils.MP.blankToNull(pageWrap.getModel());
|
queryWrapper.selectAs(Coupon::getId,CouponDTO::getId)
|
.selectAs(Coupon::getLimitPrice,CouponDTO::getLimitPrice)
|
.selectAs(Coupon::getPrice,CouponDTO::getPrice)
|
.selectAs(Coupon::getName,CouponDTO::getName)
|
.selectAs(Coupon::getStartDate,CouponDTO::getStartDate)
|
.selectAs(Coupon::getEndDate,CouponDTO::getEndDate)
|
.selectAs(Coupon::getNum,CouponDTO::getNum)
|
.selectCount(MemberCoupon::getCouponId,CouponDTO::getDrawNum);
|
queryWrapper.leftJoin(MemberCoupon.class,MemberCoupon::getCouponId,Coupon::getId);
|
|
if ("not_start".equalsIgnoreCase(pageWrap.getModel().getCouponStatus())){
|
queryWrapper.lt(Coupon::getStartDate,new Date());
|
}
|
if ("having".equalsIgnoreCase(pageWrap.getModel().getCouponStatus())){
|
queryWrapper.ge(Coupon::getStartDate,new Date());
|
queryWrapper.le(Coupon::getStartDate,new Date());
|
}
|
if ("over".equalsIgnoreCase(pageWrap.getModel().getCouponStatus())){
|
queryWrapper.gt(Coupon::getStartDate,new Date());
|
}
|
|
IPage<CouponDTO> couponDTOIPage = couponJoinMapper.selectJoinPage(page, CouponDTO.class, wrapper);
|
|
if (CollectionUtils.isEmpty(couponDTOIPage.getRecords())){
|
return PageData.from(new Page<>());
|
}
|
Date now = new Date();
|
couponDTOIPage.getRecords().stream().forEach(s->{
|
if (now.before(s.getStartDate())){
|
s.setCouponStatus("not_start");
|
}
|
if (now.after(s.getStartDate())){
|
s.setCouponStatus("over");
|
}
|
});
|
return PageData.from(couponDTOIPage);
|
}
|
}
|