羊只分组页面下拉框调整为嵌套查询,羊舍管理添加可视化

This commit is contained in:
wyt 2025-08-20 18:42:01 +08:00
parent 80d82c031f
commit 9183e99413
11 changed files with 541 additions and 8 deletions

View File

@ -1,12 +1,15 @@
package com.zhyc.module.base.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import com.zhyc.common.core.domain.entity.SysDictData;
import com.zhyc.common.utils.StringUtils;
import com.zhyc.module.base.domain.BasGroupTreeVo;
import com.zhyc.module.base.domain.BasSheepGroupMapping;
import com.zhyc.module.base.domain.BasSheepGroupExportVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@ -22,6 +25,13 @@ import com.zhyc.common.core.page.TableDataInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
// 1. 引入若依字典工具
import com.zhyc.common.utils.DictUtils;
/**
* 羊只分组关联Controller
@ -33,6 +43,20 @@ import org.slf4j.LoggerFactory;
@RequestMapping("/sheep_grouping/sheep_grouping")
public class BasSheepGroupMappingController extends BaseController
{
// 工具方法LocalDateTime -> Date
private Date toDate(Object obj) {
if (obj == null) {
return null;
}
if (obj instanceof Date) {
return (Date) obj;
}
if (obj instanceof LocalDateTime) {
return Date.from(((LocalDateTime) obj).atZone(ZoneId.systemDefault()).toInstant());
}
throw new IllegalArgumentException("不支持的日期类型:" + obj.getClass());
}
@Autowired
private IBasSheepGroupMappingService basSheepGroupMappingService;
@ -73,16 +97,73 @@ public class BasSheepGroupMappingController extends BaseController
/**
* 导出羊只分组关联列表
*/
// @PreAuthorize("@ss.hasPermi('sheep_grouping:sheep_grouping:export')")
// @Log(title = "羊只分组关联", businessType = BusinessType.EXPORT)
// @PostMapping("/export")
// public void export(HttpServletResponse response, BasSheepGroupMapping basSheepGroupMapping)
// {
// List<BasSheepGroupMapping> list = basSheepGroupMappingService.selectBasSheepGroupMappingList(basSheepGroupMapping);
// ExcelUtil<BasSheepGroupMapping> util = new ExcelUtil<BasSheepGroupMapping>(BasSheepGroupMapping.class);
// util.exportExcel(response, list, "羊只分组关联数据");
// }
@PreAuthorize("@ss.hasPermi('sheep_grouping:sheep_grouping:export')")
@Log(title = "羊只分组关联", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, BasSheepGroupMapping basSheepGroupMapping)
{
List<BasSheepGroupMapping> list = basSheepGroupMappingService.selectBasSheepGroupMappingList(basSheepGroupMapping);
ExcelUtil<BasSheepGroupMapping> util = new ExcelUtil<BasSheepGroupMapping>(BasSheepGroupMapping.class);
util.exportExcel(response, list, "羊只分组关联数据");
public void exportJoin(HttpServletResponse response,
@RequestParam(required = false) Long sheepId,
@RequestParam(required = false) Long groupId,
@RequestParam(required = false) String bsManageTags) throws IOException {
// 1. 把前端传来的耳号字符串转 List
List<String> earList = null;
if (StringUtils.hasText(bsManageTags)) {
earList = Arrays.asList(bsManageTags.split("[,\\s]+"));
}
// 2. 先查出来这里返回 List<Map<String,Object>>
List<Map<String, Object>> list = basSheepGroupMappingService
.selectBasSheepGroupMappingList(sheepId, groupId, earList);
// 放在 stream 外面避免每次都查缓存
Map<String, String> genderMap = new HashMap<>();
List<SysDictData> genderDict = DictUtils.getDictCache("sheep_gender");
if (genderDict != null) {
genderDict.forEach(d -> genderMap.put(d.getDictValue(), d.getDictLabel()));
}
// 3. Map VO
List<BasSheepGroupExportVo> voList = list.stream()
.map(m -> {
BasSheepGroupExportVo vo = new BasSheepGroupExportVo();
vo.setGroupId(m.get("group_id") == null ? null : ((Number) m.get("group_id")).longValue());
vo.setGroupName((String) m.get("group_name"));
vo.setBsManageTags((String) m.get("bs_manage_tags"));
vo.setVariety((String) m.get("variety"));
vo.setFamily((String) m.get("family"));
vo.setName((String) m.get("name"));
vo.setGender(m.get("gender") == null ? null : ((Number) m.get("gender")).longValue());
Long genderCode = m.get("gender") == null ? null : ((Number) m.get("gender")).longValue();
vo.setGender(genderCode);
vo.setGenderText(genderMap.get(String.valueOf(genderCode)));
vo.setBirthday(toDate(m.get("birthday")));
vo.setMonthAge(m.get("month_age") == null ? null : ((Number) m.get("month_age")).longValue());
vo.setParity(m.get("parity") == null ? null : ((Number) m.get("parity")).longValue());
vo.setBirthWeight(m.get("birth_weight") == null ? null : ((Number) m.get("birth_weight")).longValue());
vo.setWeaningWeight(m.get("weaning_weight") == null ? null : ((Number) m.get("weaning_weight")).longValue());
vo.setCurrentWeight(m.get("current_weight") == null ? null : ((Number) m.get("current_weight")).longValue());
vo.setBreed((String) m.get("breed"));
vo.setFatherManageTags((String) m.get("father_manage_tags"));
vo.setMotherManageTags((String) m.get("mother_manage_tags"));
return vo;
})
.collect(Collectors.toList());
// 4. 导出
ExcelUtil<BasSheepGroupExportVo> util = new ExcelUtil<>(BasSheepGroupExportVo.class);
util.exportExcel(response, voList, "羊只分组关联数据");
}
/**
* 获取羊只分组关联详细信息
*/
@ -144,5 +225,13 @@ public class BasSheepGroupMappingController extends BaseController
}
/**
* 树形分组下拉框
*/
@GetMapping("/group/tree")
public AjaxResult groupTree() {
return success(basSheepGroupMappingService.selectGroupTree());
}
}

