|
@@ -9,6 +9,8 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
import java.util.List;
|
|
|
|
|
|
/**
|
|
@@ -40,13 +42,10 @@ public class FaqServiceImpl extends ServiceImpl<FaqMapper, Faq> implements FaqSe
|
|
|
public RestResponse add(Faq faq) {
|
|
|
List<Faq> faqs = this.list(new QueryWrapper<Faq>()
|
|
|
.select("id", "serial")
|
|
|
- .eq("type", faq.getType()).eq("parent_id", faq.getParentId())
|
|
|
+ .eq("type", faq.getType())
|
|
|
+ .eq("parent_id", faq.getParentId())
|
|
|
.orderByDesc("serial"));
|
|
|
- if (faqs.isEmpty()) {
|
|
|
- faq.setSerial(1);
|
|
|
- } else {
|
|
|
- faq.setSerial(faqs.get(0).getSerial() + 1);
|
|
|
- }
|
|
|
+ faq.setSerial(faqs.size() + 1);
|
|
|
if (this.save(faq)) {
|
|
|
return RestResponse.success(null, "成功");
|
|
|
}
|
|
@@ -83,61 +82,44 @@ public class FaqServiceImpl extends ServiceImpl<FaqMapper, Faq> implements FaqSe
|
|
|
//type 1上移 0下移
|
|
|
public RestResponse serial(Long id, Integer type) {
|
|
|
Faq byId = this.getById(id);
|
|
|
- if (byId == null) {
|
|
|
+ if (null == byId) {
|
|
|
return RestResponse.success();
|
|
|
}
|
|
|
-
|
|
|
- List<Faq> faqs;
|
|
|
- if (type == 1) {
|
|
|
- faqs = this.list(new QueryWrapper<Faq>().eq("type", byId.getType())
|
|
|
- .eq("parent_id", byId.getParentId())
|
|
|
- .orderByDesc("serial"));
|
|
|
- } else {
|
|
|
- faqs = this.list(new QueryWrapper<Faq>().eq("type", byId.getType())
|
|
|
- .eq("parent_id", byId.getParentId())
|
|
|
- .orderByAsc(("serial")));
|
|
|
- }
|
|
|
-
|
|
|
- if (faqs.isEmpty()) {
|
|
|
- byId.setSerial(1);
|
|
|
- this.updateById(byId);
|
|
|
+ List<Faq> faqs = this.list(new QueryWrapper<Faq>()
|
|
|
+ .eq("parent_id", byId.getParentId())
|
|
|
+ .eq("type", byId.getType())
|
|
|
+ .orderByDesc("serial"));
|
|
|
+ if (faqs.size() == 1) {
|
|
|
return RestResponse.success(null, "成功");
|
|
|
}
|
|
|
-
|
|
|
- Faq faq;
|
|
|
- int i;
|
|
|
+ //type 0下移
|
|
|
+ //type 1上移
|
|
|
if (type == 1) {
|
|
|
- for (i = 0; i < faqs.size(); i++) {
|
|
|
- if (faqs.get(i).getSerial() < byId.getSerial()) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
- } else {
|
|
|
- for (i = 0; i < faqs.size(); i++) {
|
|
|
- if (faqs.get(i).getSerial() > byId.getSerial()) {
|
|
|
- break;
|
|
|
- }
|
|
|
- }
|
|
|
+ faqs.sort(Comparator.comparing(Faq::getSerial));
|
|
|
}
|
|
|
-
|
|
|
- if (i == faqs.size()) {
|
|
|
- return RestResponse.success(null,"成功");
|
|
|
+ if (faqs.get(0).getSerial().equals(byId.getSerial())) {
|
|
|
+ return RestResponse.success(null, "成功");
|
|
|
}
|
|
|
-
|
|
|
- faq = faqs.get(i);
|
|
|
-
|
|
|
-
|
|
|
- if (byId.getId().equals(faq.getId())) {
|
|
|
+ if (upOrDown(byId, faqs)) {
|
|
|
return RestResponse.success(null, "成功");
|
|
|
}
|
|
|
+ return RestResponse.fail();
|
|
|
+ }
|
|
|
|
|
|
- Integer serial = faq.getSerial();
|
|
|
- faq.setSerial(byId.getSerial());
|
|
|
- byId.setSerial(serial);
|
|
|
-
|
|
|
- this.updateById(faq);
|
|
|
- this.updateById(byId);
|
|
|
-
|
|
|
- return RestResponse.success(null, "成功");
|
|
|
+ private boolean upOrDown(Faq byId, List<Faq> faqs) {
|
|
|
+ System.out.println(faqs);
|
|
|
+ Faq f = new Faq();
|
|
|
+ for (Faq faq : faqs) {
|
|
|
+ if (faq.getSerial().equals(byId.getSerial())) {
|
|
|
+ Integer serial = f.getSerial();
|
|
|
+ f.setSerial(byId.getSerial());
|
|
|
+ byId.setSerial(serial);
|
|
|
+ if (this.updateById(byId) && this.updateById(f)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ f = faq;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
}
|
|
|
}
|