ShopServiceImpl.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. package com.caimei.service.impl;
  2. import com.caimei.mapper.ShopMapper;
  3. import com.caimei.mapper.UserMapper;
  4. import com.caimei.model.ResponseJson;
  5. import com.caimei.model.dto.ShopInfoDto;
  6. import com.caimei.model.po.CmBrandAuthFilePo;
  7. import com.caimei.model.po.UserPo;
  8. import com.caimei.model.vo.*;
  9. import com.caimei.service.ShopService;
  10. import com.caimei.utils.*;
  11. import com.github.pagehelper.PageHelper;
  12. import com.github.pagehelper.PageInfo;
  13. import lombok.extern.slf4j.Slf4j;
  14. import org.apache.commons.codec.digest.DigestUtils;
  15. import org.apache.commons.lang3.StringUtils;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import javax.annotation.Resource;
  19. import java.io.File;
  20. import java.io.FileInputStream;
  21. import java.util.*;
  22. /**
  23. * Description
  24. *
  25. * @author : Aslee
  26. * @date : 2021/5/11
  27. */
  28. @Slf4j
  29. @Service
  30. public class ShopServiceImpl implements ShopService {
  31. @Resource
  32. private ShopMapper shopMapper;
  33. @Resource
  34. private UserMapper userMapper;
  35. @Override
  36. public ResponseJson<PageInfo<ShopListVo>> getShopList(Integer listType, String shopName, Integer shopType, Integer brandId, String mobile, String linkMan, Integer lowerAuditStatus, Integer pageNum, Integer pageSize) {
  37. listType = null == listType ? 1 : listType;
  38. PageHelper.startPage(pageNum, pageSize);
  39. List<ShopListVo> shopList = shopMapper.getShopList(listType, shopName, shopType, brandId, mobile, linkMan, lowerAuditStatus);
  40. ListIterator<ShopListVo> iterator = shopList.listIterator();
  41. while (iterator.hasNext()) {
  42. // 根据是否完成商品信息审核筛选项,设置下级审核状态
  43. ShopListVo shop = iterator.next();
  44. int articleWaitNum = shop.getArticleWaitNum();
  45. int imageWaitNum = shop.getImageWaitNum();
  46. int videoWaitNum = shop.getVideoWaitNum();
  47. int fileWaitNum = shop.getFileWaitNum();
  48. int doctorWaitNum = shop.getDoctorWaitNum();
  49. int waitAuditNum = 2 == listType ? shop.getWaitAuditNum() : (3 == listType ? (articleWaitNum + imageWaitNum + videoWaitNum + fileWaitNum) : (4 == listType ? doctorWaitNum : 0));
  50. if (waitAuditNum > 0) {
  51. shop.setLowerAuditStatus(0);
  52. }else {
  53. shop.setLowerAuditStatus(1);
  54. }
  55. }
  56. PageInfo<ShopListVo> pageData = new PageInfo<>(shopList);
  57. return ResponseJson.success(pageData);
  58. }
  59. @Override
  60. public ResponseJson updateShopStatus(Integer authUserId, Integer status) {
  61. if (authUserId == null) {
  62. return ResponseJson.error("请输入用户id");
  63. }
  64. if (status == null) {
  65. return ResponseJson.error("请输入要更新的状态值");
  66. } else if (status != 0 && status != 1) {
  67. return ResponseJson.error("状态值只能为0或1");
  68. }
  69. shopMapper.updateShopStatusByUserId(authUserId, status);
  70. if (status == 0) {
  71. return ResponseJson.success("停用供应商成功");
  72. } else {
  73. return ResponseJson.success("启用供应商成功");
  74. }
  75. }
  76. @Override
  77. public ResponseJson resetShopPassword(Integer authUserId) {
  78. if (authUserId == null) {
  79. return ResponseJson.error("请输入用户id");
  80. }
  81. String newPassword = CodeUtil.generateCode(8);
  82. String md5Password = Md5Util.md5(newPassword);
  83. userMapper.updatePasswordByUserId(authUserId, md5Password);
  84. String mobile = shopMapper.getShopMobileByUserId(authUserId);
  85. boolean smsFlag = AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}");
  86. if (!smsFlag) {
  87. // 短信发送失败重试一次
  88. AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}");
  89. }
  90. log.info("供应商重置密码,用户id:" + authUserId);
  91. return ResponseJson.success("密码重置成功");
  92. }
  93. @Override
  94. public ResponseJson<AuthFileVo> uploadFile(Integer authUserId, Integer brandId, MultipartFile file) {
  95. authUserId = (null != authUserId && authUserId > 0) ? authUserId : null;
  96. String fileAllName = file.getOriginalFilename();
  97. String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
  98. String fileName = file.getResource().getFilename();
  99. String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  100. String filePath = uuid + "." + fileType;
  101. String contentType = OSSUtils.getContentType(fileAllName);
  102. log.info(">>>>>>>>>>>>>上传文件,用户id:" + authUserId);
  103. try {
  104. //保存本地
  105. File uploadFile = OSSUtils.ossUpload(file);
  106. //判断文件的唯一性,转换成16进制md5值
  107. String md5Hex = DigestUtils.md5Hex(new FileInputStream(uploadFile));
  108. CmBrandAuthFilePo cmBrandAuthFile = null;
  109. if (null != authUserId) {
  110. // 查找该供应商下是否已存在相同文件
  111. CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo();
  112. searchFile.setAuthUserId(authUserId);
  113. searchFile.setBrandId(brandId);
  114. searchFile.setMd5Hex(md5Hex);
  115. cmBrandAuthFile = shopMapper.getStatementFile(searchFile);
  116. }
  117. if (cmBrandAuthFile == null) {
  118. log.info("默认路径>>>" + uploadFile.getAbsolutePath());
  119. // 修改情况下,如果原来已经选择了文件代理声明,并且旧文件与新文件不同,需要将旧文件删除
  120. if (null != authUserId) {
  121. deleteFile(authUserId, brandId);
  122. }
  123. // 查找oss中是否已存在该文件,若存在则不需要重新上传
  124. CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo();
  125. searchFile.setMd5Hex(md5Hex);
  126. CmBrandAuthFilePo sameFile = shopMapper.getStatementFile(searchFile);
  127. if (null == sameFile) {
  128. //将新文件上传oss
  129. OSSUtils.ossUpload(filePath, uploadFile, contentType, null);
  130. }
  131. //删除本地文件
  132. OSSUtils.deleteFile(uploadFile);
  133. //保存关联关系
  134. cmBrandAuthFile = new CmBrandAuthFilePo();
  135. cmBrandAuthFile.setAuthUserId(authUserId);
  136. cmBrandAuthFile.setBrandId(brandId);
  137. cmBrandAuthFile.setName(fileName);
  138. cmBrandAuthFile.setOssName(null == sameFile ? filePath : sameFile.getOssName());
  139. cmBrandAuthFile.setMd5Hex(md5Hex);
  140. cmBrandAuthFile.setUploadTime(new Date());
  141. shopMapper.insertStatementFile(cmBrandAuthFile);
  142. } else {
  143. //删除本地文件
  144. OSSUtils.deleteFile(uploadFile);
  145. }
  146. AuthFileVo fileVo = new AuthFileVo();
  147. fileVo.setFileId(cmBrandAuthFile.getId());
  148. fileVo.setFileName(cmBrandAuthFile.getName());
  149. return ResponseJson.success("文件上传成功", fileVo);
  150. } catch (Exception e) {
  151. log.error("<<<<< 文件上传异常 >>>>>");
  152. return ResponseJson.error("文件上传失败", null);
  153. }
  154. }
  155. @Override
  156. public void deleteFile(Integer authUserId, Integer brandId) {
  157. CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo();
  158. searchFile.setAuthUserId(authUserId);
  159. searchFile.setBrandId(brandId);
  160. CmBrandAuthFilePo oldFile = shopMapper.getStatementFile(searchFile);
  161. if (oldFile != null) {
  162. Integer num = shopMapper.getFileNumByMd5Hex(oldFile.getMd5Hex());
  163. log.info(">>>>>>>>>>>>>>>文件使用数:" + num);
  164. if (num == 1) {
  165. log.info(">>>>>>>>>>>>>>>删除文件:" + oldFile.getOssName());
  166. // 如果这个文件只有这个供应商在使用,删除oss服务器上的文件
  167. OSSUtils.deleteSingleFile(oldFile.getOssName());
  168. }
  169. shopMapper.deleteStatementFile(oldFile.getId());
  170. }
  171. }
  172. @Override
  173. public ResponseJson saveShop(Integer authUserId, Integer shopType, String shopName, String mobile, String linkMan, Integer shopStatus, String logo, String qrCodeImage, Integer wxAccountType, String appId, String appSecret, Integer createBy, List<ShopInfoDto> shopInfoList) {
  174. // 是否为添加操作
  175. boolean insertFlag = null == authUserId;
  176. Integer userIdByMobile = shopMapper.getUserIdByMobile(mobile);
  177. Integer userIdByAppId = shopMapper.getUserIdByAppId(appId);
  178. if (insertFlag) {
  179. // 添加时验证手机号是否已被使用
  180. if (null != userIdByMobile) {
  181. return ResponseJson.error("该手机号已被使用,请重新输入", null);
  182. }
  183. // 添加时验证供应商名称是否已被使用
  184. Integer userIdByShopName = shopMapper.getUserIdByShopName(shopName);
  185. if (null != userIdByShopName) {
  186. return ResponseJson.error("该供应商名称已经存在,请重新输入", null);
  187. }
  188. // 添加时验证appId是否已被使用
  189. if (StringUtils.isNotEmpty(appId) && null != userIdByAppId) {
  190. return ResponseJson.error("该appId已被使用,请重新输入", null);
  191. }
  192. } else {
  193. // 修改时验证新手机号是否已被使用
  194. if (null != userIdByMobile && !userIdByMobile.equals(authUserId)) {
  195. return ResponseJson.error("该手机号已被使用,请重新输入", null);
  196. }
  197. // 修改时验证新appId是否已被使用
  198. if (null != userIdByAppId && !userIdByAppId.equals(authUserId)) {
  199. return ResponseJson.error("该appId已被使用,请重新输入", null);
  200. }
  201. }
  202. // 更新品牌授权logo
  203. shopInfoList.forEach(shopInfo->{
  204. shopMapper.updateBrandAuthLogo(shopInfo.getBrandId(), shopInfo.getBrandAuthLogo());
  205. });
  206. /*
  207. 组装供应商用户数据
  208. */
  209. UserPo shop = new UserPo();
  210. // 供应商名称
  211. shop.setName(shopName);
  212. // 手机号
  213. shop.setMobile(mobile);
  214. // 联系人
  215. shop.setLinkMan(linkMan);
  216. // 供应商状态
  217. shop.setStatus(shopStatus);
  218. // 代理商logo
  219. shop.setLogo(logo);
  220. // 公众号二维码图片
  221. shop.setQrCodeImage(qrCodeImage);
  222. // 公众号类型
  223. shop.setWxAccountType(wxAccountType);
  224. // 公众号appId
  225. shop.setAppId(appId);
  226. // 公众号appSecret
  227. shop.setAppSecret(appSecret);
  228. if (insertFlag) {
  229. // 用户身份:1管理员,2供应商
  230. shop.setUserIdentity(2);
  231. // 供应商类型
  232. shop.setShopType(shopType);
  233. // 创建管理员id
  234. shop.setCreateBy(createBy);
  235. // 创建时间
  236. shop.setCreateTime(new Date());
  237. // 设置随机8位密码
  238. String password = CodeUtil.generateCode(8);
  239. String md5Pwd = Md5Util.md5(password);
  240. shop.setPassword(md5Pwd);
  241. // 插入供应商用户
  242. shopMapper.insertShop(shop);
  243. // 发送短信
  244. boolean smsFlag = AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + password + "\"}");
  245. if (!smsFlag) {
  246. // 短信发送失败重试一次
  247. AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + password + "\"}");
  248. }
  249. log.info("添加供应商,供应商用户id:" + shop.getAuthUserId());
  250. } else {
  251. shop.setAuthUserId(authUserId);
  252. // 更新供应商用户
  253. shopMapper.updateShopByUserId(shop);
  254. log.info("更新供应商,供应商用户id:" + shop.getAuthUserId());
  255. }
  256. /*
  257. 组装供应商信息数据
  258. */
  259. if (insertFlag) {
  260. shopInfoList.forEach(shopInfo->{
  261. shopInfo.setAuthUserId(shop.getAuthUserId());
  262. // 插入供应商信息
  263. shopMapper.insertShopInfo(shopInfo);
  264. if (4 == shopInfo.getStatementType()) {
  265. shopMapper.updateFileUserId(shopInfo.getStatementFileId(), shop.getAuthUserId());
  266. }
  267. });
  268. } else {
  269. // 数据库中供应商信息数据
  270. List<ShopBrandVo> dbInfoBrandList = shopMapper.getDbInfoBrandList(shop.getAuthUserId());
  271. // 编辑后的品牌列表
  272. List<Integer> newInfoBrandList = new ArrayList<>();
  273. // 编辑后的品牌列表和数据库中的品牌列表重复的需要更新的部分
  274. List<Integer> updateInfoBrandList = new ArrayList<>();
  275. // 编辑后的供应商品牌id
  276. shopInfoList.forEach(shopInfo->{
  277. newInfoBrandList.add(shopInfo.getBrandId());
  278. });
  279. for (ShopBrandVo shopBrand : dbInfoBrandList) {
  280. // 判断被删除的品牌下是否还有未删除的商品,若存在,提示供应商需要删除后才能删除品牌
  281. if (!newInfoBrandList.contains(shopBrand.getBrandId())) {
  282. Integer productCount = shopMapper.getProductCount(shop.getAuthUserId(), shopBrand.getBrandId());
  283. if (null != productCount && productCount > 0) {
  284. return ResponseJson.error("该品牌已绑定供应商商品,无法进行删除");
  285. }
  286. }
  287. }
  288. for (ShopBrandVo shopBrand : dbInfoBrandList) {
  289. if (!newInfoBrandList.contains(shopBrand.getBrandId())) {
  290. // 删除被删除的数据
  291. shopMapper.deleteShopInfoById(shopBrand.getId());
  292. // 删除文件
  293. deleteFile(authUserId, shopBrand.getBrandId());
  294. } else {
  295. // 保存数据库已有且未被删除的供应商品牌id
  296. updateInfoBrandList.add(shopBrand.getBrandId());
  297. }
  298. }
  299. shopInfoList.forEach(shopInfoVo -> {
  300. shopInfoVo.setAuthUserId(shop.getAuthUserId());
  301. // 保存供应商信息数据
  302. if (updateInfoBrandList.contains(shopInfoVo.getBrandId())) {
  303. // 更新
  304. shopMapper.updateShopInfo(shopInfoVo);
  305. if (4 != shopInfoVo.getStatementType()) {
  306. // 没有选择文件代理声明的情况下,若存在原来的文件,删除代理声明文件
  307. deleteFile(shop.getAuthUserId(),shopInfoVo.getBrandId());
  308. }
  309. } else {
  310. // 插入
  311. shopMapper.insertShopInfo(shopInfoVo);
  312. }
  313. });
  314. }
  315. return ResponseJson.success("保存供应商成功", null);
  316. }
  317. @Override
  318. public ResponseJson<ShopFormVo> getShopFormData(Integer authUserId) {
  319. if (null == authUserId) {
  320. return ResponseJson.error("参数异常,请输入供应商用户id", null);
  321. }
  322. ShopFormVo shopForm = shopMapper.getShopByUserId(authUserId);
  323. if (null == shopForm) {
  324. return ResponseJson.error("供应商不存在", null);
  325. }
  326. List<ShopInfoVo> shopInfoList = shopMapper.getShopInfoByUserId(authUserId);
  327. shopForm.setShopInfo(shopInfoList);
  328. String existProductBrandIds = "";
  329. for (ShopInfoVo shopInfo : shopInfoList) {
  330. if (null != shopInfo.getBrandId()) {
  331. Integer productCount = shopMapper.getProductCount(authUserId, shopInfo.getBrandId());
  332. if (null != productCount && productCount > 0) {
  333. existProductBrandIds += shopInfo.getBrandId() + ",";
  334. }
  335. }
  336. }
  337. shopForm.setExistProductBrandIds(existProductBrandIds);
  338. return ResponseJson.success(shopForm);
  339. }
  340. @Override
  341. public ResponseJson<List<BrandVo>> getBrandList(Integer type, Integer authUserId) {
  342. type = null == type ? 2 : type;
  343. if (3 == type && null == authUserId) {
  344. return ResponseJson.error("参数异常,请输入供应商用户id", null);
  345. }
  346. List<BrandVo> brandList = shopMapper.getBrandList(type, authUserId);
  347. return ResponseJson.success(brandList);
  348. }
  349. @Override
  350. public ResponseJson<List<CountryVo>> getCountryList() {
  351. List<CountryVo> countryList = shopMapper.getCountryList();
  352. return ResponseJson.success(countryList);
  353. }
  354. @Override
  355. public ResponseJson<PageInfo<FeedbackVo>> getFeedbackList(Integer authUserId, String clubName, String mobile, Integer handleStatus, Integer pageNum, Integer pageSize) {
  356. if (null == authUserId) {
  357. return ResponseJson.error("参数异常,请输入供应商用户id", null);
  358. }
  359. PageHelper.startPage(pageNum, pageSize);
  360. List<FeedbackVo> feedbackList = shopMapper.getFeedbackList(authUserId, clubName, mobile, handleStatus);
  361. PageInfo<FeedbackVo> pageData = new PageInfo<>(feedbackList);
  362. return ResponseJson.success(pageData);
  363. }
  364. @Override
  365. public ResponseJson<FeedbackVo> getFeedbackFormData(Integer feedbackId) {
  366. if (null == feedbackId) {
  367. return ResponseJson.error("参数异常,请输入用户反馈id", null);
  368. }
  369. FeedbackVo feedback = shopMapper.getFeedback(feedbackId);
  370. return ResponseJson.success(feedback);
  371. }
  372. @Override
  373. public ResponseJson handleFeedback(Integer feedbackId, String handleResult) {
  374. if (null == feedbackId) {
  375. return ResponseJson.error("参数异常,请输入用户反馈id", null);
  376. }
  377. if (StringUtils.isEmpty(handleResult)) {
  378. return ResponseJson.error("参数异常,请输入用户反馈处理结果", null);
  379. }
  380. shopMapper.handleFeedback(feedbackId, handleResult);
  381. return ResponseJson.success("处理成功");
  382. }
  383. @Override
  384. public List<String> getShopBrands(Integer authUserId) {
  385. return shopMapper.getShopBrands(authUserId);
  386. }
  387. }