Browse Source

Merge branch 'dev' of yeshijie/jw2.0 into dev

yeshijie 7 years ago
parent
commit
511ea08c2d
18 changed files with 430 additions and 49 deletions
  1. 62 0
      base/common-data-fastdfs/src/main/java/com/yihu/base/fastdfs/FastDFSHelper.java
  2. 63 5
      common/common-entity/src/main/java/com/yihu/jw/iot/device/IotDeviceDO.java
  3. 28 0
      common/common-entity/src/main/java/com/yihu/jw/iot/device/IotDeviceImportRecordDO.java
  4. 12 0
      common/common-entity/src/main/java/com/yihu/jw/iot/device/IotOrderPurchaseDO.java
  5. 3 1
      common/common-request-mapping/src/main/java/com/yihu/jw/rm/iot/IotRequestMapping.java
  6. 1 1
      common/common-rest-model/src/main/java/com/yihu/jw/restmodel/common/Envelop.java
  7. 4 3
      common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/company/IotCompanyCertificateChangeRecordVO.java
  8. 4 3
      common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/company/IotCompanyCertificateVO.java
  9. 5 5
      common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/device/IotDeviceVO.java
  10. 10 0
      common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/device/IotOrderPurchaseVO.java
  11. 38 0
      svr/svr-iot/src/main/java/com/yihu/iot/controller/common/FileUploadController.java
  12. 1 1
      svr/svr-iot/src/main/java/com/yihu/iot/controller/company/IotCompanyController.java
  13. 14 26
      svr/svr-iot/src/main/java/com/yihu/iot/controller/device/IotDeviceController.java
  14. 1 2
      svr/svr-iot/src/main/java/com/yihu/iot/controller/device/IotDeviceOrderController.java
  15. 1 0
      svr/svr-iot/src/main/java/com/yihu/iot/controller/product/IotProductController.java
  16. 3 0
      svr/svr-iot/src/main/java/com/yihu/iot/service/company/IotCompanyService.java
  17. 155 0
      svr/svr-iot/src/main/java/com/yihu/iot/service/device/IotDeviceImportRecordService.java
  18. 25 2
      svr/svr-iot/src/main/java/com/yihu/iot/service/device/IotDeviceService.java

+ 62 - 0
base/common-data-fastdfs/src/main/java/com/yihu/base/fastdfs/FastDFSHelper.java

