CustomerService.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package com.dk.mdm.service.mst;
  2. import com.dk.common.infrastructure.annotaiton.Pagination;
  3. import com.dk.common.infrastructure.constant.Constant;
  4. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  5. import com.dk.common.model.pojo.PageList;
  6. import com.dk.common.model.pojo.mst.StaffRight;
  7. import com.dk.common.model.response.mst.OrgResponse;
  8. import com.dk.common.model.response.mst.StaffResponse;
  9. import com.dk.common.response.ResponseCodeEnum;
  10. import com.dk.common.response.ResponseResultUtil;
  11. import com.dk.common.response.ResponseResultVO;
  12. import com.dk.common.util.ExcelUtils;
  13. import com.dk.mdm.infrastructure.convert.mst.CustomerConvert;
  14. import com.dk.mdm.infrastructure.util.AuthUtils;
  15. import com.dk.mdm.mapper.common.CommonMapper;
  16. import com.dk.mdm.mapper.core.OrganizationMapper;
  17. import com.dk.mdm.mapper.mst.OrgMapper;
  18. import com.dk.mdm.mapper.mst.StaffMapper;
  19. import com.dk.mdm.model.pojo.mst.Customer;
  20. import com.dk.mdm.mapper.mst.CustomerMapper;
  21. import com.dk.common.service.BaseService;
  22. import com.dk.common.mapper.BaseMapper;
  23. import com.dk.mdm.model.pojo.mst.DictionaryData;
  24. import com.dk.mdm.model.pojo.mst.GoodsSku;
  25. import com.dk.mdm.model.query.mst.CustomerQuery;
  26. import com.dk.mdm.model.query.mst.OrgQuery;
  27. import com.dk.mdm.model.query.mst.StaffQuery;
  28. import com.dk.mdm.model.response.mst.CustomerResponse;
  29. import com.dk.mdm.model.vo.mac.*;
  30. import com.dk.mdm.model.vo.mst.CustomerVO;
  31. import com.dk.mdm.model.vo.mst.DictionaryDataVO;
  32. import com.dk.mdm.service.common.CommonService;
  33. import com.dk.mdm.service.mac.OtherPayableService;
  34. import com.dk.mdm.service.mac.OtherReceivableService;
  35. import com.dk.mdm.service.mac.ReceiptService;
  36. import org.apache.poi.ss.usermodel.Workbook;
  37. import org.springframework.stereotype.Service;
  38. import org.springframework.beans.factory.annotation.Autowired;
  39. import org.springframework.transaction.annotation.Transactional;
  40. import org.springframework.web.bind.annotation.PathVariable;
  41. import javax.servlet.http.HttpServletResponse;
  42. import java.io.File;
  43. import java.io.FileOutputStream;
  44. import java.io.IOException;
  45. import java.math.BigDecimal;
  46. import java.time.LocalDate;
  47. import java.time.LocalDateTime;
  48. import java.util.*;
  49. @Service
  50. @Transactional
  51. public class CustomerService extends BaseService<Customer> {
  52. @Override
  53. public BaseMapper<Customer> getRepository() {
  54. return customerMapper;
  55. }
  56. @Autowired
  57. private CustomerMapper customerMapper;
  58. @Autowired
  59. private CustomerConvert customerConvert;
  60. @Autowired
  61. private CommonService commonService;
  62. @Autowired
  63. private OrgService orgService;
  64. @Autowired
  65. private CommonMapper commonMapper;
  66. @Autowired
  67. private OtherReceivableService otherReceivableService;
  68. @Autowired
  69. private ReceiptService receiptService;
  70. @Autowired
  71. private AuthUtils authUtils;
  72. @Autowired
  73. private OrgMapper orgMapper;
  74. @Autowired
  75. private StaffMapper staffMapper;
  76. @Autowired
  77. private DictionaryDataService dictionaryDataService;
  78. /**
  79. * @desc : 重写主键
  80. * @author : 于继渤
  81. * @date : 2024/2/29 20:29
  82. */
  83. @Override
  84. public String getPrimaryKey() {
  85. return "cus_id";
  86. }
  87. /**
  88. * @desc : 查询
  89. * @author : 于继渤
  90. * @date : 2023/1/5 9:39
  91. */
  92. @Pagination
  93. public ResponseResultVO<PageList<CustomerResponse>> selectByCond(CustomerQuery customerQuery) {
  94. return super.mergeListWithCount(customerQuery, customerMapper.selectByCond(customerQuery),
  95. customerMapper.countByCond(customerQuery));
  96. }
  97. /**
  98. * @desc : 新建
  99. * @author : 于继渤
  100. * @date : 2023/1/5 9:39
  101. */
  102. @Transactional(
  103. rollbackFor = {Exception.class}
  104. )
  105. public ResponseResultVO<?> insert(CustomerVO customerVO) {
  106. Customer customer = customerConvert.convertToPo(customerVO);
  107. //设置编码
  108. Map<String, Object> uniqueNoteCode = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
  109. customer.setCusId(uniqueNoteCode.get("outId").toString());
  110. customer.setCusCode(uniqueNoteCode.get("outNote").toString());
  111. // 同商户 电话不同
  112. Long count = customerMapper.countByCond(
  113. new CustomerQuery().setCpId(customer.getCpId()).setCusPhone(customer.getCusPhone()).setFlgValid(true)
  114. );
  115. if (count > 0) {
  116. return ResponseResultUtil.error(ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getCode(),
  117. ErrorCodeEnum.CUSTOMER_SAME_COMPANY_TELEPHONE.getMessage());
  118. }
  119. List<String> followStaff = new ArrayList<>();
  120. String staffId = authUtils.getStaff().getStaffId();
  121. followStaff.add(staffId);
  122. customer.setFollowStaffs(followStaff);
  123. if (customer.getSaleStatus() == null) {
  124. //新建默认就是潜客
  125. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  126. }
  127. //报备人
  128. customer.setReportStaff(staffId);
  129. //报备日期
  130. customer.setReportTime(LocalDateTime.now());
  131. // 如果有初始欠款,那么就是成交状态
  132. if(customerVO != null && customerVO.getStartAmount() != null && customerVO.getStartAmount().compareTo(BigDecimal.ZERO) != 0){
  133. customer.setSaleStatus(Constant.SaleStatus.CHENGJIAO.getName());
  134. }
  135. ResponseResultVO<?> insert = super.insert(customer);
  136. //说明有起始欠款 需要插入其他支出
  137. if (customerVO != null && customerVO.getInitialPaymentId() != null
  138. && customerVO.getInitialPaymentId().equals(Constant.initialPaymentType.DEBT.getName())) {
  139. //先组装明细
  140. List<OtherReceivableItemVO> itemList = new ArrayList<>();
  141. OtherReceivableItemVO otherReceivableItemVO = new OtherReceivableItemVO();
  142. Map<String, Object> param = new HashMap<>();
  143. param.put("cpId", authUtils.getStaff().getCpId());
  144. param.put("dictCode", "基础资料-收入");
  145. param.put("dataValue", "期初");
  146. Map<String, Object> data = commonMapper.selectDictionaryData(param);
  147. if (data != null && data.size() > 0) {
  148. String dataId = String.valueOf(data.get("dataId"));
  149. otherReceivableItemVO.setReceivableType(dataId);
  150. otherReceivableItemVO.setAmtReceivable(customerVO.getStartAmount());
  151. itemList.add(otherReceivableItemVO);
  152. //调用 新建其他支出
  153. OtherReceivableVO otherReceivableVO = new OtherReceivableVO().setBusinessType(0).setObjectId(customer.getCusId())
  154. .setSumAmtReceivable(customerVO.getStartAmount()).setStaffId(customer.getStaffId())
  155. .setOrgId(customer.getOrgId()).setAccDate(LocalDate.now()).setMakeStaff(customer.getReportStaff())
  156. .setItemList(itemList).setSumAmtRec(BigDecimal.ZERO);
  157. otherReceivableService.insert(otherReceivableVO);
  158. }else{
  159. // 插入期初
  160. DictionaryDataVO dictionaryDataVO = new DictionaryDataVO();
  161. dictionaryDataVO.setDictCode("基础资料-收入");
  162. dictionaryDataVO.setCpId(authUtils.getStaff().getCpId());
  163. dictionaryDataVO.setDataValue("期初");
  164. ResponseResultVO<?> insertDic = dictionaryDataService.insert(dictionaryDataVO);
  165. if (insertDic.getCode() == ResponseCodeEnum.SUCCESS.getCode()) {
  166. DictionaryData dataDic = (DictionaryData)insertDic.getData();
  167. String dataId = dataDic.getDataId();
  168. otherReceivableItemVO.setReceivableType(dataId);
  169. otherReceivableItemVO.setAmtReceivable(customerVO.getStartAmount());
  170. itemList.add(otherReceivableItemVO);
  171. //调用 新建其他支出
  172. OtherReceivableVO otherReceivableVO = new OtherReceivableVO().setBusinessType(0).setObjectId(customer.getCusId())
  173. .setSumAmtReceivable(customerVO.getStartAmount()).setStaffId(customer.getStaffId())
  174. .setOrgId(customer.getOrgId()).setAccDate(LocalDate.now()).setMakeStaff(customer.getReportStaff())
  175. .setItemList(itemList).setSumAmtRec(BigDecimal.ZERO);
  176. otherReceivableService.insert(otherReceivableVO);
  177. } else {
  178. return ResponseResultUtil.error(1111,"基础资料-收入的期初插入失败");
  179. }
  180. }
  181. }
  182. //初始款项-预收 240530
  183. else if (customerVO != null && customerVO.getInitialPaymentId() != null
  184. && customerVO.getInitialPaymentId().equals(Constant.initialPaymentType.PAYMENT.getName())) {
  185. //先组装明细
  186. List<RecPayItemVO> itemList = new ArrayList<>();
  187. RecPayItemVO recPayItemVO = new RecPayItemVO();
  188. recPayItemVO.setAccDate(LocalDate.now());
  189. recPayItemVO.setCpId(customer.getCpId());
  190. recPayItemVO.setMacId(customerVO.getMacId());
  191. recPayItemVO.setAmtRec(customerVO.getStartAmount());
  192. recPayItemVO.setMakeStaff(customer.getReportStaff());
  193. itemList.add(recPayItemVO);
  194. RecPayVO recPayVO = new RecPayVO().setObjectId(customer.getCusId()).setAccDate(LocalDate.now())
  195. .setSumAmtRec(customerVO.getStartAmount()).setStaffId(customer.getStaffId())
  196. .setOrgId(customer.getOrgId()).setMakeStaff(customer.getReportStaff())
  197. .setCpId(customer.getCpId()).setRpType(Constant.RpType.SHOU_KUAN.getName())
  198. .setItemList(itemList);
  199. receiptService.insertReceipt(recPayVO);
  200. }
  201. if (insert.getCode() == ResponseCodeEnum.SUCCESS.getCode()) {
  202. return ResponseResultUtil.success(customer);
  203. } else {
  204. return insert;
  205. }
  206. }
  207. /**
  208. * @desc : 编辑
  209. * @author : 于继渤
  210. * @date : 2023/1/5 9:39
  211. */
  212. @Transactional(
  213. rollbackFor = {Exception.class}
  214. )
  215. public ResponseResultVO<?> update(CustomerVO customerVO) {
  216. Customer customer = customerConvert.convertToPo(customerVO);
  217. super.updateByUuid(customer);
  218. return ResponseResultUtil.success();
  219. }
  220. /**
  221. * @desc : 跟据id查询
  222. * @author : 于继渤
  223. * @date : 2023/1/5 9:39
  224. */
  225. public ResponseResultVO selectCustomerById(String id) {
  226. CustomerResponse customerResponse = customerMapper.selectCustomerById(id);
  227. if (customerResponse == null) {
  228. return ResponseResultUtil.error(ResponseCodeEnum.SELECT_NULL);
  229. }
  230. return ResponseResultUtil.success(customerResponse);
  231. }
  232. /**
  233. * @desc : 跟据id查询
  234. * @author : 于继渤
  235. * @date : 2023/1/5 9:39
  236. */
  237. public ResponseResultVO<CustomerResponse> selectByIdRespone(String id) {
  238. CustomerResponse customerResponse = customerMapper.selectByIdRespone(id);
  239. if (customerResponse == null) {
  240. return ResponseResultUtil.error(ResponseCodeEnum.SELECT_NULL);
  241. }
  242. return ResponseResultUtil.success(customerResponse);
  243. }
  244. /**
  245. * @desc : 不分页查询
  246. * @author : 于继渤
  247. * @date : 2023/1/5 9:39
  248. */
  249. public ResponseResultVO<List<CustomerResponse>> selectByCondNoPage(CustomerQuery customerQuery) {
  250. List<CustomerResponse> customerResponses = customerMapper.selectByCond(customerQuery);
  251. return ResponseResultUtil.success(customerResponses);
  252. }
  253. /**
  254. * @desc : 导入
  255. * @author : 常皓宁
  256. * @date : 2024/6/13 14:21
  257. */
  258. @Transactional(rollbackFor = {Exception.class})
  259. public ResponseResultVO<String> importCUS(List<Customer> list, HttpServletResponse response, @PathVariable Integer cpId) {
  260. List<Customer> messageList = new ArrayList<>();
  261. Workbook workbook = null;
  262. Boolean importMessage = false;
  263. //规则校验
  264. for (Customer customer : list) {
  265. customer.setCpId(cpId);
  266. customer.setErrorMessage("");
  267. //TODO 校验规则
  268. //客户编号不能为空
  269. if(customer.getCusCode() == null){
  270. customer.setErrorMessage(customer.getErrorMessage() + "客户编号不能为空,");
  271. importMessage = true;
  272. }
  273. //客户编号不能重复
  274. if(customer.getCusCode() != null){
  275. // 同商户 客编不同
  276. Long count = customerMapper.countByCond(
  277. new CustomerQuery().setCpId(customer.getCpId()).setCusCode(customer.getCusCode()).setFlgValid(true)
  278. );
  279. if (count > 0) {
  280. customer.setErrorMessage(customer.getErrorMessage() + "客户编码已存在,");
  281. importMessage = true;
  282. }
  283. }
  284. //客户名称不能为空
  285. if(customer.getCusName() == null){
  286. customer.setErrorMessage(customer.getErrorMessage() + "客户名称不能为空,");
  287. importMessage = true;
  288. }
  289. //电话不能相同
  290. if(customer.getCusPhone() != null){
  291. // 同商户 电话不同
  292. Long count = customerMapper.countByCond(
  293. new CustomerQuery().setCpId(customer.getCpId()).setCusPhone(customer.getCusPhone()).setFlgValid(true)
  294. );
  295. if (count > 0) {
  296. customer.setErrorMessage(customer.getErrorMessage() + "当前电话号码已存在,");
  297. importMessage = true;
  298. }
  299. //客户电话规则校验
  300. if(!customer.getCusPhone().matches("^1[34578]\\d{9}$")){
  301. customer.setErrorMessage(customer.getErrorMessage() + "客户电话规则不正确,");
  302. importMessage = true;
  303. }
  304. }
  305. //客户电话不能为空
  306. else{
  307. customer.setErrorMessage(customer.getErrorMessage() + "客户电话不能为空,");
  308. importMessage = true;
  309. }
  310. //联系电话规则校验
  311. if(!customer.getContactPhone().matches("^1[34578]\\d{9}$")){
  312. customer.setErrorMessage(customer.getErrorMessage() + "联系电话规则不正确,");
  313. importMessage = true;
  314. }
  315. //部门
  316. if(customer.getOrgName() != null){
  317. String orgName = customer.getOrgName();
  318. OrgResponse orgResponse = orgMapper.selectByUuId(new OrgQuery().setOrgName(orgName).setCpId(cpId));
  319. if(orgResponse == null){
  320. customer.setErrorMessage(customer.getErrorMessage() + "部门填写错误,");
  321. importMessage = true;
  322. }else{
  323. customer.setOrgId(orgResponse.getOrgId());
  324. }
  325. }else{
  326. customer.setErrorMessage(customer.getErrorMessage() + "部门不能为空");
  327. importMessage = true;
  328. }
  329. //业务员
  330. if(customer.getStaffCode() != null){
  331. String staffCode = customer.getStaffCode();
  332. StaffResponse staffResponse = staffMapper.selectByUuId(new StaffQuery().setStaffCode(staffCode).setCpId(cpId));
  333. if(staffResponse == null){
  334. customer.setErrorMessage(customer.getErrorMessage() + "业务员编码填写错误,");
  335. importMessage = true;
  336. }else{
  337. customer.setStaffId(staffResponse.getStaffId());
  338. }
  339. }else{
  340. customer.setErrorMessage(customer.getErrorMessage() + "业务员编码不能为空,");
  341. importMessage = true;
  342. }
  343. //报备人
  344. if(customer.getReportStaffCode() != null){
  345. String reportStaffCode = customer.getReportStaffCode();
  346. StaffResponse staffResponse = staffMapper.selectByUuId(new StaffQuery().setStaffCode(reportStaffCode).setCpId(cpId));
  347. if(staffResponse == null){
  348. customer.setErrorMessage(customer.getErrorMessage() + "报备人编码填写错误,");
  349. importMessage = true;
  350. }else{
  351. customer.setReportStaff(staffResponse.getStaffId());
  352. }
  353. }else{
  354. customer.setErrorMessage(customer.getErrorMessage() + "报备人编码不能为空,");
  355. importMessage = true;
  356. }
  357. messageList.add(customer);
  358. }
  359. //导出错误提示
  360. if (importMessage){
  361. workbook = ExcelUtils.exportExcelUrl(messageList,"错误数据反馈","错误数据反馈",Customer.class,"客户档案错误数据反馈.xls", response);
  362. UUID uuid = UUID.randomUUID();
  363. String randomUUIDString = uuid.toString();
  364. //TODO 导出文件位置
  365. String fileName = "D:/练习/" + randomUUIDString;
  366. File outputFile = new File(fileName);
  367. //创建文件夹
  368. if(!outputFile.exists()){
  369. outputFile.mkdir();
  370. }
  371. File uploadFile = new File(fileName,"客户档案错误数据反馈.xls");
  372. try (FileOutputStream fos = new FileOutputStream(uploadFile)) {
  373. workbook.write(fos);
  374. } catch (IOException e) {
  375. e.printStackTrace();
  376. }
  377. return ResponseResultUtil.error(-300,"D:/练习/" + randomUUIDString + "/客户档案错误数据反馈.xls");
  378. }
  379. else{
  380. //插入数据
  381. for(Customer customer:messageList){
  382. //新建默认就是潜客
  383. customer.setSaleStatus(Constant.saleCustomerStatusConstant.SALE_STATUS_POTE.getName());
  384. //报备日期
  385. customer.setReportTime(LocalDateTime.now());
  386. super.insert(customer);
  387. }
  388. return ResponseResultUtil.success();
  389. }
  390. }
  391. }