SupplierServiceImpl.java 33 KB

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