Browse Source

站点绑区域

jz.kai 3 năm trước cách đây
mục cha
commit
61953af3b4

+ 1 - 0
common/src/main/java/com/jpsoft/excellent/modules/base/dao/AreaDAO.java

@@ -16,4 +16,5 @@ public interface AreaDAO {
 	List<Area> list();
 	List<Area> search(Map<String,Object> searchParams,List<Sort> sortList);
 	List<Area> getListByParentId(String parentId);
+	Area getByName(String name);
 }

+ 1 - 0
common/src/main/java/com/jpsoft/excellent/modules/base/service/AreaService.java

@@ -15,4 +15,5 @@ public interface AreaService {
 	List<Area> list();
 	Page<Area> pageSearch(Map<String, Object> searchParams,int pageNum,int pageSize,boolean count,List<Sort> sortList);
 	List<Area> getListByParentId(String parentId);
+	Area getByName(String name);
 }

+ 6 - 0
common/src/main/java/com/jpsoft/excellent/modules/base/service/impl/AreaServiceImpl.java

@@ -73,4 +73,10 @@ public class AreaServiceImpl implements AreaService {
 		// TODO Auto-generated method stub
 		return areaDAO.getListByParentId(parentId);
 	}
+
+	@Override
+	public Area getByName(String name) {
+		// TODO Auto-generated method stub
+		return areaDAO.getByName(name);
+	}
 }

+ 6 - 0
common/src/main/resources/mapper/base/Area.xml

@@ -98,4 +98,10 @@ id_,parent_id,name_,del_flag,create_time,create_by,update_time,update_by		from b
 		and parent_id = #{parentId}
 		ORDER BY name_ ASC
 	</select>
+	<select id="getByName" parameterType="string" resultMap="AreaMap">
+		select * from base_area
+		where del_flag=0
+		and name_=#{0}
+		limit 1
+	</select>
 </mapper>

+ 6 - 1
common/src/main/resources/mapper/base/WorkStation.xml

@@ -73,7 +73,7 @@ id_,area_id,station_name,del_flag,create_time,create_by,update_time,update_by		f
 		select count(*) from base_work_station where id_=#{0}
 	</select>
 	<select id="list" resultMap="WorkStationMap">
