glj_list_model.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /**
  2. * 项目工料机列表数据模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/22
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model";
  9. import {default as GLJSchemas, collectionName as gljCollectionName} from "./schemas/glj";
  10. import CounterModel from "./counter_model";
  11. import UnitPriceModel from "./unit_price_model";
  12. import UnitPriceFileModel from "./unit_price_file_model";
  13. class GLJListModel extends BaseModel {
  14. /**
  15. * 材料类型id
  16. *
  17. * @var {Array}
  18. */
  19. materialIdList = [5, 6, 7];
  20. /**
  21. * 构造函数
  22. *
  23. * @return {void}
  24. */
  25. constructor() {
  26. let parent = super();
  27. parent.model = GLJSchemas;
  28. parent.init();
  29. }
  30. /**
  31. * 设置场景
  32. *
  33. * @param {string} scene
  34. * @return {void}
  35. */
  36. setScene(scene = '') {
  37. switch (scene) {
  38. // 新增数据的验证规则
  39. case 'add':
  40. this.model.schema.path('glj_repository_id').required(true);
  41. this.model.schema.path('project_id').required(true);
  42. this.model.schema.path('code').required(true);
  43. this.model.schema.path('name').required(true);
  44. break;
  45. }
  46. }
  47. /**
  48. * 根据标段对应工料机数据
  49. *
  50. * @param {Number} projectId
  51. * @return {Promise}
  52. */
  53. async getListByProjectId(projectId) {
  54. let gljData = null;
  55. try {
  56. // 首先获取对应标段下所有的项目工料机数据
  57. let condition = {project_id: projectId};
  58. let fields = {_id: 0};
  59. gljData = await this.db.find(condition, fields);
  60. // 没有数据则直接返回空
  61. if (gljData.length <= 0) {
  62. throw '无数据';
  63. }
  64. // 获取标段对应的单价文件id
  65. let unitPriceFileModel = new UnitPriceFileModel();
  66. let unitPriceFile = await unitPriceFileModel.getDataByProject(projectId);
  67. if (!unitPriceFile) {
  68. throw '没有对应的单价文件';
  69. }
  70. let unitPriceFileId = unitPriceFile.id;
  71. // 获取标段设置的单价文件数据
  72. let unitPriceModel = new UnitPriceModel();
  73. let unitPriceList = await unitPriceModel.getDataByFileId(unitPriceFileId);
  74. // 组合数据
  75. this.combineUnitPrice(gljData, unitPriceList);
  76. // 排序
  77. gljData.sort(function (a, b) {
  78. return a.unit_price.type - b.unit_price.type;
  79. });
  80. } catch (error) {
  81. console.log("glj_list_model:" + error);
  82. gljData = [];
  83. }
  84. return gljData;
  85. }
  86. /**
  87. * 组合工料机数据和单价文件数据
  88. *
  89. * @param {object} gljList
  90. * @param {object} unitPriceList
  91. * @return {void}
  92. */
  93. combineUnitPrice(gljList, unitPriceList) {
  94. // @todo 以后从js获取?
  95. let labour = 2;
  96. let machine = 64;
  97. // 循环组合数据
  98. for(let glj of gljList) {
  99. if (glj.code === undefined) {
  100. continue;
  101. }
  102. glj.unit_price = unitPriceList[glj.code + glj.name] !== undefined ? unitPriceList[glj.code + glj.name] : null;
  103. // 计算调整基价
  104. switch (glj.type + '') {
  105. // 人工: 调整基价=基价单价*调整系数
  106. case labour:
  107. glj.adjust_price = glj.adjustment * glj.unit_price.base_price;
  108. break;
  109. // 机械类型的算法
  110. case machine:
  111. console.log('机械');
  112. break;
  113. // 材料、主材、设备
  114. default:
  115. glj.adjust_price = glj.unit_price.base_price;
  116. }
  117. }
  118. }
  119. /**
  120. * 新增项目工料机数据(包括新增单价文件) 定额工料机新增时调用
  121. *
  122. * @param {object} data
  123. * @return {Promise} 返回插入成功的数据id
  124. */
  125. async addList(data) {
  126. let result = null;
  127. try {
  128. if (Object.keys(data).length <= 0) {
  129. throw '新增数据为空';
  130. }
  131. // 首先查找是否有同编码同名称的工料机数据
  132. let projectGljData = await this.findDataByCondition({code: data.code, project_id: data.project_id});
  133. // 如果找不到数据则新增
  134. if (!projectGljData) {
  135. // 新增单条记录 (两个操作本来应该是事务操作,然而mongodb事务支持比较弱,就当作是都可以顺利执行)
  136. let gljInsertData = await this.add(data);
  137. if (!gljInsertData) {
  138. throw '新增项目工料机失败!';
  139. }
  140. projectGljData = gljInsertData;
  141. }
  142. // 获取标段对应的单价文件id
  143. let unitPriceFileModel = new UnitPriceFileModel();
  144. let unitPriceFile = await unitPriceFileModel.getDataByProject(data.project_id);
  145. if (!unitPriceFile) {
  146. throw '没有对应的单价文件';
  147. }
  148. let unitPriceFileId = unitPriceFile.id;
  149. // 新增单价文件
  150. let unitPriceModel = new UnitPriceModel();
  151. let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
  152. if (!unitPriceInsertData) {
  153. throw '新增单价失败!';
  154. }
  155. projectGljData.unit_price = unitPriceInsertData;
  156. result = projectGljData;
  157. } catch (error) {
  158. console.log(error);
  159. result = null;
  160. }
  161. return result;
  162. }
  163. /**
  164. * 新增单条工料机数据
  165. *
  166. * @param {object} data
  167. * @return {Promise}
  168. */
  169. async add(data) {
  170. if (Object.keys(data).length <= 0) {
  171. throw '新增数据为空';
  172. }
  173. let counterModel = new CounterModel();
  174. data.id = await counterModel.getId(gljCollectionName);
  175. this.setScene('add');
  176. let result = await this.db.create(data);
  177. return result;
  178. }
  179. /**
  180. * 根据工料机id修改市场单价
  181. *
  182. * @param {Object} updateData
  183. * @return {Promise}
  184. */
  185. async modifyMarketPrice(updateData) {
  186. let result = {};
  187. try {
  188. if (updateData.code === undefined || updateData.market_price === undefined ||
  189. updateData.name === undefined || updateData.project_id === undefined) {
  190. throw '参数有误!';
  191. }
  192. // 先查是否有对应code的数据
  193. let gljListData = await this.findDataByCondition({code: updateData.code,
  194. project_id: updateData.project_id}, {_id: 0}, false);
  195. if (!gljListData) {
  196. throw '不存在对应code数据';
  197. }
  198. // 获取标段对应的单价文件id
  199. let unitPriceFileModel = new UnitPriceFileModel();
  200. let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
  201. if (!unitPriceFile) {
  202. throw '没有对应的单价文件';
  203. }
  204. let unitPriceFileId = unitPriceFile.id;
  205. let unitPriceModel = new UnitPriceModel();
  206. let gljCount = gljListData.length;
  207. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId, gljCount);
  208. // 判断是否已存在对应数据
  209. let includeField = [
  210. {field: 'name', value: unitPriceData.name}
  211. ];
  212. let gljIndex = this.isIncluded(gljListData, includeField);
  213. let gljData = isAdd ? {} : gljListData[gljIndex];
  214. // 如果单价数据新增则工料机也需要新增
  215. if (isAdd) {
  216. // 如果没有对应的记录则新增一条工料机数据,并更改name
  217. let regular = /\(\d\)/;
  218. let changeString = '(' + gljCount + ')';
  219. updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
  220. updateData.name + changeString;
  221. // 获取第一条数据作为数据源
  222. let originalData = gljListData[0];
  223. // 更改名称
  224. originalData.name = updateData.name;
  225. originalData = JSON.stringify(originalData);
  226. gljData = await this.add(JSON.parse(originalData));
  227. if (!gljData) {
  228. throw '新增工料机数据失败!';
  229. }
  230. }
  231. gljData.unit_price = unitPriceData;
  232. result = gljData;
  233. } catch (error) {
  234. console.log(error);
  235. result = {};
  236. }
  237. return result;
  238. }
  239. /**
  240. * 判断数据中是否包含某个数据
  241. *
  242. * @param {Array} data
  243. * @param {Array} includeField
  244. * @return {Number}
  245. */
  246. isIncluded(data, includeField) {
  247. let index = -1;
  248. if (data.length <= 0) {
  249. return index;
  250. }
  251. for(let tmp in data) {
  252. let counter = 0;
  253. for (let includeTmp of includeField) {
  254. if (data[tmp][includeTmp.field] === includeTmp.value) {
  255. counter++;
  256. }
  257. }
  258. if (counter === includeField.length) {
  259. index = tmp;
  260. break;
  261. }
  262. }
  263. return index;
  264. }
  265. }
  266. export default GLJListModel;