@ -116,6 +116,68 @@ public class FastDFSHelper {
        return message;
    }
    /**
     * 以字节的方式上传文件
     * @param fileBuffer
     * @param fileExtension
     * @param description
     * @return
     * @throws Exception
     */
    public ObjectNode uploadByByte(byte[] fileBuffer, String fileExtension,String description) throws Exception {
        NameValuePair[] fileMetaData = new NameValuePair[1];
        fileMetaData[0] = new NameValuePair("description", description == null ? "" : description);
        return uploadByByte(fileBuffer,fileExtension,fileMetaData);
    }
    /**
     * 以字节的方式上传文件
     */
    public ObjectNode uploadByByte(byte[] fileBuffer, String fileExtension,NameValuePair[] fileMetaData) throws Exception {
        StorageClient client = clientPool.getStorageClient();
        ObjectNode message = new ObjectMapper().createObjectNode();
        try {
            TrackerServer trackerServer = clientPool.getTrackerServer();
            String[] results = client.upload_file(fileBuffer, fileExtension, fileMetaData);
            if (results != null) {
                String fileId;
                int ts;
                String token;
                String fileURl;
                InetSocketAddress socketAddress;
                String groupName = results[0];
                String remoteFile = results[1];
                message.put(GroupField, groupName);
                message.put(RemoteFileField, remoteFile);
                fileId = groupName + StorageClient1.SPLIT_GROUP_NAME_AND_FILENAME_SEPERATOR + remoteFile;
                message.put(FileIdField, fileId);
                socketAddress = trackerServer.getInetSocketAddress();
                fileURl = "http://" + socketAddress.getAddress().getHostAddress();
                if (ClientGlobal.g_tracker_http_port != 80) {
                    fileURl += ":" + ClientGlobal.g_tracker_http_port;
                }
                fileURl += "/" + fileId;
                if (ClientGlobal.g_anti_steal_token) {
                    ts = (int) (System.currentTimeMillis() / 1000);
                    token = ProtoCommon.getToken(fileId, ts, ClientGlobal.g_secret_key);
                    fileURl += "?token=" + token + "&ts=" + ts;
                }
                message.put(FileUrlField, fileURl);
                logger.info(client.get_file_info(groupName, remoteFile).toString());
            }
        } finally {
            clientPool.releaseStorageClient(client);
        }
        return message;
    }
    /**

+ 63 - 5
common/common-entity/src/main/java/com/yihu/jw/iot/device/IotDeviceDO.java

@ -59,7 +59,7 @@ public class IotDeviceDO extends IdEntityWithOperation implements Serializable {
    private String hospital;//归属社区
    @Column(name = "hospital_name")
    private String hospital_name;//归属社区名称
    private String hospitalName;//归属社区名称
    @Column(name = "order_id")
    private String orderId;//订单code
@ -82,6 +82,64 @@ public class IotDeviceDO extends IdEntityWithOperation implements Serializable {
    @Column(name = "del")
    private Integer del;//删除标志(1有效,0删除)
    public enum DeviceStatus {
        normal("正常", "1"),
        scrap("报废", "2"),
        overhaul("检修", "3");
        private String name;
        private String value;
        DeviceStatus(String name, String value) {
            this.name = name;
            this.value = value;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
    public enum DeviceSource {
        purchaese("采购订单关联", "1"),
        binding("居民绑定", "2"),
        create("管理员新增", "3");
        private String name;
        private String value;
        DeviceSource(String name, String value) {
            this.name = name;
            this.value = value;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
    public String getSaasId() {
        return saasId;
    }
@ -250,11 +308,11 @@ public class IotDeviceDO extends IdEntityWithOperation implements Serializable {
        this.hospital = hospital;
    }
    public String getHospital_name() {
        return hospital_name;
    public String getHospitalName() {
        return hospitalName;
    }
    public void setHospital_name(String hospital_name) {
        this.hospital_name = hospital_name;
    public void setHospitalName(String hospitalName) {
        this.hospitalName = hospitalName;
    }
}

+ 28 - 0
common/common-entity/src/main/java/com/yihu/jw/iot/device/IotDeviceImportRecordDO.java

@ -32,6 +32,34 @@ public class IotDeviceImportRecordDO extends IdEntityWithOperation implements Se
    @Column(name = "del")
    private Integer del;//删除标志
    public enum DeviceImportRecordStatus {
        create("进行中", "1"),
        complete("已完成", "2");
        private String name;
        private String value;
        DeviceImportRecordStatus(String name, String value) {
            this.name = name;
            this.value = value;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
    public String getSaasId() {
        return saasId;
    }

+ 12 - 0
common/common-entity/src/main/java/com/yihu/jw/iot/device/IotOrderPurchaseDO.java

@ -33,6 +33,9 @@ public class IotOrderPurchaseDO extends IdEntityWithOperation implements Seriali
    @Column(name = "device_name")
    private String deviceName;//采购设备名称
    @Column(name = "product_id")
    private String productId;//产品id
//    @Column(name = "device_model")
//    private String deviceModel;//采购设备型号
//
@ -159,6 +162,15 @@ public class IotOrderPurchaseDO extends IdEntityWithOperation implements Seriali
//        this.nextQualityTime = nextQualityTime;
//    }
    public String getProductId() {
        return productId;
    }
    public void setProductId(String productId) {
        this.productId = productId;
    }
    public String getQualityStatus() {
        return qualityStatus;
    }

+ 3 - 1
common/common-request-mapping/src/main/java/com/yihu/jw/rm/iot/IotRequestMapping.java

@ -45,6 +45,7 @@ public class IotRequestMapping {
    public static class FileUpload{
        public static final String api_upload_stream = "uploadStream";//文件流上传
        public static final String api_upload_stream_img = "uploadImg";//文件流上传图片
        public static final String api_upload_stream_attachment = "uploadattAchment";//文件流上传附件
        public static final String api_upload_string = "uploadString";//base64字符串上传
        public static final String message_success_upload = "上传成功";
@ -186,7 +187,7 @@ public class IotRequestMapping {
     * 设备模块常量
     */
    public static class Device{
        public static final String api_create = "device";
        public static final String api_create = "createDevice";
        public static final String api_delete = "device";
        public static final String api_getById = "getDeviceById";
        public static final String api_update = "device";
@ -222,6 +223,7 @@ public class IotRequestMapping {
        public static final String findById = "findById";
        public static final String delOrder = "delOrder";
        public static final String updOrder = "updOrder";
        public static final String createOrder = "createOrder";
        public static final String findPurcharsePage = "findPurcharsePage";

+ 1 - 1
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/common/Envelop.java

@ -22,7 +22,7 @@ import java.util.List;
 *
 * @author llh
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonInclude(JsonInclude.Include.ALWAYS)
@ApiModel(value = "ListResult", description = "通用的实体")
public class Envelop<T> extends BaseEnvelop implements Serializable {

+ 4 - 3
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/company/IotCompanyCertificateChangeRecordVO.java

@ -1,6 +1,7 @@
package com.yihu.jw.restmodel.iot.company;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.yihu.jw.restmodel.iot.common.BaseVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ -10,9 +11,9 @@ import java.io.Serializable;
 * 企业三证变更记录表
 * @author yeshijie on 2018/1/16.
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel(value = "ListResult", description = "企业三证变更记录")
public class IotCompanyCertificateChangeRecordVO implements Serializable {
@JsonInclude(JsonInclude.Include.ALWAYS)
@ApiModel(description = "企业三证变更记录")
public class IotCompanyCertificateChangeRecordVO extends BaseVO implements Serializable {
    @ApiModelProperty("企业名称")
    private String companyName;

+ 4 - 3
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/company/IotCompanyCertificateVO.java

@ -2,6 +2,7 @@ package com.yihu.jw.restmodel.iot.company;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.yihu.jw.restmodel.iot.common.BaseVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ -12,9 +13,9 @@ import java.util.Date;
 * 企业证书表
 * @author yeshijie on 2018/1/16.
 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@ApiModel(value = "ListResult", description = "企业证书表")
public class IotCompanyCertificateVO implements Serializable {
@JsonInclude(JsonInclude.Include.ALWAYS)
@ApiModel(description = "企业证书表")
public class IotCompanyCertificateVO extends BaseVO implements Serializable {
    @ApiModelProperty("证书名称")
    private String name;

+ 5 - 5
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/device/IotDeviceVO.java

@ -40,7 +40,7 @@ public class IotDeviceVO extends BaseVO implements Serializable {
    @ApiModelProperty("归属社区")
    private String hospital;
    @ApiModelProperty("归属社区名称")
    private String hospital_name;
    private String hospitalName;
    @ApiModelProperty("订单id")
    private String orderId;
    @ApiModelProperty("订单编号")
@ -191,11 +191,11 @@ public class IotDeviceVO extends BaseVO implements Serializable {
        this.hospital = hospital;
    }
    public String getHospital_name() {
        return hospital_name;
    public String getHospitalName() {
        return hospitalName;
    }
    public void setHospital_name(String hospital_name) {
        this.hospital_name = hospital_name;
    public void setHospitalName(String hospitalName) {
        this.hospitalName = hospitalName;
    }
}

+ 10 - 0
common/common-rest-model/src/main/java/com/yihu/jw/restmodel/iot/device/IotOrderPurchaseVO.java

@ -27,6 +27,8 @@ public class IotOrderPurchaseVO extends BaseVO implements Serializable{
    private String supplierName;
    @ApiModelProperty("采购设备名称")
    private String deviceName;
    @ApiModelProperty("产品id")
    private String productId;
    @ApiModelProperty("厂商id")
    private String manufacturerId;
    @ApiModelProperty("厂商名称")
@ -158,4 +160,12 @@ public class IotOrderPurchaseVO extends BaseVO implements Serializable{
    public void setUnAssociatedNum(Long unAssociatedNum) {
        this.unAssociatedNum = unAssociatedNum;
    }
    public String getProductId() {
        return productId;
    }
    public void setProductId(String productId) {
        this.productId = productId;
    }
}

+ 38 - 0
svr/svr-iot/src/main/java/com/yihu/iot/controller/common/FileUploadController.java

@ -74,6 +74,44 @@ public class FileUploadController extends EnvelopRestController{
    }
    @PostMapping(value = IotRequestMapping.FileUpload.api_upload_stream_attachment)
    @ApiOperation(value = "文件流上传附件", notes = "文件流上传附件")
    public Envelop<UploadVO> uploadAttachment(@ApiParam(value = "文件", required = true)
                                       @RequestParam(value = "file", required = true) MultipartFile file){
        try {
            // 得到文件的完整名称  xxx.txt
            String fullName = file.getOriginalFilename();
            if(StringUtils.isBlank(fullName)){
                return Envelop.getError(IotRequestMapping.FileUpload.message_fail_upload_format,IotRequestMapping.api_iot_fail);
            }
            //得到文件类型
            String fileType = fullName.substring(fullName.lastIndexOf(".") + 1).toLowerCase();
            if(StringUtils.isBlank(fileType)||!"doc、docx、pdf、xls、xlsx、jpg、jpeg、png".contains(fileType)){
                return Envelop.getError(IotRequestMapping.FileUpload.message_fail_upload_format,IotRequestMapping.api_iot_fail);
            }
            long size = file.getSize();
            long max = 5*1024*1024;
            if(size>max){
                return Envelop.getError("文件大小不超过5M",IotRequestMapping.api_iot_fail);
            }
            String fileName = fullName.substring(0, fullName.lastIndexOf("."));
            //上传到fastdfs
            ObjectNode objectNode = fastDFSHelper.upload(file.getInputStream(), fileType, "");
            //解析返回的objectNode
            UploadVO uploadVO = new UploadVO();
            uploadVO.setFileName(fileName);
            uploadVO.setFileType(fileType);
            uploadVO.setFullUri(objectNode.get("fid").toString().replaceAll("\"", ""));
            uploadVO.setFullUrl(fastdfs_file_url + objectNode.get("fid").toString().replaceAll("\"", ""));
            return Envelop.getSuccess(IotRequestMapping.Common.message_success_create, uploadVO);
        }catch (Exception e){
            e.printStackTrace();
            return Envelop.getError(IotRequestMapping.FileUpload.message_fail_upload, IotRequestMapping.api_iot_fail);
        }
    }
    @PostMapping(value = IotRequestMapping.FileUpload.api_upload_stream)
    @ApiOperation(value = "文件流上传文件", notes = "文件流上传文件")
    public Envelop<UploadVO> uploadStream(@ApiParam(value = "文件", required = true) @RequestParam(value = "file", required = true) MultipartFile file) {

+ 1 - 1
svr/svr-iot/src/main/java/com/yihu/iot/controller/company/IotCompanyController.java

@ -184,7 +184,7 @@ public class IotCompanyController extends EnvelopRestController {
    @PostMapping(value = IotRequestMapping.Company.addCompanyCert)
    @ApiOperation(value = "创建企业证书", notes = "创建企业证书")
    public Envelop<IotCompanyVO> addCompanyCert(@ApiParam(name = "jsonData", value = "json", defaultValue = "")
    public Envelop<IotCompanyCertificateVO> addCompanyCert(@ApiParam(name = "jsonData", value = "json", defaultValue = "")
                                            @RequestParam(value = "jsonData", required = false)String jsonData) {
        try {
            IotCompanyCertificateDO iotCompanyCertificate = toEntity(jsonData, IotCompanyCertificateDO.class);

+ 14 - 26
svr/svr-iot/src/main/java/com/yihu/iot/controller/device/IotDeviceController.java

@ -1,5 +1,6 @@
package com.yihu.iot.controller.device;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.base.fastdfs.FastDFSHelper;
import com.yihu.iot.dao.device.IotDeviceImportRecordDao;
import com.yihu.iot.service.device.IotDeviceService;
@ -8,7 +9,6 @@ import com.yihu.jw.iot.device.IotDeviceImportRecordDO;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.common.EnvelopRestController;
import com.yihu.jw.restmodel.iot.common.ExistVO;
import com.yihu.jw.restmodel.iot.common.UploadVO;
import com.yihu.jw.restmodel.iot.device.IotDeviceImportRecordVO;
import com.yihu.jw.restmodel.iot.device.IotDeviceVO;
import com.yihu.jw.rm.iot.IotRequestMapping;
@ -16,7 +16,6 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
@ -29,7 +28,6 @@ import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
@ -179,7 +177,7 @@ public class IotDeviceController extends EnvelopRestController{
    @PostMapping(value = IotRequestMapping.Device.importDevice)
    @ApiOperation(value = "设备导入", notes = "设备导入")
    public Envelop<UploadVO> uploadStream(@ApiParam(value = "文件", required = true)
    public Envelop<IotDeviceImportRecordVO> uploadStream(@ApiParam(value = "文件", required = true)
                                          @RequestParam(value = "file", required = true) MultipartFile file,
                                          @ApiParam(name = "purcharseId", value = "采购id", defaultValue = "")
                                          @RequestParam(value = "purcharseId", required = true) String purcharseId) {
@ -197,36 +195,26 @@ public class IotDeviceController extends EnvelopRestController{
            }
            String fileName = fullName.substring(0, fullName.lastIndexOf("."));
            HSSFWorkbook wb = null;
            IotDeviceImportRecordVO vo = null;
            try {
                wb = new HSSFWorkbook(file.getInputStream());
                // logger.debug(wb.getNumberOfSheets());
                HSSFSheet sheet = wb.getSheetAt(0);
                logger.debug("sheet name = "+wb.getSheetName(0));
                for(int i = sheet.getFirstRowNum();i<=sheet.getLastRowNum();i++){
                    HSSFRow row = sheet.getRow(i);
                    Iterator cells = row.cellIterator();
                    while(cells.hasNext()){
                        HSSFCell cell = (HSSFCell) cells.next();
                        logger.debug(cell.getStringCellValue());
                    }
                if(sheet==null||sheet.getLastRowNum()<2){
                    return Envelop.getError("文件内容不正确");
                }
                logger.debug("last row = "+sheet.getLastRowNum());
//                return wb.getNumberOfSheets();
//            return sheet.getFirstRowNum();
                HSSFRow row = sheet.getRow(0);
                if(row.getLastCellNum()<3){
                    return Envelop.getError("文件内容不正确");
                }
                //上传到fastdfs
                ObjectNode objectNode = fastDFSHelper.upload(file.getInputStream(), fileType, "");
                vo = iotDeviceService.importDevice(objectNode.get("fid").toString().replaceAll("\"", ""),fileName,purcharseId,wb);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error(e.getMessage());
                return Envelop.getError(IotRequestMapping.FileUpload.message_fail_upload, IotRequestMapping.api_iot_fail);
            }
            //上传到fastdfs
//            ObjectNode objectNode = fastDFSHelper.upload(file.getInputStream(), fileType, "");
            //解析返回的objectNode
            UploadVO uploadVO = new UploadVO();
            uploadVO.setFileName(fileName);
            uploadVO.setFileType(fileType);
//            uploadVO.setFullUri(objectNode.get("fid").toString().replaceAll("\"", ""));
//            uploadVO.setFullUrl(fastdfs_file_url + objectNode.get("fid").toString().replaceAll("\"", ""));
            return Envelop.getSuccess(IotRequestMapping.Common.message_success_create, uploadVO);
            return Envelop.getSuccess(IotRequestMapping.Common.message_success_create,vo);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelop.getError(IotRequestMapping.FileUpload.message_fail_upload, IotRequestMapping.api_iot_fail);

+ 1 - 2
svr/svr-iot/src/main/java/com/yihu/iot/controller/device/IotDeviceOrderController.java

@ -2,7 +2,6 @@ package com.yihu.iot.controller.device;
import com.yihu.iot.service.company.IotCompanyService;
import com.yihu.iot.service.device.IotDeviceOrderService;
import com.yihu.jw.exception.ApiException;
import com.yihu.jw.iot.company.IotCompanyTypeDO;
import com.yihu.jw.iot.device.IotDeviceOrderDO;
import com.yihu.jw.restmodel.common.Envelop;
@ -35,7 +34,7 @@ public class IotDeviceOrderController extends EnvelopRestController{
    @Autowired
    private IotCompanyService iotCompanyService;
    @PostMapping(value = IotRequestMapping.DeviceOrder.api_create, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @PostMapping(value = IotRequestMapping.DeviceOrder.createOrder, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建设备订单", notes = "创建设备订单")
    public Envelop<IotOrderVO> create(@ApiParam(name = "json_data", value = "", defaultValue = "")
                          @RequestBody String jsonData) {

+ 1 - 0
svr/svr-iot/src/main/java/com/yihu/iot/controller/product/IotProductController.java

@ -136,6 +136,7 @@ public class IotProductController extends EnvelopRestController {
            IotProductVO vo = iotProductBaseInfoService.findProductById(id);
            return Envelop.getSuccess(IotRequestMapping.Common.message_success_find, vo);
        } catch (Exception e) {
            e.printStackTrace();
            return Envelop.getError(e.getMessage());
        }
    }

+ 3 - 0
svr/svr-iot/src/main/java/com/yihu/iot/service/company/IotCompanyService.java

@ -201,6 +201,9 @@ public class IotCompanyService extends BaseJpaService<IotCompanyDO,IotCompanyDao
     * @param company
     */
    public void findType(IotCompanyDO company){
        if(company==null){
            return;
        }
        //查找类型
        List<IotCompanyTypeDO> companyTypes = iotCompanyTypeDao.findByCompanyId(company.getId());
        List<IotCompanyTypeDO> list = new ArrayList<>(8);

+ 155 - 0
svr/svr-iot/src/main/java/com/yihu/iot/service/device/IotDeviceImportRecordService.java

@ -0,0 +1,155 @@
package com.yihu.iot.service.device;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.base.fastdfs.FastDFSHelper;
import com.yihu.base.mysql.query.BaseJpaService;
import com.yihu.iot.dao.device.IotDeviceDao;
import com.yihu.iot.dao.device.IotDeviceImportRecordDao;
import com.yihu.jw.iot.device.IotDeviceDO;
import com.yihu.jw.iot.device.IotDeviceImportRecordDO;
import com.yihu.jw.iot.device.IotOrderPurchaseDO;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
 * @author yeshijie on 2018/1/19.
 */
@Service
public class IotDeviceImportRecordService extends BaseJpaService<IotDeviceImportRecordDO,IotDeviceImportRecordDao> {
    private Logger logger = LoggerFactory.getLogger(IotDeviceImportRecordService.class);
    @Autowired
    private IotDeviceImportRecordDao iotDeviceImportRecordDao;
    @Autowired
    private IotDeviceDao iotDeviceDao;
    @Autowired
    private FastDFSHelper fastDFSHelper;
    @Async
    public void importDevice(IotOrderPurchaseDO purchaseDO, HSSFWorkbook wb, IotDeviceImportRecordDO recordDO){
        List<IotDeviceDO> deviceDOList = new ArrayList<IotDeviceDO>();
        Integer associatedNum = iotDeviceDao.countByPurchaseId(purchaseDO.getId());
        Long unAssociatedNum = purchaseDO.getPurchaseNum()-associatedNum;//未关联数量
        Integer count = 0;
        HSSFSheet sheet = wb.getSheetAt(0);
        for(int i = sheet.getFirstRowNum();i<=sheet.getLastRowNum();i++){
            HSSFRow row = sheet.getRow(i);
            try{
                if(i==sheet.getFirstRowNum()){
                    HSSFCell cell = row.createCell(3);
                    cell.setCellValue("导入结果");
                    continue;
                }
                String errorMsg = null;
                if(unAssociatedNum<=count){
                    errorMsg = "入库SN码数量超过采购量";
                    continue;
                }
                String sn = getStringCellValue(row,0);
                String hos = getStringCellValue(row,1);
                String sim = getStringCellValue(row,2);
                //分割字符串--思明区莲前街道社区卫生服务中心(3502030400)
                String[] ho = hos.split("\\(");
                String hospital = ho.length>1?ho[1].replace("\\)",""):null;
                String hospitalName = ho.length>1?ho[0]:null;
                //验证
                if(StringUtils.isBlank(sn)){
                    errorMsg = "sn码不能为空";
                    HSSFCell cell = row.createCell(3);
                    cell.setCellValue(errorMsg);
                    continue;
                }
                IotDeviceDO device = iotDeviceDao.findByDeviceSn(sn);
//                if(iotDeviceDao.findByDeviceSn(sn)!=null){
//                    errorMsg = "SN码重复,并不允许新增";
//                    HSSFCell cell = row.createCell(3);
//                    cell.setCellValue(errorMsg);
//                    continue;
//                }
                if(StringUtils.isNotBlank(sim)&&iotDeviceDao.findByDeviceSn(sim)!=null){
                    errorMsg = "SIM卡号重复,并不允许新增";
                    HSSFCell cell = row.createCell(3);
                    cell.setCellValue(errorMsg);
                    continue;
                }
                if(device==null){
                    device = new IotDeviceDO();
                }
                device = new IotDeviceDO();
                device.setDel(1);
                device.setOrderNo(purchaseDO.getOrderNo());
                device.setProductId(purchaseDO.getProductId());
                device.setSaasId(getCode());
                device.setStatus(IotDeviceDO.DeviceStatus.normal.getValue());
                device.setDeviceSn(sn);
                device.setDeviceSource(IotDeviceDO.DeviceSource.purchaese.getValue());
                device.setHospital(hospital);
                device.setHospitalName(hospitalName);
//                device.setIsComposite(1);
//                device.setIsPlatform(0);
                device.setManufacturerId(purchaseDO.getManufacturerId());
                device.setManufacturerName(purchaseDO.getManufacturerName());
                device.setName(purchaseDO.getDeviceName());
                device.setSimNo(sim);
                device.setSupplierId(purchaseDO.getSupplierId());
                device.setSupplierName(purchaseDO.getSupplierName());
                deviceDOList.add(device);
                HSSFCell cell = row.createCell(3);
                cell.setCellValue("新增成功");
            }catch (Exception e){
                e.printStackTrace();
                HSSFCell cell = row.createCell(3);
                cell.setCellValue("新增失败:"+e.getMessage());
            }
        }
        //保存结果
        iotDeviceDao.save(deviceDOList);
        recordDO.setStatus(IotDeviceImportRecordDO.DeviceImportRecordStatus.complete.getValue());
        ByteArrayOutputStream os = null;
        try{
            //结果上传到fastDFS
            os = new ByteArrayOutputStream();
            wb.write(os);
            ObjectNode node = fastDFSHelper.uploadByByte(os.toByteArray(),"xls","");
            recordDO.setResultUrl(node.get("fid").toString().replaceAll("\"", ""));
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //关闭流
            try {
                os.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        iotDeviceImportRecordDao.save(recordDO);
    }
    /**
     * 获取cell的值
     * @param row
     * @param num
     * @return
     */
    private String getStringCellValue(HSSFRow row,Integer num){
        HSSFCell cel = row.createCell(num);
        if(cel == null){
            return null;
        }
        return cel.getStringCellValue();
    }
}

+ 25 - 2
svr/svr-iot/src/main/java/com/yihu/iot/service/device/IotDeviceService.java

@ -4,12 +4,15 @@ import com.yihu.base.fastdfs.FastDFSHelper;
import com.yihu.base.mysql.query.BaseJpaService;
import com.yihu.iot.dao.device.IotDeviceDao;
import com.yihu.iot.dao.device.IotDeviceImportRecordDao;
import com.yihu.iot.dao.device.IotOrderPurchaseDao;
import com.yihu.jw.iot.device.IotDeviceDO;
import com.yihu.jw.iot.device.IotDeviceImportRecordDO;
import com.yihu.jw.iot.device.IotOrderPurchaseDO;
import com.yihu.jw.restmodel.common.Envelop;
import com.yihu.jw.restmodel.iot.device.IotDeviceImportRecordVO;
import com.yihu.jw.rm.iot.IotRequestMapping;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
@ -37,6 +40,10 @@ public class IotDeviceService extends BaseJpaService<IotDeviceDO,IotDeviceDao> {
    private IotDeviceImportRecordDao iotDeviceImportRecordDao;
    @Autowired
    private JdbcTemplate jdbcTempalte;
    @Autowired
    private IotOrderPurchaseDao iotOrderPurchaseDao;
    @Autowired
    private IotDeviceImportRecordService importRecordService;
    /**
     * 新增
@ -98,8 +105,24 @@ public class IotDeviceService extends BaseJpaService<IotDeviceDO,IotDeviceDao> {
    /**
     * 设备导入
     */
    public void importDevice(){
    public IotDeviceImportRecordVO importDevice(String fileUrl,String fileName,String purcharseId,HSSFWorkbook wb){
        IotDeviceImportRecordDO recordDO = new IotDeviceImportRecordDO();
        IotOrderPurchaseDO purchaseDO = iotOrderPurchaseDao.findById(purcharseId);
        recordDO.setDel(1);
        recordDO.setOrderId(purchaseDO.getOrderId());
        recordDO.setPurchaseId(purcharseId);
        recordDO.setSaasId(getCode());
        recordDO.setFileName(fileName);
        recordDO.setFileUrl(fileUrl);
        recordDO.setStatus(IotDeviceImportRecordDO.DeviceImportRecordStatus.create.getValue());
        iotDeviceImportRecordDao.save(recordDO);
        IotDeviceImportRecordVO vo = convertToModel(recordDO,IotDeviceImportRecordVO.class);
        //批量导入 异步操作
        importRecordService.importDevice(purchaseDO,wb,recordDO);
        return vo;
    };
    /**