|  |  | 
 |  |  |  | 
 |  |  | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | 
 |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
 |  |  | import com.mzl.flower.config.exception.ValidationException; | 
 |  |  | 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.mapper.point.CustomerPointDetailMapper; | 
 |  |  | import com.mzl.flower.mapper.point.CustomerPointMapper; | 
 |  |  | import com.mzl.flower.service.BaseService; | 
 |  |  | import com.mzl.flower.utils.DateUtils; | 
 |  |  | import org.apache.commons.lang3.StringUtils; | 
 |  |  | import org.springframework.beans.BeanUtils; | 
 |  |  | import org.springframework.stereotype.Service; | 
 |  |  | import org.springframework.transaction.annotation.Transactional; | 
 |  |  |  | 
 |  |  | import java.math.BigDecimal; | 
 |  |  | import java.time.LocalDate; | 
 |  |  | import java.util.ArrayList; | 
 |  |  | import java.util.List; | 
 |  |  |  | 
 |  |  | import com.mzl.flower.constant.Constants.*; | 
 |  |  | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public  Page<CustomerPointDetailDTO> queryCustomerDetails(QueryPointDetailDTO dto, Page page) { | 
 |  |  |         if(StringUtils.isNotBlank(dto.getCreateTimeStartStr())){ | 
 |  |  |             dto.setCreateTimeStart(DateUtils.dateTimeStringToLocalDateTime(dto.getCreateTimeStartStr())); | 
 |  |  |         } | 
 |  |  |         if(StringUtils.isNotBlank(dto.getCreateTimeEndStr())){ | 
 |  |  |             dto.setCreateTimeEnd(DateUtils.dateTimeStringToLocalDateTime(dto.getCreateTimeEndStr())); | 
 |  |  |         } | 
 |  |  |         List<CustomerPointDetailDTO> list = customerPointMapper.queryCustomerDetails(dto, page); | 
 |  |  |         page.setRecords(list); | 
 |  |  |  | 
 |  |  | 
 |  |  |             point.setTotalPoint(0); | 
 |  |  |             point.setUsedPoint(0); | 
 |  |  |             point.setExpiredPoint(0); | 
 |  |  |             point.setDeductionPoint(0); | 
 |  |  |             point.create(SecurityUtils.getUserId()); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         if(POINT_CHANGE_TYPE.add.name().equals(detail.getChangeType())){ | 
 |  |  |             point.setTotalPoint(point.getTotalPoint() + detail.getPoint()); | 
 |  |  |             point.setUsedPoint(point.getUsedPoint() + detail.getUsePoint()); | 
 |  |  |             point.setExpiredPoint(point.getExpiredPoint() + detail.getExpiredPoint()); | 
 |  |  |         }else if(POINT_CHANGE_TYPE.reduce.name().equals(detail.getChangeType())){ | 
 |  |  | //            if(point.getTotalPoint()!= null && point.getTotalPoint()!=0 && point.getTotalPoint()>= detail.getPoint()){ | 
 |  |  | //                point.setTotalPoint(point.getTotalPoint() - detail.getPoint()); | 
 |  |  | //            }else{ | 
 |  |  | //                //积分不足,直接清0 | 
 |  |  | //                point.setTotalPoint(0); | 
 |  |  | //            } | 
 |  |  |             point.setTotalPoint(point.getTotalPoint() - detail.getPoint()); | 
 |  |  |             Integer detailPoint = detail.getPoint(); | 
 |  |  |             if(POINT_TYPE.deduction.name().equals(detail.getType())){ //积分扣减 | 
 |  |  |                 Integer balancePoint = point.getTotalPoint() - point.getUsedPoint()-point.getExpiredPoint()-point.getDeductionPoint();//可用积分 | 
 |  |  |                 balancePoint = balancePoint.intValue()>0?balancePoint.intValue():0; | 
 |  |  |                 if(balancePoint.intValue() >= detail.getPoint().intValue()){ | 
 |  |  |                     point.setDeductionPoint(point.getDeductionPoint() + detail.getPoint()); | 
 |  |  |                 }else{ | 
 |  |  |                     //积分不足,直接清0 | 
 |  |  |                     point.setDeductionPoint(point.getDeductionPoint() + balancePoint); | 
 |  |  |                     detail.setPoint(balancePoint); | 
 |  |  |                     detail.setRemarks(detail.getRemarks()+",当前积分不足"+detailPoint+",实际扣除积分"+balancePoint); | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         if(isAdd){ | 
 |  |  |             customerPointMapper.insert(point); | 
 |  |  | 
 |  |  |         detail.setChangeType(POINT_CHANGE_TYPE.reduce.name()); | 
 |  |  |         detail.setType(POINT_TYPE.deduction.name()); | 
 |  |  |         detail.create(SecurityUtils.getUserId()); | 
 |  |  |         customerPointDetailMapper.insert(detail); | 
 |  |  |  | 
 |  |  |         //更新汇总表 | 
 |  |  |         updateCustomerPoint(detail); | 
 |  |  |  | 
 |  |  |         customerPointDetailMapper.insert(detail); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void consumptionPoint(BigDecimal orderAmount, String orderNo,String userId)  { | 
 |  |  | 
 |  |  |         //更新汇总表 | 
 |  |  |         updateCustomerPoint(detail); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public void signIn() { | 
 |  |  |         if(signToday()){ | 
 |  |  |             throw new ValidationException("今天已经签到过了"); | 
 |  |  |         } | 
 |  |  |         int signNum = customerPointDetailMapper | 
 |  |  |                 .selectCount(new LambdaQueryWrapper<CustomerPointDetail>() | 
 |  |  |                         .eq(CustomerPointDetail::getUserId, SecurityUtils.getUserId()) | 
 |  |  |                         .ge(CustomerPointDetail::getRecordDate,LocalDate.now().minusDays(9)) | 
 |  |  |                         .eq(CustomerPointDetail::getChangeType,POINT_CHANGE_TYPE.add.name()) | 
 |  |  |                         .eq(CustomerPointDetail::getType,POINT_TYPE.sign.name()) | 
 |  |  |                 ); | 
 |  |  |         int point = 1; | 
 |  |  |         if(signNum==9){ | 
 |  |  |             point = 3; | 
 |  |  |         } | 
 |  |  |         CustomerPointDetail detail = new CustomerPointDetail(); | 
 |  |  |         Customer customer = getCurrentCustomer(); | 
 |  |  |         detail.setCustomerId(customer.getId()); | 
 |  |  |         detail.setUserId(customer.getUserId()); | 
 |  |  |         detail.setPoint(point); | 
 |  |  |         detail.setRemarks("签到"); | 
 |  |  |         detail.setRecordDate(LocalDate.now()); | 
 |  |  |         detail.setChangeType(POINT_CHANGE_TYPE.add.name()); | 
 |  |  |         detail.setType(POINT_TYPE.sign.name()); | 
 |  |  |         detail.create(SecurityUtils.getUserId()); | 
 |  |  |         customerPointDetailMapper.insert(detail); | 
 |  |  |  | 
 |  |  |         //更新汇总表 | 
 |  |  |         updateCustomerPoint(detail); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public List<CustomerPointDetailDTO> signList(LocalDate startDate, LocalDate endDate) { | 
 |  |  |         LambdaQueryWrapper<CustomerPointDetail> lambdaQueryWrapper = new LambdaQueryWrapper<CustomerPointDetail>() | 
 |  |  |                 .eq(CustomerPointDetail::getUserId, SecurityUtils.getUserId()) | 
 |  |  |                 .eq(CustomerPointDetail::getChangeType, POINT_CHANGE_TYPE.add.name()) | 
 |  |  |                 .eq(CustomerPointDetail::getType, POINT_TYPE.sign.name()); | 
 |  |  |         lambdaQueryWrapper.orderByDesc(CustomerPointDetail::getRecordDate); | 
 |  |  |         if(startDate!=null){ | 
 |  |  |             lambdaQueryWrapper.ge(CustomerPointDetail::getRecordDate,startDate); | 
 |  |  |         } | 
 |  |  |         if (endDate != null) { | 
 |  |  |             lambdaQueryWrapper.le(CustomerPointDetail::getRecordDate, endDate); | 
 |  |  |         } | 
 |  |  |  | 
 |  |  |         List<CustomerPointDetail> details = customerPointDetailMapper.selectList(lambdaQueryWrapper); | 
 |  |  |         List<CustomerPointDetailDTO> dtos = new ArrayList<>(); | 
 |  |  |         for (CustomerPointDetail detail : details) { | 
 |  |  |             CustomerPointDetailDTO dto = new CustomerPointDetailDTO(); | 
 |  |  |             BeanUtils.copyProperties(detail, dto); | 
 |  |  |             dtos.add(dto); | 
 |  |  |         } | 
 |  |  |         return dtos; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     public Boolean signToday() { | 
 |  |  |         return customerPointDetailMapper.selectCount(new LambdaQueryWrapper<CustomerPointDetail>() | 
 |  |  |                         .eq(CustomerPointDetail::getUserId, SecurityUtils.getUserId()) | 
 |  |  |                         .eq(CustomerPointDetail::getRecordDate,LocalDate.now()) | 
 |  |  |                         .eq(CustomerPointDetail::getChangeType,POINT_CHANGE_TYPE.add.name()) | 
 |  |  |                         .eq(CustomerPointDetail::getType,POINT_TYPE.sign.name()))>0; | 
 |  |  |     } | 
 |  |  | } |