From a836f03a5d1fbfa81e147d09ffdfa87ba3975c13 Mon Sep 17 00:00:00 2001
From: MrShi <1878285526@qq.com>
Date: 星期二, 18 三月 2025 16:45:02 +0800
Subject: [PATCH] 1

---
 server/services/src/main/java/com/doumee/service/business/impl/DiscountServiceImpl.java |  272 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 272 insertions(+), 0 deletions(-)

diff --git a/server/services/src/main/java/com/doumee/service/business/impl/DiscountServiceImpl.java b/server/services/src/main/java/com/doumee/service/business/impl/DiscountServiceImpl.java
new file mode 100644
index 0000000..a8c5a63
--- /dev/null
+++ b/server/services/src/main/java/com/doumee/service/business/impl/DiscountServiceImpl.java
@@ -0,0 +1,272 @@
+package com.doumee.service.business.impl;
+
+import com.doumee.biz.system.SystemDictDataBiz;
+import com.doumee.core.constants.Constants;
+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.DateUtil;
+import com.doumee.core.utils.Utils;
+import com.doumee.dao.business.DiscountMapper;
+import com.doumee.dao.business.join.BikeRepairJoinMapper;
+import com.doumee.dao.business.join.DiscountJoinMapper;
+import com.doumee.dao.business.model.BaseParam;
+import com.doumee.dao.business.model.BikeRepair;
+import com.doumee.dao.business.model.Discount;
+import com.doumee.dao.business.model.Member;
+import com.doumee.dao.business.web.request.BikeRepairDTO;
+import com.doumee.dao.system.model.SystemUser;
+import com.doumee.service.business.DiscountService;
+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.commons.lang3.StringUtils;
+import org.apache.shiro.SecurityUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
+import org.springframework.util.CollectionUtils;
+
+import java.math.BigDecimal;
+import java.text.ParseException;
+import java.util.Date;
+import java.util.List;
+import java.util.Objects;
+
+/**
+ * 楠戣濂楅淇℃伅琛⊿ervice瀹炵幇
+ * @author 姹熻箘韫�
+ * @date 2025/02/17 09:43
+ */
+@Service
+public class DiscountServiceImpl implements DiscountService {
+
+    @Autowired
+    private DiscountMapper discountMapper;
+
+    @Autowired
+    private DiscountJoinMapper discountJoinMapper;
+
+    @Autowired
+    private SystemDictDataBiz systemDictDataBiz;
+
+    @Override
+    public String create(Discount discount) {
+        LoginUserInfo userInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
+        this.vaildReq(discount);
+        discount.setStatus(Constants.ZERO);
+        discount.setIsdeleted(Constants.ZERO);
+        discount.setCreateDate(new Date());
+        discount.setCreator(userInfo.getId());
+        discount.setEditDate(new Date());
+        discount.setEditor(userInfo.getId());
+        if(Constants.equalsInteger(discount.getUseType(),Constants.ZERO)){
+            discount.setUseDays((int) (DateUtil.getBetweenDays(DateUtil.dateToString(discount.getUseStartDate(),"yyyy-MM-dd"),DateUtil.dateToString(discount.getUseEndDate(),"yyyy-MM-dd"))));
+        }
+        discountMapper.insert(discount);
+        return discount.getId();
+    }
+
+
+    public void vaildReq(Discount discount){
+        if(Objects.isNull(discount)
+                || StringUtils.isBlank(discount.getName())
+                || Objects.isNull(discount.getType())
+                || Objects.isNull(discount.getLimitType())
+                || (Constants.equalsInteger(discount.getLimitTime(),Constants.ONE) && (Objects.isNull(discount.getLimitTime()) || discount.getLimitTime() > 1440))
+                || Objects.isNull(discount.getPrice()) || discount.getPrice().compareTo(BigDecimal.ZERO) <= Constants.ZERO
+                || Objects.isNull(discount.getChannel())
+                || Objects.isNull(discount.getStartDate())
+                || Objects.isNull(discount.getEndDate())
+                || Objects.isNull(discount.getUseType())
+                || (Constants.equalsInteger(discount.getUseType(),Constants.ZERO) && ( Objects.isNull(discount.getUseStartDate()) || Objects.isNull(discount.getUseEndDate()) ))
+                || (Constants.equalsInteger(discount.getUseType(),Constants.ONE) && Objects.isNull(discount.getUseDays()))
+                || (Constants.equalsInteger(discount.getUseType(),Constants.TWO) && ( Objects.isNull(discount.getUseStartDate()) || Objects.isNull(discount.getUseDays()) ))
+                || (Objects.isNull(discount.getUseHoliday()) || Objects.isNull(discount.getUseWorkday()) )
+                || (Objects.isNull(discount.getIsbike()) || Objects.isNull(discount.getIselecbike()) )
+        ){
+            throw new BusinessException(ResponseStatus.BAD_REQUEST);
+        }
+        if(discount.getStartDate().getTime()>discount.getEndDate().getTime()){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"閿�鍞椂娈典俊鎭敊璇�");
+        }
+        //鍥哄畾鏃ユ湡鐢熸晥
+        if(Constants.equalsInteger(discount.getUseType(),Constants.ZERO) &&
+                ( discount.getUseStartDate().getTime()>discount.getUseEndDate().getTime()) ){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"浣跨敤鏃舵淇℃伅閿欒");
+        }
+        //鎸囧畾鏃ユ湡鐢熸晥
+        if(Constants.equalsInteger(discount.getUseType(),Constants.TWO) &&
+                ( discount.getUseStartDate().getTime()<discount.getStartDate().getTime() ||
+                        discount.getUseStartDate().getTime() > discount.getEndDate().getTime()
+                ) ){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"浣跨敤鏃舵淇℃伅閿欒");
+        }
+        if(Objects.isNull(discount.getSaleDayLimit())){
+            discount.setSaleDayLimit(Constants.ZERO);
+        }
+        if(Objects.isNull(discount.getSaleLimit())){
+            discount.setSaleLimit(Constants.ZERO);
+        }
+        if(Constants.equalsInteger(discount.getUseType(),Constants.TWO)){
+            discount.setUseEndDate( DateUtil.StringToDateFormat(DateUtil.getXDaysAfter(discount.getUseStartDate(),discount.getUseDays()),"yyyy-MM-dd"));
+        }
+    }
+
+
+    @Override
+    public void deleteById(String id) {
+        Discount discount = discountMapper.selectById(id);
+        if(Objects.isNull(discount)){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY);
+        }
+        if(Constants.equalsInteger(discount.getStatus(),Constants.ZERO)){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"闈炵鐢ㄦ暟鎹�,鏃犳硶杩涜鍒犻櫎");
+        }
+        discountMapper.update(null,new UpdateWrapper<Discount>().lambda().set(Discount::getIsdeleted,Constants.ONE).eq(Discount::getId,discount.getId()));
+    }
+
+
+    @Override
+    public void updStatus(Discount discount) {
+        if(Objects.isNull(discount)
+            || StringUtils.isBlank(discount.getId())
+            || Objects.isNull(discount.getStatus())){
+            throw new BusinessException(ResponseStatus.BAD_REQUEST);
+        }
+        discountMapper.update(null,new UpdateWrapper<Discount>().lambda().set(Discount::getStatus,discount.getStatus()).eq(Discount::getId,discount.getId()));
+    }
+
+
+    @Override
+    public void delete(Discount discount) {
+        UpdateWrapper<Discount> deleteWrapper = new UpdateWrapper<>(discount);
+        discountMapper.delete(deleteWrapper);
+    }
+
+    @Override
+    public void deleteByIdInBatch(List<String> ids) {
+        if (CollectionUtils.isEmpty(ids)) {
+            return;
+        }
+        discountMapper.deleteBatchIds(ids);
+    }
+
+    @Override
+    public void updateById(Discount discount) {
+        LoginUserInfo userInfo = (LoginUserInfo) SecurityUtils.getSubject().getPrincipal();
+        if(Objects.isNull(discount)
+        || StringUtils.isBlank(discount.getId())){
+            throw new BusinessException(ResponseStatus.BAD_REQUEST);
+        }
+        Discount model = discountMapper.selectById(discount.getId());
+        if(Objects.isNull(model)){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY);
+        }
+        if(!Constants.equalsInteger(model.getStatus(),Constants.ONE)){
+            throw new BusinessException(ResponseStatus.NOT_ALLOWED.getCode(),"鏈鐢ㄦ棤娉曡繘琛屼慨鏀�");
+        }
+        this.vaildReq(discount);
+        discount.setEditDate(new Date());
+        discount.setEditor(userInfo.getId());
+        if(Constants.equalsInteger(discount.getUseType(),Constants.ZERO)){
+            discount.setUseDays((int) (DateUtil.getBetweenDays(DateUtil.dateToString(discount.getUseStartDate(),"yyyy-MM-dd"),DateUtil.dateToString(discount.getUseEndDate(),"yyyy-MM-dd"))));
+        }
+        discountMapper.updateById(discount);
+    }
+
+    @Override
+    public void updateByIdInBatch(List<Discount> discounts) {
+        if (CollectionUtils.isEmpty(discounts)) {
+            return;
+        }
+        for (Discount discount: discounts) {
+            this.updateById(discount);
+        }
+    }
+
+    @Override
+    public Discount findById(String id) {
+        Discount discount = discountJoinMapper.selectOne(new MPJLambdaWrapper<Discount>()
+                .selectAll(Discount.class)
+                .select(" s1.realname ",Discount::getCreatorName)
+                .select(" s2.realname ",Discount::getEditorName)
+                .select(" (select count(1) from goodsorder g where g.obj_type = 0 and g.obj_id = t.id and g.pay_status = 1 ) ",Discount::getSaleNum)
+                .leftJoin(" system_user s1 on s1.id = t.creator")
+                .leftJoin(" system_user s2 on s2.id = t.editor")
+                .eq(Discount::getId,id)
+        );
+        if(Objects.isNull(discount)){
+            throw new BusinessException(ResponseStatus.DATA_EMPTY);
+        }
+        this.dealPrice(discount);
+        if(StringUtils.isNotBlank(discount.getImgurl())){
+            String path =systemDictDataBiz.queryByCode(Constants.SYSTEM,Constants.FILE_DIR).getCode()+
+                    systemDictDataBiz.queryByCode(Constants.SYSTEM, Constants.DISCOUNT).getCode();
+            discount.setFullImgUrl(path + discount.getImgurl());
+        }
+        return discount;
+    }
+
+    @Override
+    public Discount findOne(Discount discount) {
+        QueryWrapper<Discount> wrapper = new QueryWrapper<>(discount);
+        return discountMapper.selectOne(wrapper);
+    }
+
+    @Override
+    public List<Discount> findList(Discount discount) {
+        QueryWrapper<Discount> wrapper = new QueryWrapper<>(discount);
+        return discountMapper.selectList(wrapper);
+    }
+  
+    @Override
+    public PageData<Discount> findPage(PageWrap<Discount> pageWrap) {
+        IPage<Discount> page = new Page<>(pageWrap.getPage(), pageWrap.getCapacity());
+        MPJLambdaWrapper<Discount> queryWrapper = new MPJLambdaWrapper<>();
+        Utils.MP.blankToNull(pageWrap.getModel());
+        Discount modele = pageWrap.getModel();
+        queryWrapper.selectAll(Discount.class)
+                .select(" (select count(1) from goodsorder g where g.obj_type = 0 and g.obj_id = t.id and g.pay_status = 1 ) ",Discount::getSaleNum)
+                .leftJoin(SystemUser.class,SystemUser::getId,Discount::getCreator)
+                .like(StringUtils.isNotEmpty(pageWrap.getModel().getName()),Discount::getName,pageWrap.getModel().getName())
+                .eq(Objects.nonNull(modele.getStatus()),Discount::getStatus, modele.getStatus())
+                .eq(Objects.nonNull(modele.getBikeOrElec()) && Constants.equalsInteger(modele.getBikeOrElec(),Constants.ONE),Discount::getIsbike, Constants.ONE)
+                .eq(Objects.nonNull(modele.getBikeOrElec()) && Constants.equalsInteger(modele.getBikeOrElec(),Constants.TWO),Discount::getIselecbike, Constants.ONE)
+                .eq(Discount::getIsdeleted, Constants.ZERO)
+                .eq( pageWrap.getModel().getStatus() !=null,BikeRepair::getStatus,pageWrap.getModel().getStatus())
+                .apply( Objects.nonNull(modele.getMinShow())," now() BETWEEN t.start_date and  ( t.end_date   + INTERVAL '1 day' ) ")
+        ;
+                queryWrapper.orderByDesc(Discount::getCreateDate);
+        PageData<Discount> pageData = PageData.from(discountJoinMapper.selectJoinPage(page, Discount.class,queryWrapper));
+        for (Discount discount:pageData.getRecords()) {
+            dealPrice(discount);
+        }
+        return pageData;
+    }
+
+
+
+    @Override
+    public void dealPrice(Discount discount){
+        if(Objects.isNull(discount)
+            || Objects.isNull(discount.getUseDays())
+            || Objects.isNull(discount.getPrice())
+            || discount.getPrice().compareTo(BigDecimal.ZERO)==0
+        ){
+            discount.setDayPrice(BigDecimal.ZERO);
+            return;
+        }
+        discount.setDayPrice(discount.getPrice().divide(new BigDecimal(Long.toString(discount.getUseDays())),2));
+    }
+
+
+    @Override
+    public long count(Discount discount) {
+        QueryWrapper<Discount> wrapper = new QueryWrapper<>(discount);
+        return discountMapper.selectCount(wrapper);
+    }
+}

--
Gitblit v1.9.3