Commit 21b89d9b authored by shulidong's avatar shulidong

案例crud

parent 554a8aed
...@@ -42,6 +42,11 @@ ...@@ -42,6 +42,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId> <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.4.3</version>
</dependency>
<!--json模块--> <!--json模块-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
...@@ -185,11 +190,12 @@ ...@@ -185,11 +190,12 @@
<dependency> <dependency>
<groupId>net.oschina.zcx7878</groupId> <groupId>net.oschina.zcx7878</groupId>
<artifactId>fastdfs-client-java</artifactId> <artifactId>fastdfs-client-java</artifactId>
<version>1.27.0.0</version>
</dependency>
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
</dependency> </dependency>
<!--<dependency>-->
<!--<groupId>com.github.tobato</groupId>-->
<!--<artifactId>fastdfs-client</artifactId>-->
<!--</dependency>-->
<dependency> <dependency>
<groupId>com.aspose</groupId> <!--自定义--> <groupId>com.aspose</groupId> <!--自定义-->
<artifactId>words</artifactId> <!--自定义--> <artifactId>words</artifactId> <!--自定义-->
......
package cn.wise.sc.energy.power.plant.business.controller;
import cn.hutool.core.util.StrUtil;
import cn.wise.sc.energy.power.plant.business.domain.CaseAnalysisInfo;
import cn.wise.sc.energy.power.plant.business.domain.PageQuery;
import cn.wise.sc.energy.power.plant.business.domain.UserInfoQuery;
import cn.wise.sc.energy.power.plant.business.repository.CaseAnalysisInfoRepository;
import cn.wise.sc.energy.power.plant.business.service.ICaseAnalysisInfoService;
import cn.wise.sc.energy.power.plant.business.utils.BeanUtilsExt;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
/**
* @author neo.shu
* @since 2020/9/12 15:29
*/
@CrossOrigin
@Validated
@RestController
@RequestMapping("caseanalysis/")
public class CaseAnalysisInfoController {
@Autowired
CaseAnalysisInfoRepository caseRepository;
@Autowired
ICaseAnalysisInfoService caseAnalysisInfoService;
@PostMapping("/page")
public BaseResponse<Page<CaseAnalysisInfo>> page(@RequestBody PageQuery page) {
if (StrUtil.isBlank(page.getPlantId())){
return BaseResponse.errorMsg("电厂id不能为空!");
}
return caseAnalysisInfoService.page( page, page.getPlantId());
}
@PostMapping("/newOrUpdate")
@Transactional(propagation = Propagation.REQUIRED)
public BaseResponse<Boolean> add(@RequestBody CaseAnalysisInfo info) {
CaseAnalysisInfo caseAnalysisInfo ;
if (info.getCaseId() == null) {
caseAnalysisInfo = caseRepository.save(info);
}else{
caseAnalysisInfo = caseRepository.getOne(info.getCaseId());
BeanUtils.copyProperties(info,caseAnalysisInfo, BeanUtilsExt.getNullPropertyNames(info));
caseAnalysisInfo.setUpdateTime(LocalDateTime.now());
caseAnalysisInfo = caseRepository.save(caseAnalysisInfo);
}
if (caseAnalysisInfo.getId() != null) {
return BaseResponse.okData(true);
} else {
return BaseResponse.okData(false);
}
}
@PostMapping("/del")
public BaseResponse<Boolean> del(String id){
return null;
}
}
package cn.wise.sc.energy.power.plant.business.domain; package cn.wise.sc.energy.power.plant.business.domain;
import com.fasterxml.jackson.annotation.JsonValue;
import com.sun.xml.internal.ws.api.message.Attachment;
import com.vladmihalcea.hibernate.type.json.JsonStringType;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.TypeDef;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Table; import javax.persistence.Table;
import java.time.LocalDateTime;
import java.util.List;
/** /**
* @author neo.shu * @author neo.shu
...@@ -15,12 +24,39 @@ import javax.persistence.Table; ...@@ -15,12 +24,39 @@ import javax.persistence.Table;
@Entity @Entity
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@Table(name = "caseAnalysisInfo") @Table(name = "caseanalysisinfo")
@TypeDef(name = "json", typeClass = JsonStringType.class)
public class CaseAnalysisInfo extends AbstractEntity<String>{ public class CaseAnalysisInfo extends AbstractEntity<String>{
@Id @Id
@Column(name = "caseid") @Column(name = "case_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String caseId; private String caseId;
@Column(name = "plant_id")
private String plantId;
@Column(name = "title")
private String title;
@Column(name = "info")
private String info;
@Column(name = "keywords",columnDefinition = "json")
@Type(type = "json")
private List<String> keywords;
@Column(name = "attachments",columnDefinition = "json")
@Type(type = "json")
private List<CaseAttachment> attachments;
@Column(name = "create_time")
private LocalDateTime createTime = LocalDateTime.now();
@Column(name="update_time")
private LocalDateTime updateTime = LocalDateTime.now();
@Override @Override
public String getId() { public String getId() {
return getCaseId(); return getCaseId();
......
package cn.wise.sc.energy.power.plant.business.domain;
import lombok.Data;
import java.io.Serializable;
/**
* @author neo.shu
* @since 2020/9/12 17:31
*/
@Data
public class CaseAttachment implements Serializable {
private String filePath;
private String showPath;
private String name;
private long size;
}
...@@ -3,6 +3,7 @@ package cn.wise.sc.energy.power.plant.business.domain; ...@@ -3,6 +3,7 @@ package cn.wise.sc.energy.power.plant.business.domain;
import lombok.Data; import lombok.Data;
import java.io.Serializable; import java.io.Serializable;
import java.util.List;
/** /**
* @description: 分页参数 * @description: 分页参数
...@@ -17,4 +18,11 @@ public class PageQuery implements Serializable { ...@@ -17,4 +18,11 @@ public class PageQuery implements Serializable {
private Integer pageNo = 0; private Integer pageNo = 0;
private Integer pageSize = 10; private Integer pageSize = 10;
private List<String> keywords;
private String vaguewords;
private String plantId;
private String startTime;
private String endTime;
} }
...@@ -2,12 +2,10 @@ package cn.wise.sc.energy.power.plant.business.domain; ...@@ -2,12 +2,10 @@ package cn.wise.sc.energy.power.plant.business.domain;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.GeneratedValue; import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType; import javax.persistence.GenerationType;
......
package cn.wise.sc.energy.power.plant.business.repository;
import cn.wise.sc.energy.power.plant.business.domain.CaseAnalysisInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
/**
* @author neo.shu
* @since 2020/9/12 15:20
*/
@Repository
public interface CaseAnalysisInfoRepository extends
JpaRepository<CaseAnalysisInfo, String>,
JpaSpecificationExecutor<CaseAnalysisInfo> {
}
package cn.wise.sc.energy.power.plant.business.repository; package cn.wise.sc.energy.power.plant.business.repository;
import cn.wise.sc.energy.power.plant.business.domain.UnitInfo; import cn.wise.sc.energy.power.plant.business.domain.UnitInfo;
import cn.wise.sc.energy.power.plant.business.domain.UserInfo;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
...@@ -13,4 +14,5 @@ import org.springframework.stereotype.Repository; ...@@ -13,4 +14,5 @@ import org.springframework.stereotype.Repository;
@Repository @Repository
public interface UnitInfoRepository extends public interface UnitInfoRepository extends
JpaSpecificationExecutor<UnitInfo>, JpaRepository<UnitInfo, String> { JpaSpecificationExecutor<UnitInfo>, JpaRepository<UnitInfo, String> {
} }
package cn.wise.sc.energy.power.plant.business.service;
import cn.wise.sc.energy.power.plant.business.domain.CaseAnalysisInfo;
import cn.wise.sc.energy.power.plant.business.domain.PageQuery;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import org.springframework.data.domain.Page;
import java.util.List;
/**
* @author neo.shu
* @since 2020/9/12 15:21
*/
public interface ICaseAnalysisInfoService {
BaseResponse<Page<CaseAnalysisInfo>> page(PageQuery page, String plantId);
}
package cn.wise.sc.energy.power.plant.business.service.impl;
import cn.hutool.core.util.StrUtil;
import cn.wise.sc.energy.power.plant.business.domain.CaseAnalysisInfo;
import cn.wise.sc.energy.power.plant.business.domain.PageQuery;
import cn.wise.sc.energy.power.plant.business.repository.CaseAnalysisInfoRepository;
import cn.wise.sc.energy.power.plant.business.service.ICaseAnalysisInfoService;
import cn.wise.sc.energy.power.plant.common.core.bean.BaseResponse;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Selection;
import javax.persistence.criteria.Subquery;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
/**
* @author neo.shu
* @since 2020/9/12 15:26
*/
@Service
public class CaseAnalysisInfoServiceImpl implements ICaseAnalysisInfoService {
@Autowired
CaseAnalysisInfoRepository caseRepository;
@Override
public BaseResponse<Page<CaseAnalysisInfo>> page(PageQuery page, String plantId) {
Sort sort = Sort.by(Sort.Direction.DESC, "caseId");
Pageable pages = PageRequest.of(page.getPageNo(), page.getPageSize(), sort);
Page<CaseAnalysisInfo> infoPage;
Specification<CaseAnalysisInfo> specification = (root, query, cb) -> {
List<Predicate> list = new ArrayList<Predicate>();
//模糊搜索
if (!StringUtils.isEmpty(page.getVaguewords())) {
list.add(cb.like(root.get("title").as(String.class), "%" + page.getVaguewords() + "%"));
}
//电厂id必传
if (!StringUtils.isEmpty(plantId)) {
list.add(cb.equal(root.get("plantId"),plantId));
}
//匹配关键字
if (page.getKeywords().size() > 0) {
//包含元素
/* for (String item : page.getKeywords()) {
Expression expression = query.
list.add(cb.isTrue());
}*/
}
//时间选择
Predicate timePredicate = cb.conjunction();
timePredicate.in();
if (page.getStartTime() != null && !page.getStartTime().trim().equals("")) {
list.add(cb.greaterThanOrEqualTo(root.get("createTime").as(LocalDateTime.class), LocalDateTime.parse(page.getStartTime())));
}
//结束日期
if (page.getEndTime() != null && !page.getEndTime().trim().equals("")) {
list.add(cb.lessThanOrEqualTo(root.get("createTime").as(LocalDateTime.class), LocalDateTime.parse(page.getEndTime())));
}
return cb.and(list.toArray(new Predicate[list.size()]));
};
infoPage = caseRepository.findAll(specification, pages);
return BaseResponse.okData(infoPage);
}
}
package cn.wise.sc.energy.power.plant.business.utils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import java.util.HashSet;
import java.util.Set;
public class BeanUtilsExt {
public static String[] getNullPropertyNames(Object source) {
final BeanWrapper src = new BeanWrapperImpl(source);
java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();
Set<String> emptyNames = new HashSet<String>();
for (java.beans.PropertyDescriptor pd : pds) {
Object srcValue = src.getPropertyValue(pd.getName());
if (srcValue == null) emptyNames.add(pd.getName());
}
String[] result = new String[emptyNames.size()];
return emptyNames.toArray(result);
}
}
...@@ -96,7 +96,10 @@ spring: ...@@ -96,7 +96,10 @@ spring:
# max-idle-per-key: 10 # max-idle-per-key: 10
# #每个key对应的连接池最小空闲连接数 # #每个key对应的连接池最小空闲连接数
# max_idle_per_key: 5 # max_idle_per_key: 5
jpa:
hibernate:
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
opentsdb: opentsdb:
baseUrl: http://39.105.86.33:8182 baseUrl: http://39.105.86.33:8182
server: server:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment