增加垃圾模块接口

master
panqihua 5 years ago
parent 1f67f49039
commit 6681836d79
  1. 2
      build.gradle
  2. 56
      src/main/java/com/community/pocket/api/android/GarbageController.java
  3. 33
      src/main/java/com/community/pocket/entity/po/android/Garbage.java
  4. 25
      src/main/java/com/community/pocket/entity/po/android/GarbageWasteManage.java
  5. 31
      src/main/java/com/community/pocket/entity/po/android/GarbageWastePrice.java
  6. 40
      src/main/java/com/community/pocket/entity/vo/android/GarbageCateGory.java
  7. 28
      src/main/java/com/community/pocket/entity/vo/android/GarbageSortingResponse.java
  8. 22
      src/main/java/com/community/pocket/entity/vo/android/GarbageVo.java
  9. 54
      src/main/java/com/community/pocket/entity/vo/android/GarbageWasteResponse.java
  10. 16
      src/main/java/com/community/pocket/entity/vo/android/QueryGarbageForm.java
  11. 16
      src/main/java/com/community/pocket/entity/vo/android/QueryWasteForm.java
  12. 67
      src/main/java/com/community/pocket/repository/android/GarbageDao.java
  13. 53
      src/main/java/com/community/pocket/repository/android/GarbagePriceDao.java
  14. 2
      src/main/java/com/community/pocket/util/TableName.java
  15. 7
      src/main/resources/application.yaml
  16. 15948
      src/main/resources/garbage.json
  17. 34
      src/test/java/com/community/pocket/DemoApplicationTests.java
  18. 17
      src/test/java/com/community/pocket/MyGarbage.java

