SupplierServiceImpl.java 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. package com.jsh.erp.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  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.*;
  8. import com.jsh.erp.datasource.vo.DepotHeadVo4StatementAccount;
  9. import com.jsh.erp.exception.BusinessRunTimeException;
  10. import com.jsh.erp.exception.JshException;
  11. import com.jsh.erp.query.LambdaQueryWrapperX;
  12. import com.jsh.erp.service.*;
  13. import com.jsh.erp.utils.*;
  14. import jxl.Sheet;
  15. import jxl.Workbook;
  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.util.CollectionUtils;
  21. import org.springframework.web.context.request.RequestContextHolder;
  22. import org.springframework.web.context.request.ServletRequestAttributes;
  23. import org.springframework.web.multipart.MultipartFile;
  24. import javax.annotation.Resource;
  25. import javax.servlet.http.HttpServletRequest;
  26. import java.io.File;
  27. import java.math.BigDecimal;
  28. import java.util.*;
  29. @Service
  30. public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements SupplierService {
  31. private Logger logger = LoggerFactory.getLogger(SupplierService.class);
  32. @Resource
  33. private SupplierMapper supplierMapper;
  34. @Resource
  35. private SupplierMapperEx supplierMapperEx;
  36. @Resource
  37. private LogService logService;
  38. @Resource
  39. private UserService userService;
  40. @Resource
  41. private AccountHeadMapperEx accountHeadMapperEx;
  42. @Resource
  43. private DepotHeadMapperEx depotHeadMapperEx;
  44. @Resource
  45. private AccountItemMapperEx accountItemMapperEx;
  46. @Resource
  47. private DepotHeadService depotHeadService;
  48. @Resource
  49. private UserBusinessService userBusinessService;
  50. @Resource
  51. private UserBusinessMapper userBusinessMapper;
  52. @Override
  53. public Supplier getSupplier(long id)throws Exception {
  54. Supplier result=null;
  55. try{
  56. result=supplierMapper.selectByPrimaryKey(id);
  57. }catch(Exception e){
  58. JshException.readFail(logger, e);
  59. }
  60. return result;
  61. }
  62. @Override
  63. public List<Supplier> getSupplierListByIds(String ids)throws Exception {
  64. List<Long> idList = StringUtil.strToLongList(ids);
  65. List<Supplier> list = new ArrayList<>();
  66. try{
  67. SupplierExample example = new SupplierExample();
  68. example.createCriteria().andIdIn(idList);
  69. list = supplierMapper.selectByExample(example);
  70. }catch(Exception e){
  71. JshException.readFail(logger, e);
  72. }
  73. return list;
  74. }
  75. @Override
  76. public List<Supplier> getSupplier()throws Exception {
  77. SupplierExample example = new SupplierExample();
  78. example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  79. List<Supplier> list=null;
  80. try{
  81. list=supplierMapper.selectByExample(example);
  82. }catch(Exception e){
  83. JshException.readFail(logger, e);
  84. }
  85. return list;
  86. }
  87. @Override
  88. public List<Supplier> select(String supplier, String type, String phonenum, String telephone) throws Exception{
  89. List<Supplier> list = new ArrayList<>();
  90. try{
  91. String [] creatorArray = depotHeadService.getCreatorArray();
  92. PageUtils.startPage();
  93. list = supplierMapperEx.selectByConditionSupplier(supplier, type, phonenum, telephone, creatorArray);
  94. for(Supplier s : list) {
  95. Integer supplierId = s.getId().intValue();
  96. String beginTime = Tools.getYearBegin();
  97. String endTime = Tools.getCenternTime(new Date());
  98. BigDecimal sum = BigDecimal.ZERO;
  99. String supplierType = type;
  100. String inOutType = "";
  101. String subType = "";
  102. String typeBack = "";
  103. String subTypeBack = "";
  104. String billType = "";
  105. if (("供应商").equals(supplierType)) {
  106. inOutType = "入库";
  107. subType = "采购";
  108. typeBack = "出库";
  109. subTypeBack = "采购退货";
  110. billType = "付款";
  111. } else if (("客户").equals(supplierType)) {
  112. inOutType = "出库";
  113. subType = "销售";
  114. typeBack = "入库";
  115. subTypeBack = "销售退货";
  116. billType = "收款";
  117. }
  118. List<DepotHeadVo4StatementAccount> saList = depotHeadService.getStatementAccount(beginTime, endTime, supplierId, null,
  119. 1, supplierType, inOutType, subType, typeBack, subTypeBack, billType, null, null);
  120. if(saList.size()>0) {
  121. DepotHeadVo4StatementAccount item = saList.get(0);
  122. //期初 = 起始期初金额+上期欠款金额-上期退货的欠款金额-上期收付款
  123. BigDecimal preNeed = item.getBeginNeed().add(item.getPreDebtMoney()).subtract(item.getPreReturnDebtMoney()).subtract(item.getPreBackMoney());
  124. item.setPreNeed(preNeed);
  125. //实际欠款 = 本期欠款-本期退货的欠款金额
  126. BigDecimal realDebtMoney = item.getDebtMoney().subtract(item.getReturnDebtMoney());
  127. item.setDebtMoney(realDebtMoney);
  128. //期末 = 期初+实际欠款-本期收款
  129. BigDecimal allNeedGet = preNeed.add(realDebtMoney).subtract(item.getBackMoney());
  130. sum = sum.add(allNeedGet);
  131. }
  132. if(("客户").equals(s.getType())) {
  133. s.setAllNeedGet(sum);
  134. } else if(("供应商").equals(s.getType())) {
  135. s.setAllNeedPay(sum);
  136. }
  137. }
  138. } catch(Exception e){
  139. JshException.readFail(logger, e);
  140. }
  141. return list;
  142. }
  143. @Override
  144. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  145. public int insertSupplier(JSONObject obj, HttpServletRequest request)throws Exception {
  146. Supplier supplier = JSONObject.parseObject(obj.toJSONString(), Supplier.class);
  147. int result=0;
  148. try{
  149. supplier.setEnabled(true);
  150. User userInfo=userService.getCurrentUser();
  151. supplier.setCreator(userInfo==null?null:userInfo.getId());
  152. result=supplierMapper.insertSelective(supplier);
  153. //新增客户时给当前用户自动授权
  154. setUserCustomerPermission(request, supplier);
  155. logService.insertLog("商家",
  156. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(supplier.getSupplier()).toString(),request);
  157. }catch(Exception e){
  158. JshException.writeFail(logger, e);
  159. }
  160. return result;
  161. }
  162. @Override
  163. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  164. public int updateSupplier(JSONObject obj, HttpServletRequest request)throws Exception {
  165. Supplier supplier = JSONObject.parseObject(obj.toJSONString(), Supplier.class);
  166. if(supplier.getBeginNeedPay() == null) {
  167. supplier.setBeginNeedPay(BigDecimal.ZERO);
  168. }
  169. if(supplier.getBeginNeedGet() == null) {
  170. supplier.setBeginNeedGet(BigDecimal.ZERO);
  171. }
  172. int result=0;
  173. try{
  174. result=supplierMapper.updateByPrimaryKeySelective(supplier);
  175. logService.insertLog("商家",
  176. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(supplier.getSupplier()).toString(), request);
  177. }catch(Exception e){
  178. JshException.writeFail(logger, e);
  179. }
  180. return result;
  181. }
  182. @Override
  183. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  184. public int deleteSupplier(Long id, HttpServletRequest request)throws Exception {
  185. return batchDeleteSupplierByIds(id.toString());
  186. }
  187. @Override
  188. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  189. public int batchDeleteSupplier(String ids, HttpServletRequest request) throws Exception{
  190. return batchDeleteSupplierByIds(ids);
  191. }
  192. @Override
  193. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  194. public int batchDeleteSupplierByIds(String ids)throws Exception {
  195. int result=0;
  196. String [] idArray=ids.split(",");
  197. //校验财务主表 jsh_accounthead
  198. List<AccountHead> accountHeadList=null;
  199. try{
  200. accountHeadList = accountHeadMapperEx.getAccountHeadListByOrganIds(idArray);
  201. }catch(Exception e){
  202. JshException.readFail(logger, e);
  203. }
  204. if(accountHeadList!=null&&accountHeadList.size()>0){
  205. logger.error("异常码[{}],异常提示[{}],参数,OrganIds[{}]",
  206. ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,ExceptionConstants.DELETE_FORCE_CONFIRM_MSG,ids);
  207. throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
  208. ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
  209. }
  210. //校验单据主表 jsh_depot_head
  211. List<DepotHead> depotHeadList=null;
  212. try{
  213. depotHeadList = depotHeadMapperEx.getDepotHeadListByOrganIds(idArray);
  214. }catch(Exception e){
  215. JshException.readFail(logger, e);
  216. }
  217. if(depotHeadList!=null&&depotHeadList.size()>0){
  218. logger.error("异常码[{}],异常提示[{}],参数,OrganIds[{}]",
  219. ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,ExceptionConstants.DELETE_FORCE_CONFIRM_MSG,ids);
  220. throw new BusinessRunTimeException(ExceptionConstants.DELETE_FORCE_CONFIRM_CODE,
  221. ExceptionConstants.DELETE_FORCE_CONFIRM_MSG);
  222. }
  223. //记录日志
  224. StringBuffer sb = new StringBuffer();
  225. sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
  226. List<Supplier> list = getSupplierListByIds(ids);
  227. for(Supplier supplier: list){
  228. sb.append("[").append(supplier.getSupplier()).append("]");
  229. }
  230. logService.insertLog("商家", sb.toString(),
  231. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  232. User userInfo=userService.getCurrentUser();
  233. //校验通过执行删除操作
  234. try{
  235. result = supplierMapperEx.batchDeleteSupplierByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
  236. }catch(Exception e){
  237. JshException.writeFail(logger, e);
  238. }
  239. return result;
  240. }
  241. @Override
  242. public int checkIsNameExist(Long id, String name)throws Exception {
  243. SupplierExample example = new SupplierExample();
  244. example.createCriteria().andIdNotEqualTo(id).andSupplierEqualTo(name).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  245. List<Supplier> list=null;
  246. try{
  247. list= supplierMapper.selectByExample(example);
  248. }catch(Exception e){
  249. JshException.readFail(logger, e);
  250. }
  251. return list==null?0:list.size();
  252. }
  253. @Override
  254. public int checkIsNameAndTypeExist(Long id, String name, String type)throws Exception {
  255. name = name == null? "": name;
  256. SupplierExample example = new SupplierExample();
  257. example.createCriteria().andIdNotEqualTo(id).andSupplierEqualTo(name).andTypeEqualTo(type)
  258. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  259. List<Supplier> list=null;
  260. try{
  261. list= supplierMapper.selectByExample(example);
  262. }catch(Exception e){
  263. JshException.readFail(logger, e);
  264. }
  265. return list==null?0:list.size();
  266. }
  267. @Override
  268. public int checkIsTelephoneAndTypeExist(Long id, String telephone, String type) {
  269. telephone = (telephone == null) ? "" : telephone;
  270. SupplierExample example = new SupplierExample();
  271. example.createCriteria().andIdNotEqualTo(id).andTelephoneEqualTo(telephone).andTypeEqualTo(type).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  272. List<Supplier> list = null;
  273. try {
  274. list = supplierMapper.selectByExample(example);
  275. } catch (Exception e) {
  276. JshException.readFail(logger, e);
  277. }
  278. return list == null ? 0 : list.size();
  279. }
  280. /**
  281. * 更新会员的预付款
  282. * @param supplierId
  283. */
  284. @Override
  285. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  286. public void updateAdvanceIn(Long supplierId) {
  287. try{
  288. //查询会员在收预付款单据的总金额
  289. BigDecimal financialAllPrice = accountHeadMapperEx.getFinancialAllPriceByOrganId(supplierId);
  290. //查询会员在零售出库单据的总金额
  291. BigDecimal billAllPrice = depotHeadMapperEx.getBillAllPriceByOrganId(supplierId);
  292. Supplier supplier = new Supplier();
  293. supplier.setId(supplierId);
  294. supplier.setAdvanceIn(financialAllPrice.subtract(billAllPrice));
  295. supplierMapper.updateByPrimaryKeySelective(supplier);
  296. } catch (Exception e){
  297. JshException.writeFail(logger, e);
  298. }
  299. }
  300. @Override
  301. public List<Supplier> findBySelectCus()throws Exception {
  302. SupplierExample example = new SupplierExample();
  303. example.createCriteria().andTypeLike("客户").andEnabledEqualTo(true).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  304. example.setOrderByClause("sort asc, id desc");
  305. List<Supplier> list=null;
  306. try{
  307. list = supplierMapper.selectByExample(example);
  308. }catch(Exception e){
  309. JshException.readFail(logger, e);
  310. }
  311. return list;
  312. }
  313. @Override
  314. public List<Supplier> findBySelectSup()throws Exception {
  315. SupplierExample example = new SupplierExample();
  316. example.createCriteria().andTypeLike("供应商").andEnabledEqualTo(true)
  317. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  318. example.setOrderByClause("sort asc, id desc");
  319. List<Supplier> list=null;
  320. try{
  321. list = supplierMapper.selectByExample(example);
  322. }catch(Exception e){
  323. JshException.readFail(logger, e);
  324. }
  325. return list;
  326. }
  327. @Override
  328. public List<Supplier> findBySelectRetail()throws Exception {
  329. SupplierExample example = new SupplierExample();
  330. example.createCriteria().andTypeLike("会员").andEnabledEqualTo(true)
  331. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  332. example.setOrderByClause("sort asc, id desc");
  333. List<Supplier> list=null;
  334. try{
  335. list = supplierMapper.selectByExample(example);
  336. }catch(Exception e){
  337. JshException.readFail(logger, e);
  338. }
  339. return list;
  340. }
  341. @Override
  342. public List<Supplier> findById(Long supplierId)throws Exception {
  343. SupplierExample example = new SupplierExample();
  344. example.createCriteria().andIdEqualTo(supplierId)
  345. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  346. example.setOrderByClause("sort asc, id desc");
  347. List<Supplier> list=null;
  348. try{
  349. list = supplierMapper.selectByExample(example);
  350. }catch(Exception e){
  351. JshException.readFail(logger, e);
  352. }
  353. return list;
  354. }
  355. @Override
  356. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  357. public int batchSetStatus(Boolean status, String ids)throws Exception {
  358. logService.insertLog("商家",
  359. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ENABLED).toString(),
  360. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  361. List<Long> supplierIds = StringUtil.strToLongList(ids);
  362. Supplier supplier = new Supplier();
  363. supplier.setEnabled(status);
  364. SupplierExample example = new SupplierExample();
  365. example.createCriteria().andIdIn(supplierIds);
  366. int result=0;
  367. try{
  368. result = supplierMapper.updateByExampleSelective(supplier, example);
  369. }catch(Exception e){
  370. JshException.writeFail(logger, e);
  371. }
  372. return result;
  373. }
  374. @Override
  375. public List<Supplier> findUserCustomer()throws Exception{
  376. SupplierExample example = new SupplierExample();
  377. example.createCriteria().andTypeEqualTo("客户")
  378. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  379. example.setOrderByClause("sort asc, id desc");
  380. List<Supplier> list=null;
  381. try{
  382. list = supplierMapper.selectByExample(example);
  383. }catch(Exception e){
  384. JshException.readFail(logger, e);
  385. }
  386. return list;
  387. }
  388. @Override
  389. public List<Supplier> findByAll(String supplier, String type, String phonenum, String telephone) throws Exception{
  390. List<Supplier> list=null;
  391. try{
  392. list = supplierMapperEx.findByAll(supplier, type, phonenum, telephone);
  393. }catch(Exception e){
  394. JshException.readFail(logger, e);
  395. }
  396. return list;
  397. }
  398. @Override
  399. public Map<String, Object> getBeginNeedByOrganId(Long organId) throws Exception {
  400. Supplier supplier = getSupplier(organId);
  401. Map<String, Object> map = new HashMap<>();
  402. BigDecimal needDebt = BigDecimal.ZERO;
  403. if("供应商".equals(supplier.getType())) {
  404. needDebt = supplier.getBeginNeedPay();
  405. } else if("客户".equals(supplier.getType())) {
  406. needDebt = supplier.getBeginNeedGet();
  407. }
  408. BigDecimal finishDebt = accountItemMapperEx.getFinishDebtByOrganId(organId).abs();
  409. BigDecimal eachAmount = BigDecimal.ZERO;
  410. if(needDebt != null) {
  411. eachAmount = needDebt.subtract(finishDebt);
  412. }
  413. //应收欠款
  414. map.put("needDebt", needDebt);
  415. //已收欠款
  416. map.put("finishDebt", finishDebt);
  417. //本次收款
  418. map.put("eachAmount", eachAmount);
  419. return map;
  420. }
  421. @Override
  422. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  423. public void importVendor(MultipartFile file, HttpServletRequest request) throws Exception{
  424. String type = "供应商";
  425. User userInfo = userService.getCurrentUser();
  426. Workbook workbook = Workbook.getWorkbook(file.getInputStream());
  427. Sheet src = workbook.getSheet(0);
  428. //'名称', '联系人', '手机号码', '联系电话', '电子邮箱', '纳税人识别号', '开具发票情况','税率(%)', '开户行', '账号', '地址', '供应商分级', '结算方式', '账单周期', '采购对接人', '到货天数', '备注', '排序', '状态'
  429. List<Supplier> sList = new ArrayList<>();
  430. for (int i = 2; i < src.getRows(); i++) {
  431. String supplierName = ExcelUtils.getContent(src, i, 0);
  432. String enabled = ExcelUtils.getContent(src, i, 18);
  433. if(StringUtil.isNotEmpty(supplierName) && StringUtil.isNotEmpty(enabled)) {
  434. Supplier s = new Supplier();
  435. s.setType(type);
  436. s.setSupplier(supplierName);
  437. s.setContacts(ExcelUtils.getContent(src, i, 1));
  438. s.setTelephone(ExcelUtils.getContent(src, i, 2));
  439. s.setPhoneNum(ExcelUtils.getContent(src, i, 3));
  440. s.setEmail(ExcelUtils.getContent(src, i, 4));
  441. s.setTaxNum(ExcelUtils.getContent(src, i, 5));
  442. s.setInvoiceType(ExcelUtils.getContent(src, i, 6));
  443. s.setTaxRate(parseBigDecimalEx(ExcelUtils.getContent(src, i, 7)));
  444. s.setBankName(ExcelUtils.getContent(src, i, 8));
  445. s.setAccountNumber(ExcelUtils.getContent(src, i, 9));
  446. s.setAddress(ExcelUtils.getContent(src, i, 10));
  447. s.setSupplierLevel(ExcelUtils.getContent(src, i, 11));
  448. s.setSettlementMethod(ExcelUtils.getContent(src, i, 12));
  449. s.setBillingCycleDays(parseIntegerFromExcel(ExcelUtils.getContent(src, i, 13)));
  450. s.setProcurementContact(ExcelUtils.getContent(src, i, 14));
  451. s.setDeliveryDays(parseIntegerFromExcel(ExcelUtils.getContent(src, i, 15)));
  452. s.setDescription(ExcelUtils.getContent(src, i, 16));
  453. s.setSort(ExcelUtils.getContent(src, i, 17));
  454. s.setCreator(userInfo==null?null:userInfo.getId());
  455. s.setEnabled("1".equals(enabled));
  456. sList.add(s);
  457. }
  458. }
  459. importExcel(sList, type, request);
  460. }
  461. @Override
  462. public Integer parseIntegerFromExcel(String content) {
  463. if (content != null && !content.isEmpty()) {
  464. try {
  465. return Integer.valueOf(content);
  466. } catch (NumberFormatException e) {
  467. logger.error("无法解析整数值: {}", content, e);
  468. return null;
  469. }
  470. }
  471. return null;
  472. }
  473. @Override
  474. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  475. public void importCustomer(MultipartFile file, HttpServletRequest request) throws Exception{
  476. String type = "客户";
  477. User userInfo = userService.getCurrentUser();
  478. Workbook workbook = Workbook.getWorkbook(file.getInputStream());
  479. Sheet src = workbook.getSheet(0);
  480. //'名称', '联系人', '手机号码', '联系电话', '电子邮箱', '传真', '期初应收', '纳税人识别号', '税率(%)', '开户行', '账号', '地址', '备注', '排序', '状态'
  481. List<Supplier> sList = new ArrayList<>();
  482. for (int i = 2; i < src.getRows(); i++) {
  483. String supplierName = ExcelUtils.getContent(src, i, 0);
  484. String enabled = ExcelUtils.getContent(src, i, 14);
  485. if(StringUtil.isNotEmpty(supplierName) && StringUtil.isNotEmpty(enabled)) {
  486. Supplier s = new Supplier();
  487. s.setType(type);
  488. s.setSupplier(supplierName);
  489. s.setContacts(ExcelUtils.getContent(src, i, 1));
  490. s.setTelephone(ExcelUtils.getContent(src, i, 2));
  491. s.setPhoneNum(ExcelUtils.getContent(src, i, 3));
  492. s.setEmail(ExcelUtils.getContent(src, i, 4));
  493. s.setFax(ExcelUtils.getContent(src, i, 5));
  494. s.setBeginNeedGet(parseBigDecimalEx(ExcelUtils.getContent(src, i, 6)));
  495. s.setTaxNum(ExcelUtils.getContent(src, i, 7));
  496. s.setTaxRate(parseBigDecimalEx(ExcelUtils.getContent(src, i, 8)));
  497. s.setBankName(ExcelUtils.getContent(src, i, 9));
  498. s.setAccountNumber(ExcelUtils.getContent(src, i, 10));
  499. s.setAddress(ExcelUtils.getContent(src, i, 11));
  500. s.setDescription(ExcelUtils.getContent(src, i, 12));
  501. s.setSort(ExcelUtils.getContent(src, i, 13));
  502. s.setCreator(userInfo==null?null:userInfo.getId());
  503. s.setEnabled("1".equals(enabled));
  504. sList.add(s);
  505. }
  506. }
  507. importExcel(sList, type, request);
  508. }
  509. @Override
  510. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  511. public void importMember(MultipartFile file, HttpServletRequest request) throws Exception{
  512. String type = "会员";
  513. User userInfo = userService.getCurrentUser();
  514. Workbook workbook = Workbook.getWorkbook(file.getInputStream());
  515. Sheet src = workbook.getSheet(0);
  516. //'名称', '联系人', '手机号码', '联系电话', '电子邮箱', '备注', '排序', '状态'
  517. List<Supplier> sList = new ArrayList<>();
  518. for (int i = 2; i < src.getRows(); i++) {
  519. String supplierName = ExcelUtils.getContent(src, i, 0);
  520. String enabled = ExcelUtils.getContent(src, i, 7);
  521. if(StringUtil.isNotEmpty(supplierName) && StringUtil.isNotEmpty(enabled)) {
  522. Supplier s = new Supplier();
  523. s.setType(type);
  524. s.setSupplier(supplierName);
  525. s.setContacts(ExcelUtils.getContent(src, i, 1));
  526. s.setTelephone(ExcelUtils.getContent(src, i, 2));
  527. s.setPhoneNum(ExcelUtils.getContent(src, i, 3));
  528. s.setEmail(ExcelUtils.getContent(src, i, 4));
  529. s.setDescription(ExcelUtils.getContent(src, i, 5));
  530. s.setSort(ExcelUtils.getContent(src, i, 6));
  531. s.setCreator(userInfo==null?null:userInfo.getId());
  532. s.setEnabled("1".equals(enabled));
  533. sList.add(s);
  534. }
  535. }
  536. importExcel(sList, type, request);
  537. }
  538. @Override
  539. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  540. public BaseResponseInfo importExcel(List<Supplier> mList, String type, HttpServletRequest request) throws Exception {
  541. logService.insertLog(type,
  542. new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_IMPORT).append(mList.size()).append(BusinessConstants.LOG_DATA_UNIT).toString(),
  543. ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
  544. BaseResponseInfo info = new BaseResponseInfo();
  545. Map<String, Object> data = new HashMap<>();
  546. try {
  547. for(Supplier supplier: mList) {
  548. SupplierExample example = new SupplierExample();
  549. example.createCriteria().andSupplierEqualTo(supplier.getSupplier()).andTypeEqualTo(type).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  550. List<Supplier> list= supplierMapper.selectByExample(example);
  551. if(list.size() <= 0) {
  552. supplierMapper.insertSelective(supplier);
  553. //新增客户时给当前用户自动授权
  554. setUserCustomerPermission(request, supplier);
  555. } else {
  556. Long id = list.get(0).getId();
  557. supplier.setId(id);
  558. supplierMapper.updateByPrimaryKeySelective(supplier);
  559. }
  560. }
  561. info.code = 200;
  562. data.put("message", "成功");
  563. } catch (Exception e) {
  564. logger.error(e.getMessage(), e);
  565. info.code = 500;
  566. data.put("message", e.getMessage());
  567. }
  568. info.data = data;
  569. return info;
  570. }
  571. @Override
  572. public BigDecimal parseBigDecimalEx(String str)throws Exception{
  573. if(!StringUtil.isEmpty(str)) {
  574. return new BigDecimal(str);
  575. } else {
  576. return null;
  577. }
  578. }
  579. @Override
  580. public File exportExcel(List<Supplier> dataList, String type) throws Exception {
  581. if("供应商".equals(type)) {
  582. return exportExcelVendorOrCustomer(dataList, type);
  583. } else if("客户".equals(type)) {
  584. return exportExcelVendorOrCustomer(dataList, type);
  585. } else {
  586. //会员
  587. String[] names = {"会员卡号*", "联系人", "手机号码", "联系电话", "电子邮箱", "备注", "排序", "状态*"};
  588. String title = "信息内容";
  589. List<String[]> objects = new ArrayList<String[]>();
  590. if (null != dataList) {
  591. for (Supplier s : dataList) {
  592. String[] objs = new String[names.length];
  593. objs[0] = s.getSupplier();
  594. objs[1] = s.getContacts();
  595. objs[2] = s.getTelephone();
  596. objs[3] = s.getPhoneNum();
  597. objs[4] = s.getEmail();
  598. objs[5] = s.getDescription();
  599. objs[6] = s.getSort();
  600. objs[7] = s.getEnabled() ? "1" : "0";
  601. objects.add(objs);
  602. }
  603. }
  604. return ExcelUtils.exportObjectsOneSheet(title, "*导入时本行内容请勿删除,切记!", names, title, objects);
  605. }
  606. }
  607. @Override
  608. public File exportExcelVendorOrCustomer(List<Supplier> dataList, String type) throws Exception {
  609. /*String beginNeedStr = "";
  610. if("供应商".equals(type)) {
  611. beginNeedStr = "期初应付";
  612. } else if("客户".equals(type)) {
  613. beginNeedStr = "期初应收";
  614. }*/
  615. String[] names = {"名称*", "联系人", "手机号码", "联系电话", "电子邮箱",
  616. "纳税人识别号", "发票类型","税率(%)", "开户行", "账号", "地址","供应商分级",
  617. "结算方式", "账单周期", "采购对接人", "到货天数", "备注", "排序", "状态*"};
  618. String title = "信息内容";
  619. List<String[]> objects = new ArrayList<String[]>();
  620. if (null != dataList) {
  621. for (Supplier s : dataList) {
  622. String[] objs = new String[names.length];
  623. objs[0] = s.getSupplier();
  624. objs[1] = s.getContacts();
  625. objs[2] = s.getTelephone();
  626. objs[3] = s.getPhoneNum();
  627. objs[4] = s.getEmail();
  628. /*if(("客户").equals(s.getType())) {
  629. objs[6] = s.getBeginNeedGet() == null? "" : s.getBeginNeedGet().setScale(2,BigDecimal.ROUND_HALF_UP).toString();
  630. } else if(("供应商").equals(s.getType())) {
  631. objs[6] = s.getBeginNeedPay() == null? "" : s.getBeginNeedPay().setScale(2,BigDecimal.ROUND_HALF_UP).toString();
  632. }*/
  633. objs[5] = s.getTaxNum();
  634. // 发票类型
  635. objs[6] = mapInvoiceType(s.getInvoiceType());
  636. // 税率
  637. objs[7] = s.getTaxRate() == null? "" : s.getTaxRate().setScale(2,BigDecimal.ROUND_HALF_UP).toString();
  638. // 银行信息
  639. objs[8] = s.getBankName();
  640. objs[9] = s.getAccountNumber();
  641. objs[10] = s.getAddress();
  642. // 供应商分级
  643. objs[11] = s.getSupplierLevel();
  644. // 结算方式
  645. objs[12] = mapSettlementMethod(s.getSettlementMethod());
  646. // 账单周期
  647. objs[13] = s.getBillingCycleDays() == null? "" : s.getBillingCycleDays().toString();
  648. // 采购对接人
  649. objs[14] = s.getProcurementContact();
  650. // 到货天数
  651. objs[15] = s.getDeliveryDays() == null? "" : s.getDeliveryDays().toString();
  652. objs[16] = s.getDescription();
  653. objs[17] = s.getSort();
  654. objs[18] = s.getEnabled() ? "1" : "0";
  655. objects.add(objs);
  656. }
  657. }
  658. return ExcelUtils.exportObjectsOneSheet(title, "*导入时本行内容请勿删除,切记!", names, title, objects);
  659. }
  660. @Override
  661. public String mapInvoiceType(String invoiceType) {
  662. if ("0".equals(invoiceType)) {
  663. return "普通发票";
  664. } else if ("1".equals(invoiceType)) {
  665. return "增值税专用发票";
  666. }
  667. return "";
  668. }
  669. @Override
  670. public String mapSettlementMethod(String settlementMethod) {
  671. if ("0".equals(settlementMethod)) {
  672. return "周结";
  673. } else if ("1".equals(settlementMethod)) {
  674. return "现结/按单";
  675. } else if ("2".equals(settlementMethod)) {
  676. return "预付款";
  677. } else if ("3".equals(settlementMethod)) {
  678. return "月结";
  679. }
  680. return "";
  681. }
  682. /**
  683. * 新增客户时给当前用户自动授权
  684. * @param request
  685. * @param supplier
  686. * @throws Exception
  687. */
  688. @Override
  689. public void setUserCustomerPermission(HttpServletRequest request, Supplier supplier) throws Exception {
  690. if("客户".equals(supplier.getType())) {
  691. Long userId = userService.getUserId(request);
  692. Supplier sInfo = supplierMapperEx.getSupplierByNameAndType(supplier.getSupplier(), supplier.getType());
  693. String ubKey = "[" + sInfo.getId() + "]";
  694. List<UserBusiness> ubList = userBusinessService.getBasicData(userId.toString(), "UserCustomer");
  695. if(ubList ==null || ubList.size() == 0) {
  696. JSONObject ubObj = new JSONObject();
  697. ubObj.put("type", "UserCustomer");
  698. ubObj.put("keyId", userId);
  699. ubObj.put("value", ubKey);
  700. UserBusiness userBusiness = JSONObject.parseObject(ubObj.toJSONString(), UserBusiness.class);
  701. userBusinessMapper.insertSelective(userBusiness);
  702. } else {
  703. UserBusiness ubInfo = ubList.get(0);
  704. JSONObject ubObj = new JSONObject();
  705. ubObj.put("id", ubInfo.getId());
  706. ubObj.put("type", ubInfo.getType());
  707. ubObj.put("keyId", ubInfo.getKeyId());
  708. ubObj.put("value", ubInfo.getValue() + ubKey);
  709. UserBusiness userBusiness = JSONObject.parseObject(ubObj.toJSONString(), UserBusiness.class);
  710. userBusinessMapper.updateByPrimaryKeySelective(userBusiness);
  711. }
  712. }
  713. }
  714. @Override
  715. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  716. public int batchSetAdvanceIn(String ids) throws Exception {
  717. int res = 0;
  718. List<Long> idList = StringUtil.strToLongList(ids);
  719. for(Long sId: idList) {
  720. updateAdvanceIn(sId);
  721. res = 1;
  722. }
  723. return res;
  724. }
  725. @Override
  726. public Supplier getSupplierByPhone(String type, String telephone) {
  727. List<Supplier> supplierList = supplierMapper.selectList(new LambdaQueryWrapperX<Supplier>().eq(Supplier::getType, type).eq(Supplier::getTelephone, telephone));
  728. if(CollectionUtils.isEmpty(supplierList)){
  729. return null;
  730. }
  731. return supplierList.get(0);
  732. }
  733. @Override
  734. public Supplier getCustomerByPhone(String telephone) {
  735. return getSupplierByPhone("客户", telephone);
  736. }
  737. @Override
  738. public Supplier createCustomer(Supplier supplier) {
  739. supplier.setType("客户");
  740. supplierMapper.insertSelective(supplier);
  741. return supplier;
  742. }
  743. }