From c6cc6eb066a0d4e8a1b7d4926994923695b20092 Mon Sep 17 00:00:00 2001 From: maochaoying <925670706@qq.com> Date: Thu, 23 Mar 2023 10:55:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=AD=BB=E6=B7=98=E9=A6=96=E9=A1=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/die.controller.js | 13 ++++++- src/router/die.route.js | 5 ++- src/service/chicken.service.js | 10 +++++ src/service/die.service.js | 58 +++++++++++++++++++++++++++- src/test/someFunc.js | 83 ++++++++++++++++++++++++++++++---------- 5 files changed, 144 insertions(+), 25 deletions(-) diff --git a/src/controller/die.controller.js b/src/controller/die.controller.js index 5d92ae1..7abb977 100644 --- a/src/controller/die.controller.js +++ b/src/controller/die.controller.js @@ -2,6 +2,7 @@ const { addDieLog, getDieInfoById, getAllDieInfo, + getDieDetail, } = require("../service/die.service"); const Response = require("../utils/response"); @@ -18,16 +19,24 @@ class DieController { ); ctx.body = Response(0, "新增死淘记录成功", res); } + async info(ctx, next) { const { house_id, coop_id } = ctx.request.body; const res = await getDieInfoById(house_id, coop_id); ctx.body = Response(0, "查询单条死淘记录成功", res); } + async all(ctx, next) { - const { house_id } = ctx.request.body; - const res = await getAllDieInfo(house_id); + const { house_id, coop_ids, batch_number } = ctx.request.body; + const res = await getAllDieInfo(house_id, coop_ids, batch_number); ctx.body = Response(0, "查询死淘记录成功", res); } + + async detail(ctx, next) { + const { house_id, coop_id } = ctx.request.body; + const res = await getDieDetail(house_id, coop_id); + ctx.body = Response(0, "查询死淘详情成功", res); + } } module.exports = new DieController(); diff --git a/src/router/die.route.js b/src/router/die.route.js index 972a19a..7cc2e7e 100644 --- a/src/router/die.route.js +++ b/src/router/die.route.js @@ -3,7 +3,7 @@ const Router = require("koa-router"); const router = new Router({ prefix: "/die" }); const { auth, hasAdminPermission } = require("../middleware/auth.middleware"); -const { add, info, all } = require("../controller/die.controller"); +const { add, info, all, detail } = require("../controller/die.controller"); router.post("/add", auth, add); @@ -13,4 +13,7 @@ router.post("/info", auth, info); // 根据条件查询新进记录 router.post("/all", auth, all); +// 获取死淘详情数据 +router.post("/detail", auth, detail); + module.exports = router; diff --git a/src/service/chicken.service.js b/src/service/chicken.service.js index 26bc01e..a43d4ef 100644 --- a/src/service/chicken.service.js +++ b/src/service/chicken.service.js @@ -34,6 +34,16 @@ class ChickenService { return res ? res.dataValues : null; } + async getChickenByCoopIdAndHouseId(coop_id, house_id) { + const whereOpt = {}; + coop_id && Object.assign(whereOpt, { coop_id }); + house_id && Object.assign(whereOpt, { house_id }); + const res = await Chicken.findOne({ + where: whereOpt, + }); + return res ? res.dataValues : null; + } + async getChickenInfoById(id, log_name) { const res = await Chicken.findOne({ where: { diff --git a/src/service/die.service.js b/src/service/die.service.js index fa235d5..c1bc835 100644 --- a/src/service/die.service.js +++ b/src/service/die.service.js @@ -1,5 +1,7 @@ const Die = require("../model/die.model"); const { Op } = require("sequelize"); +const { getChickenByCoopIdAndHouseId } = require("./chicken.service"); +const { getCoopById } = require("./coop.service"); class DieService { async addDieLog(house_id, coop_id, reason_id, die_number, put_time) { const res = await Die.create({ @@ -21,10 +23,64 @@ class DieService { const arr = res?.map((item) => item.dataValues); return arr; } - async getAllDieInfo(house_id) { + + // 需要根据鸡舍编号和批次号进行模糊查询 + async getAllDieInfo(house_id, coop_ids = [], batch_number = "") { const res = await Die.findAll({ where: { house_id, + coop_id: { + [Op.in]: coop_ids, + }, + }, + }); + const arr = res?.map((item) => item.dataValues); + // 对arr中的数据进行处理 + // house_id和coop_id相同的进行die_number合并 + let temp = []; + arr.map((item, index) => { + if ( + temp.every( + (it) => it.house_id != item.house_id || it.coop_id != item.coop_id + ) + ) { + temp.push(item); + } else { + const real = temp.filter( + (it) => it.house_id == item.house_id && it.coop_id == item.coop_id + ); + if (real && real.length > 0) { + real[0].die_number = item.die_number + real[0].die_number; + } + } + }); + const p = temp.map(async (item) => { + const chickenInfo = await getChickenByCoopIdAndHouseId( + item.coop_id, + item.house_id + ); + const coopInfo = await getCoopById(item.coop_id); + item.batch_number = chickenInfo.batch_number; + item.coop_name = coopInfo.coop_name; + return item; + }); + const hasBatch = await Promise.all(p); + // 根据批次号进行筛选 + const result = hasBatch.filter((item) => { + if (batch_number == "") { + return true; + } else { + return item.batch_number.indexOf(batch_number) != -1; + } + }); + return result; + } + + async getDieDetail(house_id, coop_id) { + const res = await Die.findAll({ + where: { + house_id, + coop_id, }, }); const arr = res?.map((item) => item.dataValues); diff --git a/src/test/someFunc.js b/src/test/someFunc.js index 790b8e9..a7cde69 100644 --- a/src/test/someFunc.js +++ b/src/test/someFunc.js @@ -1,24 +1,65 @@ -function generateSerialNumber() { - const now = new Date(); - const year = now.getFullYear(); //得到年份 - const month = now.getMonth() + 1; //得到月份 - const date = now.getDate(); //得到日期 - const hour = now.getHours(); //得到小时数 - const minute = now.getMinutes(); //得到分钟数 - const second = now.getSeconds(); //得到秒数 - const time = now.getTime(); - return `${year}${month}${date}${hour}${minute}${second}${time}`; -} +// function generateSerialNumber() { +// const now = new Date(); +// const year = now.getFullYear(); //得到年份 +// const month = now.getMonth() + 1; //得到月份 +// const date = now.getDate(); //得到日期 +// const hour = now.getHours(); //得到小时数 +// const minute = now.getMinutes(); //得到分钟数 +// const second = now.getSeconds(); //得到秒数 +// const time = now.getTime(); +// return `${year}${month}${date}${hour}${minute}${second}${time}`; +// } -const res = generateSerialNumber(); -console.log(res); +// const res = generateSerialNumber(); +// console.log(res); -function haveSame(arr, otherArr) { - // 合并数组 - const normalArr = [...arr, ...otherArr]; - // 合并数组并去重 - const setArr = [...new Set(normalArr)]; - return normalArr.length !== setArr.length; -} +// function haveSame(arr, otherArr) { +// // 合并数组 +// const normalArr = [...arr, ...otherArr]; +// // 合并数组并去重 +// const setArr = [...new Set(normalArr)]; +// return normalArr.length !== setArr.length; +// } -console.log(haveSame("2,5".split(","), ["1"])); +// console.log(haveSame("2,5".split(","), ["1"])); + +const arr = [ + { + id: 1, + house_id: 4, + coop_id: 2, + reason_id: 1, + die_number: 222, + }, + { + id: 2, + house_id: 4, + coop_id: 2, + reason_id: 1, + die_number: 222, + }, + { + id: 3, + house_id: 4, + coop_id: 7, + reason_id: 1, + die_number: 222, + }, +]; +let temp = []; +arr.map((item, index) => { + if ( + temp.every( + (it) => it.house_id != item.house_id || it.coop_id != item.coop_id + ) + ) { + temp.push(item); + } else { + const real = temp.filter( + (it) => it.house_id == item.house_id && it.coop_id == item.coop_id + ); + if (real && real.length > 0) { + real[0].die_number = item.die_number + real[0].die_number; + } + } +});