AccountHeadService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package com.jsh.erp.service;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.jsh.erp.constants.BusinessConstants;
  5. import com.jsh.erp.constants.ExceptionConstants;
  6. import com.jsh.erp.datasource.entities.*;
  7. import com.jsh.erp.datasource.mappers.AccountHeadMapper;
  8. import com.jsh.erp.datasource.mappers.AccountHeadMapperEx;
  9. import com.jsh.erp.datasource.mappers.AccountItemMapperEx;
  10. import com.jsh.erp.datasource.mappers.AccountMapper;
  11. import com.jsh.erp.exception.BusinessRunTimeException;
  12. import com.jsh.erp.exception.JshException;
  13. import com.jsh.erp.utils.PageUtils;
  14. import com.jsh.erp.utils.StringUtil;
  15. import com.jsh.erp.utils.Tools;
  16. import org.slf4j.Logger;
  17. import org.slf4j.LoggerFactory;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import org.springframework.web.context.request.RequestContextHolder;
  21. import org.springframework.web.context.request.ServletRequestAttributes;
  22. import javax.annotation.Resource;
  23. import javax.servlet.http.HttpServletRequest;
  24. import java.math.BigDecimal;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. import static com.jsh.erp.utils.Tools.getCenternTime;
  29. @Service
  30. public class AccountHeadService {
  31. private Logger logger = LoggerFactory.getLogger(AccountHeadService.class);
  32. @Resource
  33. private AccountHeadMapper accountHeadMapper;
  34. @Resource
  35. private AccountHeadMapperEx accountHeadMapperEx;
  36. @Resource
  37. private OrgaUserRelService orgaUserRelService;
  38. @Resource
  39. private AccountItemService accountItemService;
  40. @Resource
  41. private UserService userService;
  42. @Resource
  43. private SupplierService supplierService;
  44. @Resource
  45. private SystemConfigService systemConfigService;
  46. @Resource
  47. private LogService logService;
  48. @Resource
  49. private AccountItemMapperEx accountItemMapperEx;
  50. @Resource
  51. private AccountMapper accountMapper;
  52. public AccountHead getAccountHead(long id) throws Exception {
  53. AccountHead result=null;
  54. try{
  55. result=accountHeadMapper.selectByPrimaryKey(id);
  56. }catch(Exception e){
  57. JshException.readFail(logger, e);
  58. }
  59. return result;
  60. }
  61. public List<AccountHead> getAccountHeadListByIds(String ids)throws Exception {
  62. List<Long> idList = StringUtil.strToLongList(ids);
  63. List<AccountHead> list = new ArrayList<>();
  64. try{
  65. AccountHeadExample example = new AccountHeadExample();
  66. example.createCriteria().andIdIn(idList);
  67. list = accountHeadMapper.selectByExample(example);
  68. }catch(Exception e){
  69. JshException.readFail(logger, e);
  70. }
  71. return list;
  72. }
  73. public List<AccountHead> getAccountHead() throws Exception{
  74. AccountHeadExample example = new AccountHeadExample();
  75. List<AccountHead> list=null;
  76. try{
  77. list=accountHeadMapper.selectByExample(example);
  78. }catch(Exception e){
  79. JshException.readFail(logger, e);
  80. }
  81. return list;
  82. }
  83. public List<AccountHeadVo4ListEx> select(String type, String billNo, String beginTime, String endTime,
  84. Long organId, Long creator, Long handsPersonId, Long accountId, String status,
  85. String remark, String number, Long inOutItemId) throws Exception{
  86. List<AccountHeadVo4ListEx> list = new ArrayList<>();
  87. try{
  88. String [] creatorArray = getCreatorArray();
  89. beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
  90. endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
  91. PageUtils.startPage();
  92. list = accountHeadMapperEx.selectByConditionAccountHead(type, creatorArray, billNo,
  93. beginTime, endTime, organId, creator, handsPersonId, accountId, status, remark, number, inOutItemId);
  94. if (null != list) {
  95. for (AccountHeadVo4ListEx ah : list) {
  96. if(ah.getChangeAmount() != null) {
  97. if(BusinessConstants.TYPE_MONEY_IN.equals(ah.getType())) {
  98. ah.setChangeAmount(ah.getChangeAmount());
  99. } else if(BusinessConstants.TYPE_MONEY_OUT.equals(ah.getType())) {
  100. ah.setChangeAmount(BigDecimal.ZERO.subtract(ah.getChangeAmount()));
  101. } else {
  102. ah.setChangeAmount(ah.getChangeAmount().abs());
  103. }
  104. }
  105. if(ah.getTotalPrice() != null) {
  106. if(BusinessConstants.TYPE_MONEY_IN.equals(ah.getType())) {
  107. ah.setTotalPrice(ah.getTotalPrice());
  108. } else if(BusinessConstants.TYPE_MONEY_OUT.equals(ah.getType())) {
  109. ah.setTotalPrice(BigDecimal.ZERO.subtract(ah.getTotalPrice()));
  110. } else {
  111. ah.setTotalPrice(ah.getTotalPrice().abs());
  112. }
  113. }
  114. if(ah.getBillTime() !=null) {
  115. ah.setBillTimeStr(getCenternTime(ah.getBillTime()));
  116. }
  117. }
  118. }
  119. } catch(Exception e){
  120. JshException.readFail(logger, e);
  121. }
  122. return list;
  123. }
  124. /**
  125. * 根据角色类型获取操作员数组
  126. * @return
  127. * @throws Exception
  128. */
  129. public String[] getCreatorArray() throws Exception {
  130. String creator = "";
  131. User user = userService.getCurrentUser();
  132. String roleType = userService.getRoleTypeByUserId(user.getId()).getType(); //角色类型
  133. if(BusinessConstants.ROLE_TYPE_PRIVATE.equals(roleType)) {
  134. creator = user.getId().toString();
  135. } else if(BusinessConstants.ROLE_TYPE_THIS_ORG.equals(roleType)) {
  136. creator = orgaUserRelService.getUserIdListByUserId(user.getId());
  137. }
  138. String [] creatorArray=null;
  139. if(StringUtil.isNotEmpty(creator)){
  140. creatorArray = creator.split(",");
  141. }
  142. return creatorArray;
  143. }
  144. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  145. public int insertAccountHead(JSONObject obj, HttpServletRequest request) throws Exception{
  146. AccountHead accountHead = JSONObject.parseObject(obj.toJSONString(), AccountHead.class);
  147. int result=0;
  148. try{
  149. User userInfo=userService.getCurrentUser();
  150. accountHead.setCreator(userInfo==null?null:userInfo.getId());
  151. result = accountHeadMapper.insertSelective(accountHead);
  152. logService.insertLog("财务",
  153. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(accountHead.getBillNo()).toString(), request);
  154. }catch(Exception e){
  155. JshException.writeFail(logger, e);
  156. }
  157. return result;
  158. }
  159. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  160. public int updateAccountHead(JSONObject obj, HttpServletRequest request)throws Exception {
  161. AccountHead accountHead = JSONObject.parseObject(obj.toJSONString(), AccountHead.class);
  162. int result=0;
  163. try{
  164. result = accountHeadMapper.updateByPrimaryKeySelective(accountHead);
  165. logService.insertLog("财务",
  166. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(accountHead.getBillNo()).toString(), request);
  167. }catch(Exception e){
  168. JshException.writeFail(logger, e);
  169. }
  170. return result;
  171. }
  172. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  173. public int deleteAccountHead(Long id, HttpServletRequest request)throws Exception {
  174. return batchDeleteAccountHeadByIds(id.toString());
  175. }
  176. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  177. public int batchDeleteAccountHead(String ids, HttpServletRequest request)throws Exception {
  178. return batchDeleteAccountHeadByIds(ids);
  179. }
  180. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  181. public int batchDeleteAccountHeadByIds(String ids)throws Exception {
  182. StringBuffer sb = new StringBuffer();
  183. sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
  184. User userInfo=userService.getCurrentUser();
  185. String [] idArray=ids.split(",");
  186. List<AccountHead> list = getAccountHeadListByIds(ids);
  187. for(AccountHead accountHead: list){
  188. if(!"0".equals(accountHead.getStatus())) {
  189. throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_CODE,
  190. String.format(ExceptionConstants.ACCOUNT_HEAD_UN_AUDIT_DELETE_FAILED_MSG));
  191. }
  192. }
  193. //删除主表
  194. accountItemMapperEx.batchDeleteAccountItemByHeadIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
  195. //删除子表
  196. accountHeadMapperEx.batchDeleteAccountHeadByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
  197. //路径列表
  198. List<String> pathList = new ArrayList<>();
  199. for(AccountHead accountHead: list){
  200. sb.append("[").append(accountHead.getBillNo()).append("]");
  201. if(StringUtil.isNotEmpty(accountHead.getFileName())) {
  202. pathList.add(accountHead.getFileName());
  203. }
  204. if("收预付款".equals(accountHead.getType())){
  205. if (accountHead.getOrganId() != null) {
  206. //更新会员预付款
  207. supplierService.updateAdvanceIn(accountHead.getOrganId());
  208. }
  209. }
  210. }
  211. //逻辑删除文件
  212. systemConfigService.deleteFileByPathList(pathList);
  213. logService.insertLog("财务", sb.toString(),
  214. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  215. return 1;
  216. }
  217. /**
  218. * 校验单据编号是否存在
  219. * @param id
  220. * @param billNo
  221. * @return
  222. * @throws Exception
  223. */
  224. public int checkIsBillNoExist(Long id, String billNo)throws Exception {
  225. AccountHeadExample example = new AccountHeadExample();
  226. example.createCriteria().andIdNotEqualTo(id).andBillNoEqualTo(billNo).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  227. List<AccountHead> list = null;
  228. try{
  229. list = accountHeadMapper.selectByExample(example);
  230. }catch(Exception e){
  231. JshException.readFail(logger, e);
  232. }
  233. return list==null?0:list.size();
  234. }
  235. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  236. public int batchSetStatus(String status, String accountHeadIds)throws Exception {
  237. int result = 0;
  238. try{
  239. List<Long> ahIds = new ArrayList<>();
  240. List<Long> ids = StringUtil.strToLongList(accountHeadIds);
  241. for(Long id: ids) {
  242. AccountHead accountHead = getAccountHead(id);
  243. if("0".equals(status)){
  244. if("1".equals(accountHead.getStatus())) {
  245. ahIds.add(id);
  246. }
  247. } else if("1".equals(status)){
  248. if("0".equals(accountHead.getStatus())) {
  249. ahIds.add(id);
  250. }
  251. }
  252. }
  253. if(ahIds.size()>0) {
  254. AccountHead accountHead = new AccountHead();
  255. accountHead.setStatus(status);
  256. AccountHeadExample example = new AccountHeadExample();
  257. example.createCriteria().andIdIn(ahIds);
  258. result = accountHeadMapper.updateByExampleSelective(accountHead, example);
  259. } else {
  260. return 1;
  261. }
  262. }catch(Exception e){
  263. JshException.writeFail(logger, e);
  264. }
  265. return result;
  266. }
  267. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  268. public void addAccountHeadAndDetail(String beanJson, String rows, HttpServletRequest request) throws Exception {
  269. AccountHead accountHead = JSONObject.parseObject(beanJson, AccountHead.class);
  270. //校验单号是否重复
  271. if(checkIsBillNoExist(0L, accountHead.getBillNo())>0) {
  272. throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_BILL_NO_EXIST_CODE,
  273. String.format(ExceptionConstants.ACCOUNT_HEAD_BILL_NO_EXIST_MSG));
  274. }
  275. //校验付款账户和明细中的账户重复(转账单据)
  276. if(BusinessConstants.TYPE_GIRO.equals(accountHead.getType())) {
  277. JSONArray rowArr = JSONArray.parseArray(rows);
  278. if (null != rowArr && rowArr.size()>0) {
  279. for (int i = 0; i < rowArr.size(); i++) {
  280. JSONObject object = JSONObject.parseObject(rowArr.getString(i));
  281. if (object.get("accountId") != null && !object.get("accountId").equals("")) {
  282. Long accoutId = object.getLong("accountId");
  283. String accountName = accountMapper.selectByPrimaryKey(accoutId).getName();
  284. if(accoutId.equals(accountHead.getAccountId())) {
  285. throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_ACCOUNT_REPEAT_CODE,
  286. String.format(ExceptionConstants.ACCOUNT_HEAD_ACCOUNT_REPEAT_MSG, accountName));
  287. }
  288. }
  289. }
  290. }
  291. }
  292. User userInfo=userService.getCurrentUser();
  293. accountHead.setCreator(userInfo==null?null:userInfo.getId());
  294. if(StringUtil.isEmpty(accountHead.getStatus())) {
  295. accountHead.setStatus(BusinessConstants.BILLS_STATUS_UN_AUDIT);
  296. }
  297. accountHeadMapper.insertSelective(accountHead);
  298. //根据单据编号查询单据id
  299. AccountHeadExample dhExample = new AccountHeadExample();
  300. dhExample.createCriteria().andBillNoEqualTo(accountHead.getBillNo()).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  301. List<AccountHead> list = accountHeadMapper.selectByExample(dhExample);
  302. if(list!=null) {
  303. Long headId = list.get(0).getId();
  304. String type = list.get(0).getType();
  305. /**处理单据子表信息*/
  306. accountItemService.saveDetials(rows, headId, type, request);
  307. }
  308. if("收预付款".equals(accountHead.getType())){
  309. //更新会员预付款
  310. supplierService.updateAdvanceIn(accountHead.getOrganId());
  311. }
  312. logService.insertLog("财务单据",
  313. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(accountHead.getBillNo()).toString(), request);
  314. }
  315. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  316. public void updateAccountHeadAndDetail(String beanJson, String rows, HttpServletRequest request) throws Exception {
  317. AccountHead accountHead = JSONObject.parseObject(beanJson, AccountHead.class);
  318. //校验单号是否重复
  319. if(checkIsBillNoExist(accountHead.getId(), accountHead.getBillNo())>0) {
  320. throw new BusinessRunTimeException(ExceptionConstants.ACCOUNT_HEAD_BILL_NO_EXIST_CODE,
  321. String.format(ExceptionConstants.ACCOUNT_HEAD_BILL_NO_EXIST_MSG));
  322. }
  323. accountHeadMapper.updateByPrimaryKeySelective(accountHead);
  324. //根据单据编号查询单据id
  325. AccountHeadExample dhExample = new AccountHeadExample();
  326. dhExample.createCriteria().andBillNoEqualTo(accountHead.getBillNo()).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  327. List<AccountHead> list = accountHeadMapper.selectByExample(dhExample);
  328. if(list!=null) {
  329. Long headId = list.get(0).getId();
  330. String type = list.get(0).getType();
  331. /**处理单据子表信息*/
  332. accountItemService.saveDetials(rows, headId, type, request);
  333. }
  334. if("收预付款".equals(accountHead.getType())){
  335. //更新会员预付款
  336. supplierService.updateAdvanceIn(accountHead.getOrganId());
  337. }
  338. logService.insertLog("财务单据",
  339. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(accountHead.getBillNo()).toString(), request);
  340. }
  341. public List<AccountHeadVo4ListEx> getDetailByNumber(String billNo)throws Exception {
  342. List<AccountHeadVo4ListEx> resList = new ArrayList<AccountHeadVo4ListEx>();
  343. List<AccountHeadVo4ListEx> list = null;
  344. try{
  345. list = accountHeadMapperEx.getDetailByNumber(billNo);
  346. }catch(Exception e){
  347. JshException.readFail(logger, e);
  348. }
  349. if (null != list) {
  350. for (AccountHeadVo4ListEx ah : list) {
  351. if(ah.getChangeAmount() != null) {
  352. if(BusinessConstants.TYPE_MONEY_IN.equals(ah.getType())) {
  353. ah.setChangeAmount(ah.getChangeAmount());
  354. } else if(BusinessConstants.TYPE_MONEY_OUT.equals(ah.getType())) {
  355. ah.setChangeAmount(BigDecimal.ZERO.subtract(ah.getChangeAmount()));
  356. } else {
  357. ah.setChangeAmount(ah.getChangeAmount().abs());
  358. }
  359. }
  360. if(ah.getTotalPrice() != null) {
  361. if(BusinessConstants.TYPE_MONEY_IN.equals(ah.getType())) {
  362. ah.setTotalPrice(ah.getTotalPrice());
  363. } else if(BusinessConstants.TYPE_MONEY_OUT.equals(ah.getType())) {
  364. ah.setTotalPrice(BigDecimal.ZERO.subtract(ah.getTotalPrice()));
  365. } else {
  366. ah.setTotalPrice(ah.getTotalPrice().abs());
  367. }
  368. }
  369. if(ah.getBillTime() !=null) {
  370. ah.setBillTimeStr(getCenternTime(ah.getBillTime()));
  371. }
  372. resList.add(ah);
  373. }
  374. }
  375. return resList;
  376. }
  377. public List<AccountItem> getFinancialBillNoByBillIdList(List<Long> idList) {
  378. return accountHeadMapperEx.getFinancialBillNoByBillIdList(idList);
  379. }
  380. public List<AccountHead> getFinancialBillNoByBillId(Long billId) {
  381. return accountHeadMapperEx.getFinancialBillNoByBillId(billId);
  382. }
  383. }