ShopServiceImpl.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.ShopSaveDto;
  6. import com.caimei.model.po.CmBrandAuthFilePo;
  7. import com.caimei.model.po.ShopInfoPo;
  8. import com.caimei.model.po.UserPo;
  9. import com.caimei.model.vo.BrandVo;
  10. import com.caimei.model.vo.CountryVo;
  11. import com.caimei.model.vo.ShopFormVo;
  12. import com.caimei.model.vo.ShopListVo;
  13. import com.caimei.service.ShopService;
  14. import com.caimei.utils.*;
  15. import com.github.pagehelper.PageHelper;
  16. import com.github.pagehelper.PageInfo;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.apache.commons.codec.digest.DigestUtils;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.stereotype.Service;
  21. import org.springframework.web.multipart.MultipartFile;
  22. import javax.annotation.Resource;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.UUID;
  28. /**
  29. * Description
  30. *
  31. * @author : Aslee
  32. * @date : 2021/5/11
  33. */
  34. @Slf4j
  35. @Service
  36. public class ShopServiceImpl implements ShopService {
  37. @Resource
  38. private ShopMapper shopMapper;
  39. @Resource
  40. private UserMapper userMapper;
  41. @Override
  42. public ResponseJson<PageInfo<ShopListVo>> getShopList(String shopName, Integer shopType, Integer brandId, String mobile, String linkMan, Integer pageNum, Integer pageSize) {
  43. PageHelper.startPage(pageNum, pageSize);
  44. List<ShopListVo> shopList = shopMapper.getShopList(shopName, shopType, brandId, mobile, linkMan);
  45. PageInfo<ShopListVo> pageData = new PageInfo<>(shopList);
  46. return ResponseJson.success(pageData);
  47. }
  48. @Override
  49. public ResponseJson updateShopStatus(Integer authUserId, Integer status) {
  50. if (authUserId == null) {
  51. return ResponseJson.error("请输入用户id");
  52. }
  53. if (status == null) {
  54. return ResponseJson.error("请输入要更新的状态值");
  55. } else if (status != 0 && status != 1) {
  56. return ResponseJson.error("状态值只能为0或1");
  57. }
  58. shopMapper.updateShopStatusByUserId(authUserId, status);
  59. if (status == 0) {
  60. return ResponseJson.success("停用供应商成功");
  61. } else {
  62. return ResponseJson.success("启用供应商成功");
  63. }
  64. }
  65. @Override
  66. public ResponseJson resetShopPassword(Integer authUserId) {
  67. if (authUserId == null) {
  68. return ResponseJson.error("请输入用户id");
  69. }
  70. String newPassword = CodeUtil.generateCode(8);
  71. String md5Password = Md5Util.md5(newPassword);
  72. userMapper.updatePasswordByUserId(authUserId, md5Password);
  73. String mobile = shopMapper.getShopMobileByUserId(authUserId);
  74. boolean smsFlag = AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}");
  75. if (!smsFlag) {
  76. // 短信发送失败重试一次
  77. AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}");
  78. }
  79. log.info("供应商重置密码,用户id:" + authUserId + ",新密码:" + newPassword);
  80. return ResponseJson.success("密码重置成功");
  81. }
  82. @Override
  83. public ResponseJson<Integer> uploadFile(Integer authUserId, MultipartFile file) {
  84. authUserId = (null != authUserId && authUserId > 0) ? authUserId : null;
  85. String fileAllName = file.getOriginalFilename();
  86. String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1);
  87. String fileName = file.getResource().getFilename();
  88. String uuid = UUID.randomUUID().toString().replaceAll("-", "");
  89. String filePath = uuid + "." + fileType;
  90. String contentType = OSSUtils.getContentType(fileAllName);
  91. try {
  92. //保存本地
  93. File uploadFile = OSSUtils.ossUpload(file);
  94. //判断文件的唯一性,转换成16进制md5值
  95. String md5Hex = DigestUtils.md5Hex(new FileInputStream(uploadFile));
  96. CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo();
  97. searchFile.setAuthUserId(authUserId);
  98. searchFile.setMd5Hex(md5Hex);
  99. // 查找该供应商下是否已存在相同文件
  100. CmBrandAuthFilePo cmBrandAuthFile = shopMapper.getStatementFile(searchFile);
  101. if (cmBrandAuthFile == null) {
  102. log.info("默认路径>>>" + uploadFile.getAbsolutePath());
  103. // 修改情况下,如果原来已经选择了文件代理声明,并且旧文件与新文件不同,需要将旧文件删除
  104. deleteFileByUserId(authUserId);
  105. //将新文件上传oss
  106. OSSUtils.ossUpload(filePath, uploadFile, contentType);
  107. //删除本地文件
  108. OSSUtils.deleteFile(uploadFile);
  109. //保存关联关系
  110. cmBrandAuthFile = new CmBrandAuthFilePo();
  111. cmBrandAuthFile.setAuthUserId(authUserId);
  112. cmBrandAuthFile.setName(fileName);
  113. cmBrandAuthFile.setOssName(filePath);
  114. cmBrandAuthFile.setMd5Hex(md5Hex);
  115. cmBrandAuthFile.setUploadTime(new Date());
  116. shopMapper.insertStatementFile(cmBrandAuthFile);
  117. } else {
  118. //删除本地文件
  119. OSSUtils.deleteFile(uploadFile);
  120. }
  121. return ResponseJson.success("文件上传成功", cmBrandAuthFile.getId());
  122. } catch (Exception e) {
  123. log.error("<<<<< 文件上传异常 >>>>>");
  124. return ResponseJson.error("文件上传失败", null);
  125. }
  126. }
  127. @Override
  128. public void deleteFileByUserId(Integer authUserId) {
  129. CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo();
  130. searchFile.setAuthUserId(authUserId);
  131. CmBrandAuthFilePo oldFile = shopMapper.getStatementFile(searchFile);
  132. if (oldFile != null) {
  133. Integer num = shopMapper.getFileNumByMd5Hex(oldFile.getMd5Hex());
  134. log.info(">>>>>>>>>>>>>>>文件使用数:" + num);
  135. if (num == 1) {
  136. log.info(">>>>>>>>>>>>>>>删除文件:" + oldFile.getOssName());
  137. // 如果这个文件只有这个供应商在使用,删除oss服务器上的文件
  138. OSSUtils.deleteSingleFile(oldFile.getOssName());
  139. }
  140. shopMapper.deleteStatementFile(oldFile.getId());
  141. }
  142. }
  143. @Override
  144. public ResponseJson saveShop(ShopSaveDto shopSaveDto) {
  145. Integer shopType = shopSaveDto.getShopType();
  146. if (shopType == null) {
  147. return ResponseJson.error("请选择供应商类型", null);
  148. }
  149. // 是否为添加操作
  150. boolean insertFlag = null == shopSaveDto.getAuthUserId();
  151. // 手机号
  152. String mobile = shopSaveDto.getMobile();
  153. if (StringUtils.isBlank(mobile)) {
  154. return ResponseJson.error("参数异常,请输入手机号");
  155. }
  156. if (insertFlag) {
  157. // 添加时验证手机号是否已被使用
  158. Integer userIdByMobile = shopMapper.getUserIdByMobile(mobile);
  159. if (null != userIdByMobile) {
  160. return ResponseJson.error("该手机号已被使用,请重新输入", null);
  161. }
  162. }
  163. // 更新品牌授权logo
  164. shopMapper.updateBrandAuthLogo(shopSaveDto.getBrandId(),shopSaveDto.getBrandAuthLogo());
  165. /*
  166. 组装供应商用户数据
  167. */
  168. UserPo shop = new UserPo();
  169. // 供应商名称
  170. shop.setName(shopSaveDto.getShopName());
  171. // 手机号
  172. shop.setMobile(shopSaveDto.getMobile());
  173. // 联系人
  174. shop.setLinkMan(shopSaveDto.getLinkMan());
  175. // 供应商状态
  176. shop.setStatus(shopSaveDto.getShopStatus());
  177. if (insertFlag) {
  178. // 用户身份:1管理员,2供应商
  179. shop.setUserIdentity(2);
  180. // 创建管理员id
  181. shop.setCreateBy(shopSaveDto.getCreateBy());
  182. // 创建时间
  183. shop.setCreateTime(new Date());
  184. // 设置随机8位密码
  185. String password = CodeUtil.generateCode(8);
  186. String md5Pwd = Md5Util.md5(password);
  187. shop.setPassword(md5Pwd);
  188. // 发送短信
  189. boolean smsFlag = AliyunSmsUtil.sendSms(shopSaveDto.getMobile(), 14, "{password:\"" + password + "\"}");
  190. if (!smsFlag) {
  191. // 短信发送失败重试一次
  192. AliyunSmsUtil.sendSms(shopSaveDto.getMobile(), 14, "{password:\"" + password + "\"}");
  193. }
  194. // 插入供应商用户
  195. shopMapper.insertShop(shop);
  196. log.info("添加供应商,供应商用户id:" + shop.getAuthUserId());
  197. } else {
  198. shop.setAuthUserId(shopSaveDto.getAuthUserId());
  199. // 更新供应商用户
  200. shopMapper.updateShopByUserId(shop);
  201. log.info("更新供应商,供应商用户id:" + shop.getAuthUserId());
  202. }
  203. /*
  204. 组装供应商信息数据
  205. */
  206. ShopInfoPo shopInfo = new ShopInfoPo();
  207. // 供应商用户id
  208. shopInfo.setAuthUserId(shop.getAuthUserId());
  209. // 供应商类型:1品牌方,2代理商
  210. shopInfo.setType(shopSaveDto.getShopType());
  211. // 品牌id
  212. shopInfo.setBrandId(shopSaveDto.getBrandId());
  213. // 国家id
  214. shopInfo.setCountryId(shopSaveDto.getCountryId());
  215. // 官网认证链接
  216. shopInfo.setSecurityLink(shopSaveDto.getSecurityLink());
  217. if (null != shopSaveDto.getStatementType()) {
  218. shopInfo.setStatementType(shopSaveDto.getStatementType());
  219. if (1 == shopSaveDto.getStatementType()) {
  220. // 声明弹窗
  221. shopInfo.setStatementContent(shopSaveDto.getStatementContent());
  222. } else if (2 == shopSaveDto.getStatementType()) {
  223. // 声明链接
  224. shopInfo.setStatementLink(shopSaveDto.getStatementLink());
  225. } else if (3 == shopSaveDto.getStatementType()) {
  226. // 声明图片
  227. shopInfo.setStatementImage(shopSaveDto.getStatementImage());
  228. }
  229. if (4 == shopSaveDto.getStatementType()) {
  230. if (insertFlag){
  231. // 更新代理声明文件
  232. Integer statementFileId = shopSaveDto.getStatementFileId();
  233. shopMapper.updateFileUserId(statementFileId, shop.getAuthUserId());
  234. }
  235. } else if (!insertFlag){
  236. // 没有选择文件代理声明的情况下,若存在原来的文件,删除代理声明文件
  237. deleteFileByUserId(shop.getAuthUserId());
  238. }
  239. }
  240. // 保存供应商信息
  241. if (insertFlag) {
  242. shopMapper.insertShopInfo(shopInfo);
  243. } else {
  244. shopMapper.updateShopInfoByUserId(shopInfo);
  245. }
  246. return ResponseJson.success("保存供应商成功", null);
  247. }
  248. @Override
  249. public ResponseJson<ShopFormVo> getShopFormData(Integer authUserId) {
  250. if (null == authUserId) {
  251. return ResponseJson.error("参数异常,请输入供应商用户id", null);
  252. }
  253. ShopFormVo shopForm = shopMapper.getShopByAuthUserId(authUserId);
  254. if (null == shopForm) {
  255. return ResponseJson.error("供应商不存在", null);
  256. }
  257. return ResponseJson.success(shopForm);
  258. }
  259. @Override
  260. public ResponseJson<List<BrandVo>> getBrandList(Integer type) {
  261. List<BrandVo> brandList = shopMapper.getBrandList(type);
  262. return ResponseJson.success(brandList);
  263. }
  264. @Override
  265. public ResponseJson<List<CountryVo>> getCountryList() {
  266. List<CountryVo> countryList = shopMapper.getCountryList();
  267. return ResponseJson.success(countryList);
  268. }
  269. }