| | |
| | | package com.ruoyi.procurementrecord.service.impl; |
| | | |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.metadata.IPage; |
| | | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.utils.excel.ExcelUtils; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.procurementrecord.dto.ProcurementPageDto; |
| | | import com.ruoyi.procurementrecord.DiscountTypeEnum; |
| | | import com.ruoyi.procurementrecord.dto.SimplePP; |
| | | import com.ruoyi.procurementrecord.mapper.ProcurementPriceManagementMapper; |
| | | import com.ruoyi.procurementrecord.pojo.ProcurementPriceManagement; |
| | | import com.ruoyi.procurementrecord.service.ProcurementPriceManagementService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Calendar; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | } |
| | | } |
| | | ExcelUtil<ProcurementPriceManagement> util = new ExcelUtil<ProcurementPriceManagement>(ProcurementPriceManagement.class); |
| | | util.exportExcel(response, procurementPriceManagements, "采购价格管理");} |
| | | util.exportExcel(response, procurementPriceManagements, "采购价格管理"); |
| | | } |
| | | |
| | | @Override |
| | | @Transactional |
| | | public void autoCreateRecord(SimplePP simplePP) throws ServiceException { |
| | | // 根据供应商id 产品id 查询出最近修改的一条记录进行比对 |
| | | ProcurementPriceManagement insertPriceManagement = new ProcurementPriceManagement(); |
| | | BeanUtils.copyProperties(simplePP, insertPriceManagement); |
| | | LambdaQueryWrapper<ProcurementPriceManagement> queryWrapper = new LambdaQueryWrapper<>(); |
| | | queryWrapper.eq(ProcurementPriceManagement::getSupplierId, simplePP.getSupplierId()) |
| | | .eq(ProcurementPriceManagement::getProductId, simplePP.getProductId()) |
| | | .orderByDesc(ProcurementPriceManagement::getUpdateTime).last("limit 1"); |
| | | ProcurementPriceManagement lastRecord = procurementPriceManagementMapper.selectOne(queryWrapper); |
| | | |
| | | |
| | | insertPriceManagement.setActuallyPrice(simplePP.getFinalPrice()); |
| | | insertPriceManagement.setDiscountType(DiscountTypeEnum.DISCOUNT_TYPE_NONE.getCode()); |
| | | insertPriceManagement.setDiscountValue("0"); |
| | | // 使用 Calendar 设置远期日期 (9999-12-31) |
| | | Calendar calendar = Calendar.getInstance(); |
| | | calendar.set(2099, Calendar.DECEMBER, 31); |
| | | insertPriceManagement.setDiscountEndTime(calendar.getTime()); |
| | | insertPriceManagement.setEffectiveTime(new Date()); |
| | | insertPriceManagement.setExpireTime(calendar.getTime()); |
| | | insertPriceManagement.setReason("other"); |
| | | // 进行分别情况 |
| | | BigDecimal currentPrice = simplePP.getFinalPrice(); |
| | | |
| | | if (lastRecord == null) { |
| | | insertPriceManagement.setBasePrice(currentPrice.toString()); |
| | | |
| | | insertPriceManagement.setMinPrice(null); |
| | | insertPriceManagement.setMaxPrice(null); |
| | | procurementPriceManagementMapper.insert(insertPriceManagement); |
| | | return; |
| | | } |
| | | |
| | | int compareTo = lastRecord.getActuallyPrice().compareTo(currentPrice); |
| | | if (compareTo == 0) { |
| | | log.info("价格未变化,无需创建记录"); |
| | | return; |
| | | } |
| | | |
| | | // 👉 价格变化逻辑 |
| | | insertPriceManagement.setBasePrice(String.valueOf(lastRecord.getActuallyPrice())); |
| | | insertPriceManagement.setMaxPrice(lastRecord.getMaxPrice()); |
| | | insertPriceManagement.setMinPrice(lastRecord.getMinPrice()); |
| | | |
| | | // 计算折扣 |
| | | // if(compareTo > 0){ |
| | | BigDecimal basePrice = new BigDecimal(insertPriceManagement.getBasePrice()); |
| | | BigDecimal actuallyPrice = insertPriceManagement.getActuallyPrice(); |
| | | // 防止除0 |
| | | if (basePrice.compareTo(BigDecimal.ZERO) == 0) { |
| | | insertPriceManagement.setDiscountValue("0"); |
| | | return; |
| | | } |
| | | |
| | | // 折扣 = (原价 - 现价) / 原价 |
| | | BigDecimal discount = basePrice.subtract(actuallyPrice) |
| | | .divide(basePrice, 8, RoundingMode.HALF_UP); |
| | | if(discount.compareTo(new BigDecimal("0.01")) < 0 && discount.compareTo(new BigDecimal("0")) > 0){ |
| | | // 转为固定金额 |
| | | insertPriceManagement.setDiscountValue(String.valueOf(basePrice.subtract(actuallyPrice))); |
| | | insertPriceManagement.setDiscountType(DiscountTypeEnum.DISCOUNT_TYPE_FIXED.getCode()); |
| | | }else { |
| | | insertPriceManagement.setDiscountType(DiscountTypeEnum.DISCOUNT_TYPE_PERCENTAGE.getCode()); |
| | | // 转百分比(×100) |
| | | BigDecimal percent = discount.multiply(new BigDecimal("100")); |
| | | |
| | | // 保留2位小数 |
| | | percent = percent.setScale(2, RoundingMode.HALF_UP); |
| | | |
| | | // 设置值(比如:20.00%) |
| | | insertPriceManagement.setDiscountValue(String.valueOf(percent)); |
| | | } |
| | | // } |
| | | |
| | | // final insert |
| | | procurementPriceManagementMapper.insert(insertPriceManagement); |
| | | } |
| | | } |