123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- package vip.xiaonuo.money.service.impl;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.math.Money;
- import cn.hutool.core.util.ObjectUtil;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
- import com.baomidou.mybatisplus.core.toolkit.StringUtils;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import org.springframework.beans.factory.annotation.Autowired;
- import vip.xiaonuo.money.entity.MoneyBusiness;
- import vip.xiaonuo.money.entity.MoneyCost;
- import vip.xiaonuo.money.entity.MoneyWarning;
- import vip.xiaonuo.money.mapper.MoneyBusinessMapper;
- import vip.xiaonuo.money.mapper.MoneyCostMapper;
- import vip.xiaonuo.money.param.*;
- import vip.xiaonuo.money.service.IMoneyBusinessService;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import org.springframework.stereotype.Service;
- import vip.xiaonuo.sale.mapper.SaleCostMapper;
- import java.math.BigDecimal;
- import java.math.RoundingMode;
- import java.text.DecimalFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.List;
- import java.util.Map;
- /**
- * <p>
- * 财务营业情况 服务实现类
- * </p>
- *
- * @author author
- * @since 2024-11-19
- */
- @Service
- public class MoneyBusinessServiceImpl extends ServiceImpl<MoneyBusinessMapper, MoneyBusiness> implements IMoneyBusinessService {
- @Autowired
- private MoneyBusinessMapper businessMapper;
- @Autowired
- private MoneyCostMapper moneyCostMapper;
- @Override
- public JSONArray listBusiness(MoneyBusinessParam moneyBusinessParam) {
- String orgId = moneyBusinessParam.getOrgId();
- String time = moneyBusinessParam.getTime();//2024-10
- QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
- QueryWrapper<MoneyBusiness> queryWrapper1 = new QueryWrapper<>();
- queryWrapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);
- queryWrapper1.eq(StringUtils.isNotBlank(orgId),"org_id", orgId);
- JSONArray jsonArray = new JSONArray();
- if ("".equals(time) || null == time) {
- DateTime dateTime = DateUtil.beginOfMonth(new Date());
- queryWrapper.ge("create_time", dateTime);
- DateTime beginOfYear = DateUtil.beginOfYear(new Date());
- queryWrapper1.ge("create_time", beginOfYear);
- } else {
- String year = time.substring(0, 4);
- queryWrapper.between("create_time", time + "-01 00:00:00", time + "-31 23:59:59");
- queryWrapper1.between("create_time", year + "-01-01 00:00:00", year + "-12-31 23:59:59");
- }
- MoneyBusiness moneyBusiness = new MoneyBusiness();
- queryWrapper.select(" IFNULL(sum(business_income),'0') business_income," +
- "IFNULL(sum(business_cost),'0') businessCost," +
- "IFNULL(sum(profit),'0') profit," +
- "IFNULL(sum(taxes),'0') taxes," +
- "IFNULL(sum(balance),'0') balance");
- queryWrapper1.select(" IFNULL(sum(business_income),'0') business_income," +
- "IFNULL(sum(business_cost),'0') businessCost," +
- "IFNULL(sum(profit),'0') profit," +
- "IFNULL(sum(taxes),'0') taxes," +
- "IFNULL(sum(balance),'0') balance");
- MoneyBusiness selectOne = businessMapper.selectOne(queryWrapper);
- MoneyBusiness selectOne1 = businessMapper.selectOne(queryWrapper1);
- JSONObject jsonObject = new JSONObject();
- BigDecimal math = new BigDecimal("10000");
- BigDecimal bd = new BigDecimal(selectOne.getBusinessIncome());
- BigDecimal bd1 = new BigDecimal(selectOne.getBusinessCost());
- BigDecimal bd2 = new BigDecimal(selectOne.getProfit());
- BigDecimal bd3 = new BigDecimal(selectOne.getTaxes());
- BigDecimal bd4 = new BigDecimal(selectOne.getBalance());
- bd = bd.divide(math, 2, RoundingMode.HALF_UP);
- bd1 = bd1.divide(math, 2, RoundingMode.HALF_UP);
- bd2 = bd2.divide(math, 2, RoundingMode.HALF_UP);
- bd3 = bd3.divide(math, 2, RoundingMode.HALF_UP);
- bd4 = bd4.divide(math, 2, RoundingMode.HALF_UP);
- moneyBusiness.setBusinessIncome(bd.toString());
- moneyBusiness.setBusinessCost(bd1.toString());
- moneyBusiness.setProfit(bd2.toString());
- moneyBusiness.setTaxes(bd3.toString());
- moneyBusiness.setBalance(bd4.toString());
- jsonObject.put("type", "month");
- jsonObject.put("object", moneyBusiness);
- JSONObject jsonObject1 = new JSONObject();
- MoneyBusiness moneyBusiness1 = new MoneyBusiness();
- BigDecimal bd5 = new BigDecimal(selectOne1.getBusinessIncome());
- BigDecimal bd6 = new BigDecimal(selectOne1.getBusinessCost());
- BigDecimal bd7 = new BigDecimal(selectOne1.getProfit());
- BigDecimal bd8 = new BigDecimal(selectOne1.getTaxes());
- BigDecimal bd9 = new BigDecimal(selectOne1.getBalance());
- BigDecimal divide = bd5.divide(math);
- divide = divide.setScale(2, RoundingMode.HALF_UP);
- // bd5 = bd5.divide(math, 2, RoundingMode.HALF_UP);
- bd6 = bd6.divide(math, 2, RoundingMode.HALF_UP);
- bd7 = bd7.divide(math, 2, RoundingMode.HALF_UP);
- bd8 = bd8.divide(math, 2, RoundingMode.HALF_UP);
- bd9 = bd9.divide(math, 2, RoundingMode.HALF_UP);
- moneyBusiness1.setBusinessIncome(divide.toString());
- moneyBusiness1.setBusinessCost(bd6.toString());
- moneyBusiness1.setProfit(bd7.toString());
- moneyBusiness1.setTaxes(bd8.toString());
- moneyBusiness1.setBalance(bd9.toString());
- jsonObject1.put("type", "year");
- jsonObject1.put("object", moneyBusiness1);
- jsonArray.add(jsonObject);
- jsonArray.add(jsonObject1);
- return jsonArray;
- }
- @Override
- public Page<MoneyBusiness> getPage(MoneyBusinessPageParam pageParam) {
- String orgId = pageParam.getOrgId();
- Page<MoneyBusiness> page = new Page(pageParam.getPageNum(), pageParam.getPageSize());
- QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
- return businessMapper.selectPage(page, queryWrapper);
- }
- @Override
- public void addBusiness(MoneyBusiness business) {
- business.setCreateTime(new Date());
- businessMapper.insert(business);
- }
- @Override
- public void updateBusiness(MoneyBusiness business) {
- businessMapper.updateById(business);
- }
- @Override
- public void deleteBusiness(MoneyBusiness business) {
- businessMapper.deleteById(business);
- }
- @Override
- public DsBusAllParam dsBusAll(MoneyAnalysisParam moneyAnalysisParam) {
- String orgId = moneyAnalysisParam.getOrgId();
- String time = moneyAnalysisParam.getTime();
- Date date ;
- if (StringUtils.isBlank(time)){
- date = DateUtil.beginOfMonth(new Date());
- }else {
- date = DateUtil.beginOfMonth( DateUtil.parse(time,"yyyy-MM"));
- }
- DateTime lastYear = DateUtil.offsetMonth(date, -12);
- QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
- wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId).between("create_time", date, DateUtil.endOfMonth(date));
- DsBusAllParam busAllParam = businessMapper.dsBusAll(wapper);
- wapper.clear();
- wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId)
- .between("create_time", lastYear, DateUtil.endOfMonth(lastYear));
- DsBusAllParam lastBusAllParam = businessMapper.dsBusAll(wapper);
- //今年
- wapper.clear();
- wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId)
- .between("create_time", DateUtil.beginOfYear(date), DateUtil.endOfYear(date));
- DsBusAllParam yearBusAllParam = businessMapper.dsBusAll(wapper);
- //去年
- wapper.clear();
- wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId)
- .between("create_time", DateUtil.beginOfYear(lastYear), DateUtil.endOfYear(lastYear));
- DsBusAllParam lastYearBusAllParam = businessMapper.dsBusAll(wapper);
- //计算同比
- busAllParam.setBalanceIsOn(getDouble(lastBusAllParam.getBalance()) <getDouble(busAllParam.getBalance()));
- busAllParam.setProfitIsOn(getDouble(lastBusAllParam.getProfit()) <getDouble(busAllParam.getProfit()));
- busAllParam.setTaxesIsOn(getDouble(lastBusAllParam.getTaxes()) <getDouble(busAllParam.getTaxes()));
- busAllParam.setBusinessIncomeIsOn(getDouble(lastBusAllParam.getBusinessIncome()) <getDouble(busAllParam.getBusinessIncome()));
- busAllParam.setBorrowPriceIsOn(getDouble(lastBusAllParam.getBorrowPrice()) <getDouble(busAllParam.getBorrowPrice()));
- busAllParam.setBalanceOnYear(calculateYOYChange(getDouble(busAllParam.getBalance()),getDouble(lastBusAllParam.getBalance())));
- busAllParam.setProfitOnYear(calculateYOYChange(getDouble(busAllParam.getProfit()),getDouble(lastBusAllParam.getProfit())));
- busAllParam.setTaxesOnYear(calculateYOYChange(getDouble(busAllParam.getTaxes()),getDouble(lastBusAllParam.getTaxes())));
- busAllParam.setBusinessIncomeOnYear(calculateYOYChange(getDouble(busAllParam.getBusinessIncome()),getDouble(lastBusAllParam.getBusinessIncome())));
- busAllParam.setBorrowPriceOnYear(calculateYOYChange(getDouble(busAllParam.getBorrowPrice()),getDouble(lastBusAllParam.getBorrowPrice())));
- //本年
- busAllParam.setBalanceYear(yearBusAllParam.getBalance());
- busAllParam.setProfitYear(yearBusAllParam.getProfit());
- busAllParam.setTaxesYear(yearBusAllParam.getTaxes());
- busAllParam.setBorrowPriceYear(yearBusAllParam.getBorrowPrice());
- busAllParam.setBusinessIncomeYear(yearBusAllParam.getBusinessIncome());
- //年同比
- busAllParam.setYearBusinessIncomeIsOn(getDouble(lastYearBusAllParam.getBusinessIncome()) <getDouble(yearBusAllParam.getBusinessIncome()));
- busAllParam.setYearProfitIsOn(getDouble(lastYearBusAllParam.getProfit()) <getDouble(yearBusAllParam.getProfit()));
- busAllParam.setYearTaxesIsOn(getDouble(lastYearBusAllParam.getTaxes()) <getDouble(yearBusAllParam.getTaxes()));
- busAllParam.setYearBusinessIncomeOnYear(calculateYOYChange(getDouble(yearBusAllParam.getBusinessIncome()),getDouble(lastYearBusAllParam.getBusinessIncome())));
- busAllParam.setYearProfitOnYear(calculateYOYChange(getDouble(yearBusAllParam.getProfit()),getDouble(lastYearBusAllParam.getProfit())));
- busAllParam.setYearTaxesOnYear(calculateYOYChange(getDouble(yearBusAllParam.getTaxes()),getDouble(lastYearBusAllParam.getTaxes())));
- return busAllParam;
- }
- private Double getDouble(String balance) {
- if (StringUtils.isBlank(balance)){
- return 0.0;
- }
- return Double.parseDouble(balance);
- // return balance;
- }
- @Override
- public List<DsBusAllParam> getDsBusYyJlYjDetail(DsBusParam dsBusParam) {
- String orgId = dsBusParam.getOrgId();
- Integer type = dsBusParam.getType();
- QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
- wapper.eq(StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("create_time").groupBy("ymonth");
- List<DsBusAllParam> busAllParams;
- if (ObjectUtil.isEmpty(type) || 0 == type){
- busAllParams = businessMapper.getDsBusYyJlYjDetailMonth(wapper);
- }else {
- busAllParams = businessMapper.getDsBusYyJlYjDetailYear(wapper);
- }
- return busAllParams;
- }
- @Override
- public List<DsBusAllParam> getBalanceDetail(DsBusParam dsBusParam) {
- String orgId = dsBusParam.getOrgId();
- Integer type = dsBusParam.getType();
- QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
- wapper.eq( StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("create_time").groupBy("ymonth");
- List<DsBusAllParam> busAllParams;
- if (ObjectUtil.isEmpty(type) || 0 == type){
- busAllParams = businessMapper.getBalanceDetailMonth(wapper);
- }else {
- busAllParams = businessMapper.getBalanceDetailYear(wapper);
- }
- for (int i = 0; i < busAllParams.size() -1 ; i++) {
- DsBusAllParam lastDsBus = busAllParams.get(i);
- DsBusAllParam dsBusAllParam = busAllParams.get(i + 1);
- dsBusAllParam.setBalanceOnYear(calculateYOYZiJinYvE(getDouble(dsBusAllParam.getBalance()),getDouble(lastDsBus.getBalance()) ));
- dsBusAllParam.setBalanceIsOn(getDouble(lastDsBus.getBalance()) <getDouble(dsBusAllParam.getBalance()));
- }
- if (ObjectUtil.isNotEmpty(busAllParams)){
- DsBusAllParam dsBusAllParam = busAllParams.get(0);
- dsBusAllParam.setBalanceIsOn(true);
- dsBusAllParam.setBalanceOnYear("0%");
- }
- return busAllParams;
- }
- @Override
- public List<DsBusAllParam> getBorrowDetail(DsBusParam dsBusParam) {
- String orgId = dsBusParam.getOrgId();
- Integer type = dsBusParam.getType();
- QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
- wapper.eq( StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("borrow_time").groupBy("ymonth");
- List<DsBusAllParam> busAllParams;
- if (ObjectUtil.isEmpty(type) || 0 == type){
- busAllParams = businessMapper.getBorrowDetailMonth(wapper);
- }else {
- busAllParams = businessMapper.getBorrowDetailYear(wapper);
- }
- return busAllParams;
- }
- @Override
- public List<DsBusSaleMoneyParam> getMoneyDetail(DsBusParam dsBusParam) {
- String orgId = dsBusParam.getOrgId();
- Integer type = dsBusParam.getType();
- QueryWrapper<MoneyBusiness> wapper = new QueryWrapper<MoneyBusiness>();
- wapper.eq( StringUtils.isNotBlank(orgId),"org_id", orgId).orderByAsc("create_time").groupBy("ymonth");
- List<DsBusSaleMoneyParam> busAllParams;
- if (ObjectUtil.isEmpty(type) || 0 == type){
- busAllParams = moneyCostMapper.getMoneyDetailMonth(wapper);
- }else {
- busAllParams = moneyCostMapper.getMoneyDetailYear(wapper);
- }
- return busAllParams;
- }
- @Override
- public Page<MoneyBusiness> listBusines(MoneyOverduePageParam pageParam) {
- QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq(StringUtils.isNotBlank(pageParam.getOrgId()), "org_id", pageParam.getOrgId()).ge("taxes" ,0);
- Page<MoneyBusiness> page = new Page<MoneyBusiness>(pageParam.getPageNum(), pageParam.getPageSize());
- return this.page(page, queryWrapper);
- }
- @Override
- public Page<MoneyBusiness> listBalance(MoneyParam pageParam) {
- QueryWrapper<MoneyBusiness> queryWrapper = new QueryWrapper<>();
- queryWrapper.eq(StringUtils.isNotBlank(pageParam.getOrgId()), "org_id", pageParam.getOrgId()).ge("balance" ,0);
- Page<MoneyBusiness> page = new Page<MoneyBusiness>(pageParam.getPageNum(), pageParam.getPageSize());
- return this.page(page, queryWrapper);
- }
- public static String calculateYOYChange(double currentValue, double previousValue) {
- // 处理零值情况
- if (previousValue == 0) {
- return "0%";
- }
- // 计算同比变化率
- double percentageChange = (Math.abs((currentValue - previousValue) / previousValue)) * 100;
- if (percentageChange >500){
- return "100%";
- }
- return String.format("%.2f%%", percentageChange);
- }
- public static String calculateYOYZiJinYvE(double currentValue, double previousValue) {
- // 处理零值情况
- if (previousValue == 0) {
- return "0";
- }
- // 计算同比变化率
- double percentageChange = ((currentValue - previousValue) /previousValue) * 100;
- if (percentageChange >200){
- return "100";
- }
- return String.format("%.2f", percentageChange);
- }
- public static String calculateWanChenLv(double currentValue, double previousValue) {
- // 处理零值情况
- if (previousValue == 0) {
- return "100";
- }
- // 计算同比变化率
- double percentageChange = (currentValue /previousValue) * 100;
- if (percentageChange >200){
- return "100";
- }
- return String.format("%.2f", percentageChange);
- }
- }
|