@ -27,6 +27,8 @@ dependencies {
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation group: 'org.apache.commons', name: 'commons-lang3', version: '3.10'
implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
testImplementation 'org.springframework.boot:spring-boot-devtools'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'

@ -0,0 +1,56 @@
package com.community.pocket.api.android;
import com.community.pocket.entity.po.android.GarbageWasteManage;
import com.community.pocket.entity.po.android.GarbageWastePrice;
import com.community.pocket.entity.vo.Result;
import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.android.GarbageDao;
import com.community.pocket.repository.android.GarbagePriceDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* 垃圾分类接口
*/
@RestController
public class GarbageController {
@Autowired
private GarbageDao garbageDao;
@Autowired
private GarbagePriceDao garbagePriceDao;
//检索垃圾
@GetMapping("/garbage/sorting")
public GarbageSortingResponse search(QueryGarbageForm garbageForm) {
List<GarbageVo> garbageVoList = garbageDao.query(garbageForm);
GarbageSortingResponse response = new GarbageSortingResponse(Result.OK, GarbageSortingResponse.Msg.ok);
response.setGarbageSortings(garbageVoList);
return response;
}
//检索默认废品信息
@GetMapping("/garbage/waste")
public GarbageWasteResponse searchDefault() {
GarbageWasteResponse response = new GarbageWasteResponse(Result.OK, GarbageWasteResponse.Msg.ok);
List<GarbageWastePrice> garbageWastePrices = garbagePriceDao.query("");
GarbageWasteManage manage = garbagePriceDao.queryManager();
response.setDefaultList(garbageWastePrices);
response.setGarbageWasteManage(manage);
return response;
}
//检索废品
@GetMapping("/garbage/waste/query")
public GarbageWasteResponse search(QueryWasteForm queryWasteForm) {
GarbageWasteResponse response = new GarbageWasteResponse(Result.OK, GarbageWasteResponse.Msg.ok);
List<GarbageWastePrice> garbageWastePrices = garbagePriceDao.query(queryWasteForm.getName());
response.setSearchList(garbageWastePrices);
return response;
}
}

@ -0,0 +1,33 @@
package com.community.pocket.entity.po.android;
import com.community.pocket.util.TableName;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* 垃圾分类信息
*/
@Document(TableName.garbage)
public class Garbage {
//垃圾名
@Id
private String name;
//分类信息
private Integer category;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getCategory() {
return category;
}
public void setCategory(Integer category) {
this.category = category;
}
}

@ -0,0 +1,25 @@
package com.community.pocket.entity.po.android;
//废品管理员信息
public class GarbageWasteManage {
//管理员名
private String name;
//联系电话
private String mobie;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobie() {
return mobie;
}
public void setMobie(String mobie) {
this.mobie = mobie;
}
}

@ -0,0 +1,31 @@
package com.community.pocket.entity.po.android;
import com.community.pocket.util.TableName;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
//废品信息
@Document(TableName.garbageWastePrice)
public class GarbageWastePrice {
//废品名
@Id
private String name;
//废品价格
private Float price;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}

@ -0,0 +1,40 @@
package com.community.pocket.entity.vo.android;
public enum GarbageCateGory {
/**
* 1 (可回收垃圾)
* 2 (有害垃圾)
* 4 (湿垃圾)
* 8 (干垃圾)
* 16 (大件垃圾)
*/
type1(1, "可回收垃圾"),
type2(2, "有害垃圾"),
type4(4, "湿垃圾"),
type8(8, "干垃圾"),
type16(16, "大件垃圾");
private Integer type;
private String name;
GarbageCateGory(Integer type, String name) {
this.type = type;
this.name = name;
}
public Integer getType() {
return type;
}
public void setType(Integer type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -0,0 +1,28 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.vo.Result;
import java.util.List;
/**
* 垃圾分类信息
*/
public class GarbageSortingResponse extends AndroidResponse<GarbageSortingResponse.Msg> {
private List<GarbageVo> garbageSortings;
public GarbageSortingResponse(Result result, Msg message, Object... args) {
super(result, message, args);
}
public List<GarbageVo> getGarbageSortings() {
return garbageSortings;
}
public void setGarbageSortings(List<GarbageVo> garbageSortings) {
this.garbageSortings = garbageSortings;
}
public enum Msg implements CustomMessage {
ok
}
}

@ -0,0 +1,22 @@
package com.community.pocket.entity.vo.android;
public class GarbageVo {
private String name;
private String sorting;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSorting() {
return sorting;
}
public void setSorting(String sorting) {
this.sorting = sorting;
}
}

@ -0,0 +1,54 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.android.GarbageWasteManage;
import com.community.pocket.entity.po.android.GarbageWastePrice;
import com.community.pocket.entity.vo.Result;
import java.util.List;
/**
* 废品返回信息
*/
public class GarbageWasteResponse extends AndroidResponse<GarbageWasteResponse.Msg> {
public GarbageWasteResponse(Result result, Msg message, Object... args) {
super(result, message, args);
}
public enum Msg implements CustomMessage {
ok
}
//默认价格信息
private List<GarbageWastePrice> defaultList;
//检索价格信息
private List<GarbageWastePrice> searchList;
//管理员信息
private GarbageWasteManage garbageWasteManage;
public List<GarbageWastePrice> getDefaultList() {
return defaultList;
}
public void setDefaultList(List<GarbageWastePrice> defaultList) {
this.defaultList = defaultList;
}
public List<GarbageWastePrice> getSearchList() {
return searchList;
}
public void setSearchList(List<GarbageWastePrice> searchList) {
this.searchList = searchList;
}
public GarbageWasteManage getGarbageWasteManage() {
return garbageWasteManage;
}
public void setGarbageWasteManage(GarbageWasteManage garbageWasteManage) {
this.garbageWasteManage = garbageWasteManage;
}
}

@ -0,0 +1,16 @@
package com.community.pocket.entity.vo.android;
/**
* 垃圾分类查询
*/
public class QueryGarbageForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -0,0 +1,16 @@
package com.community.pocket.entity.vo.android;
/**
* 废品查询
*/
public class QueryWasteForm {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -0,0 +1,67 @@
package com.community.pocket.repository.android;
import com.community.pocket.entity.po.android.Garbage;
import com.community.pocket.entity.vo.android.GarbageCateGory;
import com.community.pocket.entity.vo.android.GarbageVo;
import com.community.pocket.entity.vo.android.QueryGarbageForm;
import com.community.pocket.repository.BaseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
//垃圾分类数据接口
@Repository
public class GarbageDao extends BaseDao<Garbage> {
private static final Logger LOG = LoggerFactory.getLogger(GarbageDao.class);
//查询垃圾分类信息
public List<GarbageVo> query(QueryGarbageForm garbageForm) {
List<GarbageVo> garbageVos = new ArrayList<>();
List<Garbage> garbageList = mongoTemplate.find(new Query(Criteria.where("name").regex(Pattern.compile(".*" + garbageForm.getName() + ".*"))), entityClass());
for (Garbage garbage : garbageList) {
GarbageVo garbageVo = new GarbageVo();
garbageVo.setName(garbage.getName());
switch (garbage.getCategory()) {
case 1:
garbageVo.setSorting(GarbageCateGory.type1.getName());
break;
case 2:
garbageVo.setSorting(GarbageCateGory.type2.getName());
break;
case 4:
garbageVo.setSorting(GarbageCateGory.type4.getName());
break;
case 8:
garbageVo.setSorting(GarbageCateGory.type8.getName());
break;
case 16:
garbageVo.setSorting(GarbageCateGory.type16.getName());
break;
}
garbageVos.add(garbageVo);
}
return garbageVos;
}
@Override
public Garbage save(Garbage garbage) {
try {
mongoTemplate.save(garbage);
} catch (Exception e) {
e.printStackTrace();
LOG.error(e.toString());
}
return null;
}
@Override
public Class<Garbage> entityClass() {
return Garbage.class;
}
}

@ -0,0 +1,53 @@
package com.community.pocket.repository.android;
import com.community.pocket.entity.po.android.GarbageWasteManage;
import com.community.pocket.entity.po.android.GarbageWastePrice;
import com.community.pocket.repository.BaseDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class GarbagePriceDao extends BaseDao<GarbageWastePrice> {
private static final Logger LOG = LoggerFactory.getLogger(GarbagePriceDao.class);
@Value("${waste.manage.name}")
private String name;
@Value("${waste.manage.mobie}")
private String mobie;
//查询废品数据
public List<GarbageWastePrice> query(String name) {
return mongoTemplate.find(new Query(Criteria.where("name").regex(".*" + name + ".*")), entityClass());
}
//查询废品管理员信息
public GarbageWasteManage queryManager() {
GarbageWasteManage manage = new GarbageWasteManage();
manage.setMobie(mobie);
manage.setName(name);
return manage;
}
@Override
public GarbageWastePrice save(GarbageWastePrice garbageWastePrice) {
try {
return mongoTemplate.save(garbageWastePrice);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
public Class<GarbageWastePrice> entityClass() {
return GarbageWastePrice.class;
}
}

@ -23,4 +23,6 @@ public class TableName {
//访客信息
public static final String visitor = "visitor";
public static final String visitorPeople = "visitorPeople";
public static final String garbage = "garbage";
public static final String garbageWastePrice = "garbageWastePrice";
}

@ -60,3 +60,10 @@ register:
visitor:
#日期格式
dateformat: 'yyyy/MM/dd HH:mm'
#废品回收配置
waste:
#废品回收
manage:
name: admin
mobie: 10086

File diff suppressed because it is too large Load Diff

@ -1,15 +1,24 @@
package com.community.pocket;
import com.community.pocket.entity.po.android.Garbage;
import com.community.pocket.entity.po.android.GarbageWastePrice;
import com.community.pocket.entity.vo.android.QueryUserForm;
import com.community.pocket.repository.android.GarbageDao;
import com.community.pocket.repository.android.GarbagePriceDao;
import com.community.pocket.repository.android.UserDao;
import com.community.pocket.repository.android.VisitorPeopleDao;
import com.google.gson.Gson;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.Random;
@SpringBootTest
class DemoApplicationTests {
@ -20,6 +29,12 @@ class DemoApplicationTests {
@Autowired
private VisitorPeopleDao visitorPeopleDao;
@Autowired
private GarbageDao garbageDao;
@Autowired
private GarbagePriceDao garbagePriceDao;
private static final Logger LOG = LoggerFactory.getLogger(DemoApplicationTests.class);
@Test
@ -39,4 +54,23 @@ class DemoApplicationTests {
LOG.info(String.join(",", s));
}
@Test
void loadData() throws FileNotFoundException {
Gson gson = new Gson();
MyGarbage g = gson.fromJson(new FileReader("src/main/resources/garbage.json"), MyGarbage.class);
for (Garbage garbage : g.getList()) {
garbageDao.save(garbage);
}
}
@Test
void loadPriceData() {
for (int i = 0; i < 100; i++) {
GarbageWastePrice price = new GarbageWastePrice();
price.setName(RandomStringUtils.randomAlphabetic(6));
price.setPrice(new Random().nextInt(100) * 1F);
garbagePriceDao.save(price);
}
}
}

@ -0,0 +1,17 @@
package com.community.pocket;
import com.community.pocket.entity.po.android.Garbage;
import java.util.List;
public class MyGarbage {
private List<Garbage> list;
public List<Garbage> getList() {
return list;
}
public void setList(List<Garbage> list) {
this.list = list;
}
}
Loading…
Cancel
Save