View File

@ -14,7 +14,11 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.zhyc.common.utils.DictUtils;
/**
* 羊舍管理Controller
@ -119,4 +123,73 @@ public class DaSheepfoldController extends BaseController
boolean exist = daSheepfoldService.checkSheepfoldNoExist(ranchId, sheepfoldTypeId, sheepfoldNo);
return AjaxResult.success(exist);
}
@PreAuthorize("@ss.hasPermi('sheepfold_management:sheepfold_management:list')")
@GetMapping("/seatMap")
public AjaxResult seatMap() {
List<DaSheepfold> flat = daSheepfoldService.selectDaSheepfoldList(new DaSheepfold());
Map<Long, Map<String, Object>> tree = new LinkedHashMap<>();
for (DaSheepfold s : flat) {
Long ranchId = s.getRanchId();
Long typeId = s.getSheepfoldTypeId();
String foldNo = s.getSheepfoldNo();
String rowNo = s.getRowNo();
String columns = s.getColumns();
// 翻译字典
String ranchName = DictUtils.getDictLabel("da_ranch", String.valueOf(ranchId));
String typeName = DictUtils.getDictLabel("bas_sheepfold_type", String.valueOf(typeId));
// 牧场
Map<String, Object> ranch = tree.computeIfAbsent(ranchId, k -> {
Map<String, Object> r = new LinkedHashMap<>();
r.put("ranchId", k);
r.put("ranchName", ranchName);
r.put("types", new ArrayList<>());
return r;
});
// 类型
Map<String, Object> type = ((List<Map<String, Object>>) ranch.get("types")).stream()
.filter(t -> typeId.equals(t.get("typeId")))
.findFirst()
.orElseGet(() -> {
Map<String, Object> t = new LinkedHashMap<>();
t.put("typeId", typeId);
t.put("typeName", typeName);
t.put("folds", new ArrayList<>());
((List<Map<String, Object>>) ranch.get("types")).add(t);
return t;
});
// 羊舍
Map<String, Object> fold = ((List<Map<String, Object>>) type.get("folds")).stream()
.filter(f -> foldNo.equals(f.get("foldNo")))
.findFirst()
.orElseGet(() -> {
Map<String, Object> f = new LinkedHashMap<>();
f.put("foldNo", foldNo);
f.put("rows", new ArrayList<>());
((List<Map<String, Object>>) type.get("folds")).add(f);
return f;
});
//
Map<String, Object> row = ((List<Map<String, Object>>) fold.get("rows")).stream()
.filter(r -> rowNo.equals(r.get("rowNo")))
.findFirst()
.orElseGet(() -> {
Map<String, Object> newRow = new LinkedHashMap<>();
newRow.put("rowNo", rowNo);
newRow.put("columns", new ArrayList<>());
((List<Map<String, Object>>) fold.get("rows")).add(newRow);
return newRow;
});
// 将列号添加到对应的行
((List<String>) row.get("columns")).add(columns);
}
return success(new ArrayList<>(tree.values()));
}
}

View File

@ -0,0 +1,11 @@
package com.zhyc.module.base.domain;
import lombok.Data;
import java.util.List;
@Data
public class BasGroupTreeVo {
private Long groupId;
private String groupName;
private Long parentId;
private List<BasGroupTreeVo> children;
}

View File

@ -0,0 +1,235 @@
package com.zhyc.module.base.domain;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import com.zhyc.common.annotation.Excel;
import com.zhyc.common.core.domain.BaseEntity;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class BasSheepGroupExportVo extends BaseEntity {
private static final long serialVersionUID = 1L;
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
private Long groupId;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
/** 分组名称 */
@Excel(name = "分组名称")
private String groupName;
/** 管理耳号 */
@Excel(name = "耳号")
private String bsManageTags;
/** 品种 */
@Excel(name = "品种")
private String variety;
/** 性别 */
private Long gender;
public String getGenderText() {
return genderText;
}
public void setGenderText(String genderText) {
this.genderText = genderText;
}
/* 性别文本,真正导出的列 */
@Excel(name = "性别")
private String genderText;
/** 羊只类型 */
@Excel(name = "羊只类型")
private String name;
/** 出生日期 */
@JsonFormat(pattern = "yyyy-MM-dd")
@Excel(name = "出生日期", width = 30, dateFormat = "yyyy-MM-dd")
private Date birthday;
/** 胎次 */
@Excel(name = "胎次")
private Long parity;
/** 月龄 */
@Excel(name = "月龄")
private Long monthAge;
/** 繁殖状态 */
@Excel(name = "繁殖状态")
private String breed;
/** 出生体重 */
@Excel(name = "出生体重")
private Long birthWeight;
/** 断奶体重 */
@Excel(name = "断奶体重")
private Long weaningWeight;
/** 当前体重 */
@Excel(name = "当前体重")
private Long currentWeight;
/** 父亲管理耳号 */
@Excel(name = "父号")
private String fatherManageTags;
/** 母亲管理耳号 */
@Excel(name = "母号")
private String motherManageTags;
/** 家系 */
@Excel(name = "家系")
private String family;
public String getBsManageTags() {
return bsManageTags;
}
public void setBsManageTags(String bsManageTags) {
this.bsManageTags = bsManageTags;
}
public String getVariety() {
return variety;
}
public void setVariety(String variety) {
this.variety = variety;
}
public String getFamily() {
return family;
}
public void setFamily(String family) {
this.family = family;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getGender() {
return gender;
}
public void setGender(Long gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Long getMonthAge() {
return monthAge;
}
public void setMonthAge(Long monthAge) {
this.monthAge = monthAge;
}
public Long getParity() {
return parity;
}
public void setParity(Long parity) {
this.parity = parity;
}
public Long getBirthWeight() {
return birthWeight;
}
public void setBirthWeight(Long birthWeight) {
this.birthWeight = birthWeight;
}
public Long getWeaningWeight() {
return weaningWeight;
}
public void setWeaningWeight(Long weaningWeight) {
this.weaningWeight = weaningWeight;
}
public Long getCurrentWeight() {
return currentWeight;
}
public void setCurrentWeight(Long currentWeight) {
this.currentWeight = currentWeight;
}
public String getBreed() {
return breed;
}
public void setBreed(String breed) {
this.breed = breed;
}
public String getFatherManageTags() {
return fatherManageTags;
}
public void setFatherManageTags(String fatherManageTags) {
this.fatherManageTags = fatherManageTags;
}
public String getMotherManageTags() {
return motherManageTags;
}
public void setMotherManageTags(String motherManageTags) {
this.motherManageTags = motherManageTags;
}
}

View File

@ -33,4 +33,13 @@ public class BasSheepGroupMapping extends BaseEntity
@Excel(name = "分组ID")
private Long groupId;
@Excel(name = "耳号")
private String bsManageTags; // 或者 List<String>其他类型
public String getBsManageTags() {
return bsManageTags;
}
public void setBsManageTags(String bsManageTags) {
this.bsManageTags = bsManageTags;
}
}

