CropLandService.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package com.sysu.admin.controller.crop;
  2. import com.mysql.jdbc.StringUtils;
  3. import com.querydsl.core.types.Predicate;
  4. import com.querydsl.core.types.Projections;
  5. import com.querydsl.core.types.QBean;
  6. import com.querydsl.core.types.QList;
  7. import com.querydsl.jpa.impl.JPAQuery;
  8. import com.querydsl.jpa.impl.JPAUpdateClause;
  9. import com.sysu.admin.controller.city.*;
  10. import com.sysu.admin.controller.crop.executor_table.ExecutorTable;
  11. import com.sysu.admin.controller.crop.executor_table.ExecutorTableService;
  12. import com.sysu.admin.controller.crop.executor_table.PKRP;
  13. import com.sysu.admin.controller.crop.images.CropImage;
  14. import com.sysu.admin.controller.crop.images.CropImageService;
  15. import com.sysu.admin.controller.crop.images.ImageType;
  16. import com.sysu.admin.controller.crop.interceptor.DynamicTableNames;
  17. import com.sysu.admin.controller.crop.range.AutoTableNameInterceptor;
  18. import com.sysu.admin.controller.crop.range.LandRangeIndexService;
  19. import com.sysu.admin.controller.geo.land.LandTaskStatus;
  20. import com.sysu.admin.support.base.BaseService;
  21. import com.sysu.admin.support.system.config.ConfigContext;
  22. import com.sysu.admin.support.system.user.User;
  23. import com.sysu.admin.utils.TextUtil;
  24. import com.sysu.admin.utils.file.FileUtil;
  25. import com.sysu.admin.utils.shape.GeoCastUtil;
  26. import com.sysu.admin.utils.shape.ShapeReader;
  27. import com.xiesx.fastboot.base.result.BaseResult;
  28. import com.xiesx.fastboot.core.jpa.JpaPlusRepository;
  29. import org.apache.commons.codec.binary.Base64;
  30. import org.apache.commons.lang3.ObjectUtils;
  31. import org.geolatte.geom.crs.CoordinateReferenceSystems;
  32. import org.locationtech.jts.geom.Geometry;
  33. import org.locationtech.jts.geom.Point;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.stereotype.Service;
  36. import javax.persistence.Query;
  37. import javax.transaction.Transactional;
  38. import java.io.File;
  39. import java.io.IOException;
  40. import java.nio.file.Files;
  41. import java.nio.file.Path;
  42. import java.nio.file.Paths;
  43. import java.nio.file.StandardOpenOption;
  44. import java.util.*;
  45. @Service
  46. public class CropLandService extends BaseService<CropLand, Long> {
  47. @Autowired
  48. CropImageService cropImageService;
  49. @Autowired
  50. ConfigContext configContext;
  51. @Autowired
  52. ProvinceRepository provinceRepository;
  53. @Autowired
  54. CityRepository cityRepository;
  55. @Autowired
  56. DistrictRepository districtRepository;
  57. @Autowired
  58. LandRangeIndexService landRangeIndexService;
  59. @Autowired
  60. ExecutorTableService executorTableService;
  61. @Override
  62. @DynamicTableNames(value = "aLong")
  63. public CropLand findOne(Long aLong) {
  64. CropLand bean = super.findOne(aLong);
  65. return bean;
  66. }
  67. @Override
  68. @DynamicTableNames(value = "entity")
  69. public <S extends CropLand> S save(S entity) {
  70. return super.save(entity);
  71. }
  72. @DynamicTableNames(value = "point")
  73. public List<CropPoint> findByBuffer(Double[] point, Integer meter){
  74. double degree = meter / (2 * Math.PI * 6371004) * 360;
  75. return mCropPointRepository.findByBufferAndStatus("Point("+point[0]+" "+point[1]+")",degree, new Integer[]{
  76. LandTaskStatus.published.ordinal(),LandTaskStatus.receive.ordinal()
  77. });
  78. }
  79. @DynamicTableNames(executor = "executor")
  80. public List<Object[]> findIdAndNameByExecutor(Long executor){
  81. Query query =mEntityManager.createQuery("select c.id,c.name,c.updateDate,c.confirm from CropLand c where c.executor = " + executor);
  82. return query.getResultList();
  83. }
  84. @DynamicTableNames(value = "point")
  85. public CropLand findByPoint(Double[] point){
  86. CropLand cityLand = mCropLandRepository.findByPoint("Point("+point[0]+" "+point[1]+")");
  87. return cityLand;
  88. }
  89. public String getBuffer(Double[] point, Integer meter){
  90. double degree = meter / (2 * Math.PI * 6371004) * 360;
  91. return mCropPointRepository.getBuffer("Point("+point[0]+" "+point[1]+")", degree);
  92. }
  93. @DynamicTableNames(value = "tableNames")
  94. public List<CropPoint> findCropLandNoGeomByTableNamesAndBBox(String tableNames, Double x1, Double y1, Double x2, Double y2, String statusString){
  95. String wkt = bboxToWkt(x1, y1 , x2, y2);
  96. String sql = " select "+ CropPoint.selectColumns;
  97. sql += " from p_crop where ST_Intersects(geom,st_geomfromtext('"+wkt+"',4326)) ";
  98. if(ObjectUtils.isNotEmpty(statusString)){
  99. sql += " and \"status\" in ("+statusString+") ";
  100. }
  101. List<CropPoint> cropPointList = mEntityManager.createNativeQuery(sql, CropPoint.class).getResultList();
  102. return cropPointList;
  103. }
  104. @DynamicTableNames(value = "id")
  105. @Transactional
  106. public void updateStatus(Long id, Integer status, Long updateUserId){
  107. CropLand bean = mCropLandRepository.findOne(id);
  108. bean.setStatus(status);
  109. if(status.equals(LandTaskStatus.receive.ordinal())) {
  110. bean.setReceiveDate(new Date());
  111. bean.setReceiver(updateUserId);
  112. }
  113. mCropLandRepository.saveAndFlush(bean);
  114. }
  115. public Point getCenterPoint(Geometry geometry){
  116. geometry.setSRID(4326);
  117. return (Point)GeoCastUtil.wktToGeom(mCropLandRepository.getCenterPoint(geometry));
  118. }
  119. public void setCity(CropLand bean, District district){
  120. bean.setProvince(((int)(Integer.valueOf(district.getCode()) / 10000)) * 10000);
  121. bean.setCity(((int)(Integer.valueOf(district.getCode()) / 100)) * 100);
  122. bean.setDistrict(Integer.valueOf(district.getCode()));
  123. }
  124. @DynamicTableNames(value = "bean")
  125. @Transactional
  126. public void saveCropAndImages(CropLand bean){
  127. CropImage fay = bean.getFay();
  128. CropImage center = bean.getCenter();
  129. CropImage near = bean.getNear();
  130. saveImage(fay, bean.getId(), ImageType.fay, bean.getExecutor());
  131. saveImage(center, bean.getId(), ImageType.center, bean.getExecutor());
  132. saveImage(near, bean.getId(), ImageType.near, bean.getExecutor());
  133. String tableName = AutoTableNameInterceptor.myTable.get();
  134. ExecutorTable executorTable = new ExecutorTable().setPk(new PKRP().tableName(tableName).executor(bean.getExecutor()));
  135. mCropLandRepository.saveAndFlush(bean);
  136. executorTableService.save(executorTable);
  137. }
  138. private void saveImage(CropImage image, Long cropId, ImageType type, Long userId){
  139. if(ObjectUtils.isNotEmpty(image)) {
  140. cropImageService.deleteAll(cropImageService.findByCropIdAndType(cropId, type));
  141. image.setType(type.ordinal());
  142. image.setFilename(decryptByBase64(image.getBase64(), type.name(), userId));
  143. image.setCropId(cropId);
  144. cropImageService.save(image);
  145. }
  146. }
  147. @DynamicTableNames(value = "tableName")
  148. public void batchPublishByIds(String tableName, List<Long> ids){
  149. QCropLand qCropLand = QCropLand.cropLand;
  150. JPAUpdateClause jpaUpdateClause = mJPAQueryFactory.update(qCropLand);
  151. jpaUpdateClause.set(qCropLand.status, LandTaskStatus.published.ordinal());
  152. jpaUpdateClause.set(qCropLand.updateDate, new Date());
  153. Predicate predicate = null;
  154. if(ObjectUtils.isNotEmpty(ids)){
  155. predicate = qCropLand.id.in(ids);
  156. }
  157. if(predicate != null){
  158. predicate = qCropLand.status.eq(LandTaskStatus.unpublished.ordinal()).and(predicate);
  159. jpaUpdateClause.where(predicate);
  160. jpaUpdateClause.execute();
  161. }
  162. }
  163. @DynamicTableNames(value = "tableName")
  164. public void batchPublishByTableName(String tableName){
  165. QCropLand qCropLand = QCropLand.cropLand;
  166. JPAUpdateClause jpaUpdateClause = mJPAQueryFactory.update(qCropLand);
  167. jpaUpdateClause.set(qCropLand.status, LandTaskStatus.published.ordinal());
  168. jpaUpdateClause.set(qCropLand.updateDate, new Date());
  169. if(tableName != null){
  170. Predicate predicate = qCropLand.status.eq(LandTaskStatus.unpublished.ordinal());
  171. jpaUpdateClause.where(predicate);
  172. jpaUpdateClause.execute();
  173. }
  174. }
  175. @DynamicTableNames(value = "tableNames")
  176. public List<CropLand> findAllByStatusAndDistrict(String tableNames, Integer status){
  177. Query query =mEntityManager.createQuery(
  178. "select new CropLand(c.id,c.cropType,c.growingPeriod,c.confirm,c.district,c.name,c.address,c.desc) from CropLand c where c.status = " + status);
  179. return query.getResultList();
  180. }
  181. /**
  182. * 把base64转化为文件.
  183. *
  184. * @param base64 base64
  185. * @return boolean isTrue
  186. */
  187. public String decryptByBase64(String base64, String sign, Long userId) {
  188. if (StringUtils.isNullOrEmpty(base64)) {
  189. return null;
  190. }
  191. int index = base64.indexOf(",");
  192. if(index > -1){
  193. base64 = base64.substring(index + 1);
  194. }
  195. String imageDirPath = configContext.getImageDirPath();
  196. String newFileName = System.currentTimeMillis() + "_" + userId+"_"+sign+".jpg";
  197. try {
  198. Files.write(Paths.get(imageDirPath+ newFileName),
  199. Base64.decodeBase64(base64), StandardOpenOption.CREATE);
  200. } catch (IOException e) {
  201. e.printStackTrace();
  202. }
  203. return newFileName;
  204. }
  205. /**
  206. * 把base64转化为文件.
  207. *
  208. * @param base64 base64
  209. * @return boolean isTrue
  210. */
  211. public Path fileByBase64(String base64, String sign, Long userId) {
  212. if (StringUtils.isNullOrEmpty(base64)) {
  213. return null;
  214. }
  215. int index = base64.indexOf(",");
  216. if(index > -1){
  217. base64 = base64.substring(index + 1);
  218. }
  219. String imageDirPath = configContext.getImageDirPath();
  220. String newFileName = System.currentTimeMillis() + "_" + userId+"_"+sign;
  221. Path path = null;
  222. try {
  223. path = Files.write(Paths.get(imageDirPath+ newFileName),
  224. Base64.decodeBase64(base64), StandardOpenOption.CREATE);
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. }
  228. return path;
  229. }
  230. public static String bboxToWkt(Double x1, Double y1, Double x2, Double y2){
  231. return "POLYGON (("+x1+" "+y1+", "+x1+" "+y2+", "+x2+" "+y2+", "+x2+" "+y1+", "+x1+" "+y1+"))";
  232. }
  233. @Transactional
  234. public void exec(String sql){
  235. mEntityManager.createNativeQuery(sql).executeUpdate();
  236. }
  237. @Override
  238. public JpaPlusRepository<CropLand, Long> r() {
  239. return mCropLandRepository;
  240. }
  241. }