From b6a7f6e41467f73f26fc7f209652e19ff26c3752 Mon Sep 17 00:00:00 2001 From: Cui Zhi Feng <cuizf@fengyuntec.com> Date: 星期二, 03 九月 2024 20:18:32 +0800 Subject: [PATCH] Merge branch 'master' into master-v2 --- src/main/java/com/mzl/flower/service/point/CustomerPointService.java | 134 ++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 133 insertions(+), 1 deletions(-) diff --git a/src/main/java/com/mzl/flower/service/point/CustomerPointService.java b/src/main/java/com/mzl/flower/service/point/CustomerPointService.java index 2e5c744..a75a7a9 100644 --- a/src/main/java/com/mzl/flower/service/point/CustomerPointService.java +++ b/src/main/java/com/mzl/flower/service/point/CustomerPointService.java @@ -1,9 +1,141 @@ package com.mzl.flower.service.point; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.mzl.flower.config.security.SecurityUtils; +import com.mzl.flower.dto.request.point.ChangePointDTO; +import com.mzl.flower.dto.request.point.QueryCustomerPointDTO; +import com.mzl.flower.dto.request.point.QueryPointDetailDTO; +import com.mzl.flower.dto.response.point.CustomerPointDTO; +import com.mzl.flower.dto.response.point.CustomerPointDetailDTO; +import com.mzl.flower.entity.customer.Customer; +import com.mzl.flower.entity.point.CustomerPoint; +import com.mzl.flower.entity.point.CustomerPointDetail; +import com.mzl.flower.mapper.point.CustomerPointDetailMapper; +import com.mzl.flower.mapper.point.CustomerPointMapper; +import com.mzl.flower.service.BaseService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import java.math.BigDecimal; +import java.time.LocalDate; +import java.util.List; + +import com.mzl.flower.constant.Constants.*; + @Service @Transactional -public class CustomerPointService { +public class CustomerPointService extends BaseService { + + private final CustomerPointMapper customerPointMapper; + + private final CustomerPointDetailMapper customerPointDetailMapper; + + + public CustomerPointService(CustomerPointMapper customerPointMapper, CustomerPointDetailMapper customerPointDetailMapper) { + this.customerPointMapper = customerPointMapper; + this.customerPointDetailMapper = customerPointDetailMapper; + } + + public Page<CustomerPointDTO> queryPage(QueryCustomerPointDTO dto, Page page) { + List<CustomerPointDTO> list = customerPointMapper.queryPage(dto, page); + page.setRecords(list); + return page; + } + + public Page<CustomerPointDetailDTO> queryCustomerDetails(QueryPointDetailDTO dto, Page page) { + List<CustomerPointDetailDTO> list = customerPointMapper.queryCustomerDetails(dto, page); + page.setRecords(list); + + return page; + } + + public void giveawayPoint(ChangePointDTO dto) { + CustomerPointDetail detail = new CustomerPointDetail(); + Customer customer = getCustomer(dto.getCustomerId()); + detail.setCustomerId(customer.getId()); + detail.setUserId(customer.getUserId()); + detail.setPoint(dto.getPoint()); + detail.setRemarks(dto.getRemarks()); + detail.setRecordDate(LocalDate.now()); + detail.setChangeType(POINT_CHANGE_TYPE.add.name()); + detail.setType(POINT_TYPE.giveaway.name()); + detail.create(SecurityUtils.getUserId()); + customerPointDetailMapper.insert(detail); + + //更新汇总表 + updateCustomerPoint(detail); + } + + public void updateCustomerPoint(CustomerPointDetail detail) { + CustomerPoint point = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>() + .eq(CustomerPoint::getCustomerId, detail.getCustomerId())); + boolean isAdd = false; + if(point == null){ + isAdd = true; + point = new CustomerPoint(); + point.setCustomerId(detail.getCustomerId()); + point.setUserId(detail.getUserId()); + point.setTotalPoint(0); + point.setUsedPoint(0); + point.setExpiredPoint(0); + point.create(SecurityUtils.getUserId()); + } + + if(POINT_CHANGE_TYPE.add.name().equals(detail.getChangeType())){ + point.setTotalPoint(point.getTotalPoint() + detail.getPoint()); + }else if(POINT_CHANGE_TYPE.reduce.name().equals(detail.getChangeType())){ + Integer balancePoint = point.getTotalPoint() - point.getUsedPoint()-point.getExpiredPoint();//可用积分 + balancePoint = balancePoint.intValue()>0?balancePoint.intValue():0; + if(balancePoint.intValue() >= detail.getPoint().intValue()){ + point.setUsedPoint(point.getUsedPoint() + detail.getPoint()); + }else{ + //积分不足,直接清0 + point.setUsedPoint(point.getUsedPoint() + balancePoint); + detail.setPoint(balancePoint); + detail.setRemarks(detail.getRemarks()+",积分不足,扣除积分"+balancePoint); + } + } + if(isAdd){ + customerPointMapper.insert(point); + }else{ + customerPointMapper.updateById(point); + } + + } + + public void deductionPoint(ChangePointDTO dto) { + CustomerPointDetail detail = new CustomerPointDetail(); + Customer customer = getCustomer(dto.getCustomerId()); + detail.setCustomerId(customer.getId()); + detail.setUserId(customer.getUserId()); + detail.setPoint(dto.getPoint()); + detail.setRemarks(dto.getRemarks()); + detail.setRecordDate(LocalDate.now()); + detail.setChangeType(POINT_CHANGE_TYPE.reduce.name()); + detail.setType(POINT_TYPE.deduction.name()); + detail.create(SecurityUtils.getUserId()); + //更新汇总表 + updateCustomerPoint(detail); + + customerPointDetailMapper.insert(detail); + } + + public void consumptionPoint(BigDecimal orderAmount, String orderNo,String userId) { + CustomerPointDetail detail = new CustomerPointDetail(); + Customer customer = getCustomerByUserId(userId); + detail.setCustomerId(customer.getId()); + detail.setUserId(customer.getUserId()); + BigDecimal point = orderAmount.setScale(0, BigDecimal.ROUND_HALF_UP); + detail.setPoint(point.intValue()); + detail.setRemarks(orderNo); + detail.setRecordDate(LocalDate.now()); + detail.setChangeType(POINT_CHANGE_TYPE.add.name()); + detail.setType(POINT_TYPE.consume.name()); + detail.create(SecurityUtils.getUserId()); + customerPointDetailMapper.insert(detail); + + //更新汇总表 + updateCustomerPoint(detail); + } } -- Gitblit v1.9.3