123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- 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<PageInfo<ShopListVo>> 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<ShopListVo> shopList = shopMapper.getShopList(listType, shopName, shopType, brandId, mobile, linkMan, lowerAuditStatus);
- ListIterator<ShopListVo> 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<ShopListVo> 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<AuthFileVo> 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<ShopInfoDto> 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<ShopBrandVo> dbInfoBrandList = shopMapper.getDbInfoBrandList(shop.getAuthUserId());
- // 编辑后的品牌列表
- List<Integer> newInfoBrandList = new ArrayList<>();
- // 编辑后的品牌列表和数据库中的品牌列表重复的需要更新的部分
- List<Integer> 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<ShopFormVo> getShopFormData(Integer authUserId) {
- if (null == authUserId) {
- return ResponseJson.error("参数异常,请输入供应商用户id", null);
- }
- ShopFormVo shopForm = shopMapper.getShopByUserId(authUserId);
- if (null == shopForm) {
- return ResponseJson.error("供应商不存在", null);
- }
- List<ShopInfoVo> 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<List<BrandVo>> getBrandList(Integer type, Integer authUserId) {
- type = null == type ? 2 : type;
- if (3 == type && null == authUserId) {
- return ResponseJson.error("参数异常,请输入供应商用户id", null);
- }
- List<BrandVo> brandList = shopMapper.getBrandList(type, authUserId);
- return ResponseJson.success(brandList);
- }
- @Override
- public ResponseJson<List<CountryVo>> getCountryList() {
- List<CountryVo> countryList = shopMapper.getCountryList();
- return ResponseJson.success(countryList);
- }
- @Override
- public ResponseJson<PageInfo<FeedbackVo>> 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<FeedbackVo> feedbackList = shopMapper.getFeedbackList(authUserId, clubName, mobile, handleStatus);
- PageInfo<FeedbackVo> pageData = new PageInfo<>(feedbackList);
- return ResponseJson.success(pageData);
- }
- @Override
- public ResponseJson<FeedbackVo> 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<String> getShopBrands(Integer authUserId) {
- return shopMapper.getShopBrands(authUserId);
- }
- }
|