产羔导出修复

This commit is contained in:
zyk
2026-03-03 10:03:59 +08:00
parent e3774cfde4
commit 204bbc5ee2
6 changed files with 282 additions and 13 deletions

View File

@@ -48,6 +48,7 @@ public class ScEmbryoFlushController extends BaseController
return getDataTable(list);
}
/**
* 导出冲胚记录列表
*/
@@ -126,4 +127,35 @@ public class ScEmbryoFlushController extends BaseController
List<Map<String, Object>> list = scEmbryoFlushService.selectDonorFemaleList();
return success(list);
}
/**
* 获取供体公羊下拉列表
*/
@PreAuthorize("@ss.hasPermi('embryo:flush:query')")
@GetMapping("/donorMaleList")
public AjaxResult getDonorMaleList()
{
List<Map<String, Object>> list = scEmbryoFlushService.selectDonorMaleList();
return success(list);
}
/**
* 根据耳号获取羊只基础信息
*/
@PreAuthorize("@ss.hasPermi('embryo:flush:query')")
@GetMapping("/getSheepInfo")
public AjaxResult getSheepInfo(@RequestParam("manageTag") String manageTag) {
return success(scEmbryoFlushService.getSheepInfoByTag(manageTag));
}
/**
* 根据父母品种实时计算胚胎品种
*/
@PreAuthorize("@ss.hasPermi('embryo:flush:query')")
@GetMapping("/calculateVariety")
public AjaxResult calculateVariety(@RequestParam("maleVariety") String maleVariety,
@RequestParam("femaleVariety") String femaleVariety) {
String result = scEmbryoFlushService.calculateEmbryoVariety(maleVariety, femaleVariety);
return success(result);
}
}

View File