View File

@ -21,6 +21,70 @@ public class DaSheepfold extends BaseEntity
{
private static final long serialVersionUID = 1L;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getRanchId() {
return ranchId;
}
public void setRanchId(Long ranchId) {
this.ranchId = ranchId;
}
public String getSheepfoldName() {
return sheepfoldName;
}
public void setSheepfoldName(String sheepfoldName) {
this.sheepfoldName = sheepfoldName;
}
public Long getSheepfoldTypeId() {
return sheepfoldTypeId;
}
public void setSheepfoldTypeId(Long sheepfoldTypeId) {
this.sheepfoldTypeId = sheepfoldTypeId;
}
public String getSheepfoldNo() {
return sheepfoldNo;
}
public void setSheepfoldNo(String sheepfoldNo) {
this.sheepfoldNo = sheepfoldNo;
}
public String getRowNo() {
return rowNo;
}
public void setRowNo(String rowNo) {
this.rowNo = rowNo;
}
public String getColumns() {
return columns;
}
public void setColumns(String columns) {
this.columns = columns;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
/** 羊舍id */
@Excel(name = "羊舍id")
private Long id;

View File

@ -4,6 +4,7 @@ import java.util.List;
import java.util.Map;
import com.zhyc.module.base.domain.BasSheepGroupMapping;
import com.zhyc.module.base.domain.BasGroupTreeVo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
@ -85,4 +86,6 @@ public interface BasSheepGroupMappingMapper
List<BasSheepGroupMapping> selectListByGroupId(@Param("groupId") Long groupId);
List<BasGroupTreeVo> selectGroupTree();
}

View File

@ -4,6 +4,7 @@ import java.util.List;
import java.util.Map;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.module.base.domain.BasGroupTreeVo;
import com.zhyc.module.base.domain.BasSheepGroupMapping;
/**
@ -70,4 +71,9 @@ public interface IBasSheepGroupMappingService
public int deleteBasSheepGroupMappingById(Long id);
public AjaxResult addByEarTags(List<String> earTags, Long groupId);
/**
* 树形分组下拉框
*/
List<BasGroupTreeVo> selectGroupTree();
}

