maochaoying 2 years ago
parent
commit
82e2d7bc15
  1. 7
      src/controller/chicken.controller.js
  2. 40
      src/controller/market.controller.js
  3. 15
      src/model/market.model.js
  4. 4
      src/router/chicken.route.js
  5. 14
      src/router/market.route.js
  6. 31
      src/service/chicken.service.js
  7. 75
      src/service/market.service.js
  8. 9
      src/test/someFunc.js
  9. 11
      src/utils/common.js

7
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();

40
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();

15
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: "出栏时间",
},
});
// 强制同步数据库(创建数据表)

4
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;

14
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;

31
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();

75
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();

9
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);
}

11
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,
};
Loading…
Cancel
Save