|
@@ -457,7 +457,120 @@ rationItemDAO.prototype.updateAnnotation = function (lastOpr, repId, updateArr,
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
-}
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 根据条件获取定额数据
|
|
|
+ *
|
|
|
+ * @param {Object} condition
|
|
|
+ * @param {Object} fields
|
|
|
+ * @param {String} indexBy
|
|
|
+ * @return {Promise|Array}
|
|
|
+ */
|
|
|
+rationItemDAO.prototype.getRationItemByCondition = async function (condition, fields = null, indexBy = null) {
|
|
|
+ let result = [];
|
|
|
+ if (Object.keys(condition).length <= 0) {
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+ result = await rationItemModel.find(condition, fields);
|
|
|
+ if (indexBy !== null && result.length > 0) {
|
|
|
+ let tmpResult = {};
|
|
|
+ for(let tmp of result) {
|
|
|
+ tmpResult[tmp[indexBy]] = tmp;
|
|
|
+ }
|
|
|
+ result = tmpResult;
|
|
|
+ }
|
|
|
+ return result;
|
|
|
+};
|
|
|
+
|
|
|
+/**
|
|
|
+ * 从excel中批量新增数据
|
|
|
+ *
|
|
|
+ * @param {Number} rationRepId
|
|
|
+ * @param {Array} data
|
|
|
+ * @return {bool}
|
|
|
+ */
|
|
|
+rationItemDAO.prototype.batchAddFromExcel = async function(rationRepId, data) {
|
|
|
+ if (data.length <= 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ let lastData = {};
|
|
|
+ const rationData = [];
|
|
|
+ // 编码列表,用于查找库中是否有对应数据
|
|
|
+ let rationCodeList = [];
|
|
|
+ let gljCodeList = [];
|
|
|
+ for (const tmp of data) {
|
|
|
+ if (tmp.length <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ // 如果第一个字段为null则是工料机数据,放入上一个数据的工料机字段
|
|
|
+ if (tmp[0] === undefined && Object.keys(lastData).length > 0) {
|
|
|
+ const tmpRationGlj = {
|
|
|
+ gljId: 1,
|
|
|
+ consumeAmt: tmp[4],
|
|
|
+ proportion: 0,
|
|
|
+ };
|
|
|
+ lastData.rationGljList.push(tmpRationGlj);
|
|
|
+ if (gljCodeList.indexOf(tmp[1]) < 0) {
|
|
|
+ gljCodeList.push(tmp[1]);
|
|
|
+ }
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (tmp[0] === '定额' && Object.keys(lastData).length > 0) {
|
|
|
+ rationData.push(lastData);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 组装数据
|
|
|
+ lastData = {
|
|
|
+ code: tmp[1],
|
|
|
+ name: tmp[2],
|
|
|
+ unit: tmp[3],
|
|
|
+ caption: tmp[2],
|
|
|
+ rationRepId: rationRepId,
|
|
|
+ rationGljList: []
|
|
|
+ };
|
|
|
+ // 防止重复加入
|
|
|
+ if (rationCodeList.indexOf(tmp[1]) < 0) {
|
|
|
+ rationCodeList.push(tmp[1]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 最后一个入数组
|
|
|
+ rationData.push(lastData);
|
|
|
+
|
|
|
+ // 查找数据库中是否已存在待插入数据
|
|
|
+ const condition = {
|
|
|
+ rationRepId: rationRepId,
|
|
|
+ code: { $in: rationCodeList}
|
|
|
+ };
|
|
|
+ const existCodeList = await this.getRationItemByCondition(condition, ['code'], 'code');
|
|
|
+ // 过滤插入数据
|
|
|
+ let insertData = [];
|
|
|
+ for (const ration of rationData) {
|
|
|
+ if (existCodeList[ration.code] !== undefined) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ insertData.push(ration);
|
|
|
+ }
|
|
|
+ // 如果都已经存在,直接返回
|
|
|
+ if (insertData.length <= 0) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 组织id
|
|
|
+ const counterInfo = await counter.counterDAO.getIDAfterCount(counter.moduleName.rations, insertData.length);
|
|
|
+ let maxId = counterInfo.value.sequence_value;
|
|
|
+ maxId = parseInt(maxId);
|
|
|
+
|
|
|
+ let count = 0;
|
|
|
+ for (const index in insertData) {
|
|
|
+ insertData[index].ID = maxId - (insertData.length - 1) + count;
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ // 插入数据库
|
|
|
+ const result = await rationItemModel.create(insertData);
|
|
|
+ console.log(result);
|
|
|
+};
|
|
|
|
|
|
-module.exports = new rationItemDAO()
|
|
|
+module.exports = new rationItemDAO();
|
|
|
|