MoneyBusinessServiceImpl.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package vip.xiaonuo.money.service.impl;
  2. import cn.hutool.core.date.DateTime;
  3. import cn.hutool.core.date.DateUtil;
  4. import cn.hutool.core.math.Money;
  5. import cn.hutool.core.util.ObjectUtil;
  6. import com.alibaba.fastjson.JSONArray;
  7. import com.alibaba.fastjson.JSONObject;
  8. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  9. import com.baomidou.mybatisplus.core.toolkit.StringUtils;
  10. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import vip.xiaonuo.money.entity.MoneyBusiness;
  13. import vip.xiaonuo.money.entity.MoneyCost;
  14. import vip.xiaonuo.money.entity.MoneyWarning;
  15. import vip.xiaonuo.money.mapper.MoneyBusinessMapper;
  16. import vip.xiaonuo.money.mapper.MoneyCostMapper;
  17. import vip.xiaonuo.money.param.*;
  18. import vip.xiaonuo.money.service.IMoneyBusinessService;
  19. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  20. import org.springframework.stereotype.Service;
  21. import vip.xiaonuo.sale.mapper.SaleCostMapper;
  22. import java.math.BigDecimal;
  23. import java.math.RoundingMode;
  24. import java.text.DecimalFormat;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. import java.util.Map;
  29. /**
  30. * <p>
  31. * 财务营业情况 服务实现类
  32. * </p>
  33. *
  34. * @author author
  35. * @since 2024-11-19
  36. */
  37. @Service
  38. public class MoneyBusinessServiceImpl extends ServiceImpl<MoneyBusinessMapper, MoneyBusiness> implements IMoneyBusinessService {
  39. @Autowired
  40. private MoneyBusinessMapper businessMapper;
  41. @Autowired
  42. private MoneyCostMapper moneyCostMapper;
  43. @Override
  44. public JSONArray listBusiness(MoneyBusinessParam moneyBusinessParam) {
  45. String orgId = moneyBusinessParam.getOrgId();
  46. String time = moneyBusinessParam.getTime();//2024-10
  47. QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
  48. QueryWrapper<MoneyBusiness> queryWrapper1 = new QueryWrapper<>();
  49. queryWrapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);
  50. queryWrapper1.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);
  51. JSONArray jsonArray = new JSONArray();
  52. if ("".equals(time) || null == time) {
  53. DateTime dateTime = DateUtil.beginOfMonth(new Date());
  54. queryWrapper.ge("create_time", dateTime);
  55. DateTime beginOfYear = DateUtil.beginOfYear(new Date());
  56. queryWrapper1.ge("create_time", beginOfYear);
  57. } else {
  58. String year = time.substring(0, 4);
  59. queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
  60. queryWrapper1.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
  61. }
  62. MoneyBusiness moneyBusiness = new MoneyBusiness();
  63. queryWrapper.select(" IFNULL(sum(business_income),'0') business_income," +
  64. "IFNULL(sum(business_cost),'0') businessCost," +
  65. "IFNULL(sum(profit),'0') profit," +
  66. "IFNULL(sum(taxes),'0') taxes," +
  67. "IFNULL(sum(balance),'0') balance");
  68. queryWrapper1.select(" IFNULL(sum(business_income),'0') business_income," +
  69. "IFNULL(sum(business_cost),'0') businessCost," +
  70. "IFNULL(sum(profit),'0') profit," +
  71. "IFNULL(sum(taxes),'0') taxes," +
  72. "IFNULL(sum(balance),'0') balance");
  73. MoneyBusiness selectOne = businessMapper.selectOne(queryWrapper);
  74. MoneyBusiness selectOne1 = businessMapper.selectOne(queryWrapper1);
  75. JSONObject jsonObject = new JSONObject();
  76. BigDecimal math = new BigDecimal("10000");
  77. BigDecimal bd = new BigDecimal(selectOne.getBusinessIncome());
  78. BigDecimal bd1 = new BigDecimal(selectOne.getBusinessCost());
  79. BigDecimal bd2 = new BigDecimal(selectOne.getProfit());
  80. BigDecimal bd3 = new BigDecimal(selectOne.getTaxes());
  81. BigDecimal bd4 = new BigDecimal(selectOne.getBalance());
  82. bd = bd.divide(math, 2, RoundingMode.HALF_UP);
  83. bd1 = bd1.divide(math, 2, RoundingMode.HALF_UP);
  84. bd2 = bd2.divide(math, 2, RoundingMode.HALF_UP);
  85. bd3 = bd3.divide(math, 2, RoundingMode.HALF_UP);
  86. bd4 = bd4.divide(math, 2, RoundingMode.HALF_UP);
  87. moneyBusiness.setBusinessIncome(bd.toString());
  88. moneyBusiness.setBusinessCost(bd1.toString());
  89. moneyBusiness.setProfit(bd2.toString());
  90. moneyBusiness.setTaxes(bd3.toString());
  91. moneyBusiness.setBalance(bd4.toString());
  92. jsonObject.put("type", "month");
  93. jsonObject.put("object", moneyBusiness);
  94. JSONObject jsonObject1 = new JSONObject();
  95. MoneyBusiness moneyBusiness1 = new MoneyBusiness();
  96. BigDecimal bd5 = new BigDecimal(selectOne1.getBusinessIncome());
  97. BigDecimal bd6 = new BigDecimal(selectOne1.getBusinessCost());
  98. BigDecimal bd7 = new BigDecimal(selectOne1.getProfit());
  99. BigDecimal bd8 = new BigDecimal(selectOne1.getTaxes());
  100. BigDecimal bd9 = new BigDecimal(selectOne1.getBalance());
  101. BigDecimal divide = bd5.divide(math);
  102. divide = divide.setScale(2, RoundingMode.HALF_UP);
  103. // bd5 = bd5.divide(math, 2, RoundingMode.HALF_UP);
  104. bd6 = bd6.divide(math, 2, RoundingMode.HALF_UP);
  105. bd7 = bd7.divide(math, 2, RoundingMode.HALF_UP);
  106. bd8 = bd8.divide(math, 2, RoundingMode.HALF_UP);
  107. bd9 = bd9.divide(math, 2, RoundingMode.HALF_UP);
  108. moneyBusiness1.setBusinessIncome(divide.toString());
  109. moneyBusiness1.setBusinessCost(bd6.toString());
  110. moneyBusiness1.setProfit(bd7.toString());
  111. moneyBusiness1.setTaxes(bd8.toString());
  112. moneyBusiness1.setBalance(bd9.toString());
  113. jsonObject1.put("type", "year");
  114. jsonObject1.put("object", moneyBusiness1);
  115. jsonArray.add(jsonObject);
  116. jsonArray.add(jsonObject1);
  117. return jsonArray;
  118. }
  119. @Override
  120. public Page<MoneyBusiness> getPage(MoneyBusinessPageParam pageParam) {
  121. String orgId = pageParam.getOrgId();
  122. Page<MoneyBusiness> page = new Page(pageParam.getPageNum(), pageParam.getPageSize());
  123. QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
  124. queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
  125. return businessMapper.selectPage(page, queryWrapper);
  126. }
  127. @Override
  128. public void addBusiness(MoneyBusiness business) {
  129. business.setCreateTime(new Date());
  130. businessMapper.insert(business);
  131. }
  132. @Override
  133. public void updateBusiness(MoneyBusiness business) {
  134. businessMapper.updateById(business);
  135. }
  136. @Override
  137. public void deleteBusiness(MoneyBusiness business) {
  138. businessMapper.deleteById(business);
  139. }
  140. @Override
  141. public DsBusAllParam dsBusAll(MoneyAnalysisParam moneyAnalysisParam) {
  142. String orgId = moneyAnalysisParam.getOrgId();
  143. String time = moneyAnalysisParam.getTime();
  144. Date date ;
  145. if (StringUtils.isBlank(time)){
  146. date = DateUtil.beginOfMonth(new Date());
  147. }else {
  148. date = DateUtil.beginOfMonth( DateUtil.parse(time,"yyyy-MM"));
  149. }
  150. DateTime lastYear = DateUtil.offsetMonth(date, -12);
  151. QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
  152. wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId).between("create_time", date, DateUtil.endOfMonth(date));
  153. DsBusAllParam busAllParam = businessMapper.dsBusAll(wapper);
  154. wapper.clear();
  155. wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId)
  156. .between("create_time", lastYear, DateUtil.endOfMonth(lastYear));
  157. DsBusAllParam lastBusAllParam = businessMapper.dsBusAll(wapper);
  158. //今年
  159. wapper.clear();
  160. wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId)
  161. .between("create_time", DateUtil.beginOfYear(date), DateUtil.endOfYear(date));
  162. DsBusAllParam yearBusAllParam = businessMapper.dsBusAll(wapper);
  163. //去年
  164. wapper.clear();
  165. wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId)
  166. .between("create_time", DateUtil.beginOfYear(lastYear), DateUtil.endOfYear(lastYear));
  167. DsBusAllParam lastYearBusAllParam = businessMapper.dsBusAll(wapper);
  168. //计算同比
  169. busAllParam.setBalanceIsOn(getDouble(lastBusAllParam.getBalance()) <getDouble(busAllParam.getBalance()));
  170. busAllParam.setProfitIsOn(getDouble(lastBusAllParam.getProfit()) <getDouble(busAllParam.getProfit()));
  171. busAllParam.setTaxesIsOn(getDouble(lastBusAllParam.getTaxes()) <getDouble(busAllParam.getTaxes()));
  172. busAllParam.setBusinessIncomeIsOn(getDouble(lastBusAllParam.getBusinessIncome()) <getDouble(busAllParam.getBusinessIncome()));
  173. busAllParam.setBorrowPriceIsOn(getDouble(lastBusAllParam.getBorrowPrice()) <getDouble(busAllParam.getBorrowPrice()));
  174. busAllParam.setBalanceOnYear(calculateYOYChange(getDouble(busAllParam.getBalance()),getDouble(lastBusAllParam.getBalance())));
  175. busAllParam.setProfitOnYear(calculateYOYChange(getDouble(busAllParam.getProfit()),getDouble(lastBusAllParam.getProfit())));
  176. busAllParam.setTaxesOnYear(calculateYOYChange(getDouble(busAllParam.getTaxes()),getDouble(lastBusAllParam.getTaxes())));
  177. busAllParam.setBusinessIncomeOnYear(calculateYOYChange(getDouble(busAllParam.getBusinessIncome()),getDouble(lastBusAllParam.getBusinessIncome())));
  178. busAllParam.setBorrowPriceOnYear(calculateYOYChange(getDouble(busAllParam.getBorrowPrice()),getDouble(lastBusAllParam.getBorrowPrice())));
  179. //本年
  180. busAllParam.setBalanceYear(yearBusAllParam.getBalance());
  181. busAllParam.setProfitYear(yearBusAllParam.getProfit());
  182. busAllParam.setTaxesYear(yearBusAllParam.getTaxes());
  183. busAllParam.setBorrowPriceYear(yearBusAllParam.getBorrowPrice());
  184. busAllParam.setBusinessIncomeYear(yearBusAllParam.getBusinessIncome());
  185. //年同比
  186. busAllParam.setYearBusinessIncomeIsOn(getDouble(lastYearBusAllParam.getBusinessIncome()) <getDouble(yearBusAllParam.getBusinessIncome()));
  187. busAllParam.setYearProfitIsOn(getDouble(lastYearBusAllParam.getProfit()) <getDouble(yearBusAllParam.getProfit()));
  188. busAllParam.setYearTaxesIsOn(getDouble(lastYearBusAllParam.getTaxes()) <getDouble(yearBusAllParam.getTaxes()));
  189. busAllParam.setYearBusinessIncomeOnYear(calculateYOYChange(getDouble(yearBusAllParam.getBusinessIncome()),getDouble(lastYearBusAllParam.getBusinessIncome())));
  190. busAllParam.setYearProfitOnYear(calculateYOYChange(getDouble(yearBusAllParam.getProfit()),getDouble(lastYearBusAllParam.getProfit())));
  191. busAllParam.setYearTaxesOnYear(calculateYOYChange(getDouble(yearBusAllParam.getTaxes()),getDouble(lastYearBusAllParam.getTaxes())));
  192. return busAllParam;
  193. }
  194. private Double getDouble(String balance) {
  195. if (StringUtils.isBlank(balance)){
  196. return 0.0;
  197. }
  198. return Double.parseDouble(balance);
  199. // return balance;
  200. }
  201. @Override
  202. public List<DsBusAllParam> getDsBusYyJlYjDetail(DsBusParam dsBusParam) {
  203. String orgId = dsBusParam.getOrgId();
  204. Integer type = dsBusParam.getType();
  205. QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
  206. wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("create_time").groupBy("ymonth");
  207. List<DsBusAllParam> busAllParams;
  208. if (ObjectUtil.isEmpty(type) || 0 == type){
  209. busAllParams = businessMapper.getDsBusYyJlYjDetailMonth(wapper);
  210. }else {
  211. busAllParams = businessMapper.getDsBusYyJlYjDetailYear(wapper);
  212. }
  213. return busAllParams;
  214. }
  215. @Override
  216. public List<DsBusAllParam> getBalanceDetail(DsBusParam dsBusParam) {
  217. String orgId = dsBusParam.getOrgId();
  218. Integer type = dsBusParam.getType();
  219. QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
  220. wapper.eq( StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("create_time").groupBy("ymonth");
  221. List<DsBusAllParam> busAllParams;
  222. if (ObjectUtil.isEmpty(type) || 0 == type){
  223. busAllParams = businessMapper.getBalanceDetailMonth(wapper);
  224. }else {
  225. busAllParams = businessMapper.getBalanceDetailYear(wapper);
  226. }
  227. for (int i = 0; i < busAllParams.size() -1 ; i++) {
  228. DsBusAllParam lastDsBus = busAllParams.get(i);
  229. DsBusAllParam dsBusAllParam = busAllParams.get(i + 1);
  230. dsBusAllParam.setBalanceOnYear(calculateYOYZiJinYvE(getDouble(dsBusAllParam.getBalance()),getDouble(lastDsBus.getBalance()) ));
  231. dsBusAllParam.setBalanceIsOn(getDouble(lastDsBus.getBalance()) <getDouble(dsBusAllParam.getBalance()));
  232. }
  233. if (ObjectUtil.isNotEmpty(busAllParams)){
  234. DsBusAllParam dsBusAllParam = busAllParams.get(0);
  235. dsBusAllParam.setBalanceIsOn(true);
  236. dsBusAllParam.setBalanceOnYear("0%");
  237. }
  238. return busAllParams;
  239. }
  240. @Override
  241. public List<DsBusAllParam> getBorrowDetail(DsBusParam dsBusParam) {
  242. String orgId = dsBusParam.getOrgId();
  243. Integer type = dsBusParam.getType();
  244. QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
  245. wapper.eq( StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("borrow_time").groupBy("ymonth");
  246. List<DsBusAllParam> busAllParams;
  247. if (ObjectUtil.isEmpty(type) || 0 == type){
  248. busAllParams = businessMapper.getBorrowDetailMonth(wapper);
  249. }else {
  250. busAllParams = businessMapper.getBorrowDetailYear(wapper);
  251. }
  252. return busAllParams;
  253. }
  254. @Override
  255. public List<DsBusSaleMoneyParam> getMoneyDetail(DsBusParam dsBusParam) {
  256. String orgId = dsBusParam.getOrgId();
  257. Integer type = dsBusParam.getType();
  258. QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
  259. wapper.eq( StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("create_time").groupBy("ymonth");
  260. List<DsBusSaleMoneyParam> busAllParams;
  261. if (ObjectUtil.isEmpty(type) || 0 == type){
  262. busAllParams = moneyCostMapper.getMoneyDetailMonth(wapper);
  263. }else {
  264. busAllParams = moneyCostMapper.getMoneyDetailYear(wapper);
  265. }
  266. return busAllParams;
  267. }
  268. @Override
  269. public Page<MoneyBusiness> listBusines(MoneyOverduePageParam pageParam) {
  270. QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
  271. queryWrapper.eq(StringUtils.isNotBlank(pageParam.getOrgId()), "org_id", pageParam.getOrgId()).ge("taxes" ,0);
  272. Page<MoneyBusiness> page = new Page<MoneyBusiness>(pageParam.getPageNum(), pageParam.getPageSize());
  273. return this.page(page, queryWrapper);
  274. }
  275. @Override
  276. public Page<MoneyBusiness> listBalance(MoneyParam pageParam) {
  277. QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
  278. queryWrapper.eq(StringUtils.isNotBlank(pageParam.getOrgId()), "org_id", pageParam.getOrgId()).ge("balance" ,0);
  279. Page<MoneyBusiness> page = new Page<MoneyBusiness>(pageParam.getPageNum(), pageParam.getPageSize());
  280. return this.page(page, queryWrapper);
  281. }
  282. public static String calculateYOYChange(double currentValue, double previousValue) {
  283. // 处理零值情况
  284. if (previousValue == 0) {
  285. return "0%";
  286. }
  287. // 计算同比变化率
  288. double percentageChange = (Math.abs((currentValue - previousValue) / previousValue)) * 100;
  289. if (percentageChange >500){
  290. return "100%";
  291. }
  292. return String.format("%.2f%%", percentageChange);
  293. }
  294. public static String calculateYOYZiJinYvE(double currentValue, double previousValue) {
  295. // 处理零值情况
  296. if (previousValue == 0) {
  297. return "0";
  298. }
  299. // 计算同比变化率
  300. double percentageChange = ((currentValue - previousValue) /previousValue) * 100;
  301. if (percentageChange >200){
  302. return "100";
  303. }
  304. return String.format("%.2f", percentageChange);
  305. }
  306. public static String calculateWanChenLv(double currentValue, double previousValue) {
  307. // 处理零值情况
  308. if (previousValue == 0) {
  309. return "100";
  310. }
  311. // 计算同比变化率
  312. double percentageChange = (currentValue /previousValue) * 100;
  313. if (percentageChange >200){
  314. return "100";
  315. }
  316. return String.format("%.2f", percentageChange);
  317. }
  318. }