|  | @ -0,0 +1,188 @@
 | 
	
		
			
				|  |  | package com.yihu.iot.service.device;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.core.type.TypeReference;
 | 
	
		
			
				|  |  | import com.fasterxml.jackson.databind.ObjectMapper;
 | 
	
		
			
				|  |  | import com.yihu.iot.dao.device.IotDeviceDao;
 | 
	
		
			
				|  |  | import com.yihu.iot.dao.device.IotDeviceInventoryLogDao;
 | 
	
		
			
				|  |  | import com.yihu.iot.dao.device.IotDeviceOrderDao;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.iot.device.IotDeviceDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.iot.device.IotDeviceInventoryLogDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.entity.iot.device.IotDeviceOrderDO;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.iot.device.IotDeviceInventoryLogVO;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.iot.device.IotDeviceVO;
 | 
	
		
			
				|  |  | import com.yihu.jw.restmodel.web.MixEnvelop;
 | 
	
		
			
				|  |  | import com.yihu.jw.rm.iot.IotRequestMapping;
 | 
	
		
			
				|  |  | import com.yihu.mysql.query.BaseJpaService;
 | 
	
		
			
				|  |  | import org.apache.commons.lang.StringUtils;
 | 
	
		
			
				|  |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.BeanPropertyRowMapper;
 | 
	
		
			
				|  |  | import org.springframework.jdbc.core.JdbcTemplate;
 | 
	
		
			
				|  |  | import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.io.IOException;
 | 
	
		
			
				|  |  | import java.util.ArrayList;
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | //create by hmf on 2020.4.29
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class IotDeviceInventoryService extends BaseJpaService<IotDeviceInventoryLogDO, IotDeviceInventoryLogDao> {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private JdbcTemplate jdbcTempalte;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotDeviceDao iotDeviceDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotDeviceInventoryLogDao deviceInventoryLogDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private IotDeviceOrderDao deviceOrderDao;
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private ObjectMapper objectMapper;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 获取库存列表接口
 | 
	
		
			
				|  |  |      *
 | 
	
		
			
				|  |  |      * @param name
 | 
	
		
			
				|  |  |      * @param categoryCode
 | 
	
		
			
				|  |  |      * @param hospitalName
 | 
	
		
			
				|  |  |      * @param page
 | 
	
		
			
				|  |  |      * @param pageSize
 | 
	
		
			
				|  |  |      * @return
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public MixEnvelop<IotDeviceVO, IotDeviceVO> getDeviceInventoryList(String name, String categoryCode, String hospitalName, Integer page, Integer pageSize) {
 | 
	
		
			
				|  |  |         String sql = "select d.id,d.product_id,d.`name`,d.category_code,d.category_name,d.manufacturer_id,d.manufacturer_name,d.hospital,d.hospital_name,count(id) inventoryCount FROM `iot_device` d where del=1  ";
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(name)) {
 | 
	
		
			
				|  |  |             sql += "and name like '%" + name + "%' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(categoryCode)) {
 | 
	
		
			
				|  |  |             sql += "and category_code = '" + categoryCode + "' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(hospitalName)) {
 | 
	
		
			
				|  |  |             sql += "and hospital_name like '%" + hospitalName + "%' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         sql += "GROUP BY d.product_id, d.manufacturer_id, d.hospital, d.category_code ";
 | 
	
		
			
				|  |  |         //判断是否需要分页
 | 
	
		
			
				|  |  |         if (page != null && pageSize != null) {
 | 
	
		
			
				|  |  |             Long count = Long.valueOf(jdbcTempalte.queryForList(sql).size());
 | 
	
		
			
				|  |  |             sql += "limit " + (page - 1) * pageSize + "," + pageSize;
 | 
	
		
			
				|  |  |             List<IotDeviceVO> deviceVOList = jdbcTempalte.query(sql, new BeanPropertyRowMapper<>(IotDeviceVO.class));
 | 
	
		
			
				|  |  |             return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Common.message_success_find, deviceVOList, page, pageSize, count);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             List<IotDeviceVO> deviceVOList = jdbcTempalte.query(sql, new BeanPropertyRowMapper<>(IotDeviceVO.class));
 | 
	
		
			
				|  |  |             return MixEnvelop.getSuccessList(IotRequestMapping.Common.message_success_find, deviceVOList);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public Integer updateInventory(String jsonData) {
 | 
	
		
			
				|  |  |         List<IotDeviceDO> deviceDOList = new ArrayList<>();
 | 
	
		
			
				|  |  |         JSONArray jsonArray = JSONArray.parseArray(jsonData);
 | 
	
		
			
				|  |  |         for (Object object : jsonArray) {
 | 
	
		
			
				|  |  |             JSONObject jsonObject = (JSONObject) object;
 | 
	
		
			
				|  |  |             String orderId = null;
 | 
	
		
			
				|  |  |             if (jsonObject.containsKey("orderNo") && StringUtils.isNotBlank(jsonObject.getString("orderNo"))) {
 | 
	
		
			
				|  |  |                 String orderNo = jsonObject.getString("orderNo");//订单编号
 | 
	
		
			
				|  |  |                 //根据订单编号获取订单id
 | 
	
		
			
				|  |  |                 IotDeviceOrderDO deviceOrderDO = deviceOrderDao.findByOrderNoAndDel(orderNo, 1);
 | 
	
		
			
				|  |  |                 orderId = deviceOrderDO.getId();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             String deviceList = jsonObject.getString("deviceList");//需进行操作的设备列表
 | 
	
		
			
				|  |  |             IotDeviceInventoryLogDO deviceInventoryLogDO = null;
 | 
	
		
			
				|  |  |             List<IotDeviceDO> deviceDOS = new ArrayList<>();
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 deviceInventoryLogDO = objectMapper.readValue(jsonObject.toString(), IotDeviceInventoryLogDO.class);
 | 
	
		
			
				|  |  |                 deviceDOS = new ObjectMapper().readValue(deviceList, new TypeReference<List<IotDeviceDO>>() {
 | 
	
		
			
				|  |  |                 });
 | 
	
		
			
				|  |  |             } catch (IOException e) {
 | 
	
		
			
				|  |  |                 e.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             //保存出入库日志
 | 
	
		
			
				|  |  |             deviceInventoryLogDO.setSaasId(getCode());
 | 
	
		
			
				|  |  |             deviceInventoryLogDO.setOrderId(orderId);
 | 
	
		
			
				|  |  |             deviceInventoryLogDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |             deviceInventoryLogDO.setUpdateTime(new Date());
 | 
	
		
			
				|  |  | //            deviceInventoryLogDO.setNum(deviceDOS.size());
 | 
	
		
			
				|  |  |             deviceInventoryLogDao.save(deviceInventoryLogDO);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //根据类型处理出入库
 | 
	
		
			
				|  |  |             for (IotDeviceDO deviceDO : deviceDOS) {
 | 
	
		
			
				|  |  |                 String deviceSn = deviceDO.getDeviceSn();
 | 
	
		
			
				|  |  |                 Integer type = deviceInventoryLogDO.getType();//调拨类型(1入库 2设备调拨出库 3核销出库)
 | 
	
		
			
				|  |  |                 //设备出库,更换相关信息
 | 
	
		
			
				|  |  |                 if (type.equals("1")) {
 | 
	
		
			
				|  |  |                     deviceDO.setSaasId(getCode());
 | 
	
		
			
				|  |  |                 } else {
 | 
	
		
			
				|  |  |                     deviceDO = iotDeviceDao.findByDeviceSn(deviceSn);
 | 
	
		
			
				|  |  |                     if (type.equals("3")) {
 | 
	
		
			
				|  |  |                         deviceDO.setDel(0);
 | 
	
		
			
				|  |  |                         deviceDO.setStatus("2");
 | 
	
		
			
				|  |  |                         deviceDO.setInventoryLogId(deviceInventoryLogDO.getId());
 | 
	
		
			
				|  |  |                         deviceDO.setUpdateTime(new Date());
 | 
	
		
			
				|  |  |                         deviceDOList.add(deviceDO);
 | 
	
		
			
				|  |  |                         continue;
 | 
	
		
			
				|  |  |                     }
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 deviceDO.setProductId(jsonObject.getString("productId"));
 | 
	
		
			
				|  |  |                 deviceDO.setName(jsonObject.getString("name"));
 | 
	
		
			
				|  |  |                 deviceDO.setCategoryCode(jsonObject.getString("categoryCode"));
 | 
	
		
			
				|  |  |                 deviceDO.setCategoryName(jsonObject.getString("categoryName"));
 | 
	
		
			
				|  |  |                 deviceDO.setManufacturerId(jsonObject.getString("manufacturerId"));
 | 
	
		
			
				|  |  |                 deviceDO.setManufacturerName(jsonObject.getString("manufacturerName"));
 | 
	
		
			
				|  |  |                 deviceDO.setHospital(jsonObject.getString("hospital"));
 | 
	
		
			
				|  |  |                 deviceDO.setHospitalName(jsonObject.getString("hospitalName"));
 | 
	
		
			
				|  |  |                 if (StringUtils.isNotBlank(orderId)) {
 | 
	
		
			
				|  |  |                     deviceInventoryLogDO.setOrderId(orderId);
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 if (jsonObject.containsKey("orderNo") && StringUtils.isNotBlank(jsonObject.getString("orderNo"))) {
 | 
	
		
			
				|  |  |                     deviceInventoryLogDO.setOrderNo(jsonObject.getString("orderNo"));
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |                 deviceDO.setDel(1);
 | 
	
		
			
				|  |  |                 deviceDO.setStatus("1");
 | 
	
		
			
				|  |  |                 deviceDO.setInventoryLogId(deviceInventoryLogDO.getId());
 | 
	
		
			
				|  |  |                 deviceDO.setCreateTime(new Date());
 | 
	
		
			
				|  |  |                 deviceDO.setUpdateTime(new Date());
 | 
	
		
			
				|  |  |                 deviceDOList.add(deviceDO);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         iotDeviceDao.save(deviceDOList);
 | 
	
		
			
				|  |  |         return deviceDOList.size();
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public MixEnvelop<IotDeviceInventoryLogVO, IotDeviceInventoryLogVO> getDeviceInventoryLog(String name, String categoryCode, Integer type, Integer page, Integer pageSize) {
 | 
	
		
			
				|  |  |         String sql = "SELECT l.*, count(d.id) num FROM `iot_device_inventory_log` l LEFT JOIN iot_device d on l.id = d.inventory_log_id where 1=1 ";
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(name)) {
 | 
	
		
			
				|  |  |             sql += "and l.name like '%" + name + "%' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (StringUtils.isNotBlank(categoryCode)) {
 | 
	
		
			
				|  |  |             sql += "and l.category_code = '" + categoryCode + "' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         if (type != null) {
 | 
	
		
			
				|  |  |             sql += "and l.type = '" + type + "' ";
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         sql += "GROUP BY l.id order BY l.create_time desc ";
 | 
	
		
			
				|  |  |         //判断是否需要分页
 | 
	
		
			
				|  |  |         if (page != null && pageSize != null) {
 | 
	
		
			
				|  |  |             Long count = Long.valueOf(jdbcTempalte.queryForList(sql).size());
 | 
	
		
			
				|  |  |             sql += "limit " + (page - 1) * pageSize + "," + pageSize;
 | 
	
		
			
				|  |  |             List<IotDeviceInventoryLogVO> inventoryLogVOList = jdbcTempalte.query(sql, new BeanPropertyRowMapper<>(IotDeviceInventoryLogVO.class));
 | 
	
		
			
				|  |  |             return MixEnvelop.getSuccessListWithPage(IotRequestMapping.Common.message_success_find, inventoryLogVOList, page, pageSize, count);
 | 
	
		
			
				|  |  |         } else {
 | 
	
		
			
				|  |  |             List<IotDeviceInventoryLogVO> inventoryLogVOList = jdbcTempalte.query(sql, new BeanPropertyRowMapper<>(IotDeviceInventoryLogVO.class));
 | 
	
		
			
				|  |  |             return MixEnvelop.getSuccessList(IotRequestMapping.Common.message_success_find, inventoryLogVOList);
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public IotDeviceInventoryLogVO getDeviceInventoryLogDetail(String id) {
 | 
	
		
			
				|  |  |         IotDeviceInventoryLogDO deviceInventoryLogDO = deviceInventoryLogDao.findOne(id);
 | 
	
		
			
				|  |  |         //获取出入库详情设备列表
 | 
	
		
			
				|  |  |         List<IotDeviceDO> deviceDOS = iotDeviceDao.findListByInventoryLogId(id);
 | 
	
		
			
				|  |  |         IotDeviceInventoryLogVO deviceInventoryLogVO = convertToModel(deviceInventoryLogDO, IotDeviceInventoryLogVO.class);
 | 
	
		
			
				|  |  |         if (deviceDOS != null && deviceDOS.size() > 0) {
 | 
	
		
			
				|  |  |             List<IotDeviceVO> deviceVOS = new ArrayList<>();
 | 
	
		
			
				|  |  |             deviceInventoryLogVO.setDeviceVOList(convertToModels(deviceDOS, deviceVOS, IotDeviceVO.class));
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         deviceInventoryLogVO.setNum(deviceDOS.size());
 | 
	
		
			
				|  |  |         return deviceInventoryLogVO;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |