9 changed files with 203 additions and 3 deletions
-
7src/controller/chicken.controller.js
-
40src/controller/market.controller.js
-
15src/model/market.model.js
-
4src/router/chicken.route.js
-
14src/router/market.route.js
-
31src/service/chicken.service.js
-
75src/service/market.service.js
-
9src/test/someFunc.js
-
11src/utils/common.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(); |
@ -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; |
@ -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(); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue