diff --git a/src/controller/chicken.controller.js b/src/controller/chicken.controller.js index f0a02eb..af528d2 100644 --- a/src/controller/chicken.controller.js +++ b/src/controller/chicken.controller.js @@ -2,6 +2,7 @@ const { addChicken, getChickenInfoById, getAllChickenInfo, + getChickenInfoByHouseId, } = require("../service/chicken.service"); const Response = require("../utils/response"); @@ -49,6 +50,12 @@ class ChickenController { ); ctx.body = Response(0, "查询新进记录成功", res); } + + async batchs(ctx, next) { + const { house_id } = ctx.request.query; + const res = await getChickenInfoByHouseId(house_id); + ctx.body = Response(0, "查询批次号成功", res); + } } module.exports = new ChickenController(); diff --git a/src/controller/market.controller.js b/src/controller/market.controller.js new file mode 100644 index 0000000..5a23648 --- /dev/null +++ b/src/controller/market.controller.js @@ -0,0 +1,40 @@ +const { + addNewMarket, + getAllMarketList, + getMarketByBatchId, +} = require("../service/market.service"); +const Response = require("../utils/response"); + +class MarketController { + async add(ctx, next) { + const { + batch_id, + market_number, + market_counter_balance, + house_id, + out_time, + } = ctx.request.body; + const res = await addNewMarket( + batch_id, + market_number, + market_counter_balance, + house_id, + out_time + ); + ctx.body = Response(0, "新增出栏记录成功", res); + } + + async list(ctx, next) { + const { house_id } = ctx.request.body; + const res = await getAllMarketList(house_id); + ctx.body = Response(0, "查询出栏记录成功", res); + } + + async detail(ctx, next) { + const { batch_id } = ctx.request.body; + const res = await getMarketByBatchId(batch_id); + ctx.body = Response(0, "查询出栏详情成功", res); + } +} + +module.exports = new MarketController(); diff --git a/src/model/market.model.js b/src/model/market.model.js index a842df9..f1d774b 100644 --- a/src/model/market.model.js +++ b/src/model/market.model.js @@ -6,10 +6,10 @@ const seq = require("../db/seq"); const Market = seq.define("chicken_market", { // id 会被sequelize自动创建, 管理 batch_id: { - type: DataTypes.INTEGER, + type: DataTypes.STRING, allowNull: false, unique: true, - comment: "批次号对应的新进鸡苗id", + comment: "批次号", }, market_number: { type: DataTypes.INTEGER, @@ -23,6 +23,17 @@ const Market = seq.define("chicken_market", { defaultValue: 0, comment: "出栏均重", }, + house_id: { + type: DataTypes.INTEGER, + allowNull: false, + comment: "鸡场id", + }, + out_time: { + type: DataTypes.DATE, + allowNull: false, + defaultValue: DataTypes.NOW, + comment: "出栏时间", + }, }); // 强制同步数据库(创建数据表) diff --git a/src/router/chicken.route.js b/src/router/chicken.route.js index 9c29ca6..bd353d9 100644 --- a/src/router/chicken.route.js +++ b/src/router/chicken.route.js @@ -3,7 +3,7 @@ const Router = require("koa-router"); const router = new Router({ prefix: "/chicken" }); const { auth, hasAdminPermission } = require("../middleware/auth.middleware"); -const { add, info, all } = require("../controller/chicken.controller"); +const { add, info, all, batchs } = require("../controller/chicken.controller"); router.post("/add", auth, add); @@ -13,4 +13,6 @@ router.get("/info", auth, info); // 根据条件查询新进记录 router.post("/all", auth, all); +router.get("/batchs", auth, batchs); + module.exports = router; diff --git a/src/router/market.route.js b/src/router/market.route.js new file mode 100644 index 0000000..face129 --- /dev/null +++ b/src/router/market.route.js @@ -0,0 +1,14 @@ +const Router = require("koa-router"); + +const router = new Router({ prefix: "/market" }); + +const { auth } = require("../middleware/auth.middleware"); +const { add, list, detail } = require("../controller/market.controller"); + +router.post("/add", auth, add); + +router.post("/list", auth, list); + +router.post("/detail", auth, detail); + +module.exports = router; diff --git a/src/service/chicken.service.js b/src/service/chicken.service.js index a43d4ef..ff5a18b 100644 --- a/src/service/chicken.service.js +++ b/src/service/chicken.service.js @@ -4,6 +4,7 @@ const { getHouseById } = require("./house.service"); const { getFactoryById } = require("./factory.service"); const { getVarietyById } = require("./variety.service"); const { getBatchCoopByIds } = require("./coop.service"); +const { getAccountInfo } = require("./account.service"); const { Op } = require("sequelize"); const { DATE_FILTER } = require("../constant/constant"); class ChickenService { @@ -66,6 +67,15 @@ class ChickenService { return null; } + async getChickenByBatchId(batch_number) { + const res = await Chicken.findOne({ + where: { + batch_number, + }, + }); + return res ? res.dataValues : null; + } + async getAllChickenInfo( batch_number = "", coop_ids = [], @@ -104,6 +114,27 @@ class ChickenService { const real = Promise.all(p); return real; } + + async getChickenInfoByHouseId(house_id) { + const res = await Chicken.findAll({ + where: { + house_id, + }, + }); + // 批量拿coopinfo + const arr = res?.map((item) => item.dataValues); + const p = arr.map(async (item) => { + const varietyName = await getVarietyById(item.variety_id); + const allCoops = await getBatchCoopByIds(item.coop_id.split(",")); + const userinfo = await getAccountInfo({ id: item.log_user_id }); + item.varietyName = varietyName; + item.allCoops = allCoops; + item.username = userinfo.name; + return item; + }); + const real = await Promise.all(p); + return real; + } } module.exports = new ChickenService(); diff --git a/src/service/market.service.js b/src/service/market.service.js new file mode 100644 index 0000000..a787e25 --- /dev/null +++ b/src/service/market.service.js @@ -0,0 +1,75 @@ +const Market = require("../model/market.model"); +const { getChickenByBatchId } = require("./chicken.service"); +const { getBatchCoopByIds } = require("./coop.service"); +const { getVarietyById } = require("./variety.service"); +const { getAccountInfo } = require("./account.service"); +const { getDayLife } = require("../utils/common"); +class MarketService { + async addNewMarket( + batch_id, + market_number, + market_counter_balance, + house_id, + out_time + ) { + const res = await Market.create({ + batch_id, + market_number, + market_counter_balance, + house_id, + out_time, + }); + return res ? res.dataValues : null; + } + + async getAllMarketList(house_id) { + const res = await Market.findAll({ + where: { + house_id, + }, + }); + const arr = res?.map((item) => item.dataValues); + const p = arr.map(async (item) => { + const childrenInfo = await getChickenByBatchId(item.batch_id); + console.log(childrenInfo); + const coop_ids = childrenInfo.coop_id.split(","); + // // 根据coop_id批量查询 + const coopInfo = await getBatchCoopByIds(coop_ids); + item.coopInfo = coopInfo; + return item; + }); + const real = await Promise.all(p); + return real; + } + + async getMarketByBatchId(batch_id) { + const res = await Market.findOne({ + where: { + batch_id, + }, + }); + // 根据当前的batch_id查询基本信息 + const chickenInfo = await getChickenByBatchId(batch_id); + console.log(chickenInfo); + const log_user_id = chickenInfo.log_user_id; + const variety_id = chickenInfo.variety_id; + const userInfo = await getAccountInfo({ id: log_user_id }); + const varietyInfo = await getVarietyById(variety_id); + const coop_ids = chickenInfo.coop_id.split(","); + // // 根据coop_id批量查询 + const coopInfo = await getBatchCoopByIds(coop_ids); + if (res) { + res.dataValues.coopInfo = coopInfo; + res.dataValues.varietyInfo = varietyInfo; + res.dataValues.username = userInfo.name; + res.dataValues.dayLife = getDayLife( + res.dataValues.out_time, + chickenInfo.put_time + ); + return res; + } + return null; + } +} + +module.exports = new MarketService(); diff --git a/src/test/someFunc.js b/src/test/someFunc.js index a7cde69..33c3df0 100644 --- a/src/test/someFunc.js +++ b/src/test/someFunc.js @@ -63,3 +63,12 @@ arr.map((item, index) => { } } }); + +function getDayLife(out_time, in_time) { + // 出栏日龄=出栏时间-进鸡时间 + const dateBegin = new Date(in_time); + const dateEnd = new Date(out_time); + var result = dateEnd.getTime() - dateBegin.getTime(); + var joinTime = Math.floor(result / (24 * 3600 * 1000)); + console.log(joinTime); +} diff --git a/src/utils/common.js b/src/utils/common.js index b707828..87ca9d8 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -28,8 +28,19 @@ function haveSame(arr, otherArr) { return normalArr.length !== setArr.length; } +// 获得日龄 +function getDayLife(out_time, in_time) { + // 出栏日龄=出栏时间-进鸡时间 + const dateBegin = new Date(in_time); + const dateEnd = new Date(out_time); + var result = dateEnd.getTime() - dateBegin.getTime(); + var joinTime = Math.floor(result / (24 * 3600 * 1000)); + return joinTime; +} + module.exports = { isJsonString, generateSerialNumber, haveSame, + getDayLife, };