ShopServiceImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.springframework.stereotype.Service;
  16. import org.springframework.web.multipart.MultipartFile;
  17. import javax.annotation.Resource;
  18. import java.io.File;
  19. import java.io.FileInputStream;
  20. import java.util.*;
  21. /**
  22. * Description
  23. *
  24. * @author : Aslee
  25. * @date : 2021/5/11
  26. */
  27. @Slf4j
  28. @Service
  29. public class ShopServiceImpl implements ShopService {
  30. @Resource
  31. private ShopMapper shopMapper;
  32. @Resource
  33. private UserMapper userMapper;
  34. @Override
  35. public ResponseJson<PageInfo<ShopListVo>> getShopList(Integer listType, String shopName, Integer shopType, Integer brandId, String mobile, String linkMan, Integer lowerAuditStatus, Integer pageNum, Integer pageSize) {
  36. listType = null == listType ? 1 : listType;
  37. PageHelper.startPage(pageNum, pageSize);
  38. List<ShopListVo> shopList = shopMapper.getShopList(listType, shopName, shopType, brandId, mobile, linkMan);
  39. ListIterator<ShopListVo> iterator = shopList.listIterator();
  40. while (iterator.hasNext()) {
  41. ShopListVo shop = iterator.next();
  42. int waitAuditNum = shop.getWaitAuditNum();
  43. if (waitAuditNum > 0) {
  44. shop.setLowerAuditStatus(0);
  45. // 查询筛选项处理
  46. if (null != lowerAuditStatus && 1 == lowerAuditStatus) {
  47. iterator.remove();
  48. }
  49. }else {
  50. shop.setLowerAuditStatus(1);
  51. // 查询筛选项处理
  52. if (null != lowerAuditStatus && 0 == lowerAuditStatus) {
  53. iterator.remove();
  54. }
  55. }
  56. }
  57. PageInfo<ShopListVo> pageData = new PageInfo<>(shopList);
  58. return ResponseJson.success(pageData);
  59. }
  60. @Override
  61. public ResponseJson updateShopStatus(Integer authUserId, Integer status) {
  62. if (authUserId == null) {
  63. return ResponseJson.error("请输入用户id");
  64. }
  65. if (status == null) {
  66. return ResponseJson.error("请输入要更新的状态值");
  67. } else if (status != 0 && status != 1) {
  68. return ResponseJson.error("状态值只能为0或1");
  69. }
  70. shopMapper.updateShopStatusByUserId(authUserId, status);
  71. if (status == 0) {
  72. return ResponseJson.success("停用供应商成功");
  73. } else {
  74. return ResponseJson.success("启用供应商成功");
  75. }
  76. }
  77. @Override
  78. public ResponseJson resetShopPassword(Integer authUserId) {
  79. if (authUserId == null) {
  80. return ResponseJson.error("请输入用户id");
  81. }
  82. String newPassword = CodeUtil.generateCode(8);
  83. String md5Password = Md5Util.md5(newPassword);
  84. userMapper.updatePasswordByUserId(authUserId, md5Password);
  85. String mobile = shopMapper.getShopMobileByUserId(authUserId);
  86. boolean smsFlag = AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}");
  87. if (!smsFlag) {
  88. // 短信发送失败重试一次
  89. AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}");
  90. }
  91. log.info("供应商重置密码,用户id:" + authUserId + ",新密码:" + newPassword);
  92. return ResponseJson.success("密码重置成功");
  93. }
  94. @Override
  95. public ResponseJson<FileVo> uploadFile(Integer authUserId, Integer brandId, MultipartFile file) {
  96. authUserId = (null != authUserId && authUserId > 0) ? authUserId : null;
  97. String fileAllName = file.getOriginalFilename();
  98. String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
  99. String fileName = file.getResource().getFilename();
  100. String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  101. String filePath = uuid + "." + fileType;
  102. String contentType = OSSUtils.getContentType(fileAllName);
  103. log.info(">>>>>>>>>>>>>上传文件,用户id:" + authUserId);
  104. try {
  105. //保存本地
  106. File uploadFile = OSSUtils.ossUpload(file);
  107. //判断文件的唯一性,转换成16进制md5值
  108. String md5Hex = DigestUtils.md5Hex(new FileInputStream(uploadFile));
  109. CmBrandAuthFilePo cmBrandAuthFile = null;
  110. if (null != authUserId) {
  111. // 查找该供应商下是否已存在相同文件
  112. CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo();
  113. searchFile.setAuthUserId(authUserId);
  114. searchFile.setBrandId(brandId);
  115. searchFile.setMd5Hex(md5Hex);
  116. cmBrandAuthFile = shopMapper.getStatementFile(searchFile);
  117. }
  118. if (cmBrandAuthFile == null) {
  119. log.info("默认路径>>>" + uploadFile.getAbsolutePath());
  120. // 修改情况下,如果原来已经选择了文件代理声明,并且旧文件与新文件不同,需要将旧文件删除
  121. if (null != authUserId) {
  122. deleteFile(authUserId, brandId);
  123. }
  124. //将新文件上传oss
  125. OSSUtils.ossUpload(filePath, uploadFile, contentType);
  126. //删除本地文件
  127. OSSUtils.deleteFile(uploadFile);
  128. //保存关联关系
  129. cmBrandAuthFile = new CmBrandAuthFilePo();
  130. cmBrandAuthFile.setAuthUserId(authUserId);
  131. cmBrandAuthFile.setBrandId(brandId);
  132. cmBrandAuthFile.setName(fileName);
  133. cmBrandAuthFile.setOssName(filePath);
  134. cmBrandAuthFile.setMd5Hex(md5Hex);
  135. cmBrandAuthFile.setUploadTime(new Date());
  136. shopMapper.insertStatementFile(cmBrandAuthFile);
  137. } else {
  138. //删除本地文件
  139. OSSUtils.deleteFile(uploadFile);
  140. }
  141. FileVo fileVo = new FileVo();
  142. fileVo.setFileId(cmBrandAuthFile.getId());
  143. fileVo.setFileName(cmBrandAuthFile.getName());
  144. return ResponseJson.success("文件上传成功", fileVo);
  145. } catch (Exception e) {
  146. log.error("<<<<< 文件上传异常 >>>>>");
  147. return ResponseJson.error("文件上传失败", null);
  148. }
  149. }
  150. @Override
  151. public void deleteFile(Integer authUserId, Integer brandId) {
  152. CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo();
  153. searchFile.setAuthUserId(authUserId);
  154. searchFile.setBrandId(brandId);
  155. CmBrandAuthFilePo oldFile = shopMapper.getStatementFile(searchFile);
  156. if (oldFile != null) {
  157. Integer num = shopMapper.getFileNumByMd5Hex(oldFile.getMd5Hex());
  158. log.info(">>>>>>>>>>>>>>>文件使用数:" + num);
  159. if (num == 1) {
  160. log.info(">>>>>>>>>>>>>>>删除文件:" + oldFile.getOssName());
  161. // 如果这个文件只有这个供应商在使用,删除oss服务器上的文件
  162. OSSUtils.deleteSingleFile(oldFile.getOssName());
  163. }
  164. shopMapper.deleteStatementFile(oldFile.getId());
  165. }
  166. }
  167. @Override
  168. public ResponseJson saveShop(Integer authUserId, Integer shopType, String shopName, String mobile, String linkMan, Integer shopStatus, Integer createBy, List<ShopInfoDto> shopInfoList) {
  169. // 是否为添加操作
  170. boolean insertFlag = null == authUserId;
  171. if (insertFlag) {
  172. // 添加时验证手机号是否已被使用
  173. Integer userIdByMobile = shopMapper.getUserIdByMobile(mobile);
  174. if (null != userIdByMobile) {
  175. return ResponseJson.error("该手机号已被使用,请重新输入", null);
  176. }
  177. // 添加时验证供应商名称是否已被使用
  178. Integer userIdByShopName = shopMapper.getUserIdByShopName(shopName);
  179. if (null != userIdByShopName) {
  180. return ResponseJson.error("该供应商名称已经存在,请重新输入", null);
  181. }
  182. }
  183. // 更新品牌授权logo
  184. shopInfoList.forEach(shopInfo->{
  185. shopMapper.updateBrandAuthLogo(shopInfo.getBrandId(), shopInfo.getBrandAuthLogo());
  186. });
  187. /*
  188. 组装供应商用户数据
  189. */
  190. UserPo shop = new UserPo();
  191. // 供应商名称
  192. shop.setName(shopName);
  193. // 手机号
  194. shop.setMobile(mobile);
  195. // 联系人
  196. shop.setLinkMan(linkMan);
  197. // 供应商状态
  198. shop.setStatus(shopStatus);
  199. if (insertFlag) {
  200. // 用户身份:1管理员,2供应商
  201. shop.setUserIdentity(2);
  202. // 供应商类型
  203. shop.setShopType(shopType);
  204. // 创建管理员id
  205. shop.setCreateBy(createBy);
  206. // 创建时间
  207. shop.setCreateTime(new Date());
  208. // 设置随机8位密码
  209. String password = CodeUtil.generateCode(8);
  210. String md5Pwd = Md5Util.md5(password);
  211. shop.setPassword(md5Pwd);
  212. // 插入供应商用户
  213. shopMapper.insertShop(shop);
  214. // 发送短信
  215. boolean smsFlag = AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + password + "\"}");
  216. if (!smsFlag) {
  217. // 短信发送失败重试一次
  218. AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + password + "\"}");
  219. }
  220. log.info("添加供应商,供应商用户id:" + shop.getAuthUserId());
  221. } else {
  222. shop.setAuthUserId(authUserId);
  223. // 更新供应商用户
  224. shopMapper.updateShopByUserId(shop);
  225. log.info("更新供应商,供应商用户id:" + shop.getAuthUserId());
  226. }
  227. /*
  228. 组装供应商信息数据
  229. */
  230. if (insertFlag) {
  231. shopInfoList.forEach(shopInfo->{
  232. shopInfo.setAuthUserId(shop.getAuthUserId());
  233. // 插入供应商信息
  234. shopMapper.insertShopInfo(shopInfo);
  235. if (4 == shopInfo.getStatementType()) {
  236. shopMapper.updateFileUserId(shopInfo.getStatementFileId(), shop.getAuthUserId());
  237. }
  238. });
  239. } else {
  240. // 数据库中供应商信息数据
  241. List<ShopBrandVo> dbInfoBrandList = shopMapper.getDbInfoBrandList(shop.getAuthUserId());
  242. List<Integer> updateInfoBrandList = new ArrayList<>();
  243. List<Integer> newInfoBrandList = new ArrayList<>();
  244. // 编辑后的供应商品牌id
  245. shopInfoList.forEach(shopInfo->{
  246. newInfoBrandList.add(shopInfo.getBrandId());
  247. });
  248. for (ShopBrandVo shopBrand : dbInfoBrandList) {
  249. if (!newInfoBrandList.contains(shopBrand.getBrandId())) {
  250. // 删除被删除的数据
  251. shopMapper.deleteShopInfoById(shopBrand.getId());
  252. // 删除文件
  253. deleteFile(authUserId, shopBrand.getBrandId());
  254. } else {
  255. // 保存数据库已有且未被删除的供应商品牌id
  256. updateInfoBrandList.add(shopBrand.getBrandId());
  257. }
  258. }
  259. shopInfoList.forEach(shopInfoVo -> {
  260. shopInfoVo.setAuthUserId(shop.getAuthUserId());
  261. // 保存供应商信息数据
  262. if (updateInfoBrandList.contains(shopInfoVo.getBrandId())) {
  263. // 更新
  264. shopMapper.updateShopInfo(shopInfoVo);
  265. if (4 != shopInfoVo.getStatementType()) {
  266. // 没有选择文件代理声明的情况下,若存在原来的文件,删除代理声明文件
  267. deleteFile(shop.getAuthUserId(),shopInfoVo.getBrandId());
  268. }
  269. } else {
  270. // 插入
  271. shopMapper.insertShopInfo(shopInfoVo);
  272. }
  273. });
  274. }
  275. return ResponseJson.success("保存供应商成功", null);
  276. }
  277. @Override
  278. public ResponseJson<ShopFormVo> getShopFormData(Integer authUserId) {
  279. if (null == authUserId) {
  280. return ResponseJson.error("参数异常,请输入供应商用户id", null);
  281. }
  282. ShopFormVo shopForm = shopMapper.getShopByUserId(authUserId);
  283. if (null == shopForm) {
  284. return ResponseJson.error("供应商不存在", null);
  285. }
  286. List<ShopInfoVo> shopInfoList = shopMapper.getShopInfoByUserId(authUserId);
  287. shopForm.setShopInfo(shopInfoList);
  288. return ResponseJson.success(shopForm);
  289. }
  290. @Override
  291. public ResponseJson<List<BrandVo>> getBrandList(Integer type, Integer authUserId) {
  292. type = null == type ? 2 : type;
  293. if (3 == type && null == authUserId) {
  294. return ResponseJson.error("参数异常,请输入供应商用户id", null);
  295. }
  296. List<BrandVo> brandList = shopMapper.getBrandList(type, authUserId);
  297. return ResponseJson.success(brandList);
  298. }
  299. @Override
  300. public ResponseJson<List<CountryVo>> getCountryList() {
  301. List<CountryVo> countryList = shopMapper.getCountryList();
  302. return ResponseJson.success(countryList);
  303. }
  304. }