Browse Source

改变chicken表结构

master
maochaoying 2 years ago
parent
commit
2b9bb148c6
  1. 7
      src/controller/market.controller.js
  2. 6
      src/model/chicken.model.js
  3. 9
      src/router/market.route.js
  4. 8
      src/service/chicken.service.js
  5. 19
      src/service/die.service.js
  6. 31
      src/service/market.service.js

7
src/controller/market.controller.js

@ -2,6 +2,7 @@ const {
addNewMarket, addNewMarket,
getAllMarketList, getAllMarketList,
getMarketByBatchId, getMarketByBatchId,
getDefaultMarketInfo,
} = require("../service/market.service"); } = require("../service/market.service");
const Response = require("../utils/response"); const Response = require("../utils/response");
@ -35,6 +36,12 @@ class MarketController {
const res = await getMarketByBatchId(batch_id); const res = await getMarketByBatchId(batch_id);
ctx.body = Response(0, "查询出栏详情成功", res); ctx.body = Response(0, "查询出栏详情成功", res);
} }
async defaultInfo(ctx, next) {
const { house_id, batch_number } = ctx.request.body;
const res = await getDefaultMarketInfo(house_id, batch_number);
ctx.body = Response(0, "查询出栏初始化信息成功", res);
}
} }
module.exports = new MarketController(); module.exports = new MarketController();

6
src/model/chicken.model.js

@ -60,6 +60,12 @@ const Chicken = seq.define("chicken_chicken", {
unique: true, unique: true,
comment: "批次号", comment: "批次号",
}, },
is_marketed: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: 0,
comment: "0代表 未出栏, 1代表出栏",
},
}); });
// 强制同步数据库(创建数据表) // 强制同步数据库(创建数据表)

9
src/router/market.route.js

@ -3,7 +3,12 @@ const Router = require("koa-router");
const router = new Router({ prefix: "/market" }); const router = new Router({ prefix: "/market" });
const { auth } = require("../middleware/auth.middleware"); const { auth } = require("../middleware/auth.middleware");
const { add, list, detail } = require("../controller/market.controller");
const {
add,
list,
detail,
defaultInfo,
} = require("../controller/market.controller");
router.post("/add", auth, add); router.post("/add", auth, add);
@ -11,4 +16,6 @@ router.post("/list", auth, list);
router.post("/detail", auth, detail); router.post("/detail", auth, detail);
router.post("/default", auth, defaultInfo);
module.exports = router; module.exports = router;

8
src/service/chicken.service.js

