glj_list_model.js 9.2 KB

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