package com.wms_admin.server.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson2.JSONObject; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.wms_admin.server.entity.Product; import com.wms_admin.server.mapper.ProductMapper; import com.wms_admin.server.service.ProductModelService; import com.wms_admin.server.service.ProductNameService; import com.wms_admin.server.service.ProductService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.wms_admin.utils.MyUtils; import com.wms_admin.utils.RedisUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.*; /** *

* 服务实现类 *

* * @author 江苏鵷雏网络科技有限公司 * @since 2023-05-24 */ @Service public class ProductServiceImpl extends ServiceImpl implements ProductService { @Resource private ProductMapper mapper; @Autowired private ProductModelService modelService; @Autowired private ProductNameService nameService; /** * 入库添加操作 * @param product 实体类 * @return 返回结果:1 成功;0:失败 */ @Override public Integer AddProductMessage(Product product) { Integer productModelId = modelService.SelectProductModelId(product.getProductModel(), product.getProductCode()); product.setProductModelId(productModelId); Integer productNameId = nameService.SelectProductNameId(product.getProductName()); product.setProductNameId(productNameId); return mapper.insert(product); } /** * 根据条件分页查询数据 * @param startTime 开始时间,可以为空,但是一旦有开始时间那么一定有结束时间 * @param endTime 结束时间 * @param productModel 库存型号 * @param page 分页条件 * @return 返回数据 */ @Override public IPage ListProductMessage(String startTime, String endTime, String productModel, Page page) { return mapper.SelectHistory(startTime, endTime, productModel, page); } /** * 根据条件导出所有的数据 * @param startTime 开始时间,可以为空,但是一旦有开始时间那么一定有结束时间 * @param endTime 结束时间 * @param productModel 库存型号 * @return 返回查询到的数据 */ @Override public List ExcelDerive(String startTime, String endTime, String productModel) { return mapper.SelectHistory(startTime, endTime, productModel); } @Override public List> SelectExceedThirtyDayData() { return mapper.ExceedThirtyDay(); } @Override public Map SelectWeekNumData() { List> maps = mapper.SelectWeekNumData(); List xList = new ArrayList<>(); List enterList = new ArrayList<>(); List outList = new ArrayList<>(); for (Map map : maps) { Object date = map.get("date"); String time0 = MyUtils.getTime_0((Date) date); xList.add(time0); enterList.add(map.get("product_num")); outList.add(map.get("out_product_num")); } Collections.reverse(xList); Collections.reverse(enterList); Collections.reverse(outList); Map map = new HashMap<>(); map.put("xList", xList); map.put("yEnterList", enterList); map.put("yOutList", outList); return map; } @Override public Integer TimerCountWeekDayData() { return mapper.TimerCountWeekDayData(); } @Override public List> SelectNameData(String productName) { return mapper.SelectNameData(productName); } @Override public List> PieData() { String beginningMonthKey = "beginningMonth"; String endOfMonthKey = "endOfMonth"; // 将月初,月末数据拼接到该数据中,并且时间对应 List> maps = mapper.PieData(); // 月初剩余库总数数据 Map beginningMonth = JSONObject.parseObject(JSON.toJSONString(RedisUtil.get("beginningMonth"))); // 月末剩余库总数数据 Map endOfMonth = JSONObject.parseObject(JSON.toJSONString(RedisUtil.get("endOfMonth"))); // 拼接月初剩余库总数数据 List> beginningMonthMaps = publicCode(maps, beginningMonth, beginningMonthKey); // 在拼接完月初的数据后以此为基础拼接 return publicCode(beginningMonthMaps, endOfMonth, endOfMonthKey); } @Override public Product SelectIdProduct(String code) { return mapper.SelectIdProduct(code); } // 将Redis中的数据与Mysql中的数据拼接 private List> publicCode(List> mainMaps, Map maps, String jsonKey){ for (Map map : mainMaps){ // mysql中的时间,以此为基础拼接 String mainKey = (String)map.get("datetime"); // 库存参数 List num = JSON.parseArray(maps.get("num").toString(), String.class); // Redis中存储的月份 List datetime = JSON.parseArray(maps.get("datetime").toString(), String.class); // 循环,如果时间相同则进行拼接 for (int i = 0; i < datetime.size(); i++){ if (mainKey.equals(datetime.get(i))){ map.put(jsonKey, num.get(i)); } } // 判断是否存在key,如果不存在拼接为0 if (!map.containsKey(jsonKey)){ // 如果不存在赋值为0 map.put(jsonKey, 0); } } return mainMaps; } }