UserBillingController.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. package com.ruoyi.app.pay;
  2. import cn.hutool.core.convert.Convert;
  3. import com.alibaba.fastjson.JSONArray;
  4. import com.alibaba.fastjson.JSONObject;
  5. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.ruoyi.app.order.ZaloPayCommonService;
  10. import com.ruoyi.app.pay.dto.QsBillingDto;
  11. import com.ruoyi.common.core.domain.entity.SysDictData;
  12. import com.ruoyi.common.utils.DictUtils;
  13. import com.ruoyi.system.domain.vo.ShBillingDto;
  14. import com.ruoyi.app.utils.DateUtil;
  15. import com.ruoyi.app.utils.zaloPay.ZaloPay;
  16. import com.ruoyi.app.utils.zaloPay.ZaloPayConfig;
  17. import com.ruoyi.common.annotation.Anonymous;
  18. import com.ruoyi.common.annotation.Log;
  19. import com.ruoyi.common.core.controller.BaseController;
  20. import com.ruoyi.common.core.domain.AjaxResult;
  21. import com.ruoyi.common.core.page.TableDataInfo;
  22. import com.ruoyi.common.enums.BusinessType;
  23. import com.ruoyi.common.utils.MessageUtils;
  24. import com.ruoyi.common.utils.poi.ExcelUtil;
  25. import com.ruoyi.system.domain.*;
  26. import com.ruoyi.system.domain.vo.BillDTO;
  27. import com.ruoyi.system.mapper.UserBillingMapper;
  28. import com.ruoyi.system.domain.flash.FlashDeliveryOrder;
  29. import com.ruoyi.system.mapper.flash.FlashDeliveryOrderMapper;
  30. import com.ruoyi.system.service.*;
  31. import com.ruoyi.system.utils.Auth;
  32. import com.ruoyi.system.utils.JwtUtil;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.security.access.prepost.PreAuthorize;
  35. import org.springframework.web.bind.annotation.*;
  36. import jakarta.servlet.http.HttpServletResponse;
  37. import java.math.BigDecimal;
  38. import java.util.ArrayList;
  39. import java.util.Arrays;
  40. import java.util.List;
  41. import java.util.stream.Collectors;
  42. /**
  43. * BillingController
  44. *
  45. * @author ruoyi
  46. * @date 2023-09-06
  47. */
  48. @RestController
  49. @RequestMapping("/Billing/billing")
  50. public class UserBillingController extends BaseController {
  51. @Autowired
  52. private IUserBillingService userBillingService;
  53. @Autowired
  54. private UserBillingMapper userBillingMapper;
  55. @Autowired
  56. private IPosOrderService posOrderService;
  57. @Autowired
  58. private IInfoUserService infoUserService;
  59. @Autowired
  60. private ZaloPayCommonService zaloPayCommonService;
  61. @Autowired
  62. private ZaloPayConfig zaloPayConfig;
  63. @Autowired
  64. private ZaloPay zaloPay;
  65. @Autowired
  66. private IPosOrderRatingService posOrderRatingService;
  67. @Autowired
  68. private ICommissionRatesService commissionRatesService;
  69. @Autowired
  70. private IPosStoreService posStoreService;
  71. @Autowired
  72. private FlashDeliveryOrderMapper flashDeliveryOrderMapper;
  73. /**
  74. * 商家端账单列表
  75. */
  76. @Anonymous
  77. @Auth
  78. @GetMapping("/storeBillingList")
  79. public AjaxResult storeBillingList(@RequestHeader String token,
  80. @RequestParam Integer pageNum,
  81. @RequestParam Integer pageSize,
  82. @RequestParam(required = false) Long mdId,
  83. @RequestParam(required = false) String type,
  84. @RequestParam(required = false) String startDate,
  85. @RequestParam(required = false) String endDate) {
  86. JwtUtil jwtUtil = new JwtUtil();
  87. Long userId = Long.valueOf(jwtUtil.getusid(token));
  88. InfoUser user = infoUserService.getById(userId);
  89. if (user == null) {
  90. return error(MessageUtils.message("no.user.not.exist"));
  91. }
  92. QueryWrapper<UserBilling> wrapper = new QueryWrapper<>();
  93. String userType = user.getUserType();
  94. if ("1".equals(userType) || "3".equals(userType)) {
  95. // 商家/夜市用户:查自己名下所有门店的账单
  96. wrapper.eq("user_id", userId);
  97. if (mdId != null) {
  98. wrapper.eq("md_id", mdId);
  99. }
  100. } else if ("4".equals(userType)) {
  101. // 摊位主/门店:仅查自己关联门店的账单
  102. Long storeId = user.getStoreId();
  103. if (storeId == null) {
  104. return error("未关联门店");
  105. }
  106. wrapper.eq("md_id", storeId);
  107. } else {
  108. return error("无权限访问");
  109. }
  110. if (type != null && !type.isEmpty()) {
  111. wrapper.eq("type", type);
  112. }
  113. if (startDate != null && !startDate.isEmpty()) {
  114. wrapper.apply("date_format(cretim,'%Y-%m-%d %H:%i:%s') >= date_format('" + startDate + " 00:00:00','%Y-%m-%d %H:%i:%s')");
  115. }
  116. if (endDate != null && !endDate.isEmpty()) {
  117. wrapper.apply("date_format(cretim,'%Y-%m-%d %H:%i:%s') <= date_format('" + endDate + " 23:59:59','%Y-%m-%d %H:%i:%s')");
  118. }
  119. wrapper.orderByDesc("id");
  120. Page<UserBilling> page = new Page<>(pageNum, pageSize);
  121. IPage<UserBilling> pageResult = userBillingService.page(page, wrapper);
  122. // 填充门店名称
  123. List<UserBilling> records = pageResult.getRecords();
  124. fillStoreName(records);
  125. JSONObject org = new JSONObject();
  126. org.put("list", records);
  127. org.put("total", pageResult.getTotal());
  128. org.put("pageNum", pageResult.getCurrent());
  129. org.put("pageSize", pageResult.getSize());
  130. return success(MessageUtils.message("no.success"), org);
  131. }
  132. /**
  133. * 商家端账单汇总
  134. */
  135. @Anonymous
  136. @Auth
  137. @GetMapping("/storeBillingSummary")
  138. public AjaxResult storeBillingSummary(@RequestHeader String token,
  139. @RequestParam(required = false) Long mdId,
  140. @RequestParam(required = false) String type,
  141. @RequestParam(required = false) String startDate,
  142. @RequestParam(required = false) String endDate) {
  143. JwtUtil jwtUtil = new JwtUtil();
  144. Long userId = Long.valueOf(jwtUtil.getusid(token));
  145. InfoUser user = infoUserService.getById(userId);
  146. if (user == null) {
  147. return error(MessageUtils.message("no.user.not.exist"));
  148. }
  149. QueryWrapper<UserBilling> wrapper = new QueryWrapper<>();
  150. String userType = user.getUserType();
  151. if ("1".equals(userType) || "3".equals(userType)) {
  152. wrapper.eq("user_id", userId);
  153. if (mdId != null) {
  154. wrapper.eq("md_id", mdId);
  155. }
  156. } else if ("4".equals(userType)) {
  157. Long storeId = user.getStoreId();
  158. if (storeId == null) {
  159. return error("未关联门店");
  160. }
  161. wrapper.eq("md_id", storeId);
  162. } else {
  163. return error("无权限访问");
  164. }
  165. if (type != null && !type.isEmpty()) {
  166. wrapper.eq("type", type);
  167. }
  168. if (startDate != null && !startDate.isEmpty()) {
  169. wrapper.apply("date_format(cretim,'%Y-%m-%d %H:%i:%s') >= date_format('" + startDate + " 00:00:00','%Y-%m-%d %H:%i:%s')");
  170. }
  171. if (endDate != null && !endDate.isEmpty()) {
  172. wrapper.apply("date_format(cretim,'%Y-%m-%d %H:%i:%s') <= date_format('" + endDate + " 23:59:59','%Y-%m-%d %H:%i:%s')");
  173. }
  174. wrapper.select(
  175. "IFNULL(SUM(amount),0) as amount",
  176. "IFNULL(SUM(divvy),0) as divvy"
  177. );
  178. UserBilling summary = userBillingService.getOne(wrapper);
  179. double totalAmount = summary != null && summary.getAmount() != null ? summary.getAmount() : 0;
  180. double totalDivvy = summary != null && summary.getDivvy() != null ? summary.getDivvy() : 0;
  181. double netIncome = totalAmount - totalDivvy;
  182. JSONObject org = new JSONObject();
  183. org.put("totalAmount", totalAmount);
  184. org.put("totalDivvy", totalDivvy);
  185. org.put("netIncome", netIncome);
  186. return success(MessageUtils.message("no.success"), org);
  187. }
  188. /**
  189. * 填充门店名称
  190. */
  191. private void fillStoreName(List<UserBilling> list) {
  192. if (list == null || list.isEmpty()) return;
  193. List<Integer> mdIds = list.stream()
  194. .map(UserBilling::getMdId)
  195. .filter(id -> id != null)
  196. .map(Long::intValue)
  197. .distinct()
  198. .collect(Collectors.toList());
  199. if (mdIds.isEmpty()) return;
  200. List<PosStore> stores = posStoreService.listByIds(mdIds);
  201. list.forEach(billing -> {
  202. if (billing.getMdId() != null) {
  203. stores.stream()
  204. .filter(s -> Long.valueOf(s.getId()).equals(billing.getMdId()))
  205. .findFirst()
  206. .ifPresent(s -> billing.setMdName(s.getPosName()));
  207. }
  208. });
  209. }
  210. /**
  211. * 商家日账单数据
  212. *
  213. * @param token
  214. * @param page
  215. * @param size
  216. * @param riqi
  217. * @return
  218. */
  219. @Anonymous
  220. @Auth
  221. @GetMapping("/getzddaylist")
  222. public AjaxResult getzddaylist(@RequestHeader String token,
  223. @RequestParam Long page,
  224. @RequestParam Long size,
  225. @RequestParam String riqi,
  226. @RequestParam(required = false) Boolean isCancel,
  227. @RequestParam(required = false) Long mdId
  228. ) {
  229. JwtUtil jwtUtil = new JwtUtil();
  230. Long id = Long.valueOf(jwtUtil.getusid(token));
  231. InfoUser user = infoUserService.getById(id);
  232. //获取日的取消订单列表
  233. if (isCancel != null && isCancel) {
  234. return getDayCancleOrderList(page, size, riqi, user);
  235. }
  236. List<UserBilling> list = userBillingMapper.getdaylist(id, riqi, mdId, (page - 1) * size, size);
  237. setYhInfo(list);
  238. BillDTO sum = userBillingMapper.getdaysum(id, riqi, mdId);
  239. JSONObject org = new JSONObject();
  240. org.put("shul", sum.getSul());
  241. org.put("sum", sum.getShouru());
  242. org.put("commission", sum.getCommission());
  243. org.put("tax", sum.getTax());
  244. org.put("behalfAmount", sum.getBehalfAmount());
  245. LambdaQueryWrapper<PosOrder> queryWrapper = new LambdaQueryWrapper<PosOrder>()
  246. .in(PosOrder::getState, new ArrayList<Long>(Arrays.asList(9L, 10L)))
  247. .apply("(sh_id=" + id + " or qs_id=" + id + ") and date_format (cretim,'%Y-%m-%d %H:%i:%s') >= date_format ('" + riqi + " 00:00:00','%Y-%m-%d %H:%i:%s') and date_format (cretim,'%Y-%m-%d %H:%i:%s') <= date_format ('" + riqi + " 23:59:59','%Y-%m-%d %H:%i:%s')");
  248. // 取消总数 ,作废、客服介入的数据
  249. long cancleCount = posOrderService.count(queryWrapper);
  250. org.put("cancelCount", cancleCount);
  251. if ("2".equals(user.getUserType())) {
  252. setQsList(list, org, id);
  253. } else {
  254. ShBillingDto shBillingDto = userBillingMapper.getShDaysum(id, riqi, mdId);
  255. org.put("foodAmount", shBillingDto.getFoodAmount());
  256. org.put("shYh", shBillingDto.getShYh());
  257. setShList(list, org, user);
  258. }
  259. return success(MessageUtils.message("no.success"), org);
  260. }
  261. //按日获取取消订单列表
  262. public AjaxResult getDayCancleOrderList(Long page, Long size, String riqi, InfoUser user) {
  263. JSONObject org = new JSONObject();
  264. Page<PosOrder> pageInput = new Page<>(page, size);
  265. LambdaQueryWrapper<PosOrder> queryWrapper = new LambdaQueryWrapper<>();
  266. queryWrapper.in(PosOrder::getState, new ArrayList<Long>(Arrays.asList(9L, 10L)));
  267. queryWrapper.apply("(sh_id=" + user.getUserId() + " or qs_id=" + user.getUserId() + ") and date_format (cretim,'%Y-%m-%d %H:%i:%s') >= date_format ('" + riqi + " 00:00:00','%Y-%m-%d %H:%i:%s') and date_format (cretim,'%Y-%m-%d %H:%i:%s') <= date_format ('" + riqi + " 23:59:59','%Y-%m-%d %H:%i:%s')");
  268. queryWrapper.orderByDesc(PosOrder::getCretim);
  269. IPage<PosOrder> datas = posOrderService.page(pageInput, queryWrapper);
  270. org.put("list", datas.getRecords());
  271. return success(MessageUtils.message("no.success"), org);
  272. }
  273. /**
  274. * 商家周账单数据
  275. *
  276. * @param token
  277. * @param page
  278. * @param size
  279. * @param riqi
  280. * @return
  281. */
  282. @Anonymous
  283. @Auth
  284. @GetMapping("/getzdmeeklist")
  285. public AjaxResult getzdmeeklist(@RequestHeader String token,
  286. @RequestParam Long page,
  287. @RequestParam Long size,
  288. @RequestParam String riqi,
  289. @RequestParam(required = false) Boolean isCancel,
  290. @RequestParam(required = false) Long mdId) {
  291. JwtUtil jwtUtil = new JwtUtil();
  292. Long id = Long.valueOf(jwtUtil.getusid(token));
  293. InfoUser user = infoUserService.getById(id);
  294. //获取日的取消订单列表
  295. if (isCancel != null && isCancel) {
  296. return getWeekCancleOrderList(page, size, riqi, user);
  297. }
  298. List<UserBilling> list = userBillingMapper.getmeeklist(id, riqi, mdId, (page - 1) * size, size);
  299. setYhInfo(list);
  300. BillDTO sum = userBillingMapper.getmeeksum(id, riqi, mdId);
  301. JSONObject org = new JSONObject();
  302. org.put("shul", sum.getSul()); //数量
  303. org.put("sum", sum.getShouru());
  304. org.put("commission", sum.getCommission());
  305. org.put("tax", sum.getTax());
  306. org.put("behalfAmount", sum.getBehalfAmount());
  307. LambdaQueryWrapper<PosOrder> queryWrapper = new LambdaQueryWrapper<PosOrder>()
  308. .in(PosOrder::getState, new ArrayList<Long>(Arrays.asList(9L, 10L)))
  309. .apply("(sh_id=" + id + " or qs_id=" + id + ") and WEEK(cretim) = WEEK(" + riqi + ") and YEAR(cretim) = YEAR(" + riqi + ")");
  310. // 取消总数 ,作废、客服介入的数据
  311. long cancleCount = posOrderService.count(queryWrapper);
  312. org.put("cancelCount", cancleCount);
  313. if ("2".equals(user.getUserType())) {
  314. setQsList(list, org, id);
  315. } else {
  316. ShBillingDto shBillingDto = userBillingMapper.getShMeeksum(id, riqi, mdId);
  317. org.put("foodAmount", shBillingDto.getFoodAmount());
  318. org.put("shYh", shBillingDto.getShYh());
  319. setShList(list, org, user);
  320. }
  321. return success(MessageUtils.message("no.success"), org);
  322. }
  323. //按周获取取消订单列表
  324. public AjaxResult getWeekCancleOrderList(Long page, Long size, String riqi, InfoUser user) {
  325. JSONObject org = new JSONObject();
  326. Page<PosOrder> pageInput = new Page<>(page, size);
  327. LambdaQueryWrapper<PosOrder> queryWrapper = new LambdaQueryWrapper<>();
  328. queryWrapper.in(PosOrder::getState, new ArrayList<Long>(Arrays.asList(9L, 10L)));
  329. queryWrapper.apply("(sh_id=" + user.getUserId() + " or qs_id=" + user.getUserId() + ") and WEEK(cretim) = WEEK(" + riqi + ") and YEAR(cretim) = YEAR(" + riqi + ")");
  330. queryWrapper.orderByDesc(PosOrder::getCretim);
  331. IPage<PosOrder> datas = posOrderService.page(pageInput, queryWrapper);
  332. org.put("list", datas.getRecords());
  333. return success(MessageUtils.message("no.success"), org);
  334. }
  335. //返回商户账单信息
  336. private void setShList(List<UserBilling> list, JSONObject org, InfoUser user) {
  337. List<ShBillingDto> result = new ArrayList<>();
  338. if (!list.isEmpty()) {
  339. List<String> ddIds = list.stream().map(x -> x.getDdId()).collect(Collectors.toList());
  340. List<PosOrder> orders = posOrderService.list(new LambdaQueryWrapper<PosOrder>().in(PosOrder::getDdId, ddIds));
  341. List<PosOrderRating> ratings = posOrderRatingService.list(new LambdaQueryWrapper<PosOrderRating>().in(PosOrderRating::getDdId, ddIds));
  342. list.forEach(x -> {
  343. ShBillingDto dto = Convert.convert(ShBillingDto.class, x);
  344. //货到付款的订单金额
  345. orders.stream().filter(o -> o.getDdId().equals(x.getDdId())).findFirst().ifPresent(o -> {
  346. dto.setFoodAmount(o.getFoodAmount());
  347. int shYh = (o.getMdDiscountAmount() == null ? 0 : o.getMdDiscountAmount()) + (o.getMdSalesReduction() == null ? 0 : o.getMdSalesReduction());
  348. dto.setShYh((double) shYh);
  349. });
  350. ratings.stream().filter(r -> r.getDdId().equals(x.getDdId())).findFirst().ifPresent(r -> dto.setShStarts(r.getQsStars()));
  351. result.add(dto);
  352. });
  353. }
  354. double bl = user.getCommission() == null ? 0.00 : user.getCommission();
  355. bl = commissionRatesService.getShCommissionRate(bl, user.getUserId());
  356. org.put("commissionRate", bl);
  357. org.put("list", result);
  358. }
  359. //骑手返回骑手账单信息
  360. private void setQsList(List<UserBilling> list, JSONObject org, Long riderId) {
  361. List<QsBillingDto> result = new ArrayList<>();
  362. if (!list.isEmpty()) {
  363. // 两类订单独立关联,不能拿闪送订单号查询 pos_order 或依赖编号前缀猜测来源。
  364. List<String> ddIds = list.stream().filter(x -> !"FLASH".equals(x.getOrderSource()))
  365. .map(UserBilling::getDdId).collect(Collectors.toList());
  366. List<String> flashNos = list.stream().filter(x -> "FLASH".equals(x.getOrderSource()))
  367. .map(UserBilling::getDdId).collect(Collectors.toList());
  368. List<PosOrder> orders = ddIds.isEmpty() ? List.of() : posOrderService.list(
  369. new LambdaQueryWrapper<PosOrder>().in(PosOrder::getDdId, ddIds));
  370. List<PosOrderRating> ratings = ddIds.isEmpty() ? List.of() : posOrderRatingService.list(
  371. new LambdaQueryWrapper<PosOrderRating>().in(PosOrderRating::getDdId, ddIds));
  372. List<FlashDeliveryOrder> flashOrders = flashNos.isEmpty() ? List.of() : flashDeliveryOrderMapper.selectList(
  373. new LambdaQueryWrapper<FlashDeliveryOrder>().eq(FlashDeliveryOrder::getRiderId, riderId)
  374. .in(FlashDeliveryOrder::getOrderNo, flashNos));
  375. list.forEach(x -> {
  376. QsBillingDto dto = Convert.convert(QsBillingDto.class, x);
  377. if ("FLASH".equals(x.getOrderSource())) {
  378. flashOrders.stream().filter(o -> o.getOrderNo().equals(x.getDdId())).findFirst().ifPresent(o -> {
  379. dto.setFlashOrderId(o.getId());
  380. dto.setTipAmount(o.getTipAmount());
  381. // amount 已含小费;freight 只含基础/距离/加急费,保持运费与小费可分别展示。
  382. dto.setFreight(o.getAmount() == null ? null :
  383. o.getAmount().doubleValue() - (o.getTipAmount() == null ? 0 : o.getTipAmount()));
  384. dto.setJvli(o.getDistanceMeters() == null ? null : o.getDistanceMeters() / 1000.0);
  385. dto.setQsStarts(o.getRiderStars());
  386. dto.setPayType(o.getPayType());
  387. dto.setPaymentStatus(o.getPaymentStatus());
  388. });
  389. result.add(dto);
  390. return;
  391. }
  392. // 原有外卖账单没有来源字段,在骑手订单账单响应中统一兼容为 FOOD。
  393. dto.setOrderSource("FOOD");
  394. //货到付款的订单金额
  395. orders.stream().filter(o -> o.getDdId().equals(x.getDdId())).findFirst().ifPresent(o -> {
  396. if ("1".equals(o.getCollectPayment())) {
  397. dto.setOrderAmount(o.getAmount());
  398. }
  399. dto.setFreight(o.getFreight());
  400. dto.setJvli(o.getJvli());
  401. });
  402. ratings.stream().filter(r -> r.getDdId().equals(x.getDdId())).findFirst().ifPresent(r -> dto.setQsStarts(r.getQsStars()));
  403. result.add(dto);
  404. });
  405. }
  406. String taxRate="0";
  407. List<SysDictData> taxBlData = DictUtils.getDictCache("sys_qs_freight_bl");
  408. if (taxBlData != null && taxBlData.get(0) != null) {
  409. taxRate = taxBlData.get(0).getDictValue();
  410. }
  411. org.put("taxRate", taxRate);
  412. org.put("list", result);
  413. }
  414. private void setYhInfo(List<UserBilling> list) {
  415. List<UserBilling> foodBills = list.stream().filter(x -> !"FLASH".equals(x.getOrderSource())).collect(Collectors.toList());
  416. if (!foodBills.isEmpty()) {
  417. List<String> orderIds = foodBills.stream().map(UserBilling::getDdId).collect(Collectors.toList());
  418. LambdaQueryWrapper<PosOrder> query = new LambdaQueryWrapper<>();
  419. query.in(PosOrder::getDdId, orderIds);
  420. List<PosOrder> orders = posOrderService.list(query);
  421. foodBills.forEach(billing -> {
  422. orders.stream().filter(o -> o.getDdId().toString().equals(billing.getDdId())).findFirst().ifPresent(o -> {
  423. billing.setActivity(o.getActivity());
  424. billing.setMdActivity(o.getMdActivity());
  425. billing.setMdActivity(o.getMdActivity());
  426. billing.setYhId(o.getYhId());
  427. billing.setYhName(o.getYhName());
  428. billing.setDiscountAmount(o.getDiscountAmount());
  429. billing.setMdYhId(o.getMdYhId());
  430. billing.setMdYhName(o.getMdYhName());
  431. billing.setMdDiscountAmount(o.getMdDiscountAmount());
  432. billing.setSalesName(o.getSalesName());
  433. billing.setMdSalesName(o.getMdSalesName());
  434. billing.setSalesReduction(o.getSalesReduction());
  435. billing.setMdSalesReduction(o.getMdSalesReduction());
  436. billing.setPoints(o.getPoints());
  437. billing.setPointsReduction(o.getPointsReduction());
  438. });
  439. });
  440. }
  441. }
  442. /**
  443. * 商家月账单数据
  444. *
  445. * @param token
  446. * @param page
  447. * @param size
  448. * @param riqi
  449. * @return
  450. */
  451. @Anonymous
  452. @Auth
  453. @GetMapping("/getzdmatlist")
  454. public AjaxResult getzdmatlist(@RequestHeader String token,
  455. @RequestParam Long page,
  456. @RequestParam Long size,
  457. @RequestParam String riqi,
  458. @RequestParam(required = false) Boolean isCancel,
  459. @RequestParam(required = false) Long mdId) {
  460. JwtUtil jwtUtil = new JwtUtil();
  461. Long id = Long.valueOf(jwtUtil.getusid(token));
  462. InfoUser user = infoUserService.getById(id);
  463. if (isCancel != null && isCancel) {
  464. return getMonthCancleOrderList(page, size, riqi, user);
  465. }
  466. List<UserBilling> list = userBillingMapper.getmatlist(id, riqi, mdId, (page - 1) * size, size);
  467. setYhInfo(list);
  468. BillDTO sum = userBillingMapper.getmatsum(id, riqi, mdId);
  469. JSONObject org = new JSONObject();
  470. org.put("shul", sum.getSul());
  471. org.put("sum", sum.getShouru());
  472. org.put("commission", sum.getCommission());
  473. org.put("tax", sum.getTax());
  474. org.put("behalfAmount", sum.getBehalfAmount());
  475. LambdaQueryWrapper<PosOrder> queryWrapper = new LambdaQueryWrapper<PosOrder>()
  476. .in(PosOrder::getState, new ArrayList<Long>(Arrays.asList(9L, 10L)))
  477. .apply("(sh_id=" + id + " or qs_id=" + id + ") and DATE_FORMAT(cretim, '%Y-%m') = " + riqi);
  478. // 取消总数 ,作废、客服介入的数据
  479. long cancleCount = posOrderService.count(queryWrapper);
  480. org.put("cancelCount", cancleCount);
  481. if ("2".equals(user.getUserType())) {
  482. setQsList(list, org, id);
  483. } else {
  484. ShBillingDto shBillingDto = userBillingMapper.getshMonthsum(id, riqi, mdId);
  485. org.put("foodAmount", shBillingDto.getFoodAmount());
  486. org.put("shYh", shBillingDto.getShYh());
  487. setShList(list, org, user);
  488. }
  489. return success(MessageUtils.message("no.success"), org);
  490. }
  491. //按周获取取消订单列表
  492. public AjaxResult getMonthCancleOrderList(Long page, Long size, String riqi, InfoUser user) {
  493. JSONObject org = new JSONObject();
  494. Page<PosOrder> pageInput = new Page<>(page, size);
  495. LambdaQueryWrapper<PosOrder> queryWrapper = new LambdaQueryWrapper<>();
  496. queryWrapper.in(PosOrder::getState, new ArrayList<Long>(Arrays.asList(9L, 10L)));
  497. queryWrapper.apply("(sh_id=" + user.getUserId() + " or qs_id=" + user.getUserId() + ") and DATE_FORMAT(cretim, '%Y-%m') = " + riqi);
  498. queryWrapper.orderByDesc(PosOrder::getCretim);
  499. IPage<PosOrder> datas = posOrderService.page(pageInput, queryWrapper);
  500. org.put("list", datas.getRecords());
  501. return success(MessageUtils.message("no.success"), org);
  502. }
  503. //查询用户账户及余额
  504. @Anonymous
  505. @Auth
  506. @GetMapping("/getUsbalance")
  507. public AjaxResult getUsbalance(@RequestHeader String token,
  508. @RequestParam Integer page,
  509. @RequestParam Integer size,
  510. @RequestParam boolean sort,
  511. @RequestParam(defaultValue = "") String type) {
  512. Long userId = Long.valueOf(new JwtUtil().getusid(token));
  513. BigDecimal sum = userBillingService.getUserBalance(userId);
  514. QueryWrapper<UserBilling> wrapper = new QueryWrapper<>();
  515. wrapper.eq("user_id", userId);
  516. if (!type.isEmpty()) {
  517. wrapper.eq("type", type);
  518. userBillingMapper.gettypesum(userId, Long.valueOf(type));
  519. }
  520. wrapper.orderBy(true, sort, "cretim");
  521. IPage<UserBilling> mmlist = userBillingService.page(new Page<>(page, size), wrapper);
  522. JSONArray arr = buildBillingList(mmlist.getRecords());
  523. JSONObject org = new JSONObject();
  524. org.put("sum", sum);
  525. org.put("list", arr);
  526. return success(MessageUtils.message("no.success"), org);
  527. }
  528. private JSONArray buildBillingList(List<UserBilling> list) {
  529. DateUtil dateUtil = new DateUtil();
  530. JSONArray arr = new JSONArray();
  531. for (UserBilling item : list) {
  532. JSONObject ject = new JSONObject();
  533. ject.put("id", item.getId());
  534. ject.put("user", infoUserService.getById(item.getUserId()));
  535. ject.put("type", item.getType());
  536. PosOrder order = "FLASH".equals(item.getOrderSource()) ? null : posOrderService.getById(item.getDdId());
  537. ject.put("orderSource", item.getOrderSource());
  538. ject.put("ddId", order == null ? item.getDdId() : order.getDdId());
  539. ject.put("amount", item.getAmount());
  540. ject.put("divvy", item.getDivvy());
  541. ject.put("state", item.getState());
  542. ject.put("mdId", item.getMdId());
  543. ject.put("illustrate", item.getIllustrate());
  544. ject.put("paymentMethod", item.getPaymentMethod());
  545. ject.put("accountNumber", item.getAccountNumber());
  546. ject.put("url", item.getUrl());
  547. ject.put("cretim", dateUtil.setDate(item.getCretim()));
  548. arr.add(ject);
  549. }
  550. return arr;
  551. }
  552. /**
  553. * 查询Billing列表
  554. */
  555. @PreAuthorize("@ss.hasPermi('Billing:billing:list')")
  556. @GetMapping("/list")
  557. public TableDataInfo list(UserBilling userBilling) {
  558. startPage();
  559. String[] dr = userBilling.getDateRange();
  560. if (dr != null && dr.length == 2) {
  561. userBilling.setCretimStart(dr[0]);
  562. userBilling.setCretimEnd(dr[1]);
  563. }
  564. List<UserBilling> list = userBillingService.selectUserBillingList(userBilling);
  565. if (!list.isEmpty()) {
  566. List<Long> userIds = list.stream().map(UserBilling::getUserId).collect(Collectors.toList());
  567. List<InfoUser> users = infoUserService.list(new LambdaQueryWrapper<InfoUser>().in(InfoUser::getUserId, userIds));
  568. list.forEach(x -> {
  569. users.stream().filter(u -> u.getUserId().equals(x.getUserId())).findFirst().ifPresent(y -> {
  570. x.setUserName(y.getUserName());
  571. if ("0".equals(y.getUserType())) {
  572. x.setUserName(y.getPhone());
  573. }
  574. x.setUserType(y.getUserType());
  575. });
  576. });
  577. }
  578. return getDataTable(list);
  579. }
  580. /**
  581. * 导出Billing列表
  582. */
  583. @PreAuthorize("@ss.hasPermi('Billing:billing:export')")
  584. @Log(title = "Billing", businessType = BusinessType.EXPORT)
  585. @PostMapping("/export")
  586. public void export(HttpServletResponse response, UserBilling userBilling) {
  587. String[] dr = userBilling.getDateRange();
  588. if (dr != null && dr.length == 2) {
  589. userBilling.setCretimStart(dr[0]);
  590. userBilling.setCretimEnd(dr[1]);
  591. }
  592. List<UserBilling> list = userBillingService.selectUserBillingList(userBilling);
  593. if (!list.isEmpty()) {
  594. List<Long> userIds = list.stream().map(UserBilling::getUserId).collect(Collectors.toList());
  595. List<InfoUser> users = infoUserService.list(new LambdaQueryWrapper<InfoUser>().in(InfoUser::getUserId, userIds));
  596. list.forEach(x -> {
  597. users.stream().filter(u -> u.getUserId().equals(x.getUserId())).findFirst().ifPresent(y -> {
  598. x.setUserName(y.getUserName());
  599. if ("0".equals(y.getUserType())) {
  600. x.setUserName(y.getPhone());
  601. }
  602. x.setUserType(y.getUserType());
  603. });
  604. });
  605. }
  606. ExcelUtil<UserBilling> util = new ExcelUtil<UserBilling>(UserBilling.class);
  607. util.exportExcel(response, list, MessageUtils.message("no.export.excel.billing"));
  608. }
  609. /**
  610. * 获取Billing详细信息
  611. */
  612. @PreAuthorize("@ss.hasPermi('Billing:billing:query')")
  613. @GetMapping(value = "/{id}")
  614. public AjaxResult getInfo(@PathVariable("id") Long id) {
  615. UserBilling result = userBillingService.selectUserBillingById(id);
  616. if (result != null) {
  617. InfoUser user = infoUserService.getById(result.getUserId());
  618. result.setUserName(user.getUserName());
  619. if ("0".equals(user.getUserType())) {
  620. result.setUserName(user.getPhone());
  621. }
  622. result.setUserType(user.getUserType());
  623. }
  624. return success(result);
  625. }
  626. /**
  627. * 新增Billing
  628. */
  629. @PreAuthorize("@ss.hasPermi('Billing:billing:add')")
  630. @Log(title = "Billing", businessType = BusinessType.INSERT)
  631. @PostMapping
  632. public AjaxResult add(@RequestBody UserBilling userBilling) {
  633. return toAjax(userBillingService.insertUserBilling(userBilling));
  634. }
  635. /**
  636. * 修改Billing
  637. */
  638. @PreAuthorize("@ss.hasPermi('Billing:billing:edit')")
  639. @Log(title = "Billing", businessType = BusinessType.UPDATE)
  640. @PutMapping
  641. public AjaxResult edit(@RequestBody UserBilling userBilling) {
  642. return toAjax(userBillingService.updateUserBilling(userBilling));
  643. }
  644. /**
  645. * 删除Billing
  646. */
  647. @PreAuthorize("@ss.hasPermi('Billing:billing:remove')")
  648. @Log(title = "Billing", businessType = BusinessType.DELETE)
  649. @DeleteMapping("/{ids}")
  650. public AjaxResult remove(@PathVariable Long[] ids) {
  651. return toAjax(userBillingService.deleteUserBillingByIds(ids));
  652. }
  653. }