Compare commits

...

2 Commits

Author SHA1 Message Date
wyt
e1dab09512 Merge remote-tracking branch 'origin/main' 2025-07-21 18:17:20 +08:00
wyt
e14341d804 修改羊只分组后端接口 2025-07-21 18:17:12 +08:00
5 changed files with 160 additions and 12 deletions

View File

@ -18,6 +18,11 @@ import com.zhyc.module.base.service.IBasSheepGroupMappingService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;
// 1. 导入
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 羊只分组关联Controller
*
@ -43,6 +48,10 @@ public class BasSheepGroupMappingController extends BaseController
return getDataTable(list);
}
// 2. 声明放在类内部方法外部
private static final Logger log = LoggerFactory.getLogger(BasSheepGroupMappingController.class);
@PreAuthorize("@ss.hasPermi('sheep_grouping:sheep_grouping:list')")
@GetMapping("/listJoin")
public TableDataInfo list(
@ -103,7 +112,11 @@ public class BasSheepGroupMappingController extends BaseController
@PutMapping
public AjaxResult edit(@RequestBody BasSheepGroupMapping basSheepGroupMapping)
{
try {
return toAjax(basSheepGroupMappingService.updateBasSheepGroupMapping(basSheepGroupMapping));
} catch (RuntimeException e) {
return error(e.getMessage());
}
}
/**
@ -116,4 +129,20 @@ public class BasSheepGroupMappingController extends BaseController
{
return toAjax(basSheepGroupMappingService.deleteBasSheepGroupMappingByIds(ids));
}
@PostMapping("/addByEarTags")
public AjaxResult addByEarTags(@RequestBody Map<String, Object> params) {
List<String> earTags = (List<String>) params.get("earTags");
Long groupId = Long.valueOf(params.get("groupId").toString());
if (earTags == null || earTags.isEmpty()) {
return error("耳号列表不能为空");
}
return basSheepGroupMappingService.addByEarTags(earTags, groupId);
}
}

View File

@ -76,4 +76,11 @@ public interface BasSheepGroupMappingMapper
public int deleteBasSheepGroupMappingByIds(Long[] ids);
List<Map<String, Object>> selectSheepIdsByEarTags(@Param("earTags") List<String> earTags);
int batchInsert(@Param("list") List<BasSheepGroupMapping> list);
List<BasSheepGroupMapping> selectListByGroupId(@Param("groupId") Long groupId);
}

View File

@ -3,6 +3,7 @@ package com.zhyc.module.base.service;
import java.util.List;
import java.util.Map;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.module.base.domain.BasSheepGroupMapping;
/**
@ -67,4 +68,6 @@ public interface IBasSheepGroupMappingService
* @return 结果
*/
public int deleteBasSheepGroupMappingById(Long id);
public AjaxResult addByEarTags(List<String> earTags, Long groupId);
}

View File

