|  | @ -1,6 +1,9 @@
 | 
	
		
			
				|  |  | package com.yihu.jw.healthyhouse.controller.facilities;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.JavaType;
 | 
	
		
			
				|  |  | import com.yihu.jw.healthyhouse.model.facility.Facility;
 | 
	
		
			
				|  |  | import com.yihu.jw.healthyhouse.model.facility.FacilityServerRelation;
 | 
	
		
			
				|  |  | import com.yihu.jw.healthyhouse.service.facility.FacilityServerRelationService;
 | 
	
		
			
				|  |  | import com.yihu.jw.healthyhouse.service.facility.FacilityService;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.*;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
 | 
	
	
		
			
				|  | @ -12,6 +15,8 @@ import io.swagger.annotations.ApiParam;
 | 
	
		
			
				|  |  | import org.apache.commons.lang3.StringUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.http.MediaType;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Propagation;
 | 
	
		
			
				|  |  | import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | import org.springframework.web.bind.annotation.*;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.IOException;
 | 
	
	
		
			
				|  | @ -33,6 +38,8 @@ public class FacilitiesController extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private FacilityService facilityService;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private FacilityServerRelationService facilityServerRelationService;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @ApiOperation(value = "获取设施列表", responseContainer = "List")
 | 
	
		
			
				|  |  |     @GetMapping(value = HealthyHouseMapping.HealthyHouse.Facilities.PAGE)
 | 
	
	
		
			
				|  | @ -47,20 +54,19 @@ public class FacilitiesController extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  |             @RequestParam(value = "size", required = false) Integer size,
 | 
	
		
			
				|  |  |             @ApiParam(name = "page", value = "页码", defaultValue = "1")
 | 
	
		
			
				|  |  |             @RequestParam(value = "page", required = false) Integer page) throws Exception {
 | 
	
		
			
				|  |  |          List<Facility> facilityList = facilityService.search(fields, filters, sorts, page, size);
 | 
	
		
			
				|  |  |         return success(facilityList, (null==facilityList)?0:facilityList.size(), page, size);
 | 
	
		
			
				|  |  |         List<Facility> facilityList = facilityService.search(fields, filters, sorts, page, size);
 | 
	
		
			
				|  |  |         return success(facilityList, (null == facilityList) ? 0 : facilityList.size(), page, size);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @ApiOperation(value = "创建设施")
 | 
	
		
			
				|  |  |     @ApiOperation(value = "创建/更新(id存在)设施,包含设施与服务的关联关系")
 | 
	
		
			
				|  |  |     @Transactional(rollbackFor = Exception.class)
 | 
	
		
			
				|  |  |     @PostMapping(value = HealthyHouseMapping.HealthyHouse.Facilities.CREATE)
 | 
	
		
			
				|  |  |     public ObjEnvelop<Facility> createFacilities(
 | 
	
		
			
				|  |  |             @ApiParam(name = "facility", value = "设施JSON结构")
 | 
	
		
			
				|  |  |             @RequestParam(value = "facility")String facility,
 | 
	
		
			
				|  |  |             @ApiParam(name = "facilityServerStr", value = "服务字符串,使用‘,’隔开")
 | 
	
		
			
				|  |  |             @RequestParam(value = "facilityServerStr")String facilityServerStr) throws IOException {
 | 
	
		
			
				|  |  |         Facility facility1=toEntity(facility,Facility.class);
 | 
	
		
			
				|  |  |         String[] server=facilityServerStr.split(",");
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             @RequestParam(value = "facility") String facility,
 | 
	
		
			
				|  |  |             @ApiParam(name = "facilityServerJson", value = "设施与服务关联关系")
 | 
	
		
			
				|  |  |             @RequestParam(value = "facilityServerJson") String facilityServerJson) throws IOException {
 | 
	
		
			
				|  |  |         Facility facility1 = toEntity(facility, Facility.class);
 | 
	
		
			
				|  |  |         List<Facility> facilityList = null;
 | 
	
		
			
				|  |  |         if (StringUtils.isEmpty(facility1.getCode())) {
 | 
	
		
			
				|  |  |             return failed("设施编码不能为空!", ObjEnvelop.class);
 | 
	
	
		
			
				|  | @ -87,8 +93,16 @@ public class FacilitiesController extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  |         if (StringUtils.isEmpty(facility1.getCategory().toString())) {
 | 
	
		
			
				|  |  |             return failed("设施类别不正确,请参考系统字典:设施类别!", ObjEnvelop.class);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         Facility Facility = facilityService.save(facility1);
 | 
	
		
			
				|  |  |         return success(Facility);
 | 
	
		
			
				|  |  |         Facility facilityBack = facilityService.save(facility1);
 | 
	
		
			
				|  |  |         JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, FacilityServerRelation.class);
 | 
	
		
			
				|  |  |         List<FacilityServerRelation> models = objectMapper.readValue(facilityServerJson, javaType);
 | 
	
		
			
				|  |  |         if (null != models && models.size() > 0) {
 | 
	
		
			
				|  |  |             for (FacilityServerRelation facilityServerRelation : models) {
 | 
	
		
			
				|  |  |                 facilityServerRelationService.save(facilityServerRelation);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         facilityBack.setFacilityServerRelation(models);
 | 
	
		
			
				|  |  |         return success(facilityBack);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @ApiOperation(value = "获取设施")
 | 
	
	
		
			
				|  | @ -97,9 +111,11 @@ public class FacilitiesController extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  |             @ApiParam(name = "id", value = "设施ID", defaultValue = "")
 | 
	
		
			
				|  |  |             @RequestParam(value = "id") String id) throws Exception {
 | 
	
		
			
				|  |  |         Facility facility = facilityService.findById(id);
 | 
	
		
			
				|  |  |         List<FacilityServerRelation> facilityServerRelationList = facilityServerRelationService.findByField("facilitieCode", facility.getCode());
 | 
	
		
			
				|  |  |         if (facility == null) {
 | 
	
		
			
				|  |  |             return failed("设施不存在!", ObjEnvelop.class);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         facility.setFacilityServerRelation(facilityServerRelationList);
 | 
	
		
			
				|  |  |         return success(facility);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
	
		
			
				|  | @ -111,13 +127,17 @@ public class FacilitiesController extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  |             @ApiParam(name = "value", value = "检索值")
 | 
	
		
			
				|  |  |             @RequestParam(value = "value") String value) throws Exception {
 | 
	
		
			
				|  |  |         List<Facility> facilityList = facilityService.findByField(field, value);
 | 
	
		
			
				|  |  |         for (Facility facility : facilityList) {
 | 
	
		
			
				|  |  |             List<FacilityServerRelation> facilityServerRelationList = facilityServerRelationService.findByField("facilitieCode", facility.getCode());
 | 
	
		
			
				|  |  |             facility.setFacilityServerRelation(facilityServerRelationList);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return success(facilityList);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @ApiOperation(value = "更新设施")
 | 
	
		
			
				|  |  |     @ApiOperation(value = "新增/更新(idy已存在)设施")
 | 
	
		
			
				|  |  |     @PutMapping(value = HealthyHouseMapping.HealthyHouse.Facilities.UPDATE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
 | 
	
		
			
				|  |  |     public ObjEnvelop<Facility> updateFacilities(
 | 
	
		
			
				|  |  |             @ApiParam(name = "facility", value = "设施JSON结构")
 | 
	
		
			
				|  |  |             @ApiParam(name = "facility", value = "设施JSON结构(不包括设施与服务关联关系)")
 | 
	
		
			
				|  |  |             @RequestBody Facility facility) throws Exception {
 | 
	
		
			
				|  |  |         if (StringUtils.isEmpty(facility.getCode())) {
 | 
	
		
			
				|  |  |             return failed("设施编码不能为空!", ObjEnvelop.class);
 | 
	
	
		
			
				|  | @ -143,8 +163,8 @@ public class FacilitiesController extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  |     public Envelop deleteFacilities(
 | 
	
		
			
				|  |  |             @ApiParam(name = "facilitiesId", value = "设施ID")
 | 
	
		
			
				|  |  |             @RequestParam(value = "facilitiesId") String facilitiesId) throws Exception {
 | 
	
		
			
				|  |  |         Facility facility = new Facility();
 | 
	
		
			
				|  |  |         facility.setId(facilitiesId);
 | 
	
		
			
				|  |  |         Facility facility = facilityService.findById(facilitiesId);
 | 
	
		
			
				|  |  |         facilityServerRelationService.deleteByFacilitieCode(facility.getCode());
 | 
	
		
			
				|  |  |         facilityService.delete(facility);
 | 
	
		
			
				|  |  |         return success("success");
 | 
	
		
			
				|  |  |     }
 | 
	
	
		
			
				|  | @ -152,24 +172,22 @@ public class FacilitiesController extends EnvelopRestEndpoint {
 | 
	
		
			
				|  |  |     @ApiOperation(value = "设施统计:设施总数/今日新增设施")
 | 
	
		
			
				|  |  |     @DeleteMapping(value = HealthyHouseMapping.HealthyHouse.Facilities.COUNT_FACILITIES)
 | 
	
		
			
				|  |  |     public ObjEnvelop<Long> countFacilities(
 | 
	
		
			
				|  |  |             @ApiParam(name = "totalCountFlag", value = "设施总数:true;今日新增设施:false",defaultValue = "true")
 | 
	
		
			
				|  |  |             @ApiParam(name = "totalCountFlag", value = "设施总数:true;今日新增设施:false", defaultValue = "true")
 | 
	
		
			
				|  |  |             @RequestParam(value = "totalCountFlag") boolean totalCountFlag) throws Exception {
 | 
	
		
			
				|  |  |         String filters="";
 | 
	
		
			
				|  |  |         String filters = "";
 | 
	
		
			
				|  |  |         long count;
 | 
	
		
			
				|  |  |         //设施总数:true
 | 
	
		
			
				|  |  |         if (totalCountFlag) {
 | 
	
		
			
				|  |  |             count= facilityService.getCount(filters);
 | 
	
		
			
				|  |  |             count = facilityService.getCount(filters);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             //今日新增设施:false
 | 
	
		
			
				|  |  |             String todayStart = DateUtil.getStringDateShort()+" "+"00:00:00";
 | 
	
		
			
				|  |  |             String todayEnd = DateUtil.getStringDateShort()+" "+"23:59:59";
 | 
	
		
			
				|  |  |             filters= "createTime>="+todayStart+";createTime<="+todayEnd;
 | 
	
		
			
				|  |  |             count= facilityService.getCount(filters);
 | 
	
		
			
				|  |  |             String todayStart = DateUtil.getStringDateShort() + " " + "00:00:00";
 | 
	
		
			
				|  |  |             String todayEnd = DateUtil.getStringDateShort() + " " + "23:59:59";
 | 
	
		
			
				|  |  |             filters = "createTime>=" + todayStart + ";createTime<=" + todayEnd;
 | 
	
		
			
				|  |  |             count = facilityService.getCount(filters);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return success("success", count);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | }
 |