package com.ydtech.modules.admin.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.ydtech.core.page.HttpResult; import com.ydtech.modules.admin.dao.*; import com.ydtech.modules.admin.model.*; import com.ydtech.modules.admin.model.dto.DeptBalanceDto; import com.ydtech.modules.admin.model.dto.MySumAmountQueryDto; import com.ydtech.modules.admin.model.dto.SysPartnerConsoleDto; import com.ydtech.modules.admin.model.po.SysUserAccount; import com.ydtech.modules.admin.model.vo.*; import com.ydtech.modules.admin.service.*; import com.ydtech.modules.base.model.vo.PageVo; import com.ydtech.modules.order.entity.InsAreaCompany; import com.ydtech.modules.order.entity.InsOrders; import com.ydtech.modules.order.service.InsOrdersService; import com.ydtech.modules.protocols.service.InsFeeOrderNewService; import com.ydtech.utils.StringUtils; import io.swagger.annotations.ApiModelProperty; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.math.BigDecimal; import java.math.RoundingMode; import java.text.SimpleDateFormat; import java.util.*; import java.util.stream.Collectors; @Service public class DeptBalanceServiceImpl implements DeptBalanceService { @Autowired private SysAgencyLeaderService sysAgencyLeaderService; @Autowired private DeptBalanceMapper deptBalanceMapper; @Autowired private SysRelationPersonMapper sysRelationPersonMapper; @Autowired private SysPartnerMapper sysPartnerMapper; @Autowired private InsOrdersService insOrdersService; @Autowired private SysUserService sysUserService; @Autowired private SysAmountAuditingService sysAmountAuditingService; @Autowired private InsFeeOrderNewService insFeeOrderNewService; @Autowired private SysUserAccountService sysUserAccountService; @Resource private SysAgencyLeaderMapper sysAgencyLeaderMapper; @Autowired private SysAgencyFeeApprovalMapper sysAgencyFeeApprovalMapper; /** * @version * @author: whp * @Date: 2024/08/14 9:04 * @Description: 机构负责人统计 */ @Override public HttpResult> queryDeptLeaderStatistics(DeptBalanceVo deptBalanceVo) { try { PageVo pageVo = new PageVo<>(); Page page = new Page(deptBalanceVo.getPageNum(), deptBalanceVo.getPageSize()); pageVo.setPageNum(deptBalanceVo.getPageNum()); pageVo.setPageSize(deptBalanceVo.getPageSize()); //通过机构id 用户等条件机构负责人集合 IPage sysLeaderPage = sysAgencyLeaderMapper.queryPage(page, deptBalanceVo); List records = sysLeaderPage.getRecords(); List newList =new ArrayList<>(); if (records !=null && records.size()>0) { for (DeptBalanceDto itemLeader : records) { BigDecimal allTotalIncome = BigDecimal.ZERO; // 总保费 BigDecimal allDeptManagementFee = BigDecimal.ZERO;//机构管理费 BigDecimal allWithdrawn = BigDecimal.ZERO;//已体现 BigDecimal allNotWithdrawn = BigDecimal.ZERO;//未提现 String deptId = itemLeader.getDeptId(); String proportional = itemLeader.getProportional(); Date updateTime = itemLeader.getUpdateTime(); Date createTime = itemLeader.getCreateTime(); String dateStr = ""; if (updateTime != null) { dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(updateTime); } else { dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createTime); } HashMap orderAmount = deptBalanceMapper.getByDeptId(deptId, dateStr,deptBalanceVo.getBeginDate(),deptBalanceVo.getEndDate()); if (orderAmount != null && !orderAmount.isEmpty()) { BigDecimal totalIncome = orderAmount.get("totalIncome")==null ? BigDecimal.ZERO : orderAmount.get("totalIncome"); allTotalIncome = totalIncome.multiply(new BigDecimal(proportional)) .divide(new BigDecimal("100")).setScale(2, RoundingMode.DOWN); allDeptManagementFee = this.calculateTotal(orderAmount.get("deptManagementFee"), allDeptManagementFee, null); } // 填充 已提现抽成费用和可提现抽成费用 List sysAgencyFeeApprovals = sysAgencyFeeApprovalMapper.selectList(new LambdaQueryWrapper() .eq(SysAgencyFeeApproval::getUserId, itemLeader.getUserId()) .eq(SysAgencyFeeApproval::getStatus, 1)); if (sysAgencyFeeApprovals.size() > 0) { for (SysAgencyFeeApproval sysAgencyFeeApproval : sysAgencyFeeApprovals) { allWithdrawn = allWithdrawn.add(sysAgencyFeeApproval.getHistoryFee()); } } //可提现 = 总抽成-已提现 allNotWithdrawn = allTotalIncome.subtract(allWithdrawn); itemLeader.setDeptManagementFee(allTotalIncome); itemLeader.setTotalIncome(allDeptManagementFee); itemLeader.setWithdrawn(allWithdrawn); itemLeader.setNotWithdrawn(allNotWithdrawn); newList.add(itemLeader); } } sysLeaderPage.setRecords(newList); return HttpResult.ok(sysLeaderPage); } catch (Exception e) { e.printStackTrace(); return HttpResult.error("查询异常"); } } /** * @version * @author: whp * @Date: 2024/08/23 9:04 * @Description: 合伙人统计 */ @Override public HttpResult> queryPartnerStatistics(DeptBalanceVo deptBalanceVo) { try { PageVo pageVo = new PageVo<>(); Page page = new Page(deptBalanceVo.getPageNum(), deptBalanceVo.getPageSize()); pageVo.setPageNum(deptBalanceVo.getPageNum()); pageVo.setPageSize(deptBalanceVo.getPageSize()); //通过机构id 用户等条件查询合伙人集合 IPage sysPartnersPage = sysPartnerMapper.queryPage(page, deptBalanceVo); List sysPartners = sysPartnersPage.getRecords(); for (SysPartnerConsoleDto item : sysPartners) { BigDecimal allTotalIncome = BigDecimal.ZERO; // 总保费 BigDecimal allDeptManagementFee = BigDecimal.ZERO;//机构管理费 BigDecimal allWithdrawn = BigDecimal.ZERO;//已体现 BigDecimal allNotWithdrawn = BigDecimal.ZERO;//未提现 List deptLeader = sysRelationPersonMapper.selectListByLeaderId(item.getUserId(),1); // 机构负责人 for (SysAgencyLeader itemLeader : deptLeader) { String deptId = itemLeader.getDeptId(); if (StringUtils.isNotEmpty(deptId)) { String proportional = item.getProportional(); Date updateTime = itemLeader.getUpdateTime(); Date createTime = itemLeader.getCreateTime(); String dateStr=""; if(updateTime!=null){ dateStr= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(updateTime); }else{ dateStr= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(createTime); } HashMap orderAmount = deptBalanceMapper.getByDeptId(deptId,dateStr,deptBalanceVo.getBeginDate(),deptBalanceVo.getEndDate()); if (orderAmount != null && !orderAmount.isEmpty()) { BigDecimal totalIncome = orderAmount.get("totalIncome")==null ? BigDecimal.ZERO : orderAmount.get("totalIncome"); allTotalIncome = totalIncome.multiply(new BigDecimal(proportional)) .divide(new BigDecimal("100")).setScale(2, RoundingMode.DOWN); allDeptManagementFee = this.calculateTotal(orderAmount.get("deptManagementFee"), allDeptManagementFee, null); } // 填充 已提现抽成费用和可提现抽成费用 List sysAgencyFeeApprovals = sysAgencyFeeApprovalMapper.selectList(new LambdaQueryWrapper() .eq(SysAgencyFeeApproval::getUserId, item.getUserId()) .eq(SysAgencyFeeApproval::getStatus, 1)); if (!sysAgencyFeeApprovals.isEmpty()) { for (SysAgencyFeeApproval sysAgencyFeeApproval : sysAgencyFeeApprovals) { allWithdrawn = allWithdrawn.add(sysAgencyFeeApproval.getHistoryFee()); } } //可提现 = 总抽成-已提现 allNotWithdrawn = allTotalIncome.subtract(allWithdrawn); } } item.setTotalIncome(allDeptManagementFee); item.setDeptManagementFee(allTotalIncome); item.setWithdrawn(allWithdrawn); item.setNotWithdrawn(allNotWithdrawn); } return HttpResult.ok(sysPartnersPage); } catch (Exception e) { e.printStackTrace(); return HttpResult.error("查询异常"); } } private List recursion(String userId) { QueryWrapper objectQueryWrapper = new QueryWrapper<>(); // 合伙人 QueryWrapper sysRelationPerson = objectQueryWrapper.eq("suggest_id", userId) .eq("type", "0"); List sysRelationPeople = this.sysRelationPersonMapper.selectList(sysRelationPerson); List collect = sysRelationPeople.stream().map(SysRelationPerson::getAgencyLeaderId).collect(Collectors.toList()); collect.add(userId); return collect; } private BigDecimal calculateTotal(BigDecimal value1, BigDecimal value2, BigDecimal value3) { BigDecimal total = BigDecimal.ZERO; if (value1 != null) { total = total.add(value1); } if (value2 != null) { total = total.add(value2); } if (value3 != null) { total = total.add(value3); } return total; } /** * @version * @author: whp * @Date: 2024/08/26 * @Description: 机构树形 */ @Override public List getTreeDeptUser(PartnerBalanceVo deptBalanceVo) { // 所有最上级合伙人 List result = sysPartnerMapper.getTreeDeptUser(deptBalanceVo); // 合伙人的下级 for (SysPartnerConsoleDto item : result) { this.treeRecursion(item); } return result; } private void treeRecursion(SysPartnerConsoleDto item) { String userId = item.getUserId(); if (StringUtils.isNotEmpty(userId)) { // 合伙人 SysPartnerConsoleDto sysPartnerConsoleDto = new SysPartnerConsoleDto(); sysPartnerConsoleDto.setUserId(userId); sysPartnerConsoleDto.setType("0"); List sysPartnerList = this.sysRelationPersonMapper.getBySuggestId(sysPartnerConsoleDto); item.setChildrenList(sysPartnerList); // 机构负责人 sysPartnerConsoleDto.setType("1"); List sysHeadList = this.sysRelationPersonMapper.getBySuggestId(sysPartnerConsoleDto); List childrenList = item.getChildrenList(); childrenList.addAll(sysHeadList); if (!sysPartnerList.isEmpty()) { for (SysPartnerConsoleDto sysPartnerItem : sysPartnerList) { this.treeRecursion(sysPartnerItem); } } } } /** * @version * @author: whp * @Date: 2024/08/26 * @Description: 钱包代理人 */ @Override public IPage getWalletAgent(DeptBalanceVo deptBalanceVo) { PageVo pageVo = new PageVo<>(); Page page = new Page(deptBalanceVo.getPageNum(), deptBalanceVo.getPageSize()); pageVo.setPageNum(deptBalanceVo.getPageNum()); pageVo.setPageSize(deptBalanceVo.getPageSize()); IPage sysUserListPage = deptBalanceMapper.getWalletAgent(page, deptBalanceVo); List records = sysUserListPage.getRecords(); if (!records.isEmpty()) { for (SysPartnerConsoleDto record : records) { String userId = record.getUserId(); HashMap orderAmount = deptBalanceMapper.getWalletAgentAmount(userId,deptBalanceVo.getBeginDate(),deptBalanceVo.getEndDate()); // 总金额 if (orderAmount != null && !orderAmount.isEmpty()) { record.setTotalIncome(orderAmount.get("totalIncome")); } // 已提现 BigDecimal ysTXAmount= insFeeOrderNewService.findMyAmountYTXmountByDate(record.getUserId(),"3",deptBalanceVo.getBeginDate(),deptBalanceVo.getEndDate()); // 未提现 MySumAmountQueryDto mySumAmountQueryDto = new MySumAmountQueryDto(); mySumAmountQueryDto.setAuditstatus("1"); SysUserAccount sysUserAccountEntity = sysUserAccountService.getById(record.getUserId()); BigDecimal ssKTAmount =BigDecimal.ZERO; BigDecimal txzTXAmount= insFeeOrderNewService.findMyAmountYTXmountByDate(mySumAmountQueryDto.getUserId(),"2",deptBalanceVo.getBeginDate(),deptBalanceVo.getEndDate()); if(sysUserAccountEntity!=null){ ssKTAmount = sysUserAccountEntity.getSumAmount()==null?BigDecimal.ZERO: sysUserAccountEntity.getSumAmount(); } BigDecimal sumAmount=ysTXAmount.add(ssKTAmount).add(txzTXAmount); record.setWithdrawn(ysTXAmount); record.setNotWithdrawn(ssKTAmount); record.setTotalIncome(sumAmount); record.setWithdrawingAmount(txzTXAmount); } } return sysUserListPage; } }