TFeeServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package com.ruoyi.finance.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.ruoyi.approvalFlow.domain.AuditItems;
  5. import com.ruoyi.approvalFlow.service.impl.AuditItemsServiceImpl;
  6. import com.ruoyi.basicData.domain.TCorps;
  7. import com.ruoyi.basicData.domain.TFees;
  8. import com.ruoyi.basicData.mapper.TCorpsMapper;
  9. import com.ruoyi.basicData.mapper.TFeesMapper;
  10. import com.ruoyi.common.core.domain.AjaxResult;
  11. import com.ruoyi.common.core.domain.model.LoginUser;
  12. import com.ruoyi.common.utils.DateUtils;
  13. import com.ruoyi.common.utils.StringUtils;
  14. import com.ruoyi.finance.domain.TFee;
  15. import com.ruoyi.finance.domain.TFeeDo;
  16. import com.ruoyi.finance.domain.TWareHouseFees;
  17. import com.ruoyi.finance.mapper.TFeeDoMapper;
  18. import com.ruoyi.finance.mapper.TFeeMapper;
  19. import com.ruoyi.finance.service.ITFeeService;
  20. import com.ruoyi.system.domain.SysConfig;
  21. import com.ruoyi.system.mapper.SysConfigMapper;
  22. import com.ruoyi.warehouseBusiness.domain.*;
  23. import com.ruoyi.warehouseBusiness.mapper.BillnoDelMapper;
  24. import com.ruoyi.warehouseBusiness.mapper.TWarehouseBillsMapper;
  25. import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsfeesMapper;
  26. import com.ruoyi.warehouseBusiness.service.impl.BillnoSerialServiceImpl;
  27. import org.springframework.beans.factory.annotation.Autowired;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.transaction.annotation.Transactional;
  30. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  31. import java.math.BigDecimal;
  32. import java.util.*;
  33. /**
  34. * 财务数据主Service业务层处理
  35. *
  36. * @author ruoyi
  37. * @date 2021-01-18
  38. */
  39. @Service
  40. public class TFeeServiceImpl implements ITFeeService {
  41. @Autowired
  42. private TFeeMapper tFeeMapper;
  43. @Autowired
  44. private TFeeDoMapper tFeeDoMapper;
  45. @Autowired
  46. private BillnoSerialServiceImpl billnoSerialServiceImpl;
  47. @Autowired
  48. private BillnoDelMapper billnoDelMapper;
  49. @Autowired
  50. private TCorpsMapper tCorpsMapper;
  51. @Autowired
  52. private TFeesMapper tFeesMapper;
  53. @Autowired
  54. private TWarehouseBillsMapper tWarehouseBillsMapper;
  55. @Autowired
  56. private TWarehousebillsfeesMapper tWarehousebillsfeesMapper;
  57. @Autowired
  58. private SysConfigMapper sysConfigMapper;
  59. @Autowired
  60. private AuditItemsServiceImpl auditItemsService;
  61. /**
  62. * 查询财务数据主
  63. *
  64. * @param fId 财务数据主ID
  65. * @return 财务数据主
  66. */
  67. @Override
  68. public Map<String, Object> selectTFeeById(Long fId) {
  69. Map<String, Object> map = new HashMap<>();
  70. // 客户表
  71. List<Long> corpsId = new ArrayList<>();
  72. // 费用
  73. List<Long> feesId = new ArrayList<>();
  74. TFee tFee=tFeeMapper.selectTFeeById(fId);
  75. if (StringUtils.isNotNull(tFee.getfCorpid())) {
  76. corpsId.add(tFee.getfCorpid());
  77. }
  78. corpsId.add(tFee.getfCorpid());
  79. // 查询从表数据
  80. TFeeDo tFeeDo = new TFeeDo();
  81. tFeeDo.setfPid(fId);
  82. List<TFeeDo> tFeeDoList= tFeeDoMapper.selectTFeeDoList(tFeeDo);
  83. List<Map<String, Object>> feeDoList=new ArrayList<>();
  84. if (StringUtils.isNotEmpty(tFeeDoList)) {
  85. for (TFeeDo fees : tFeeDoList) {
  86. Map<String, Object> map1 = new HashMap<>();
  87. feesId.add(fees.getfFeeid());
  88. // 查询主表
  89. TWarehouseBills tWarehousebills = tWarehouseBillsMapper.selectTWarehousebillsById(fees.getfSrcpid());
  90. // 费用名称
  91. TFees tFees= tFeesMapper.selectTFeesById(fees.getfFeeid());
  92. map1.put("fId",fees.getfId());
  93. map1.put("fSrcid",fees.getfSrcid());
  94. map1.put("fSrcpid",fees.getfSrcpid());
  95. map1.put("fMblno",tWarehousebills.getfMblno());
  96. map1.put("fBscorpno",tWarehousebills.getfBscorpno());
  97. map1.put("fProductName",tWarehousebills.getfProductName());
  98. map1.put("fBsdate",tWarehousebills.getfBsdate());
  99. map1.put("fFeeid",fees.getfFeeid());
  100. map1.put("fFeeName",tFees.getfName());
  101. map1.put("fSrcdc",fees.getfSrcdc());
  102. map1.put("fAmt",fees.getfAmt());
  103. map1.put("fAmtdr",fees.getfAmtdr());
  104. map1.put("fRemarks",fees.getfRemarks());
  105. if(tWarehousebills.getfBilltype().equals("SJRK")){
  106. map1.put("fBilltype","入库");
  107. } else if(tWarehousebills.getfBilltype().equals("SJCK")){
  108. map1.put("fBilltype","出库");
  109. } else if(tWarehousebills.getfBilltype().equals("CKDB")){
  110. map1.put("fBilltype","调拨");
  111. } else if(tWarehousebills.getfBilltype().equals("HQZY")){
  112. map1.put("fBilltype","货权转移");
  113. }
  114. feeDoList.add(map1);
  115. }
  116. }
  117. List<TCorps> corpsList = new ArrayList<>();
  118. List<Long> corpsIdList = StringUtils.integerDeduplication(corpsId);
  119. for (Long corpId : corpsIdList) {
  120. TCorps corps = tCorpsMapper.selectTCorpsById(corpId);
  121. if (StringUtils.isNotNull(corps)) {
  122. corpsList.add(corps);
  123. }
  124. }
  125. List<TFees> feesList = new ArrayList<>();
  126. List<Long> longList = StringUtils.integerDeduplication(feesId);
  127. for (Long fees : longList) {
  128. TFees tFees = tFeesMapper.selectTFeesById(fees);
  129. if (StringUtils.isNotNull(tFees)) {
  130. feesList.add(tFees);
  131. }
  132. }
  133. map.put("tFee",tFee);
  134. map.put("corps",corpsList);
  135. map.put("feesList", feesList);
  136. map.put("feeDoList",feeDoList);
  137. return map;
  138. }
  139. /**
  140. * 查询财务数据主列表
  141. *
  142. * @param tFee 财务数据主
  143. * @return 财务数据主
  144. */
  145. @Override
  146. public List<TFee> selectTFeeList(TFee tFee) {
  147. return tFeeMapper.selectTFeeList(tFee);
  148. }
  149. @Override
  150. public List<Map<String, Object>> selectTFeeList1(TFee tFee) {
  151. return tFeeMapper.selectTFeeList1(tFee);
  152. }
  153. /**
  154. * 新增财务数据主
  155. *
  156. * @param tFee 财务数据主
  157. * @return 结果
  158. */
  159. @Override
  160. public int insertTFee(TFee tFee) {
  161. tFee.setCreateTime(DateUtils.getNowDate());
  162. return tFeeMapper.insertTFee(tFee);
  163. }
  164. /**
  165. * 新增对账 、收费、
  166. * @param tfee
  167. * @param tfeeDo
  168. * @param loginUser
  169. * @param fBilltype
  170. * @return
  171. */
  172. @Override
  173. public AjaxResult insertTFeeTFeeDo(String tfee, String tfeeDo, LoginUser loginUser, String fBilltype) {
  174. Long fPid = null;
  175. Map<String, Object> map = new HashMap<>();
  176. TFee tFee = JSONArray.parseObject(tfee, TFee.class);
  177. if (StringUtils.isNull(tFee.getfId())) {
  178. // 如果是新数据
  179. tFee.setCreateBy(loginUser.getUser().getUserName());
  180. tFee.setfBilltype(fBilltype);
  181. // 业务编码
  182. Date time = new Date();
  183. String billNo = billnoSerialServiceImpl.getBillNo(fBilltype, time);
  184. tFee.setfBillno(billNo);
  185. tFeeMapper.insertTFee(tFee);
  186. fPid = tFee.getfId();
  187. } else {
  188. fPid = tFee.getfId();
  189. tFee.setUpdateBy(loginUser.getUser().getUserName());
  190. tFee.setUpdateTime(new Date());
  191. tFeeMapper.updateTFee(tFee);
  192. // 删除从表
  193. tFeeDoMapper.deleteByFPid(fPid);
  194. }
  195. // 财务从表
  196. if (StringUtils.isNotNull(tfeeDo)) {
  197. JSONArray jsonDrArray = JSONArray.parseArray(tfeeDo);
  198. List<TFeeDo> tFeeDoList = JSONObject.parseArray(jsonDrArray.toJSONString(), TFeeDo.class);
  199. for (TFeeDo tFeeDo : tFeeDoList) {
  200. tFeeDo.setfPid(fPid);
  201. tFeeDo.setCreateBy(loginUser.getUser().getUserName());
  202. tFeeDo.setCreateTime(new Date());
  203. tFeeDoMapper.insertTFeeDo(tFeeDo);
  204. }
  205. }
  206. map.put("tFee", tFee);
  207. return AjaxResult.success("成功", map);
  208. }
  209. /**
  210. * 修改财务数据主
  211. *
  212. * @param tFee 财务数据主
  213. * @return 结果
  214. */
  215. @Override
  216. public int updateTFee(TFee tFee) {
  217. tFee.setUpdateTime(DateUtils.getNowDate());
  218. return tFeeMapper.updateTFee(tFee);
  219. }
  220. /**
  221. * 批量删除财务数据主
  222. *
  223. * @param fIds 需要删除的财务数据主ID
  224. * @return 结果
  225. */
  226. @Override
  227. public int deleteTFeeByIds(Long[] fIds) {
  228. // 取出业务编号、 放入 billno_del
  229. for (Long id : fIds) {
  230. // 1、查询主表信息
  231. TFee tFee = tFeeMapper.selectTFeeById(id);
  232. // 2、业务编号、客存编号 放入 billno_del
  233. BillnoDel billnoDel = new BillnoDel();
  234. billnoDel.setBillType(tFee.getfBilltype());
  235. billnoDel.setBillNo(tFee.getfBillno());
  236. billnoDelMapper.insertBillnoDel(billnoDel);
  237. }
  238. return tFeeMapper.deleteTFeeByIds(fIds);
  239. }
  240. /**
  241. * 删除财务数据主信息
  242. *
  243. * @param fId 财务数据主ID
  244. * @return 结果
  245. */
  246. @Override
  247. public int deleteTFeeById(Long fId) {
  248. return tFeeMapper.deleteTFeeById(fId);
  249. }
  250. @Override
  251. public List<Map<String, Object>> warehouseBillsFeesList(TWareHouseFees tWareHouseFees) {
  252. List<Map<String, Object>> maps =new ArrayList<Map<String, Object>>();
  253. Map<String, Object> map = new HashMap<>();
  254. map.put("tWareHouseFees", tWareHouseFees);
  255. List<Map<String, Object>> mapList = tFeeMapper.warehouseBillsFeesList(map);
  256. if (mapList != null && mapList.size() > 0) {
  257. String fAmtdr = null; // 金额
  258. String fAmt = null; // 结算
  259. for (Map<String, Object> m : mapList) {
  260. for (String k : m.keySet()) {
  261. if (k.equals("fAmtdr")) {
  262. fAmtdr = m.get(k).toString();
  263. }
  264. if (k.equals("fAmt")) {
  265. fAmt = m.get(k).toString();
  266. }
  267. }
  268. BigDecimal result = calculation1(fAmtdr, fAmt);
  269. m.put("fAmtdr",result);
  270. m.put("fAmt",result);
  271. if(result.compareTo(BigDecimal.ZERO) > 0) {
  272. maps.add(m);
  273. }
  274. }
  275. }
  276. return maps;
  277. }
  278. public static BigDecimal calculation1(String fTotalgross, String fGrossweightblc) {
  279. BigDecimal num1 = new BigDecimal(fTotalgross);
  280. BigDecimal num2 = new BigDecimal(fGrossweightblc);
  281. BigDecimal result = num1.subtract(num2);
  282. return result.setScale(2, BigDecimal.ROUND_HALF_UP);
  283. }
  284. @Override
  285. @Transactional
  286. public AjaxResult confirm(String tfee, String tfeeDo, LoginUser loginUser, String fBilltype) {
  287. TFee tFee = JSONArray.parseObject(tfee, TFee.class);
  288. if (StringUtils.isNull(tFee.getfId())) {
  289. // 如果是新数据
  290. tFee.setCreateBy(loginUser.getUser().getUserName());
  291. tFee.setfBilltype(fBilltype);
  292. // 业务编码
  293. Date time = new Date();
  294. String billNo = billnoSerialServiceImpl.getBillNo(fBilltype, time);
  295. tFee.setfBillno(billNo);
  296. tFeeMapper.insertTFee(tFee);
  297. } else {
  298. tFee.setUpdateBy(loginUser.getUser().getUserName());
  299. tFee.setUpdateTime(new Date());
  300. tFeeMapper.updateTFee(tFee);
  301. // 删除从表
  302. tFeeDoMapper.deleteByFPid(tFee.getfId());
  303. }
  304. // 财务从表
  305. if (StringUtils.isNotNull(tfeeDo) && !"[]".equals(tfeeDo)) {
  306. JSONArray jsonDrArray = JSONArray.parseArray(tfeeDo);
  307. List<TFeeDo> tFeeDoList = JSONObject.parseArray(jsonDrArray.toJSONString(), TFeeDo.class);
  308. for (TFeeDo tFeeDo : tFeeDoList) {
  309. tFeeDo.setfPid(tFee.getfId());
  310. tFeeDo.setCreateBy(loginUser.getUser().getUserName());
  311. tFeeDo.setCreateTime(new Date());
  312. tFeeDoMapper.insertTFeeDo(tFeeDo);
  313. // 跟新费用明细
  314. int m = updateBillsFees(tFee.getfId(),tFeeDo,fBilltype);
  315. if (m == 0) {
  316. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  317. return AjaxResult.error("更新费用明细失败");
  318. }
  319. }
  320. }
  321. // 这里加个判断取系统参数来确定是否需要启用审批流
  322. String key = "";
  323. boolean isApprove = false;
  324. long actId = 0L;
  325. if ("DZ".equals(fBilltype)) {
  326. actId = 210L;
  327. key = "warehouse.contrast.ApprovalFlow";
  328. } else if ("SF".equals(fBilltype)) {
  329. actId = 220L;
  330. key = "warehouse.charge.ApprovalFlow";
  331. } else if ("FF".equals(fBilltype)) {
  332. actId = 230L;
  333. key = "warehouse.payment.ApprovalFlow";
  334. }
  335. SysConfig sysConfig = sysConfigMapper.checkConfigKeyUnique(key);
  336. if (StringUtils.isNull(sysConfig)) {
  337. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  338. return AjaxResult.error("系统参数异常,未找到开启审批流参数");
  339. }
  340. if ("0".equals(sysConfig.getConfigValue())) {
  341. isApprove = true;
  342. }
  343. if (isApprove) {
  344. AuditItems auditItems = new AuditItems();
  345. auditItems.setLevelId(0L);
  346. auditItems.setBillId(tFee.getfId());
  347. auditItems.setActId(actId);
  348. auditItems.setIffinalItem("F");
  349. auditItems.setBillNo(tFee.getfBillno()); // 业务编号
  350. auditItems.setRefno1(String.valueOf(tFee.getfCorpid())); // 货权方
  351. auditItems.setRefno2(tFee.getfBilltype());// 财务类型
  352. auditItems.setRefno3(tFee.gettMblno());// 提单号
  353. auditItems.setSendUserId(loginUser.getUser().getUserId());
  354. auditItems.setSendName(loginUser.getUsername());
  355. auditItems.setSendTime(new Date());
  356. auditItems.setAuditUserId(loginUser.getUser().getUserId());
  357. auditItems.setAuditItem(new Date());
  358. auditItems.setAuditMsg("提交");
  359. auditItems.setAuditStatus("O");
  360. AjaxResult approvalFlow = auditItemsService.createApprovalFlow(auditItems);
  361. Long code = Long.valueOf(String.valueOf(approvalFlow.get("code"))).longValue();
  362. if (code.equals(500L)) {
  363. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  364. }
  365. return approvalFlow;
  366. }else {
  367. AjaxResult ajaxResult = tfeeFollow(tFee.getfId(), 6L);
  368. Long code = Long.valueOf(String.valueOf(ajaxResult.get("code"))).longValue();
  369. return ajaxResult;
  370. }
  371. }
  372. /**
  373. * 根据财务主表id 更新对应明细表状态
  374. *
  375. * @param fPid 财务主表id
  376. * @return 结果
  377. */
  378. @Transactional
  379. public AjaxResult tfeeFollow(Long fPid, long fettle) {
  380. if (StringUtils.isNull(fPid)) {
  381. return AjaxResult.error("财务更新状态未找到主表信息,请与管理员联系");
  382. }
  383. TFee tFee = new TFee();
  384. tFee.setfId(fPid);
  385. tFee.setfBillstatus(String.valueOf(fettle));
  386. int tFeeUpdateResult = tFeeMapper.updateTFee(tFee);
  387. if (tFeeUpdateResult <= 0) {
  388. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  389. return AjaxResult.error("更新财务明细状态失败,请联系管理员");
  390. }
  391. int itemUpdateResult = tFeeDoMapper.tfeeDoFollowUpdate(fPid,fettle);
  392. if (itemUpdateResult <= 0) {
  393. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  394. return AjaxResult.error("更新财务明细状态失败,请联系管理员");
  395. }
  396. return AjaxResult.success();
  397. }
  398. @Override
  399. @Transactional
  400. public AjaxResult revoke(String tfee, String tfeeDo, LoginUser loginUser, String fBilltype) {
  401. // 更新 主表、从表
  402. TFee tFee = JSONArray.parseObject(tfee, TFee.class);
  403. tFee.setUpdateBy(loginUser.getUser().getUserName());
  404. tFee.setUpdateTime(new Date());
  405. tFeeMapper.updateTFee(tFee);
  406. // 删除从表
  407. tFeeDoMapper.deleteByFPid(tFee.getfId());
  408. // 财务从表
  409. if (StringUtils.isNotNull(tfeeDo)) {
  410. JSONArray jsonDrArray = JSONArray.parseArray(tfeeDo);
  411. List<TFeeDo> tFeeDoList = JSONObject.parseArray(jsonDrArray.toJSONString(), TFeeDo.class);
  412. for (TFeeDo tFeeDo : tFeeDoList) {
  413. tFeeDo.setfPid(tFee.getfId());
  414. tFeeDo.setCreateBy(loginUser.getUser().getUserName());
  415. tFeeDo.setCreateTime(new Date());
  416. tFeeDoMapper.insertTFeeDo(tFeeDo);
  417. // 跟新费用明细
  418. int m = updateBillsFees(tFee.getfId(),tFeeDo,fBilltype);
  419. if (m == 0) {
  420. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  421. return AjaxResult.error("更新费用明细失败");
  422. }
  423. }
  424. }
  425. return AjaxResult.success();
  426. }
  427. @Override
  428. public AjaxResult queryRemove(Long fId) {
  429. TFee tFee=tFeeMapper.selectTFeeById(fId);
  430. TFeeDo tFeeDo = new TFeeDo();
  431. tFeeDo.setfPid(tFee.getfId());
  432. List<TFeeDo> tFeeDoList= tFeeDoMapper.selectTFeeDoList(tFeeDo);
  433. if( StringUtils.isNull(tFee)){
  434. return AjaxResult.success("0");
  435. } else if(StringUtils.isNotNull(tFee) && StringUtils.isEmpty(tFeeDoList) ){
  436. return AjaxResult.success("1");
  437. } else {
  438. return AjaxResult.success("2");
  439. }
  440. }
  441. /**
  442. * 更新费用明细
  443. * @param fid
  444. * @param tFeeDo
  445. * @param billsType
  446. * @return
  447. */
  448. @Transactional
  449. public int updateBillsFees(Long fid,TFeeDo tFeeDo,String billsType){
  450. // 查询从表数据
  451. TFee tFee=tFeeMapper.selectTFeeById(fid);
  452. if(billsType.equals("SF") || billsType.equals("FF")){
  453. billsType="DC";
  454. }
  455. Map<String, Object> map = new HashMap<>();
  456. map.put("tFee", tFee);
  457. map.put("billType", billsType);
  458. map.put("tFeeDo", tFeeDo);
  459. return tWarehousebillsfeesMapper.updateTWarehousebillsfee(map);
  460. }
  461. }