修改羊只分组后端接口
This commit is contained in:
parent
862c86d130
commit
e14341d804
@ -18,6 +18,11 @@ import com.zhyc.module.base.service.IBasSheepGroupMappingService;
|
|||||||
import com.zhyc.common.utils.poi.ExcelUtil;
|
import com.zhyc.common.utils.poi.ExcelUtil;
|
||||||
import com.zhyc.common.core.page.TableDataInfo;
|
import com.zhyc.common.core.page.TableDataInfo;
|
||||||
|
|
||||||
|
// 1. 导入
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 羊只分组关联Controller
|
* 羊只分组关联Controller
|
||||||
*
|
*
|
||||||
@ -43,6 +48,10 @@ public class BasSheepGroupMappingController extends BaseController
|
|||||||
return getDataTable(list);
|
return getDataTable(list);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2. 声明(放在类内部、方法外部)
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(BasSheepGroupMappingController.class);
|
||||||
|
|
||||||
|
|
||||||
@PreAuthorize("@ss.hasPermi('sheep_grouping:sheep_grouping:list')")
|
@PreAuthorize("@ss.hasPermi('sheep_grouping:sheep_grouping:list')")
|
||||||
@GetMapping("/listJoin")
|
@GetMapping("/listJoin")
|
||||||
public TableDataInfo list(
|
public TableDataInfo list(
|
||||||
@ -103,7 +112,11 @@ public class BasSheepGroupMappingController extends BaseController
|
|||||||
@PutMapping
|
@PutMapping
|
||||||
public AjaxResult edit(@RequestBody BasSheepGroupMapping basSheepGroupMapping)
|
public AjaxResult edit(@RequestBody BasSheepGroupMapping basSheepGroupMapping)
|
||||||
{
|
{
|
||||||
return toAjax(basSheepGroupMappingService.updateBasSheepGroupMapping(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));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -76,4 +76,11 @@ public interface BasSheepGroupMappingMapper
|
|||||||
public int deleteBasSheepGroupMappingByIds(Long[] ids);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package com.zhyc.module.base.service;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import com.zhyc.common.core.domain.AjaxResult;
|
||||||
import com.zhyc.module.base.domain.BasSheepGroupMapping;
|
import com.zhyc.module.base.domain.BasSheepGroupMapping;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -67,4 +68,6 @@ public interface IBasSheepGroupMappingService
|
|||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
public int deleteBasSheepGroupMappingById(Long id);
|
public int deleteBasSheepGroupMappingById(Long id);
|
||||||
|
|
||||||
|
public AjaxResult addByEarTags(List<String> earTags, Long groupId);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
package com.zhyc.module.base.service.impl;
|
package com.zhyc.module.base.service.impl;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
import java.util.Map;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import com.zhyc.common.core.domain.AjaxResult;
|
||||||
import com.zhyc.module.base.domain.BasSheepGroupMapping;
|
import com.zhyc.module.base.domain.BasSheepGroupMapping;
|
||||||
import com.zhyc.module.base.mapper.BasSheepGroupMappingMapper;
|
import com.zhyc.module.base.mapper.BasSheepGroupMappingMapper;
|
||||||
import com.zhyc.module.base.service.IBasSheepGroupMappingService;
|
import com.zhyc.module.base.service.IBasSheepGroupMappingService;
|
||||||
@ -70,9 +71,21 @@ public class BasSheepGroupMappingServiceImpl implements IBasSheepGroupMappingSer
|
|||||||
* @param basSheepGroupMapping 羊只分组关联
|
* @param basSheepGroupMapping 羊只分组关联
|
||||||
* @return 结果
|
* @return 结果
|
||||||
*/
|
*/
|
||||||
|
// @Override
|
||||||
|
// public int updateBasSheepGroupMapping(BasSheepGroupMapping basSheepGroupMapping)
|
||||||
|
// {
|
||||||
|
// return basSheepGroupMappingMapper.updateBasSheepGroupMapping(basSheepGroupMapping);
|
||||||
|
// }
|
||||||
@Override
|
@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);
|
return basSheepGroupMappingMapper.updateBasSheepGroupMapping(basSheepGroupMapping);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,4 +113,77 @@ public class BasSheepGroupMappingServiceImpl implements IBasSheepGroupMappingSer
|
|||||||
return basSheepGroupMappingMapper.deleteBasSheepGroupMappingById(id);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -52,19 +52,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
<if test="groupId != null"> AND m.group_id = #{groupId}</if>
|
<if test="groupId != null"> AND m.group_id = #{groupId}</if>
|
||||||
<if test="bsManageTags != null and bsManageTags.size > 0">
|
<if test="bsManageTags != null and bsManageTags.size > 0">
|
||||||
AND s.bs_manage_tags IN
|
AND s.bs_manage_tags IN
|
||||||
<foreach collection="bsManageTags" item="bsManageTag " open="(" separator="," close=")">
|
<foreach collection="bsManageTags" item="tag" open="(" separator="," close=")">
|
||||||
#{bsManageTag}
|
#{tag}
|
||||||
</foreach>
|
</foreach>
|
||||||
</if>
|
</if>
|
||||||
</where>
|
</where>
|
||||||
ORDER BY m.id
|
ORDER BY m.id
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<select id="selectBasSheepGroupMappingById" parameterType="Long" resultMap="BasSheepGroupMappingResult">
|
<select id="selectBasSheepGroupMappingById" parameterType="Long" resultMap="BasSheepGroupMappingResult">
|
||||||
<include refid="selectBasSheepGroupMappingVo"/>
|
<include refid="selectBasSheepGroupMappingVo"/>
|
||||||
@ -102,4 +97,32 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
|||||||
#{id}
|
#{id}
|
||||||
</foreach>
|
</foreach>
|
||||||
</delete>
|
</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>
|
</mapper>
|
Loading…
x
Reference in New Issue
Block a user