分类删除校验&代码优化

This commit is contained in:
linfeng 2022-11-07 12:49:41 +08:00
parent 8408d0fd3c
commit a68c7a875f
10 changed files with 55 additions and 143 deletions

View File

@ -1,37 +0,0 @@
package io.linfeng.common.utils;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;
/**
* 分页查询复制响应对象封装
* @author linfeng
* @date 2022/1/23 18:02
*/
public class CopyPageQueryUtil {
public static AppPageUtils copyPageQuery(AppPageUtils page, Object object) {
List<?> list = page.getData();
List<Object> responseList = new ArrayList<>();
list.forEach(l -> {
Object objects = new Object();
BeanUtils.copyProperties(l, objects);
responseList.add(objects);
});
page.setData(responseList);
return page;
}
public static List<?> copyPageQueryList(AppPageUtils page, Object object) {
List<?> list = page.getData();
List<Object> responseList = new ArrayList<>();
list.forEach(l -> {
Object objects = new Object();
BeanUtils.copyProperties(l, objects);
responseList.add(objects);
});
return responseList;
}
}

View File

@ -13,8 +13,6 @@ public class JsonUtils {
public static List<String> JsonToList(String jsonString){
List<String> list = JSON.parseArray(jsonString, String.class);
return list;
return JSON.parseArray(jsonString, String.class);
}
}

View File

@ -19,7 +19,7 @@ public class R extends HashMap<String, Object> {
}
public static R error() {
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "未知异常,请联系管理员");
return error(HttpStatus.SC_INTERNAL_SERVER_ERROR, "服务器开小差啦");
}
public static R error(String msg) {

View File

@ -1,15 +0,0 @@
package io.linfeng.common.utils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
public class RequestUtils {
public static HttpServletRequest getRequest() {
ServletRequestAttributes ra= (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
return ra.getRequest();
}
}

View File

@ -1,78 +0,0 @@
package io.linfeng.common.utils;
/**
* @author linfeng
* @date 2022/1/20 13:15
*/
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
public class WechatUtil {
/**
* 获取小程序codeid换取openid
* @param code
* @return
*/
public static JSONObject getOpenId(String code,String appId,String secret) {
String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+ appId +"&secret=" +
secret +"&js_code="+code+"&grant_type=authorization_code";
PrintWriter out = null;
BufferedReader in = null;
String line;
StringBuffer stringBuffer = new StringBuffer();
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性 设置请求格式
//设置返回类型
conn.setRequestProperty("contentType", "text/plain");
//设置请求类型
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//设置超时时间
conn.setConnectTimeout(1000);
conn.setReadTimeout(1000);
conn.setDoOutput(true);
conn.connect();
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应 设置接收格式
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(), "UTF-8"));
while ((line = in.readLine()) != null) {
stringBuffer.append(line);
}
JSONObject jsonObject = JSONObject.parseObject(stringBuffer.toString());
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
}
//使用finally块来关闭输出流输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return null;
}
}

View File

@ -91,7 +91,7 @@ public class CategoryController {
@PostMapping("/delete")
@RequiresPermissions("admin:category:delete")
public R delete(@RequestBody Integer[] cateIds){
categoryService.removeByIds(Arrays.asList(cateIds));
categoryService.deleteByIdList(Arrays.asList(cateIds));
return R.ok();
}

View File

@ -16,6 +16,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import io.linfeng.common.utils.PageUtils;
import io.linfeng.modules.admin.entity.CategoryEntity;
import java.util.List;
import java.util.Map;
/**
@ -30,5 +31,7 @@ public interface CategoryService extends IService<CategoryEntity> {
PageUtils queryPage(Map<String, Object> params);
void saveCategory(CategoryEntity category);
void deleteByIdList(List<Integer> list);
}

View File

@ -42,6 +42,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.linfeng.modules.admin.dao.AppUserDao;
import io.linfeng.modules.admin.entity.AppUserEntity;
import org.springframework.transaction.annotation.Transactional;
import javax.servlet.http.HttpServletRequest;
@ -104,15 +105,19 @@ public class AppUserServiceImpl extends ServiceImpl<AppUserDao, AppUserEntity> i
}
@Override
@Transactional(rollbackFor = Exception.class)
public void openBan(Integer id) {
Integer status = this.lambdaQuery().eq(AppUserEntity::getUid, id).one().getStatus();
if (status.equals(Constant.USER_NORMAL)) {
throw new LinfengException("该用户已解除禁用");
}
this.lambdaUpdate()
boolean update = this.lambdaUpdate()
.set(AppUserEntity::getStatus, 0)
.eq(AppUserEntity::getUid, id)
.update();
if(!update){
throw new LinfengException("解除失败");
}
}
@Override

View File

@ -13,8 +13,14 @@
package io.linfeng.modules.admin.service.impl;
import io.linfeng.common.exception.LinfengException;
import io.linfeng.modules.admin.entity.PostEntity;
import io.linfeng.modules.admin.service.PostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@ -24,11 +30,16 @@ import io.linfeng.common.utils.Query;
import io.linfeng.modules.admin.dao.CategoryDao;
import io.linfeng.modules.admin.entity.CategoryEntity;
import io.linfeng.modules.admin.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Autowired
private PostService postService;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryEntity> page = this.page(
@ -39,13 +50,37 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity
return new PageUtils(page);
}
/**
* 分类保存
*
* @param category
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void saveCategory(CategoryEntity category) {
Integer count = this.lambdaQuery().eq(CategoryEntity::getCateName, category.getCateName()).count();
if(count!=0){
if (count != 0) {
throw new LinfengException("分类名不能重复");
}
this.save(category);
}
/**
* 删除分类
*
* @param list
*/
@Override
@Transactional(rollbackFor = Exception.class)
public void deleteByIdList(List<Integer> list) {
list.forEach(id -> {
Integer count = postService.lambdaQuery().eq(PostEntity::getCut, id).count();
if (count > 0) {
CategoryEntity category = this.getById(id);
throw new LinfengException(category.getCateName() + "分类下存在帖子未删除");
}
});
this.removeByIds(list);
}
}

View File

@ -46,13 +46,17 @@ public class FollowServiceImpl extends ServiceImpl<FollowDao, FollowEntity> impl
@Override
public Integer getFollowCount(Integer uid) {
return this.lambdaQuery().eq(FollowEntity::getUid, uid).count();
return this.lambdaQuery()
.eq(FollowEntity::getUid, uid)
.count();
}
@Override
public Integer getFans(Integer uid) {
return this.lambdaQuery().eq(FollowEntity::getFollowUid,uid).count();
return this.lambdaQuery()
.eq(FollowEntity::getFollowUid,uid)
.count();
}
@Override
@ -83,10 +87,7 @@ public class FollowServiceImpl extends ServiceImpl<FollowDao, FollowEntity> impl
lambdaQueryWrapper2.eq(FollowEntity::getUid, followUid);
lambdaQueryWrapper2.eq(FollowEntity::getFollowUid, uid);
Integer num2 = baseMapper.selectCount(lambdaQueryWrapper2);
if(num2==0){
return 2;
}
return 1;
return num2==0?2:1;
}
@Override