فهرست منبع

2021/6/21 转栏管理

yinhao 4 سال پیش
والد
کامیت
3ae71c4421

+ 4 - 8
huimv-smart-management/src/main/java/com/huimv/management/Abc.java

@@ -2,6 +2,7 @@ package com.huimv.management;
 
 import com.huimv.common.utils.Constant;
 import com.huimv.management.rsa.JdkSignatureUtil;
+import com.huimv.management.util.GetUserUtil;
 import org.apache.tomcat.util.codec.binary.Base64;
 
 import javax.crypto.Cipher;
@@ -77,14 +78,9 @@ public class Abc {
 //        /*获取加密内容以UTF-8为标准转化的字节进行加密后再使用base64编码成字符串*/
 //        /*加密后的字符串*/
 //        String enmsg=Base64.encodeBase64String(cipher.doFinal(plainText.getBytes("UTF-8")));
-        Date date = new Date(0L);
-        System.out.println(date);
+//        Date date = new Date(0L);
+//        System.out.println(date);
 
-        int i =(int) Double.parseDouble("39.5");
-        System.out.println(i);
-
-        Object o = null;
-        String s = String.valueOf(o);
-        if (s != null) System.out.println(s);
+//        GetUserUtil.getUser(null);
     }
 }

+ 3 - 1
huimv-smart-management/src/main/java/com/huimv/management/HuimvSmartManagementApplication.java

