SaleProduceServiceImpl.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package vip.xiaonuo.sale.service.impl;
  2. import cn.hutool.core.date.DateUtil;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  6. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import vip.xiaonuo.sale.entity.ErpBaseMaterial;
  9. import vip.xiaonuo.sale.entity.SaleProduce;
  10. import vip.xiaonuo.sale.mapper.ErpBaseMaterialMapper;
  11. import vip.xiaonuo.sale.mapper.SaleProduceMapper;
  12. import vip.xiaonuo.sale.param.SaleProduceParam;
  13. import vip.xiaonuo.sale.service.ISaleProduceService;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import org.springframework.stereotype.Service;
  16. import java.text.DecimalFormat;
  17. import java.text.SimpleDateFormat;
  18. import java.time.LocalDate;
  19. import java.util.ArrayList;
  20. import java.util.Date;
  21. import java.util.List;
  22. /**
  23. * <p>
  24. * 销售二级指标 服务实现类
  25. * </p>
  26. *
  27. * @author author
  28. * @since 2024-12-13
  29. */
  30. @Service
  31. public class SaleProduceServiceImpl extends ServiceImpl<SaleProduceMapper, SaleProduce> implements ISaleProduceService {
  32. @Autowired
  33. private SaleProduceMapper produceMapper;
  34. @Autowired
  35. private ErpBaseMaterialMapper materialMapper;
  36. @Override
  37. public JSONObject getProduceOne(SaleProduceParam produceParam) {
  38. Integer parentId = produceParam.getParentId();
  39. String time = produceParam.getDate();
  40. String year = time.substring(0, 4);
  41. QueryWrapper<ErpBaseMaterial> materialQueryWrapper = new QueryWrapper<>();
  42. materialQueryWrapper.eq("parent_id", parentId);
  43. List<ErpBaseMaterial> materials = materialMapper.selectList(materialQueryWrapper);
  44. Double monthSaleMoney = 0.00;
  45. Double yearSaleMoney = 0.00;
  46. Double monthPrice = 0.00;
  47. Double yearPrice = 0.00;
  48. if (materials.size() != 0) {
  49. for (ErpBaseMaterial material : materials) {
  50. QueryWrapper<SaleProduce> queryWrapper = new QueryWrapper<>();
  51. queryWrapper.like("produce_name", material.getMaterialName());
  52. queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
  53. queryWrapper.select(" IFNULL(sum(sale_money),'0') saleMoney,convert(IFNULL(avg(price),'0'),decimal(10,2))");
  54. SaleProduce monthProduce = produceMapper.selectOne(queryWrapper);
  55. QueryWrapper<SaleProduce> queryWrapper1 = new QueryWrapper<>();
  56. queryWrapper1.like("produce_name", material.getMaterialName());
  57. queryWrapper1.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
  58. queryWrapper1.select(" IFNULL(sum(sale_money),'0') saleMoney,convert(IFNULL(avg(price),'0'),decimal(10,2))");
  59. SaleProduce yearProduce = produceMapper.selectOne(queryWrapper1);
  60. monthSaleMoney = monthSaleMoney + Double.valueOf(monthProduce.getSaleMoney());
  61. yearSaleMoney = yearSaleMoney + Double.valueOf(yearProduce.getSaleMoney());
  62. monthPrice = monthPrice + Double.valueOf(monthProduce.getPrice());
  63. yearPrice = yearPrice + Double.valueOf(yearProduce.getPrice());
  64. }
  65. }
  66. JSONObject jsonObject = new JSONObject();
  67. jsonObject.put("monthSaleMoney", monthSaleMoney);
  68. jsonObject.put("yearSaleMoney",yearSaleMoney);
  69. DecimalFormat def = new DecimalFormat("0.00");
  70. if (materials.size() != 0) {
  71. double v = monthPrice / materials.size();
  72. double v1 = yearPrice / materials.size();
  73. jsonObject.put("monthPrice", def.format(v));
  74. jsonObject.put("yearPrice", v1);
  75. } else {
  76. jsonObject.put("monthPrice", "0");
  77. jsonObject.put("yearPrice", "0");
  78. }
  79. return jsonObject;
  80. }
  81. @Override
  82. public JSONArray getPriceList(SaleProduceParam produceParam) {
  83. Integer parentId = produceParam.getParentId();
  84. String time = produceParam.getDate();
  85. String year = time.substring(0, 4);
  86. QueryWrapper<ErpBaseMaterial> materialQueryWrapper = new QueryWrapper<>();
  87. materialQueryWrapper.eq("parent_id", parentId);
  88. List<ErpBaseMaterial> materials = materialMapper.selectList(materialQueryWrapper);
  89. List<SaleProduce> yearList = new ArrayList<>();
  90. List<SaleProduce> lastYearList = new ArrayList<>();
  91. if (materials.size() != 0) {
  92. for (ErpBaseMaterial material : materials) {
  93. QueryWrapper<SaleProduce> queryWrapper = new QueryWrapper<>();
  94. queryWrapper.like("produce_name", material.getMaterialName());
  95. queryWrapper.between("create_time", year + "-01 00:00:00", year + "-31 23:59:59");
  96. queryWrapper.select(" sum(sale_money) saleMoney,create_time createTime");
  97. queryWrapper.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
  98. SaleProduce lastProduce = produceMapper.selectOne(queryWrapper);
  99. lastYearList.add(lastProduce);
  100. QueryWrapper<SaleProduce> queryWrapper1 = new QueryWrapper<>();
  101. queryWrapper1.like("produce_name", material.getMaterialName());
  102. queryWrapper1.between("create_time", Integer.parseInt(year)-1 + "-01-01 00:00:00", Integer.parseInt(year)-1 + "-12-31 23:59:59");
  103. queryWrapper1.select(" sum(sale_money) saleMoney,create_time createTime");
  104. queryWrapper1.groupBy("DATE_FORMAT(create_time,'%Y-%m')");
  105. SaleProduce yearProduce = produceMapper.selectOne(queryWrapper1);
  106. yearList.add(yearProduce);
  107. }
  108. }
  109. SimpleDateFormat sdf = new SimpleDateFormat("MM");
  110. JSONArray jsonArray = new JSONArray();
  111. for (int i = 1; i < 13; i++) {
  112. JSONObject jsonObject = new JSONObject();
  113. jsonObject.put("time", i + "月");
  114. jsonObject.put("year", "0");
  115. jsonObject.put("lastYear", "0");
  116. if (yearList.size() != 0) {
  117. for (SaleProduce produce : yearList) {
  118. String format = sdf.format(produce.getCreateTime());
  119. if (i == Integer.parseInt(format)) {
  120. jsonObject.put("year", produce.getSaleMoney());
  121. }
  122. }
  123. } else {
  124. jsonObject.put("year", "0");
  125. }
  126. if (lastYearList.size() != 0) {
  127. for (SaleProduce produce : lastYearList) {
  128. String format = sdf.format(produce.getCreateTime());
  129. if (i == Integer.parseInt(format)) {
  130. jsonObject.put("lastYear", produce.getSaleMoney());
  131. }
  132. }
  133. } else {
  134. jsonObject.put("lastYear", "0");
  135. }
  136. jsonArray.add(jsonObject);
  137. }
  138. return jsonArray;
  139. }
  140. }