123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- 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.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);
- ListIterator<ShopListVo> iterator = shopList.listIterator();
- while (iterator.hasNext()) {
- ShopListVo shop = iterator.next();
- int waitAuditNum = shop.getWaitAuditNum();
- if (waitAuditNum > 0) {
- shop.setLowerAuditStatus(0);
- // 查询筛选项处理
- if (null != lowerAuditStatus && 1 == lowerAuditStatus) {
- iterator.remove();
- }
- }else {
- shop.setLowerAuditStatus(1);
- // 查询筛选项处理
- if (null != lowerAuditStatus && 0 == lowerAuditStatus) {
- iterator.remove();
- }
- }
- }
- 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 + ",新密码:" + newPassword);
- return ResponseJson.success("密码重置成功");
- }
- @Override
- public ResponseJson<FileVo> 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
- OSSUtils.ossUpload(filePath, uploadFile, contentType);
- //删除本地文件
- OSSUtils.deleteFile(uploadFile);
- //保存关联关系
- cmBrandAuthFile = new CmBrandAuthFilePo();
- cmBrandAuthFile.setAuthUserId(authUserId);
- cmBrandAuthFile.setBrandId(brandId);
- cmBrandAuthFile.setName(fileName);
- cmBrandAuthFile.setOssName(filePath);
- cmBrandAuthFile.setMd5Hex(md5Hex);
- cmBrandAuthFile.setUploadTime(new Date());
- shopMapper.insertStatementFile(cmBrandAuthFile);
- } else {
- //删除本地文件
- OSSUtils.deleteFile(uploadFile);
- }
- FileVo fileVo = new FileVo();
- 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, Integer createBy, List<ShopInfoDto> shopInfoList) {
- // 是否为添加操作
- boolean insertFlag = null == authUserId;
- if (insertFlag) {
- // 添加时验证手机号是否已被使用
- Integer userIdByMobile = shopMapper.getUserIdByMobile(mobile);
- if (null != userIdByMobile) {
- return ResponseJson.error("该手机号已被使用,请重新输入", null);
- }
- // 添加时验证供应商名称是否已被使用
- Integer userIdByShopName = shopMapper.getUserIdByShopName(shopName);
- if (null != userIdByShopName) {
- return ResponseJson.error("该供应商名称已经存在,请重新输入", 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);
- 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> updateInfoBrandList = new ArrayList<>();
- List<Integer> newInfoBrandList = new ArrayList<>();
- // 编辑后的供应商品牌id
- shopInfoList.forEach(shopInfo->{
- newInfoBrandList.add(shopInfo.getBrandId());
- });
- 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);
- 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);
- }
- }
|