@@ -18,7 +18,9 @@ public class HuimvSmartManagementApplication {
     public static void main(String[] args) {
         SpringApplication.run(HuimvSmartManagementApplication.class, args);
     }
-    @Bean
+
+
+    @Bean("restTemplate")
     public RestTemplate getRestTemplate(){
         return new RestTemplate();
     }

+ 38 - 0
huimv-smart-management/src/main/java/com/huimv/management/SpringContextUtils.java

@@ -0,0 +1,38 @@
+package com.huimv.management;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.stereotype.Component;
+
+@Component
+public class SpringContextUtils implements ApplicationContextAware {
+	public static ApplicationContext applicationContext;
+
+	@Override
+	public void setApplicationContext(ApplicationContext applicationContext)
+			throws BeansException {
+		SpringContextUtils.applicationContext = applicationContext;
+	}
+
+	public static Object getBean(String name) {
+		return applicationContext.getBean(name);
+	}
+
+	public static <T> T getBean(String name, Class<T> requiredType) {
+		return applicationContext.getBean(name, requiredType);
+	}
+
+	public static boolean containsBean(String name) {
+		return applicationContext.containsBean(name);
+	}
+
+	public static boolean isSingleton(String name) {
+		return applicationContext.isSingleton(name);
+	}
+
+	public static Class<? extends Object> getType(String name) {
+		return applicationContext.getType(name);
+	}
+
+}

+ 4 - 0
huimv-smart-management/src/main/java/com/huimv/management/entity/TransferPeriodLogEntity.java

@@ -27,6 +27,10 @@ public class TransferPeriodLogEntity implements Serializable {
 	@TableId
 	private Integer id;
 	/**
+	 * 牧场id
+	 */
+	private Integer farmId;
+	/**
 	 * 耳标
 	 */
 	private String earTag;

+ 36 - 28
huimv-smart-management/src/main/java/com/huimv/management/service/impl/YearPigBaseServiceImpl.java

@@ -11,13 +11,11 @@ import com.huimv.common.utils.R;
 import com.huimv.management.dao.PeriodDao;
 import com.huimv.management.dao.TransferPeriodLogDao;
 import com.huimv.management.dao.UnitDao;
-import com.huimv.management.entity.PeriodEntity;
-import com.huimv.management.entity.SysUserEntity;
-import com.huimv.management.entity.TransferPeriodLogEntity;
-import com.huimv.management.entity.YearPigBaseEntity;
+import com.huimv.management.entity.*;
 import com.huimv.management.entity.vo.CountOut;
 import com.huimv.management.entity.vo.DrinkingWaterVo;
 import com.huimv.management.entity.vo.PigBaseVo;
+import com.huimv.management.util.GetUserUtil;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -288,57 +286,67 @@ public class YearPigBaseServiceImpl extends ServiceImpl<YearPigBaseDao, YearPigB
     @Transactional(rollbackFor = Exception.class)
     public void transferPeriod(HashMap<String, Object> map, HttpServletRequest request) {
 
-        String token = request.getHeader("token");
-
-        HttpHeaders headers = new HttpHeaders();
-        headers.add("token", token);
-        HttpEntity httpEntity = new HttpEntity<>(null, headers);
+//        String token = request.getHeader("token");
+//
+//        HttpHeaders headers = new HttpHeaders();
+//        headers.add("token", token);
+//        HttpEntity httpEntity = new HttpEntity<>(null, headers);
+//
+//        ResponseEntity<R> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, R.class);
+//        R sysUserEntity = responseEntity.getBody();
+//        if (401 == (Integer) (sysUserEntity.get("code"))) {
+//            throw new RRException((String) sysUserEntity.get("msg"), (Integer) sysUserEntity.get("code"));
+//        }
+//        String jsonString = JSONObject.toJSONString(sysUserEntity.get("user"));
+//        SysUserEntity user = JSONObject.parseObject(jsonString, SysUserEntity.class);
 
-        ResponseEntity<R> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, R.class);
-        R sysUserEntity = responseEntity.getBody();
-        if (401 == (Integer) (sysUserEntity.get("code"))) {
-            throw new RRException((String) sysUserEntity.get("msg"), (Integer) sysUserEntity.get("code"));
-        }
-        String jsonString = JSONObject.toJSONString(sysUserEntity.get("user"));
-        SysUserEntity user = JSONObject.parseObject(jsonString, SysUserEntity.class);
-
-        String username = user.getUsername();
-        System.out.println(username);
 
         List<Integer> pigIds = (List<Integer>) map.get("pigIds");
-        Integer periodId = (Integer) map.get("periodId");
+        Integer unitId = (Integer) map.get("unitId");
 
-        if (CollUtil.isEmpty(pigIds) || periodId == null) {
+        if (CollUtil.isEmpty(pigIds) || unitId == null) {
             throw new RRException("参数有误,请检查!",1001);
         }
 
         List<TransferPeriodLogEntity> logEntityList = new ArrayList<>();
         YearPigBaseEntity yearPigBaseEntity = new YearPigBaseEntity();
+        //获取当前操作用户
+        SysUserEntity user = GetUserUtil.getUser(request);
+        String username = user.getUsername();
 
         for (Integer pigId : pigIds) {
             YearPigBaseEntity oldEntity = baseMapper.selectById(pigId);
-
+            if (oldEntity == null) {
+                throw new RRException("查不到id为" + pigId + "的猪的信息,请检查参数!",1001);
+            }
+            if (oldEntity.getUnitId().equals(unitId)) {
+                throw new RRException("所转单元不能和原单元相同!",1001);
+            }
             Integer oldPeriodId = oldEntity.getPeriodId();
             String oldPeriodName = periodDao.selectById(oldPeriodId).getNumber();
             Integer oldUnitId = oldEntity.getUnitId();
             String oldUnitName = unitDao.selectById(oldUnitId).getNumber();
 
-            PeriodEntity newEntity = periodDao.selectById(periodId);
-            Integer newUnitId = newEntity.getUnitId();
-            String newUnitName = unitDao.selectById(newUnitId).getNumber();
+            UnitEntity unitEntity = unitDao.selectById(unitId);
+            if (unitEntity == null) {
+                throw new RRException("查不到id为" + unitId + "的单元的信息,请检查参数!",1001);
+            }
+            String newUnitName = unitEntity.getNumber();
+            String newPeriodName = periodDao.selectById(unitEntity.getCurrentPeriodId()).getNumber();
+
 
             TransferPeriodLogEntity entity = new TransferPeriodLogEntity();
+            entity.setFarmId(unitEntity.getFarmId());
             entity.setEarTag(oldEntity.getEartag());
             entity.setOldPeriod(oldPeriodName);
             entity.setOldUnit(oldUnitName);
-            entity.setNewPeriod(newEntity.getNumber());
+            entity.setNewPeriod(newPeriodName);
             entity.setNewUnit(newUnitName);
             entity.setOperatorUsername(username);
             logEntityList.add(entity);
 
-
             yearPigBaseEntity.setId(pigId);
-            yearPigBaseEntity.setPeriodId(periodId);
+            yearPigBaseEntity.setPeriodId(unitEntity.getCurrentPeriodId());
             updateById(yearPigBaseEntity);
         }
         transferPeriodLogDao.batchInsert(logEntityList);

+ 62 - 0
huimv-smart-management/src/main/java/com/huimv/management/util/GetUserUtil.java

@@ -0,0 +1,62 @@
+package com.huimv.management.util;
+
+import com.alibaba.fastjson.JSONObject;
+import com.huimv.common.exception.RRException;
+import com.huimv.common.utils.R;
+import com.huimv.management.SpringContextUtils;
+import com.huimv.management.entity.SysUserEntity;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.support.PropertiesLoaderUtils;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.client.RestTemplate;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.IOException;
+import java.util.Properties;
+
+/**
+ * <p>
+ * 通过restTemplate获取用户的工具类
+ * </p>
+ *
+ * @author yinhao
+ * @date 2021/6/21 9:02
+ */
+public class GetUserUtil {
+
+    private static String url;
+
+    private static RestTemplate restTemplate = SpringContextUtils.getBean("restTemplate",RestTemplate.class);
+
+
+    static {
+        Resource resource = new ClassPathResource("application.properties");
+        try {
+            Properties properties = PropertiesLoaderUtils.loadProperties(resource);
+            url = properties.getProperty("user.info.url");
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    public static SysUserEntity getUser(HttpServletRequest request) {
+        String token = request.getHeader("token");
+
+        HttpHeaders headers = new HttpHeaders();
+        headers.add("token", token);
+        HttpEntity httpEntity = new HttpEntity<>(null, headers);
+
+        ResponseEntity<R> responseEntity = restTemplate.exchange(url, HttpMethod.GET, httpEntity, R.class);
+        R sysUserEntity = responseEntity.getBody();
+        if (401 == (Integer) (sysUserEntity.get("code"))) {
+            throw new RRException((String) sysUserEntity.get("msg"), (Integer) sysUserEntity.get("code"));
+        }
+        String jsonString = JSONObject.toJSONString(sysUserEntity.get("user"));
+        SysUserEntity user = JSONObject.parseObject(jsonString, SysUserEntity.class);
+        return user;
+    }
+}

+ 3 - 1
huimv-smart-management/src/main/resources/mapper/management/TransferPeriodLogDao.xml

@@ -6,6 +6,7 @@
 	<!-- 可根据自己的需求,是否要使用 -->
     <resultMap type="com.huimv.management.entity.TransferPeriodLogEntity" id="transferPeriodLogMap">
         <result property="id" column="id"/>
+        <result property="farmId" column="farm_id" />
         <result property="earTag" column="ear_tag"/>
         <result property="oldPeriod" column="old_period"/>
         <result property="newPeriod" column="new_period"/>
@@ -17,9 +18,10 @@
     </resultMap>
 
     <insert id="batchInsert">
-        INSERT INTO mgt_transfer_period_log(ear_tag, old_period, new_period, old_unit, new_unit, gmt_create, gmt_modified, operator_username) VALUES
+        INSERT INTO mgt_transfer_period_log(farm_id,ear_tag, old_period, new_period, old_unit, new_unit, gmt_create, gmt_modified, operator_username) VALUES
         <foreach collection="list" item="item" separator=",">
             (
+            #{item.farmId},
             #{item.earTag},
             #{item.oldPeriod},
             #{item.newPeriod},