View File

@ -4,12 +4,14 @@ import java.util.*;
import java.util.stream.Collectors;
import com.zhyc.common.core.domain.AjaxResult;
import com.zhyc.module.base.domain.BasGroupTreeVo;
import com.zhyc.module.base.domain.BasSheepGroupMapping;
import com.zhyc.module.base.mapper.BasSheepGroupMappingMapper;
import com.zhyc.module.base.service.IBasSheepGroupMappingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* 羊只分组关联Service业务层处理
*
@ -185,5 +187,29 @@ public class BasSheepGroupMappingServiceImpl implements IBasSheepGroupMappingSer
}
private List<BasGroupTreeVo> buildTree(List<BasGroupTreeVo> all) {
Map<Long, BasGroupTreeVo> map = all.stream()
.collect(Collectors.toMap(BasGroupTreeVo::getGroupId, v -> v));
List<BasGroupTreeVo> tree = new ArrayList<>();
for (BasGroupTreeVo n : all) {
if (n.getParentId() == null || n.getParentId() == 0L) {
tree.add(n);
} else {
BasGroupTreeVo parent = map.get(n.getParentId());
if (parent != null) {
if (parent.getChildren() == null) {
parent.setChildren(new ArrayList<>());
}
parent.getChildren().add(n);
}
}
}
return tree;
}
@Override
public List<BasGroupTreeVo> selectGroupTree() {
List<BasGroupTreeVo> list = basSheepGroupMappingMapper.selectGroupTree();
return buildTree(list);
}
}

View File

@ -30,6 +30,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
m.id,
m.sheep_id,
m.group_id,
bsg.group_name,
s.id AS bs_sheep_id,
s.bs_manage_tags,
s.variety,
@ -47,6 +48,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
s.family
FROM bas_sheep_group_mapping m
JOIN sheep_file s ON s.id = m.sheep_id
JOIN bas_sheep_group bsg ON m.group_id = bsg.group_id
<where>
<if test="sheepId != null"> AND m.sheep_id = #{sheepId}</if>
<if test="groupId != null"> AND m.group_id = #{groupId}</if>
@ -125,4 +127,14 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
FROM bas_sheep_group_mapping
WHERE group_id = #{groupId}
</select>
<!-- 树形分组 -->
<select id="selectGroupTree"
resultType="com.zhyc.module.base.domain.BasGroupTreeVo">
SELECT group_id AS groupId,
group_name AS groupName,
parent_id AS parentId
FROM bas_sheep_group
ORDER BY parent_id, group_id
</select>
</mapper>

View File

@ -25,6 +25,7 @@
columns,
comment
from da_sheepfold
</sql>
<select id="selectDaSheepfoldList" parameterType="DaSheepfold" resultMap="DaSheepfoldResult">
@ -33,6 +34,10 @@
<if test="ranchId != null ">and ranch_id = #{ranchId}</if>
<if test="sheepfoldTypeId != null ">and sheepfold_type_id = #{sheepfoldTypeId}</if>
</where>
order by
SUBSTRING_INDEX(row_no, '-', 1),
CAST(columns AS UNSIGNED)
</select>
<select id="selectDaSheepfoldById" parameterType="Long" resultMap="DaSheepfoldResult">