@ -36,7 +36,9 @@ class ChickenService {
} }
async getChickenByCoopIdAndHouseId(coop_id, house_id) { async getChickenByCoopIdAndHouseId(coop_id, house_id) {
const whereOpt = {};
const whereOpt = {
is_marketed: 0,
};
coop_id && Object.assign(whereOpt, { coop_id }); coop_id && Object.assign(whereOpt, { coop_id });
house_id && Object.assign(whereOpt, { house_id }); house_id && Object.assign(whereOpt, { house_id });
const res = await Chicken.findOne({ const res = await Chicken.findOne({
@ -49,6 +51,7 @@ class ChickenService {
const res = await Chicken.findOne({ const res = await Chicken.findOne({
where: { where: {
id, id,
is_marketed: 0,
}, },
}); });
if (res) { if (res) {
@ -71,6 +74,7 @@ class ChickenService {
const res = await Chicken.findOne({ const res = await Chicken.findOne({
where: { where: {
batch_number, batch_number,
is_marketed: 0,
}, },
}); });
return res ? res.dataValues : null; return res ? res.dataValues : null;
@ -83,6 +87,7 @@ class ChickenService {
house_id house_id
) { ) {
let selectObj = { let selectObj = {
is_marketed: 0,
batch_number: { batch_number: {
[Op.like]: `%${batch_number}%`, [Op.like]: `%${batch_number}%`,
}, },
@ -120,6 +125,7 @@ class ChickenService {
const res = await Chicken.findAll({ const res = await Chicken.findAll({
where: { where: {
house_id, house_id,
is_marketed: 0,
}, },
}); });
// 批量拿coopinfo // 批量拿coopinfo

19
src/service/die.service.js

@ -105,6 +105,7 @@ class DieService {
const res = await Chicken.findOne({ const res = await Chicken.findOne({
where: { where: {
house_id, house_id,
is_marketed: 0,
coop_id: { coop_id: {
[Op.like]: `%${coop_id}%`, [Op.like]: `%${coop_id}%`,
}, },
@ -135,6 +136,24 @@ class DieService {
} }
return null; return null;
} }
async getDieTotal(coop_ids, house_id) {
const res = await Die.findAll({
where: {
coop_id: {
[Op.in]: coop_ids,
},
house_id,
},
});
let total = 0;
res
.map((item) => item.dataValues)
.map((it) => {
total += it.die_number;
});
return total;
}
} }
module.exports = new DieService(); module.exports = new DieService();

31
src/service/market.service.js

@ -1,8 +1,10 @@
const Market = require("../model/market.model"); const Market = require("../model/market.model");
const Chicken = require("../model/chicken.model");
const { getChickenByBatchId } = require("./chicken.service"); const { getChickenByBatchId } = require("./chicken.service");
const { getBatchCoopByIds } = require("./coop.service"); const { getBatchCoopByIds } = require("./coop.service");
const { getVarietyById } = require("./variety.service"); const { getVarietyById } = require("./variety.service");
const { getAccountInfo } = require("./account.service"); const { getAccountInfo } = require("./account.service");
const { getDieTotal } = require("./die.service");
const { getDayLife } = require("../utils/common"); const { getDayLife } = require("../utils/common");
const { Op } = require("sequelize"); const { Op } = require("sequelize");
const { DATE_FILTER } = require("../constant/constant"); const { DATE_FILTER } = require("../constant/constant");
@ -40,7 +42,6 @@ class MarketService {
const arr = res?.map((item) => item.dataValues); const arr = res?.map((item) => item.dataValues);
const p = arr.map(async (item) => { const p = arr.map(async (item) => {
const childrenInfo = await getChickenByBatchId(item.batch_id); const childrenInfo = await getChickenByBatchId(item.batch_id);
console.log(childrenInfo);
const coop_ids = childrenInfo.coop_id.split(","); const coop_ids = childrenInfo.coop_id.split(",");
// // 根据coop_id批量查询 // // 根据coop_id批量查询
const coopInfo = await getBatchCoopByIds(coop_ids); const coopInfo = await getBatchCoopByIds(coop_ids);
@ -59,7 +60,6 @@ class MarketService {
}); });
// 根据当前的batch_id查询基本信息 // 根据当前的batch_id查询基本信息
const chickenInfo = await getChickenByBatchId(batch_id); const chickenInfo = await getChickenByBatchId(batch_id);
console.log(chickenInfo);
const log_user_id = chickenInfo.log_user_id; const log_user_id = chickenInfo.log_user_id;
const variety_id = chickenInfo.variety_id; const variety_id = chickenInfo.variety_id;
const userInfo = await getAccountInfo({ id: log_user_id }); const userInfo = await getAccountInfo({ id: log_user_id });
@ -79,6 +79,33 @@ class MarketService {
} }
return null; return null;
} }
async getDefaultMarketInfo(house_id, batch_number) {
const res = await Chicken.findOne({
where: {
is_marketed: 0,
house_id,
batch_number,
},
});
if (res) {
const coop_ids = res.dataValues.coop_id.split(",");
const coopInfos = await getBatchCoopByIds(coop_ids);
res.dataValues.coopInfos = coopInfos;
// 出栏数量默认等于“进鸡数量-死淘数量”
const dieTotal = await getDieTotal(coop_ids, house_id);
res.dataValues.market_default_number =
res.dataValues.chicken_number - dieTotal;
// 出栏日龄=出栏时间-进鸡时间
res.dataValues.day_life = getDayLife(new Date(), res.put_time);
const varietyInfo = await getVarietyById(res.dataValues.variety_id);
res.dataValues.varietyInfo = varietyInfo;
const userInfo = await getAccountInfo({ id: res.dataValues.log_user_id });
res.dataValues.log_user = userInfo.name;
return res.dataValues;
}
return null;
}
} }
module.exports = new MarketService(); module.exports = new MarketService();
Loading…
Cancel
Save