@@ -1,8 +1,14 @@
package com.zhyc.module.produce.breed.controller;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@@ -13,7 +19,6 @@ import com.zhyc.common.enums.BusinessType;
import com.zhyc.module.produce.breed.domain.ScLambingRecord;
import com.zhyc.module.produce.breed.domain.ScLambDetail;
import com.zhyc.module.produce.breed.service.IScLambingRecordService;
import com.zhyc.common.utils.poi.ExcelUtil;
import com.zhyc.common.core.page.TableDataInfo;
/**
@@ -55,6 +60,7 @@ public class ScLambingRecordController extends BaseController {
return error("查询配种信息失败:" + e.getMessage());
}
}
@PreAuthorize("@ss.hasPermi('breed:lambing_records:query')")
@GetMapping("/search_ear_numbers")
public AjaxResult searchEarNumbers(@RequestParam("query") String query) {
@@ -66,25 +72,217 @@ public class ScLambingRecordController extends BaseController {
return error("搜索耳号失败:" + e.getMessage());
}
}
/**
* 导出产羔记录列表
* 导出产羔记录列表多SheetSheet1=产羔记录Sheet2=羔羊详情)
*/
@PreAuthorize("@ss.hasPermi('breed:lambing_records:export')")
@Log(title = "产羔记录", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, ScLambingRecord scLambingRecord) {
public void export(HttpServletResponse response, ScLambingRecord scLambingRecord) throws IOException {
// 不分页,查全量
List<ScLambingRecord> list = scLambingRecordService.selectScLambingRecordList(scLambingRecord);
ExcelUtil<ScLambingRecord> util = new ExcelUtil<ScLambingRecord>(ScLambingRecord.class);
util.exportExcel(response, list, "产羔记录数据");
// 为每条记录补充羔羊详情
for (ScLambingRecord record : list) {
List<ScLambDetail> details = scLambingRecordService.selectLambDetailByLambingRecordId(record.getId());
record.setLambDetails(details);
}
// 构建多Sheet工作簿
XSSFWorkbook workbook = buildExportWorkbook(list);
// 写出响应
String filename = java.net.URLEncoder.encode("产羔记录_" + new java.text.SimpleDateFormat("yyyyMMddHHmmss").format(new java.util.Date()), "UTF-8");
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");
workbook.write(response.getOutputStream());
workbook.close();
}
/**
* 获取产羔记录详细信息(修改:改为获取包含关联信息的详细数据
* 构建导出工作簿两个Sheet
*/
private XSSFWorkbook buildExportWorkbook(List<ScLambingRecord> list) {
XSSFWorkbook workbook = new XSSFWorkbook();
SimpleDateFormat dateFmt = new SimpleDateFormat("yyyy-MM-dd");
// ==================== Sheet1产羔记录 ====================
Sheet sheet1 = workbook.createSheet("产羔记录");
// 标题样式
CellStyle headerStyle = createHeaderStyle(workbook);
// 普通单元格样式
CellStyle dataStyle = createDataStyle(workbook);
// 产羔记录表头(与界面完全一致)
String[] headers1 = {
"母羊耳号", "母羊品种", "配种日期", "胎次", "公羊耳号", "公羊品种",
"产羔数量", "活羔数量", "折损数", "技术员", "月龄", "产羔评分",
"公羔数量", "母羔数量", "留养公羔数量", "留养母羔数量",
"未留养公羔数量", "未留养母羔数量", "产羔时怀孕天数",
"当前羊舍", "创建人", "创建日期", "所在牧场", "备注"
};
// 列宽(字符数 * 256
int[] colWidths1 = {
14, 12, 13, 8, 14, 12,
10, 10, 8, 10, 8, 10,
10, 10, 14, 14,
16, 16, 16,
12, 10, 12, 12, 20
};
Row headerRow1 = sheet1.createRow(0);
headerRow1.setHeightInPoints(22);
for (int i = 0; i < headers1.length; i++) {
sheet1.setColumnWidth(i, colWidths1[i] * 256);
Cell cell = headerRow1.createCell(i);
cell.setCellValue(headers1[i]);
cell.setCellStyle(headerStyle);
}
// 产羔记录数据行
int rowNum1 = 1;
for (ScLambingRecord r : list) {
Row row = sheet1.createRow(rowNum1++);
row.setHeightInPoints(18);
int col = 0;
setCellValue(row, col++, r.getFemaleEarNumber(), dataStyle);
setCellValue(row, col++, r.getFemaleBreed(), dataStyle);
setCellValue(row, col++, r.getBreedingDate() != null ? dateFmt.format(r.getBreedingDate()) : "", dataStyle);
setCellValue(row, col++, r.getParity(), dataStyle);
setCellValue(row, col++, r.getMaleEarNumber(), dataStyle);
setCellValue(row, col++, r.getMaleBreed(), dataStyle);
setCellValue(row, col++, r.getLambsBorn(), dataStyle);
setCellValue(row, col++, r.getSurvival(), dataStyle);
// 折损数 = 产羔数 - 活羔数
int loss = (int) ((r.getLambsBorn() != null ? r.getLambsBorn() : 0)
- (r.getSurvival() != null ? r.getSurvival() : 0));
setCellValue(row, col++, loss, dataStyle);
setCellValue(row, col++, r.getTechnician(), dataStyle);
setCellValue(row, col++, r.getMonthAge(), dataStyle);
setCellValue(row, col++, r.getScore(), dataStyle);
setCellValue(row, col++, r.getMaleCount(), dataStyle);
setCellValue(row, col++, r.getFemaleCount(), dataStyle);
setCellValue(row, col++, r.getRetainedMaleCount(), dataStyle);
setCellValue(row, col++, r.getRetainedFemaleCount(), dataStyle);
setCellValue(row, col++, r.getUnretainedMaleCount(), dataStyle);
setCellValue(row, col++, r.getUnretainedFemaleCount(), dataStyle);
setCellValue(row, col++, r.getPregnancyDays(), dataStyle);
setCellValue(row, col++, r.getCurrentShed(), dataStyle);
setCellValue(row, col++, r.getCreateBy(), dataStyle);
setCellValue(row, col++, r.getCreateTime() != null ? dateFmt.format(r.getCreateTime()) : "", dataStyle);
setCellValue(row, col++, r.getFarm(), dataStyle);
setCellValue(row, col++, r.getComment(), dataStyle);
}
// ==================== Sheet2羔羊详情 ====================
Sheet sheet2 = workbook.createSheet("羔羊详情");
String[] headers2 = {
"母羊耳号", "胎次", "羔羊耳号", "性别", "出生体重(kg)",
"是否留养", "家系", "出生日期"
};
int[] colWidths2 = { 14, 8, 14, 8, 14, 10, 16, 13 };
Row headerRow2 = sheet2.createRow(0);
headerRow2.setHeightInPoints(22);
for (int i = 0; i < headers2.length; i++) {
sheet2.setColumnWidth(i, colWidths2[i] * 256);
Cell cell = headerRow2.createCell(i);
cell.setCellValue(headers2[i]);
cell.setCellStyle(headerStyle);
}
int rowNum2 = 1;
for (ScLambingRecord r : list) {
if (r.getLambDetails() == null || r.getLambDetails().isEmpty()) {
continue;
}
for (ScLambDetail d : r.getLambDetails()) {
Row row = sheet2.createRow(rowNum2++);
row.setHeightInPoints(18);
int col = 0;
setCellValue(row, col++, r.getFemaleEarNumber(), dataStyle);
setCellValue(row, col++, r.getParity(), dataStyle);
setCellValue(row, col++, d.getLambEarNumber(), dataStyle);
// 性别1=公0=母2=阉羊3=兼性(参照后端校验逻辑)
String genderLabel = "";
if (d.getGender() != null) {
switch (d.getGender()) {
case 0: genderLabel = ""; break;
case 1: genderLabel = ""; break;
case 2: genderLabel = "阉羊"; break;
case 3: genderLabel = "兼性"; break;
default: genderLabel = String.valueOf(d.getGender());
}
}
setCellValue(row, col++, genderLabel, dataStyle);
setCellValue(row, col++, d.getBirthWeight() != null ? d.getBirthWeight().toPlainString() : "", dataStyle);
setCellValue(row, col++, Boolean.TRUE.equals(d.getIsRetained()) ? "" : "", dataStyle);
setCellValue(row, col++, d.getLineage(), dataStyle);
setCellValue(row, col++, d.getBirthday() != null ? dateFmt.format(d.getBirthday()) : "", dataStyle);
}
}
return workbook;
}
/** 创建表头样式 */
private CellStyle createHeaderStyle(XSSFWorkbook workbook) {
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBold(true);
font.setFontHeightInPoints((short) 11);
style.setFont(font);
style.setFillForegroundColor(IndexedColors.LIGHT_CORNFLOWER_BLUE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
return style;
}
/** 创建数据行样式 */
private CellStyle createDataStyle(XSSFWorkbook workbook) {
CellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
return style;
}
/** 统一设置单元格值 */
private void setCellValue(Row row, int col, Object value, CellStyle style) {
Cell cell = row.createCell(col);
if (value == null) {
cell.setCellValue("");
} else if (value instanceof Integer) {
cell.setCellValue((Integer) value);
} else if (value instanceof Long) {
cell.setCellValue((Long) value);
} else if (value instanceof Double) {
cell.setCellValue((Double) value);
} else {
cell.setCellValue(value.toString());
}
cell.setCellStyle(style);
}
/**
* 获取产羔记录详细信息
*/
@PreAuthorize("@ss.hasPermi('breed:lambing_records:query')")
@GetMapping(value = "/{id}")
public AjaxResult getInfo(@PathVariable("id") Long id) {
// 修改:改为调用详细查询方法,获取包含母羊耳号、公羊耳号等关联信息
return success(scLambingRecordService.selectScLambingRecordDetailById(id));
}
@@ -96,16 +294,11 @@ public class ScLambingRecordController extends BaseController {
@PostMapping
public AjaxResult add(@RequestBody ScLambingRecord scLambingRecord) {
try {
// 设置创建人
scLambingRecord.setCreateBy(getUsername());
// 如果没有设置创建时间,使用当前时间
if (scLambingRecord.getCreateTime() == null) {
scLambingRecord.setCreateTime(new java.util.Date());
}
int result = scLambingRecordService.insertScLambingRecord(scLambingRecord);
if (result > 0) {
String message = "新增产羔记录成功";
if (scLambingRecord.getLambDetails() != null && !scLambingRecord.getLambDetails().isEmpty()) {

View File

@@ -83,4 +83,11 @@ public interface ScEmbryoFlushMapper
* @return 母羊列表
*/
public List<Map<String, Object>> selectDonorFemaleList();
/**
* 查询所有公羊列表(用于下拉选择)
*
* @return 公羊列表
*/
public List<Map<String, Object>> selectDonorMaleList();
}

View File

@@ -83,4 +83,13 @@ public interface IScEmbryoFlushService
* @return 母羊列表
*/
public List<Map<String, Object>> selectDonorFemaleList();
/**
* 查询所有供体公羊列表(用于下拉选择)
*
* @return 公羊列表
*/
public List<Map<String, Object>> selectDonorMaleList();
public Map<String, Object> getSheepInfoByTag(String manageTag);
}

View File

@@ -105,7 +105,7 @@ public class ScEmbryoFlushServiceImpl implements IScEmbryoFlushService
int unfertilized = flush.getUnfertilized() != null ? flush.getUnfertilized() : 0;
int degenerated = flush.getDegenerated() != null ? flush.getDegenerated() : 0;
// 有效胚 = A+ + A + B + C + D
// 有效胚 = A+ + A + B + C + D(16细胞D级) + 16细胞期
flush.setValidEmbryo(gradeAPlus + gradeA + gradeB + gradeC + gradeD);
// 冲胚数 = 所有数量求和
@@ -220,6 +220,14 @@ public class ScEmbryoFlushServiceImpl implements IScEmbryoFlushService
return calculateEmbryoVarietyById(maleId, femaleId);
}
@Override
public Map<String, Object> getSheepInfoByTag(String manageTag) {
if (manageTag == null || manageTag.trim().isEmpty()) {
return new HashMap<>();
}
return scEmbryoFlushMapper.selectSheepInfoByManageTag(manageTag);
}
/**
* 根据父母品种ID计算胚胎品种核心计算方法
*
@@ -324,4 +332,10 @@ public class ScEmbryoFlushServiceImpl implements IScEmbryoFlushService
{
return scEmbryoFlushMapper.selectDonorFemaleList();
}
@Override
public List<Map<String, Object>> selectDonorMaleList()
{
return scEmbryoFlushMapper.selectDonorMaleList();
}
}

View File

@@ -222,4 +222,18 @@
AND (sf.is_delete = 0 OR sf.is_delete IS NULL)
ORDER BY sf.bs_manage_tags
</select>
<!-- 查询所有公羊列表(用于下拉选择) -->
<select id="selectDonorMaleList" resultType="java.util.Map">
SELECT DISTINCT
sf.bs_manage_tags AS manageTag,
sf.variety,
sf.variety_id AS varietyId,
sf.ranch_id AS ranchId,
sf.dr_ranch AS ranchName
FROM sheep_file sf
WHERE sf.gender = 2
AND (sf.is_delete = 0 OR sf.is_delete IS NULL)
ORDER BY sf.bs_manage_tags
</select>
</mapper>