package com.caimei.service.impl; import com.caimei.mapper.ShopMapper; import com.caimei.mapper.UserMapper; import com.caimei.model.ResponseJson; import com.caimei.model.dto.ShopInfoDto; import com.caimei.model.po.CmBrandAuthFilePo; import com.caimei.model.po.UserPo; import com.caimei.model.vo.*; import com.caimei.service.ShopService; import com.caimei.utils.*; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; import java.io.FileInputStream; import java.util.*; /** * Description * * @author : Aslee * @date : 2021/5/11 */ @Slf4j @Service public class ShopServiceImpl implements ShopService { @Resource private ShopMapper shopMapper; @Resource private UserMapper userMapper; @Override public ResponseJson> getShopList(Integer listType, String shopName, Integer shopType, Integer brandId, String mobile, String linkMan, Integer lowerAuditStatus, Integer pageNum, Integer pageSize) { listType = null == listType ? 1 : listType; PageHelper.startPage(pageNum, pageSize); List shopList = shopMapper.getShopList(listType, shopName, shopType, brandId, mobile, linkMan, lowerAuditStatus); ListIterator iterator = shopList.listIterator(); while (iterator.hasNext()) { // 根据是否完成商品信息审核筛选项,设置下级审核状态 ShopListVo shop = iterator.next(); int articleWaitNum = shop.getArticleWaitNum(); int imageWaitNum = shop.getImageWaitNum(); int videoWaitNum = shop.getVideoWaitNum(); int fileWaitNum = shop.getFileWaitNum(); int doctorWaitNum = shop.getDoctorWaitNum(); int waitAuditNum = 2 == listType ? shop.getWaitAuditNum() : (3 == listType ? (articleWaitNum + imageWaitNum + videoWaitNum + fileWaitNum) : (4 == listType ? doctorWaitNum : 0)); if (waitAuditNum > 0) { shop.setLowerAuditStatus(0); }else { shop.setLowerAuditStatus(1); } } PageInfo pageData = new PageInfo<>(shopList); return ResponseJson.success(pageData); } @Override public ResponseJson updateShopStatus(Integer authUserId, Integer status) { if (authUserId == null) { return ResponseJson.error("请输入用户id"); } if (status == null) { return ResponseJson.error("请输入要更新的状态值"); } else if (status != 0 && status != 1) { return ResponseJson.error("状态值只能为0或1"); } shopMapper.updateShopStatusByUserId(authUserId, status); if (status == 0) { return ResponseJson.success("停用供应商成功"); } else { return ResponseJson.success("启用供应商成功"); } } @Override public ResponseJson resetShopPassword(Integer authUserId) { if (authUserId == null) { return ResponseJson.error("请输入用户id"); } String newPassword = CodeUtil.generateCode(8); String md5Password = Md5Util.md5(newPassword); userMapper.updatePasswordByUserId(authUserId, md5Password); String mobile = shopMapper.getShopMobileByUserId(authUserId); boolean smsFlag = AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}"); if (!smsFlag) { // 短信发送失败重试一次 AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + newPassword + "\"}"); } log.info("供应商重置密码,用户id:" + authUserId); return ResponseJson.success("密码重置成功"); } @Override public ResponseJson uploadFile(Integer authUserId, Integer brandId, MultipartFile file) { authUserId = (null != authUserId && authUserId > 0) ? authUserId : null; String fileAllName = file.getOriginalFilename(); String fileType = fileAllName.substring(fileAllName.lastIndexOf(".") + 1); String fileName = file.getResource().getFilename(); String uuid = UUID.randomUUID().toString().replaceAll("-", ""); String filePath = uuid + "." + fileType; String contentType = OSSUtils.getContentType(fileAllName); log.info(">>>>>>>>>>>>>上传文件,用户id:" + authUserId); try { //保存本地 File uploadFile = OSSUtils.ossUpload(file); //判断文件的唯一性,转换成16进制md5值 String md5Hex = DigestUtils.md5Hex(new FileInputStream(uploadFile)); CmBrandAuthFilePo cmBrandAuthFile = null; if (null != authUserId) { // 查找该供应商下是否已存在相同文件 CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo(); searchFile.setAuthUserId(authUserId); searchFile.setBrandId(brandId); searchFile.setMd5Hex(md5Hex); cmBrandAuthFile = shopMapper.getStatementFile(searchFile); } if (cmBrandAuthFile == null) { log.info("默认路径>>>" + uploadFile.getAbsolutePath()); // 修改情况下,如果原来已经选择了文件代理声明,并且旧文件与新文件不同,需要将旧文件删除 if (null != authUserId) { deleteFile(authUserId, brandId); } // 查找oss中是否已存在该文件,若存在则不需要重新上传 CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo(); searchFile.setMd5Hex(md5Hex); CmBrandAuthFilePo sameFile = shopMapper.getStatementFile(searchFile); if (null == sameFile) { //将新文件上传oss OSSUtils.ossUpload(filePath, uploadFile, contentType, null); } //删除本地文件 OSSUtils.deleteFile(uploadFile); //保存关联关系 cmBrandAuthFile = new CmBrandAuthFilePo(); cmBrandAuthFile.setAuthUserId(authUserId); cmBrandAuthFile.setBrandId(brandId); cmBrandAuthFile.setName(fileName); cmBrandAuthFile.setOssName(null == sameFile ? filePath : sameFile.getOssName()); cmBrandAuthFile.setMd5Hex(md5Hex); cmBrandAuthFile.setUploadTime(new Date()); shopMapper.insertStatementFile(cmBrandAuthFile); } else { //删除本地文件 OSSUtils.deleteFile(uploadFile); } AuthFileVo fileVo = new AuthFileVo(); fileVo.setFileId(cmBrandAuthFile.getId()); fileVo.setFileName(cmBrandAuthFile.getName()); return ResponseJson.success("文件上传成功", fileVo); } catch (Exception e) { log.error("<<<<< 文件上传异常 >>>>>"); return ResponseJson.error("文件上传失败", null); } } @Override public void deleteFile(Integer authUserId, Integer brandId) { CmBrandAuthFilePo searchFile = new CmBrandAuthFilePo(); searchFile.setAuthUserId(authUserId); searchFile.setBrandId(brandId); CmBrandAuthFilePo oldFile = shopMapper.getStatementFile(searchFile); if (oldFile != null) { Integer num = shopMapper.getFileNumByMd5Hex(oldFile.getMd5Hex()); log.info(">>>>>>>>>>>>>>>文件使用数:" + num); if (num == 1) { log.info(">>>>>>>>>>>>>>>删除文件:" + oldFile.getOssName()); // 如果这个文件只有这个供应商在使用,删除oss服务器上的文件 OSSUtils.deleteSingleFile(oldFile.getOssName()); } shopMapper.deleteStatementFile(oldFile.getId()); } } @Override 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 shopInfoList) { // 是否为添加操作 boolean insertFlag = null == authUserId; Integer userIdByMobile = shopMapper.getUserIdByMobile(mobile); Integer userIdByAppId = shopMapper.getUserIdByAppId(appId); if (insertFlag) { // 添加时验证手机号是否已被使用 if (null != userIdByMobile) { return ResponseJson.error("该手机号已被使用,请重新输入", null); } // 添加时验证供应商名称是否已被使用 Integer userIdByShopName = shopMapper.getUserIdByShopName(shopName); if (null != userIdByShopName) { return ResponseJson.error("该供应商名称已经存在,请重新输入", null); } // 添加时验证appId是否已被使用 if (StringUtils.isNotEmpty(appId) && null != userIdByAppId) { return ResponseJson.error("该appId已被使用,请重新输入", null); } } else { // 修改时验证新手机号是否已被使用 if (null != userIdByMobile && !userIdByMobile.equals(authUserId)) { return ResponseJson.error("该手机号已被使用,请重新输入", null); } // 修改时验证新appId是否已被使用 if (null != userIdByAppId && !userIdByAppId.equals(authUserId)) { return ResponseJson.error("该appId已被使用,请重新输入", null); } } // 更新品牌授权logo shopInfoList.forEach(shopInfo->{ shopMapper.updateBrandAuthLogo(shopInfo.getBrandId(), shopInfo.getBrandAuthLogo()); }); /* 组装供应商用户数据 */ UserPo shop = new UserPo(); // 供应商名称 shop.setName(shopName); // 手机号 shop.setMobile(mobile); // 联系人 shop.setLinkMan(linkMan); // 供应商状态 shop.setStatus(shopStatus); // 代理商logo shop.setLogo(logo); // 公众号二维码图片 shop.setQrCodeImage(qrCodeImage); // 公众号类型 shop.setWxAccountType(wxAccountType); // 公众号appId shop.setAppId(appId); // 公众号appSecret shop.setAppSecret(appSecret); if (insertFlag) { // 用户身份:1管理员,2供应商 shop.setUserIdentity(2); // 供应商类型 shop.setShopType(shopType); // 创建管理员id shop.setCreateBy(createBy); // 创建时间 shop.setCreateTime(new Date()); // 设置随机8位密码 String password = CodeUtil.generateCode(8); String md5Pwd = Md5Util.md5(password); shop.setPassword(md5Pwd); // 插入供应商用户 shopMapper.insertShop(shop); // 发送短信 boolean smsFlag = AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + password + "\"}"); if (!smsFlag) { // 短信发送失败重试一次 AliyunSmsUtil.sendSms(mobile, 14, "{password:\"" + password + "\"}"); } log.info("添加供应商,供应商用户id:" + shop.getAuthUserId()); } else { shop.setAuthUserId(authUserId); // 更新供应商用户 shopMapper.updateShopByUserId(shop); log.info("更新供应商,供应商用户id:" + shop.getAuthUserId()); } /* 组装供应商信息数据 */ if (insertFlag) { shopInfoList.forEach(shopInfo->{ shopInfo.setAuthUserId(shop.getAuthUserId()); // 插入供应商信息 shopMapper.insertShopInfo(shopInfo); if (4 == shopInfo.getStatementType()) { shopMapper.updateFileUserId(shopInfo.getStatementFileId(), shop.getAuthUserId()); } }); } else { // 数据库中供应商信息数据 List dbInfoBrandList = shopMapper.getDbInfoBrandList(shop.getAuthUserId()); // 编辑后的品牌列表 List newInfoBrandList = new ArrayList<>(); // 编辑后的品牌列表和数据库中的品牌列表重复的需要更新的部分 List updateInfoBrandList = new ArrayList<>(); // 编辑后的供应商品牌id shopInfoList.forEach(shopInfo->{ newInfoBrandList.add(shopInfo.getBrandId()); }); for (ShopBrandVo shopBrand : dbInfoBrandList) { // 判断被删除的品牌下是否还有未删除的商品,若存在,提示供应商需要删除后才能删除品牌 if (!newInfoBrandList.contains(shopBrand.getBrandId())) { Integer productCount = shopMapper.getProductCount(shop.getAuthUserId(), shopBrand.getBrandId()); if (null != productCount && productCount > 0) { return ResponseJson.error("该品牌已绑定供应商商品,无法进行删除"); } } } for (ShopBrandVo shopBrand : dbInfoBrandList) { if (!newInfoBrandList.contains(shopBrand.getBrandId())) { // 删除被删除的数据 shopMapper.deleteShopInfoById(shopBrand.getId()); // 删除文件 deleteFile(authUserId, shopBrand.getBrandId()); } else { // 保存数据库已有且未被删除的供应商品牌id updateInfoBrandList.add(shopBrand.getBrandId()); } } shopInfoList.forEach(shopInfoVo -> { shopInfoVo.setAuthUserId(shop.getAuthUserId()); // 保存供应商信息数据 if (updateInfoBrandList.contains(shopInfoVo.getBrandId())) { // 更新 shopMapper.updateShopInfo(shopInfoVo); if (4 != shopInfoVo.getStatementType()) { // 没有选择文件代理声明的情况下,若存在原来的文件,删除代理声明文件 deleteFile(shop.getAuthUserId(),shopInfoVo.getBrandId()); } } else { // 插入 shopMapper.insertShopInfo(shopInfoVo); } }); } return ResponseJson.success("保存供应商成功", null); } @Override public ResponseJson getShopFormData(Integer authUserId) { if (null == authUserId) { return ResponseJson.error("参数异常,请输入供应商用户id", null); } ShopFormVo shopForm = shopMapper.getShopByUserId(authUserId); if (null == shopForm) { return ResponseJson.error("供应商不存在", null); } List shopInfoList = shopMapper.getShopInfoByUserId(authUserId); shopForm.setShopInfo(shopInfoList); String existProductBrandIds = ""; for (ShopInfoVo shopInfo : shopInfoList) { if (null != shopInfo.getBrandId()) { Integer productCount = shopMapper.getProductCount(authUserId, shopInfo.getBrandId()); if (null != productCount && productCount > 0) { existProductBrandIds += shopInfo.getBrandId() + ","; } } } shopForm.setExistProductBrandIds(existProductBrandIds); return ResponseJson.success(shopForm); } @Override public ResponseJson> getBrandList(Integer type, Integer authUserId) { type = null == type ? 2 : type; if (3 == type && null == authUserId) { return ResponseJson.error("参数异常,请输入供应商用户id", null); } List brandList = shopMapper.getBrandList(type, authUserId); return ResponseJson.success(brandList); } @Override public ResponseJson> getCountryList() { List countryList = shopMapper.getCountryList(); return ResponseJson.success(countryList); } @Override public ResponseJson> getFeedbackList(Integer authUserId, String clubName, String mobile, Integer handleStatus, Integer pageNum, Integer pageSize) { if (null == authUserId) { return ResponseJson.error("参数异常,请输入供应商用户id", null); } PageHelper.startPage(pageNum, pageSize); List feedbackList = shopMapper.getFeedbackList(authUserId, clubName, mobile, handleStatus); PageInfo pageData = new PageInfo<>(feedbackList); return ResponseJson.success(pageData); } @Override public ResponseJson getFeedbackFormData(Integer feedbackId) { if (null == feedbackId) { return ResponseJson.error("参数异常,请输入用户反馈id", null); } FeedbackVo feedback = shopMapper.getFeedback(feedbackId); return ResponseJson.success(feedback); } @Override public ResponseJson handleFeedback(Integer feedbackId, String handleResult) { if (null == feedbackId) { return ResponseJson.error("参数异常,请输入用户反馈id", null); } if (StringUtils.isEmpty(handleResult)) { return ResponseJson.error("参数异常,请输入用户反馈处理结果", null); } shopMapper.handleFeedback(feedbackId, handleResult); return ResponseJson.success("处理成功"); } @Override public List getShopBrands(Integer authUserId) { return shopMapper.getShopBrands(authUserId); } }