-		select * from base_work_station where del_flag=0
+		select * from base_work_station where del_flag=0 order by area_id desc
 	</select>
 	<select id="search" parameterType="hashmap" resultMap="WorkStationMap">
 		<![CDATA[
@@ -84,6 +84,11 @@ id_,area_id,station_name,del_flag,create_time,create_by,update_time,update_by		f
 			<if test="searchParams.areaId != null">
 				and area_id like #{searchParams.areaId}
 			</if>
+			<if test="searchParams.areaIds != null">
+				<foreach collection="searchParams.areaIds" item="areaId" open="and area_id in (" separator="," close=")">
+					#{areaId}
+				</foreach>
+			</if>
 			<if test="searchParams.stationName != null">
 				and station_name like #{searchParams.stationName}
 			</if>

+ 1 - 1
web/src/main/java/com/jpsoft/excellent/modules/base/controller/AreaController.java

@@ -248,7 +248,7 @@ public class AreaController {
             searchParams.put("name", "%" + keywords + "%");
         }
 
-        Page<Area> page = areaService.pageSearch(searchParams,1,1000,false,sortList);
+        Page<Area> page = areaService.pageSearch(searchParams,1,10000,false,sortList);
         List<Area> areaList = page.getResult();
         msgResult.setResult(true);
         msgResult.setData(areaList);

+ 40 - 6
web/src/main/java/com/jpsoft/excellent/modules/base/controller/WorkStationController.java

@@ -2,7 +2,9 @@ package com.jpsoft.excellent.modules.base.controller;
 
 import com.github.pagehelper.Page;
 import com.jpsoft.excellent.config.OSSConfig;
+import com.jpsoft.excellent.modules.base.entity.Area;
 import com.jpsoft.excellent.modules.base.entity.WorkWindow;
+import com.jpsoft.excellent.modules.base.service.AreaService;
 import com.jpsoft.excellent.modules.base.service.WorkWindowService;
 import com.jpsoft.excellent.modules.common.utils.OSSUtil;
 import com.jpsoft.excellent.modules.common.utils.POIUtils;
@@ -41,6 +43,8 @@ public class WorkStationController {
     @Autowired
     private OSSConfig ossConfig;
     @Autowired
+    private AreaService areaService;
+    @Autowired
     private WorkStationService workStationService;
     @Autowired
     private WorkWindowService workWindowService;
@@ -220,25 +224,28 @@ public class WorkStationController {
             @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
             @RequestParam(value="pageSize",defaultValue="20") int pageSize,
             @RequestAttribute String subject){
-
-        //当前用户ID
-        System.out.println(subject);
-
         MessageResult<Map> msgResult = new MessageResult<>();
 
         Map<String,Object> searchParams = new HashMap<>();
 
         List<Sort> sortList = new ArrayList<>();
-        sortList.add(new Sort("id_","asc"));
+        sortList.add(new Sort("area_id","desc"));
 
         if (StringUtils.isNotEmpty(areaId)) {
-            searchParams.put("areaId","%"+areaId+"%");
+            List<String> areaIds = new ArrayList<>();
+            areaIds.add(areaId);
+            areaIds.addAll(parentIds(areaId));
+
+            searchParams.put("areaIds",areaIds);
         }
         if (StringUtils.isNotEmpty(stationName)) {
             searchParams.put("stationName","%"+stationName+"%");
         }
 
         Page<WorkStation> page = workStationService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);
+        for(WorkStation workStation : page.getResult()){
+            workStation.setAreaName(parentFullName(workStation.getAreaId()));
+        }
 
         msgResult.setResult(true);
         msgResult.setData(PojoUtils.pageWrapper(page));
@@ -366,4 +373,31 @@ public class WorkStationController {
 
         return msgResult;
     }
+
+    private String parentFullName(String parentId){
+        String fullName = "";
+
+        Area area = areaService.get(parentId);
+        if(StringUtils.isNotEmpty(area.getParentId())){
+            fullName = parentFullName(area.getParentId()) + "-" + area.getName();
+        }
+        else {
+            fullName = area.getName();
+        }
+
+        return fullName;
+    }
+
+    private List<String> parentIds(String parentId){
+        List<String> ids = new ArrayList<>();
+
+        List<Area> areaList = areaService.getListByParentId(parentId);
+        for(Area area : areaList){
+            ids.add(area.getId());
+
+            ids.addAll(parentIds(area.getId()));
+        }
+
+        return ids;
+    }
 }

+ 189 - 6
web/src/main/java/com/jpsoft/excellent/modules/textController.java

@@ -1,31 +1,46 @@
 package com.jpsoft.excellent.modules;
 
-import com.jpsoft.excellent.modules.base.entity.Incident;
-import com.jpsoft.excellent.modules.base.entity.IncidentStep;
-import com.jpsoft.excellent.modules.base.entity.Person;
-import com.jpsoft.excellent.modules.base.service.IncidentService;
-import com.jpsoft.excellent.modules.base.service.IncidentStepService;
-import com.jpsoft.excellent.modules.base.service.PersonService;
+import com.jpsoft.excellent.config.OSSConfig;
+import com.jpsoft.excellent.modules.base.entity.*;
+import com.jpsoft.excellent.modules.base.service.*;
 import com.jpsoft.excellent.modules.common.dto.MessageResult;
 import com.jpsoft.excellent.modules.common.utils.DES3;
+import com.jpsoft.excellent.modules.common.utils.OSSUtil;
+import com.jpsoft.excellent.modules.common.utils.POIUtils;
 import com.jpsoft.excellent.modules.common.utils.SMSUtil;
+import com.jpsoft.excellent.modules.sys.entity.DataDictionary;
 import com.jpsoft.excellent.modules.sys.entity.User;
 import com.jpsoft.excellent.modules.sys.service.UserService;
 import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiImplicitParam;
+import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.web.bind.annotation.*;
+import org.springframework.web.multipart.MultipartFile;
 
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.text.SimpleDateFormat;
 import java.util.*;
 
 @RestController
 @RequestMapping("/text")
 @Api(description = "测试")
 public class textController {
+    private Logger logger = LoggerFactory.getLogger(getClass());
+
     @Value("${jwt.secret}")
     private String jwtSecret;
     @Autowired
+    private OSSConfig ossConfig;
+    @Autowired
     private UserService userService;
     @Autowired
     private PersonService personService;
@@ -33,6 +48,10 @@ public class textController {
     private IncidentService incidentService;
     @Autowired
     private IncidentStepService incidentStepService;
+    @Autowired
+    private AreaService areaService;
+    @Autowired
+    private WorkStationService workStationService;
 
     @ApiOperation(value="短信发送")
     @RequestMapping(value = "sendSMS",method = RequestMethod.POST)
@@ -133,4 +152,168 @@ public class textController {
 
         return msgResult;
     }
+
+    @ApiOperation(value="导入区域")
+    @PostMapping("importArea")
+    public MessageResult<String> importArea(MultipartFile uploadFile, @RequestAttribute String subject){
+        MessageResult<String> msgResult = new MessageResult<>();
+
+        try {
+            POIUtils poiUtils = new POIUtils(uploadFile.getInputStream());
+            Sheet sheet1 = poiUtils.getSheetAt(0);
+
+            int affectCount = 0;
+            int failCount = 0;
+            int validateColIndex = 3;
+
+            for(int rowIndex=1; rowIndex<=sheet1.getLastRowNum(); rowIndex++){
+                try {
+                    String strLv1 = poiUtils.getCellValue(0,rowIndex,0).toString();
+                    String strLv2 = poiUtils.getCellValue(0,rowIndex,1).toString();
+                    String strLv3 = poiUtils.getCellValue(0,rowIndex,2).toString();
+
+                    Area area1 = areaService.getByName(strLv1);
+                    if(area1 == null){
+                        area1 = new Area();
+                        area1.setId(UUID.randomUUID().toString());
+                        area1.setDelFlag(false);
+                        area1.setCreateBy(subject);
+                        area1.setCreateTime(new Date());
+                        area1.setParentId(null);
+                        area1.setName(strLv1);
+                        areaService.insert(area1);
+                        affectCount++;
+                    }
+
+                    Area area2 = areaService.getByName(strLv2);
+                    if(area2 == null){
+                        area2 = new Area();
+                        area2.setId(UUID.randomUUID().toString());
+                        area2.setDelFlag(false);
+                        area2.setCreateBy(subject);
+                        area2.setCreateTime(new Date());
+                        area2.setParentId(area1.getId());
+                        area2.setName(strLv2);
+                        areaService.insert(area2);
+                        affectCount++;
+                    }
+
+                    Area area3 = areaService.getByName(strLv3);
+                    if(area3 == null){
+                        area3 = new Area();
+                        area3.setId(UUID.randomUUID().toString());
+                        area3.setDelFlag(false);
+                        area3.setCreateBy(subject);
+                        area3.setCreateTime(new Date());
+                        area3.setParentId(area2.getId());
+                        area3.setName(strLv3);
+                        areaService.insert(area3);
+                        affectCount++;
+                    }
+                }
+                catch(Exception innerEx){
+                    failCount++;
+                }
+            }
+
+            if (failCount>0){
+                //有导入失败的记录
+                msgResult.setResult(false);
+                msgResult.setMessage("数据成功导入" + affectCount + "条,有" + failCount + "条数据未导入成功,错误原因请查看报表。");
+
+                //todo 只保留错误数据的sheet
+                Workbook wb = poiUtils.exportErrorXls(0,validateColIndex,1 + affectCount + failCount);
+
+                //todo 将wb保存到oss
+                ByteArrayOutputStream output = new ByteArrayOutputStream();
+                wb.write(output);
+
+                byte[] buffer = output.toByteArray();
+                ByteArrayInputStream input = new ByteArrayInputStream(buffer);
+
+                //格式化holidayInfo
+                SimpleDateFormat sim = new SimpleDateFormat("yyyyMMddHHmmss");
+                String fileName = "error" + sim.format(new Date()) + ".xls";
+                String downloadUrl = OSSUtil.upload(ossConfig,"excellent",fileName,input);
+
+                //todo 返回导入失败报表下载链接
+                msgResult.setData(downloadUrl);
+            }
+            else{
+                msgResult.setResult(true);
+                msgResult.setMessage("数据成功导入" + affectCount + "条");
+            }
+        }
+        catch(Exception ex){
+            logger.error(ex.getMessage(),ex);
+
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+
+        return msgResult;
+    }
+
+    @ApiOperation(value="更新站点区域")
+    @RequestMapping(value = "updateStation",method = RequestMethod.POST)
+    public MessageResult updateStation(@RequestAttribute String subject){
+        MessageResult msgResult = new MessageResult();
+
+        try {
+            Integer count = 0;
+            List<WorkStation> workStations = workStationService.list();
+            for(WorkStation workStation : workStations){
+                if(workStation.getStationName().indexOf("镇") > 0) {
+                    String stationName = workStation.getStationName().substring(0, workStation.getStationName().indexOf("镇")+1);
+
+                    Area area = areaService.getByName(stationName);
+                    if(area != null) {
+                        workStation.setAreaId(area.getId());
+                        workStationService.update(workStation);
+                    }
+                    else{
+                        area = areaService.getByName(workStation.getAreaId());
+                        if(area != null) {
+                            workStation.setAreaId(area.getId());
+                            workStationService.update(workStation);
+                        }
+                    }
+                }
+                else if(workStation.getStationName().indexOf("乡") > 0) {
+                    String stationName = workStation.getStationName().substring(0, workStation.getStationName().indexOf("乡")+1);
+
+                    Area area = areaService.getByName(stationName);
+                    if(area != null) {
+                        workStation.setAreaId(area.getId());
+                        workStationService.update(workStation);
+                    }
+                    else{
+                        area = areaService.getByName(workStation.getAreaId());
+                        if(area != null) {
+                            workStation.setAreaId(area.getId());
+                            workStationService.update(workStation);
+                        }
+                    }
+                }
+                else{
+                    Area area = areaService.getByName(workStation.getAreaId());
+                    if(area != null) {
+                        workStation.setAreaId(area.getId());
+                        workStationService.update(workStation);
+                    }
+                }
+
+                count++;
+            }
+
+            msgResult.setResult(true);
+            msgResult.setMessage("更新数据"+count+"条。");
+        }
+        catch(Exception ex){
+            msgResult.setResult(false);
+            msgResult.setMessage(ex.getMessage());
+        }
+
+        return msgResult;
+    }
 }