Browse Source

死淘首页

master
maochaoying 2 years ago
parent
commit
c6cc6eb066
  1. 13
      src/controller/die.controller.js
  2. 5
      src/router/die.route.js
  3. 10
      src/service/chicken.service.js
  4. 58
      src/service/die.service.js
  5. 83
      src/test/someFunc.js

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

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

10
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: {

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

83
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;
}
}
});
Loading…
Cancel
Save