package io.as.modules.daycard.controller; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import io.as.common.exception.ASException; import io.as.common.utils.PageUtils; import io.as.common.utils.Query; import io.as.common.utils.R; import io.as.modules.daycard.constants.DayCardConstants; import io.as.modules.daycard.dao.ProfitSharingDetailDao; import io.as.modules.daycard.dao.ProfitSharingOrderDao; import io.as.modules.daycard.entity.ProfitSharingDetailEntity; import io.as.modules.daycard.entity.ProfitSharingOrderEntity; import io.as.modules.sys.dao.SysOrgDao; import io.as.modules.sys.entity.SysOrgEntity; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.math.BigDecimal; import java.util.*; /** * 分账记录查询 Controller */ @Api(tags = "分账记录查询") @RestController @RequestMapping("/manage/profit-sharing") public class ProfitSharingQueryController { @Autowired private ProfitSharingOrderDao profitSharingOrderDao; @Autowired private ProfitSharingDetailDao profitSharingDetailDao; @Autowired private SysOrgDao sysOrgDao; /** * 分页查询分账单(支持 orderNo/status 筛选),返回详情列表 */ @ApiOperation("分页查询分账单") @GetMapping("/list") public R list(@RequestParam Map params) { IPage page = new Query().getPage(params, "created_at", false); QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("delete_flag", DayCardConstants.DELETE_FLAG_UNDEL); String orderNo = (String) params.get("orderNo"); if (StringUtils.isNotBlank(orderNo)) { wrapper.eq("pay_order_no", orderNo); } String status = (String) params.get("status"); if (StringUtils.isNotBlank(status)) { wrapper.eq("status", status); } String outOrderNo = (String) params.get("outOrderNo"); if (StringUtils.isNotBlank(outOrderNo)) { wrapper.eq("out_order_no", outOrderNo); } IPage result = profitSharingOrderDao.selectPage(page, wrapper); // 填充明细列表 for (ProfitSharingOrderEntity order : result.getRecords()) { QueryWrapper detailWrapper = new QueryWrapper<>(); detailWrapper.eq("sharing_order_id", order.getId()); List details = profitSharingDetailDao.selectList(detailWrapper); order.setDetails(details); } Map data = new HashMap<>(); data.put("page", new PageUtils(result)); return R.dataSuccess(data); } /** * 分账单详情 + details 子表 */ @ApiOperation("分账单详情") @GetMapping("/info/{id}") public R info(@PathVariable("id") Long id) { ProfitSharingOrderEntity order = profitSharingOrderDao.selectById(id); if (order == null) { throw new ASException("分账单不存在"); } QueryWrapper detailWrapper = new QueryWrapper<>(); detailWrapper.eq("sharing_order_id", id); List details = profitSharingDetailDao.selectList(detailWrapper); order.setDetails(details); Map data = new HashMap<>(); data.put("order", order); return R.dataSuccess(data); } /** * 统计(参数 startDate, endDate, groupBy) * 返回: { summary: {totalCount, totalAmount, successCount, successAmount}, groups: [{orgId, orgName, orgType, count, amount}] } */ @ApiOperation("分账统计") @GetMapping("/stats") public R stats(@RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate, @RequestParam(value = "groupBy", required = false) String groupBy) { QueryWrapper wrapper = new QueryWrapper<>(); wrapper.eq("delete_flag", DayCardConstants.DELETE_FLAG_UNDEL); if (StringUtils.isNotBlank(startDate)) { wrapper.ge("created_at", startDate + " 00:00:00"); } if (StringUtils.isNotBlank(endDate)) { wrapper.le("created_at", endDate + " 23:59:59"); } List orders = profitSharingOrderDao.selectList(wrapper); // 汇总 int totalCount = orders.size(); int totalAmount = 0; int successCount = 0; int successAmount = 0; for (ProfitSharingOrderEntity order : orders) { if (order.getTotalAmount() != null) { totalAmount += order.getTotalAmount(); } if (DayCardConstants.SETTLE_STATUS_SUCCESS.equals(order.getStatus())) { successCount++; if (order.getTotalAmount() != null) { successAmount += order.getTotalAmount(); } } } Map summary = new HashMap<>(); summary.put("totalCount", totalCount); summary.put("totalAmount", totalAmount); summary.put("successCount", successCount); summary.put("successAmount", successAmount); // 分组统计 List> groups = new ArrayList<>(); if (StringUtils.isNotBlank(groupBy) && "org".equals(groupBy)) { // 按组织分组:查询所有明细,按 orgId 聚合 Map> orgMap = new HashMap<>(); for (ProfitSharingOrderEntity order : orders) { QueryWrapper detailWrapper = new QueryWrapper<>(); detailWrapper.eq("sharing_order_id", order.getId()); List details = profitSharingDetailDao.selectList(detailWrapper); for (ProfitSharingDetailEntity detail : details) { Long orgId = detail.getOrgId(); if (orgId == null) { continue; } Map group = orgMap.get(orgId); if (group == null) { group = new HashMap<>(); group.put("orgId", orgId); group.put("count", 0); group.put("amount", 0); group.put("orgRole", detail.getOrgRole()); // 查询组织名称 SysOrgEntity org = sysOrgDao.selectById(orgId); if (org != null) { group.put("orgName", org.getName()); group.put("orgType", org.getType()); } orgMap.put(orgId, group); } group.put("count", (Integer) group.get("count") + 1); if (detail.getAmount() != null) { group.put("amount", (Integer) group.get("amount") + detail.getAmount()); } } } groups.addAll(orgMap.values()); } Map result = new HashMap<>(); result.put("summary", summary); result.put("groups", groups); return R.dataSuccess(result); } }