package com.ydtech.modules.admin.service.impl; 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.DeptBalanceMapper; import com.ydtech.modules.admin.dao.SysPartnerMapper; import com.ydtech.modules.admin.dao.SysRelationPersonMapper; import com.ydtech.modules.admin.model.SysAgencyLeader; import com.ydtech.modules.admin.model.SysPartner; import com.ydtech.modules.admin.model.SysRelationPerson; import com.ydtech.modules.admin.model.SysUser; 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.DeptBalanceVo; import com.ydtech.modules.admin.model.vo.PartnerBalanceVo; import com.ydtech.modules.admin.model.vo.SysAmountAuditingVo; import com.ydtech.modules.admin.model.vo.SysUserVO; import com.ydtech.modules.admin.service.*; import com.ydtech.modules.base.model.vo.PageVo; import com.ydtech.modules.order.service.InsOrdersService; import com.ydtech.modules.protocols.service.InsFeeOrderNewService; import com.ydtech.utils.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; 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; /** * @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()); IPage sysUserListPage = deptBalanceMapper.queryDeptLeaderStatistics(page, deptBalanceVo); return HttpResult.ok(sysUserListPage); } 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()); SysUser sysUserQuery = new SysUser(); sysUserQuery.setDeptId(deptBalanceVo.getDeptId()); List listByDeptIdUser = this.sysUserService.findListByDeptIdNew(sysUserQuery); List userListDept = listByDeptIdUser.stream().map(SysUserVO::getId).collect(Collectors.toList()); if (userListDept.isEmpty()) { userListDept.add("0"); } deptBalanceVo.setUserIdList(userListDept); 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 = sysAgencyLeaderService.lambdaQuery().eq(SysAgencyLeader::getSuggestId, item.getUserId()).list(); // 机构负责人 for (SysAgencyLeader itemLeader : deptLeader) { String deptId = itemLeader.getDeptId(); if (StringUtils.isNotEmpty(deptId)) { String proportional = item.getProportional(); HashMap orderAmount = deptBalanceMapper.getByDeptId(deptId); if (orderAmount != null && !orderAmount.isEmpty()) { allTotalIncome = this.calculateTotal(orderAmount.get("totalIncome").multiply(new BigDecimal(proportional).divide(new BigDecimal(100))), allTotalIncome, null); // 总保费 allDeptManagementFee = this.calculateTotal(orderAmount.get("deptManagementFee"), allDeptManagementFee, null);//机构管理费 } SysUser sysUser = new SysUser(); sysUser.setDeptId(deptId); // 机构下的userId List listByDeptId = sysUserService.findListByDeptId(sysUser); List institutionId = listByDeptId.stream().map(SysUserVO::getId).collect(Collectors.toList()); // 已提现 SysAmountAuditingVo sysAmountAuditingVo = new SysAmountAuditingVo(); sysAmountAuditingVo.setUserIdList(institutionId); sysAmountAuditingVo.setAuditingStatus("3"); BigDecimal withdrawn = sysAmountAuditingService.querySumAmount(sysAmountAuditingVo); allWithdrawn = this.calculateTotal(withdrawn, allWithdrawn, null); // 未提现 sysAmountAuditingVo.setAuditingStatus("2"); BigDecimal notWithdrawn = sysAmountAuditingService.querySumAmount(sysAmountAuditingVo); allNotWithdrawn = this.calculateTotal(notWithdrawn, allNotWithdrawn, null); } } item.setTotalIncome(allTotalIncome); item.setDeptManagementFee(allDeptManagementFee); 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); // 总金额 if (orderAmount != null && !orderAmount.isEmpty()) { record.setTotalIncome(orderAmount.get("totalIncome")); } // 已提现 BigDecimal ysTXAmount= insFeeOrderNewService.findMyAmountYTXmount(record.getUserId(),"3"); // 未提现 MySumAmountQueryDto mySumAmountQueryDto = new MySumAmountQueryDto(); mySumAmountQueryDto.setAuditstatus("1"); SysUserAccount sysUserAccountEntity = sysUserAccountService.getById(record.getUserId()); BigDecimal ssKTAmount =BigDecimal.ZERO; BigDecimal txzTXAmount= insFeeOrderNewService.findMyAmountYTXmount(mySumAmountQueryDto.getUserId(),"2"); 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); } } return sysUserListPage; } }