@ -1,8 +1,9 @@
package com.zhyc.module.base.service.impl;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.module.base.domain.BasSheepGroupMapping;
import com.zhyc.module.base.mapper.BasSheepGroupMappingMapper;
import com.zhyc.module.base.service.IBasSheepGroupMappingService;
@ -70,9 +71,21 @@ public class BasSheepGroupMappingServiceImpl implements IBasSheepGroupMappingSer
* @param basSheepGroupMapping 羊只分组关联
* @return 结果
*/
// @Override
// public int updateBasSheepGroupMapping(BasSheepGroupMapping basSheepGroupMapping)
// {
// return basSheepGroupMappingMapper.updateBasSheepGroupMapping(basSheepGroupMapping);
// }
@Override
public int updateBasSheepGroupMapping(BasSheepGroupMapping basSheepGroupMapping)
{
public int updateBasSheepGroupMapping(BasSheepGroupMapping basSheepGroupMapping) {
Long SheepId = basSheepGroupMapping.getSheepId();
Long GroupId = basSheepGroupMapping.getGroupId();
existsInGroup(SheepId,GroupId);
if (existsInGroup(basSheepGroupMapping.getSheepId(), basSheepGroupMapping.getGroupId())) {
throw new RuntimeException("该羊已在此分组,无需修改");
}
return basSheepGroupMappingMapper.updateBasSheepGroupMapping(basSheepGroupMapping);
}
@ -100,4 +113,77 @@ public class BasSheepGroupMappingServiceImpl implements IBasSheepGroupMappingSer
return basSheepGroupMappingMapper.deleteBasSheepGroupMappingById(id);
}
@Override
public AjaxResult addByEarTags(List<String> earTags, Long groupId) {
// 1. 参数判空
if (earTags == null || earTags.isEmpty()) {
return AjaxResult.error("耳号列表不能为空");
}
// 2. 根据耳号查询羊只manage_tags -> id
List<Map<String, Object>> sheepList = basSheepGroupMappingMapper.selectSheepIdsByEarTags(earTags);
Map<String, Long> tagToId = new HashMap<>();
for (Map<String, Object> s : sheepList) {
tagToId.put((String) s.get("manage_tags"), ((Number) s.get("id")).longValue());
}
// 3. 不存在的耳号
List<String> missing = new ArrayList<>();
for (String tag : earTags) {
if (!tagToId.containsKey(tag)) {
missing.add(tag);
}
}
if (!missing.isEmpty()) {
Map<String, Object> res = new HashMap<>();
res.put("success", false);
res.put("missing", missing);
return AjaxResult.success(res);
}
// 4. 查询该分组下已存在的羊只ID
List<BasSheepGroupMapping> existingRows = basSheepGroupMappingMapper.selectListByGroupId(groupId);
System.out.println("🔍 existingRows 类型 = " + existingRows.getClass());
Set<Long> existingIds = existingRows.stream()
.map(BasSheepGroupMapping::getSheepId)
.collect(Collectors.toSet());
System.out.println("🔍 existingIds = " + existingIds);
// 5. 过滤出未在该分组的羊只
List<BasSheepGroupMapping> toInsert = new ArrayList<>();
for (Map.Entry<String, Long> e : tagToId.entrySet()) {
Long sheepId = e.getValue();
if (!existingIds.contains(sheepId)) {
BasSheepGroupMapping m = new BasSheepGroupMapping();
m.setSheepId(sheepId);
m.setGroupId(groupId);
toInsert.add(m);
}
}
if (toInsert.isEmpty()) {
return AjaxResult.success("所选羊只已全部在该分组中");
}
// 6. 批量插入
int rows = basSheepGroupMappingMapper.batchInsert(toInsert);
Map<String, Object> res = new HashMap<>();
res.put("success", true);
res.put("inserted", rows);
return AjaxResult.success(res);
}
private boolean existsInGroup(Long sheepId, Long groupId) {
BasSheepGroupMapping param = new BasSheepGroupMapping();
List<Map<String, Object>> list = basSheepGroupMappingMapper
.selectBasSheepGroupMappingList(sheepId, groupId, null);
return !list.isEmpty();
}
}

View File

@ -52,8 +52,8 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="groupId != null"> AND m.group_id = #{groupId}</if>
<if test="bsManageTags != null and bsManageTags.size > 0">
AND s.bs_manage_tags IN
<foreach collection="bsManageTags" item="bsManageTag " open="(" separator="," close=")">
#{bsManageTag}
<foreach collection="bsManageTags" item="tag" open="(" separator="," close=")">
#{tag}
</foreach>
</if>
</where>
@ -61,11 +61,6 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
</select>
<select id="selectBasSheepGroupMappingById" parameterType="Long" resultMap="BasSheepGroupMappingResult">
<include refid="selectBasSheepGroupMappingVo"/>
where id = #{id}
@ -102,4 +97,32 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{id}
</foreach>
</delete>
<!-- 根据耳号查询羊只ID -->
<select id="selectSheepIdsByEarTags" parameterType="list" resultType="map">
SELECT id, manage_tags
FROM bas_sheep
WHERE manage_tags IN
<foreach collection="earTags" item="tag" open="(" separator="," close=")">
#{tag}
</foreach>
</select>
<!-- 批量插入 -->
<insert id="batchInsert" parameterType="java.util.List">
INSERT INTO bas_sheep_group_mapping (sheep_id, group_id)
VALUES
<foreach collection="list" item="item" separator=",">
(#{item.sheepId}, #{item.groupId})
</foreach>
</insert>
<select id="selectListByGroupId"
resultMap="BasSheepGroupMappingResult">
SELECT id, sheep_id, group_id
FROM bas_sheep_group_mapping
WHERE group_id = #{groupId}
</select>
</mapper>