glj_list_model.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. /**
  2. * 项目工料机列表数据模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/22
  6. * @version
  7. */
  8. import mongoose from "mongoose";
  9. import BaseModel from "../../common/base/base_model";
  10. import CounterModel from "./counter_model";
  11. import UnitPriceModel from "./unit_price_model";
  12. import UnitPriceFileModel from "./unit_price_file_model";
  13. import GLJTypeConst from "../../common/const/glj_type_const";
  14. import RationGLJFacade from "../../ration_glj/facade/ration_glj_facade";
  15. import STDGLJLibGLJListModel from "../../common/std/std_glj_lib_glj_list_model";
  16. import MixRatioModel from "./mix_ratio_model";
  17. import GljModel from "../../complementary_glj_lib/models/gljModel";
  18. const ProjectModel = require('../../pm/models/project_model').project;
  19. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  20. let gljUtil = require('../../../public/gljUtil');
  21. import decimal_facade from "../../main/facade/decimal_facade";
  22. let gljCollectionName = 'glj_list';
  23. let GLJSchemas = mongoose.model(gljCollectionName);
  24. let _ = require("lodash");
  25. let vvTaxModel = mongoose.model("std_vehicleVesselTax_items");
  26. class GLJListModel extends BaseModel {
  27. /**
  28. * 材料、主材、设备类型id
  29. *
  30. * @var {Array}
  31. */
  32. materialIdList = [GLJTypeConst.GENERAL_MATERIAL, GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
  33. GLJTypeConst.COMMERCIAL_CONCRETE, GLJTypeConst.COMMERCIAL_MORTAR, GLJTypeConst.MAIN_MATERIAL,
  34. GLJTypeConst.EQUIPMENT];
  35. /**
  36. * 拥有组成物的工料机类型id
  37. *
  38. * @var {Array}
  39. */
  40. ownCompositionTypes = [GLJTypeConst.CONCRETE, GLJTypeConst.MORTAR, GLJTypeConst.MIX_RATIO,
  41. GLJTypeConst.GENERAL_MACHINE,GLJTypeConst.MAIN_MATERIAL,GLJTypeConst.INSTRUMENT];
  42. /**
  43. * 构造函数
  44. *
  45. * @return {void}
  46. */
  47. constructor() {
  48. let parent = super();
  49. parent.model = GLJSchemas;
  50. parent.init();
  51. }
  52. /**
  53. * 设置场景
  54. *
  55. * @param {string} scene
  56. * @return {void}
  57. */
  58. setScene(scene = '') {
  59. switch (scene) {
  60. // 新增数据的验证规则
  61. case 'add':
  62. this.model.schema.path('glj_id').required(true);
  63. this.model.schema.path('project_id').required(true);
  64. this.model.schema.path('code').required(true);
  65. // this.model.schema.path('name').required(true);
  66. break;
  67. }
  68. }
  69. /**
  70. * 根据标段对应项目工料机列表
  71. *
  72. * @param {Number} projectId
  73. * @param {Number} unitPriceFileId
  74. * @return {Promise}
  75. */
  76. async getListByProjectId(projectId, unitPriceFileId,freightList,originalList) {
  77. let gljData = null;
  78. /*let decimal =await decimal_facade.getProjectDecimal(projectId);
  79. let quantity_decimal = decimal&&decimal.glj.quantity?decimal.glj.quantity:6;//取消耗量保留小数据位,默认6位*/
  80. let mixRatioConnectData = {};
  81. let mixRationMap={};
  82. let keyMap={};
  83. let unitPriceList={};
  84. try {
  85. // 首先获取对应标段下所有的项目工料机数据
  86. let condition = {project_id: projectId};
  87. let fields = {_id: 0};
  88. gljData = await this.model.find(condition, fields).lean();
  89. // 整理获取有组成物的项目工料机的数据
  90. let connect_keys = [];
  91. for(let tmp of gljData) {
  92. let c_key = this.getIndex(tmp,['code','name','specs','unit','type']);
  93. //把有组成物类型的连接字段挑出来
  94. if(this.ownCompositionTypes.indexOf(tmp.type)!=-1){
  95. connect_keys.push(c_key);
  96. }
  97. keyMap[c_key] = tmp; //工料机连接key和工料机的对照表;
  98. }
  99. //检查自采材料和综合电价功能在共享单价文件时,项目文件中可能会缺少项目工料机的情况,如果缺少,则自动插入
  100. if(freightList && freightList.length > 0) await this.materialCalcProjectGLJChecking(freightList,keyMap,gljData,connect_keys,projectId,unitPriceFileId);
  101. if(originalList && originalList.length > 0) await this.materialCalcProjectGLJChecking(originalList,keyMap,gljData,connect_keys,projectId,unitPriceFileId);
  102. // 没有数据则直接返回空
  103. if (gljData.length <= 0) {
  104. throw '无数据';
  105. }
  106. // 获取标段设置的单价文件数据
  107. let unitPriceModel = new UnitPriceModel();
  108. unitPriceList = await unitPriceModel.getDataByFileId(unitPriceFileId);
  109. //为了提高性成计算消耗量功能改成了在前端计算
  110. // 查找组成物的消耗量
  111. let totalComposition = {};
  112. let mixRatioData = {};
  113. if (connect_keys.length > 0) {
  114. let mixRatioModel = new MixRatioModel();
  115. condition = {connect_key: {"$in": connect_keys}, unit_price_file_id: unitPriceFileId};
  116. let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
  117. for (let tmp of mixRatioList) {
  118. let t_index = tmp.connect_key;
  119. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  120. if (mixRatioData[t_index] !== undefined) {
  121. mixRatioData[t_index].push(tmp);
  122. } else {
  123. mixRatioData[t_index] = [tmp];
  124. }
  125. if(mixRationMap[t_index]!=undefined){
  126. mixRationMap[t_index].push(tmp);
  127. }else {
  128. mixRationMap[t_index]=[tmp];
  129. }
  130. if (mixRatioConnectData[m_index] !== undefined) {
  131. mixRatioConnectData[m_index].push(tmp.connect_key);
  132. } else {
  133. mixRatioConnectData[m_index] = [tmp.connect_key];
  134. }
  135. }
  136. }
  137. // 组合单价数据
  138. gljData = this.combineData(gljData, unitPriceList, {}, mixRatioData, totalComposition);
  139. // 排序
  140. gljData.sort(function (a, b) {
  141. a.unit_price = a.unit_price === null ? 0 : a.unit_price;
  142. b.unit_price = b.unit_price === null ? 0 : b.unit_price;
  143. return a.unit_price.type - b.unit_price.type;
  144. });
  145. } catch (error) {
  146. console.log("glj_list_model:" + error);
  147. gljData = [];
  148. }
  149. return [gljData, mixRatioConnectData,mixRationMap,unitPriceList];
  150. }
  151. async materialCalcProjectGLJChecking(materialList,keyMap,gljData,connect_keys,projectId,unitPriceFileId){
  152. let missGLJs = [];
  153. for(let m of materialList){
  154. if(m.ration_gljs){
  155. for(let rg of m.ration_gljs){
  156. let key = this.getIndex(rg);
  157. if(keyMap[key]) continue; //如果已经存在,则跳过,不用添加
  158. missGLJs.push(rg)
  159. }
  160. }
  161. }
  162. if(missGLJs.length > 0) await this.handleMissGLJs(missGLJs,keyMap,gljData,connect_keys,projectId,unitPriceFileId)
  163. }
  164. //处理综合电价或材料计算共享的时候项目工料机缺少的情况
  165. async handleMissGLJs(missGLJs,keyMap,gljData,connect_keys,projectId,unitPriceFileId){
  166. let new_connect_keys = [];
  167. let oldProjectIDMap = {};//记录来源的项目工料机对应的项目编号
  168. let projectGLJIDList = [];
  169. let key_to_projectIDMap={};//记录5大项key对应来源的项目工料机ID
  170. for(let rg of missGLJs){
  171. projectGLJIDList.push(rg.projectGLJID);
  172. if(this.ownCompositionTypes.indexOf(rg.type)!=-1){//有工料机的类型还要检查组成物是否存在
  173. let newkey = this.getIndex(rg);
  174. new_connect_keys.push(newkey);
  175. connect_keys.push(newkey);
  176. key_to_projectIDMap[newkey] = rg.projectGLJID;
  177. }
  178. }
  179. let oldProjectGLJs = await this.model.find({'id':{'$in':projectGLJIDList}},'-_id').lean();
  180. let counterModel = new CounterModel();
  181. //处理缺少的项目工料机
  182. for(let g of oldProjectGLJs){
  183. oldProjectIDMap[g.id] = g.project_id;
  184. g.id = await counterModel.getId(gljCollectionName);
  185. g.project_id = projectId;
  186. keyMap[this.getIndex(g)] = g;
  187. }
  188. //处理缺少的项目工料机中的组成物
  189. if(new_connect_keys.length > 0){
  190. let mixRatioModel = new MixRatioModel();
  191. let e_mList = await mixRatioModel.model.find({unit_price_file_id: unitPriceFileId,connect_key: {'$in':new_connect_keys}}).lean();
  192. let p_m = {};//以项目工料机来分组
  193. for(let m of e_mList) {
  194. let mkey = this.getIndex(m);
  195. if (keyMap[mkey]) continue; //如果已经存在了,不用再增加
  196. let oldPID = key_to_projectIDMap[m.connect_key];
  197. if (p_m[oldPID]) {
  198. p_m[oldPID].push(m);
  199. } else {
  200. p_m[oldPID] = [m];
  201. }
  202. keyMap[mkey] = m;//处理过了就添加,避免重复
  203. }
  204. //按组来处理组成物信息
  205. for(let t_pid in p_m){
  206. let codeList = [];
  207. let mkeyMap = [];
  208. for(let m of p_m[t_pid]){
  209. codeList.push(m.code);
  210. mkeyMap[this.getIndex(m)] = m;
  211. }
  212. let projectID = oldProjectIDMap[t_pid];
  213. let m_projectGLJs = await this.model.find({'project_id':projectID,'code':{'$in':codeList}},'-_id').lean();
  214. for(let mg of m_projectGLJs){
  215. if(mkeyMap[this.getIndex(mg)]){//过滤掉单按编号不准确的工料机
  216. mg.id = await counterModel.getId(gljCollectionName);
  217. mg.project_id = projectId;
  218. oldProjectGLJs.push(mg);
  219. }
  220. }
  221. }
  222. }
  223. if(oldProjectGLJs.length > 0){
  224. await this.db.create(oldProjectGLJs);
  225. for(let o of oldProjectGLJs){
  226. gljData.push(o);
  227. }
  228. }
  229. }
  230. /**
  231. * 组合工料机数据和单价文件数据
  232. *
  233. * @param {object} gljList
  234. * @param {object} unitPriceList
  235. * @param {object} quantityList
  236. * @param {object} mixRatioData 组合物明细数据
  237. * @param {object} totalComposition 组合物父工料机统计数据
  238. * @return {Array}
  239. */
  240. combineData(gljList, unitPriceList, quantityList = {}, mixRatioData = {}, totalComposition = {}) {
  241. // 整理组成物消耗量(只有在总列表显示的时候才需用到,获取单项项目工料机内容则忽略)
  242. let compositionConsumption = {};
  243. if (Object.keys(mixRatioData).length > 0 && Object.keys(totalComposition).length > 0) {
  244. for(let index in mixRatioData) {
  245. for(let tmp of mixRatioData[index]) {
  246. let t_index = this.getIndex(tmp,['code','name','specs','unit','type']);//取做为组成物的工料机的总消耗量
  247. compositionConsumption[t_index] = compositionConsumption[t_index] === undefined ? tmp.consumption :
  248. compositionConsumption[t_index] + tmp.consumption;
  249. }
  250. }
  251. }
  252. let result = [];
  253. // 循环组合数据
  254. for(let index in gljList) {
  255. let glj = gljList[index];
  256. if (glj.code === undefined) {
  257. continue;
  258. }
  259. let g_index = this.getIndex(glj,['code','name','specs','unit','type']);
  260. glj.unit_price = unitPriceList !== null && unitPriceList[g_index] !== undefined ? unitPriceList[g_index] : null;
  261. if (glj.unit_price === null) {
  262. continue;
  263. }
  264. let gljId = glj.glj_id + '';
  265. let projectGljId = glj.id + '';
  266. glj.quantity = quantityList[g_index]?quantityList[g_index]:0;
  267. // 组成物数据
  268. gljList[index].ratio_data = mixRatioData[g_index] !== undefined ? mixRatioData[g_index] : [];
  269. //因为schema中设置base_price 为string 类型,所以要通过中间变量转换为数字再做计算,不然会自动变成字符串类型
  270. this.getGLJPrice(glj);
  271. result.push(glj);
  272. }
  273. return result;
  274. }
  275. getGLJPrice(glj){
  276. let glj_basePrice = parseFloat(glj.unit_price.base_price);
  277. glj.unit_price.base_price = glj_basePrice;
  278. glj.unit_price.market_price = parseFloat(glj.unit_price.market_price);
  279. }
  280. /**
  281. * 新增项目工料机数据(包括新增单价文件) 定额工料机新增时调用
  282. *
  283. * @param {object} data
  284. * @return {Promise} 返回插入成功的数据id
  285. */
  286. async addList(data,unitFileId,ext) {
  287. let result = null;
  288. try {
  289. if (Object.keys(data).length <= 0) {
  290. throw '新增数据为空';
  291. }
  292. let condition={
  293. code: data.code,
  294. project_id: data.project_id,
  295. name:data.name,
  296. specs:data.specs,
  297. type:data.type,
  298. unit:data.unit
  299. };
  300. let projectGljData = await this.findDataByCondition(condition);
  301. let isAddProjectGLJ = false;
  302. // 获取标段对应的单价文件id
  303. let unitPriceFileId = unitFileId?unitFileId:await ProjectModel.getUnitPriceFileId(data.project_id);
  304. if (unitPriceFileId <= 0) {
  305. throw '没有对应的单价文件';
  306. }
  307. let unitPriceModel = new UnitPriceModel();
  308. // 新增单条记录 (两个操作本来应该是事务操作,然而mongodb事务支持比较弱,就当作是都可以顺利执行)
  309. // 如果找不到数据则新增
  310. if (!projectGljData) {
  311. let gljInsertData = await this.add(data);
  312. if (!gljInsertData) {
  313. throw '新增项目工料机失败!';
  314. }
  315. isAddProjectGLJ = true;
  316. projectGljData = gljInsertData;
  317. }
  318. let CompositionGLJ=[];
  319. // 判断类型,如果是混凝土、砂浆、配合比或者主材则查找对应的组成物(前提是没有对应的项目工料机数据)
  320. if(this.ownCompositionTypes.indexOf(data.type)!=-1) {
  321. //如果是新增
  322. if(isAddProjectGLJ ){
  323. await this.compositionInit(data, unitPriceFileId,ext);
  324. }
  325. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  326. if(isAddProjectGLJ==false&&CompositionGLJ.length==0){//如果不是新增,并且是有组成物的类型但又没有发现组成物的情况下,有可能是错误数据,重新在库中查找一下组成物,有则插入
  327. await this.compositionInit(data, unitPriceFileId,ext);
  328. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  329. if(CompositionGLJ.length>0){//如果这次发现又有组成物了,则把旧的单价数据删除,在后面的操作中会重新增加
  330. let de_condition ={unit_price_file_id: unitPriceFileId,code:data.code,name:data.name,unit:data.unit,type:data.type};
  331. data.specs!=null&&data.specs!=undefined&&data.specs!=""?de_condition.specs = data.specs:de_condition;
  332. await unitPriceModel.db.delete(de_condition);
  333. }
  334. }
  335. }
  336. projectGljData._doc.subList=CompositionGLJ;
  337. // 新增单价文件
  338. let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
  339. if (!unitPriceInsertData) {
  340. throw '新增单价失败!';
  341. }
  342. projectGljData.unit_price = unitPriceInsertData;
  343. result = projectGljData;
  344. } catch (error) {
  345. console.log(error);
  346. result = null;
  347. }
  348. return result;
  349. }
  350. /**
  351. * 新增单条工料机数据
  352. *
  353. * @param {object} data
  354. * @return {Promise}
  355. */
  356. async add(data) {
  357. if (Object.keys(data).length <= 0) {
  358. throw '新增数据为空';
  359. }
  360. let counterModel = new CounterModel();
  361. if (data instanceof Array) {
  362. // 如果是批量新增
  363. for(let tmp in data) {
  364. data[tmp].id = await counterModel.getId(gljCollectionName);
  365. }
  366. } else {
  367. data.id = await counterModel.getId(gljCollectionName);
  368. }
  369. let result = await this.db.create(data);
  370. return result;
  371. }
  372. /**
  373. * 修改名称、规格型号、单位、市场单价等
  374. * @param data
  375. * @returns {Promise.<void>}
  376. */
  377. async modifyGLJ(data,ration_glj){
  378. let unitPriceFileModel = new UnitPriceFileModel();
  379. let unitPriceFile = await unitPriceFileModel.getDataByProject(data.project_id);
  380. if (!unitPriceFile) {
  381. throw '没有对应的单价文件';
  382. }
  383. //查找单价信息,有则返回,没有则新增并返回
  384. let unitPriceFileId = unitPriceFile.id;
  385. let unitPriceModel = new UnitPriceModel();
  386. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId,"modify");
  387. let gljData=null;
  388. if(isAdd){ //如果是新增,则新增一条新的项目工料机
  389. data.code = unitPriceData.code;
  390. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  391. }else { //如果不是新增,则查找是否有对应的项目工料机,有则返回,没有则新增
  392. let condition = {
  393. project_id:data.project_id,
  394. original_code: data.original_code,
  395. name:data.name,
  396. specs:data.specs,
  397. type:data.type,
  398. unit:data.unit
  399. }
  400. let gljList = await this.findDataByCondition(condition,{_id: 0},false);
  401. if(gljList&&gljList.length>0){
  402. for(let tem of gljList){
  403. if(tem.code == unitPriceData.code){
  404. gljData = tem;
  405. }
  406. }
  407. }
  408. if(gljData==null){
  409. data.code = unitPriceData.code;
  410. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  411. }
  412. }
  413. gljData.unit_price = unitPriceData;
  414. return gljData
  415. }
  416. //修改属性后插入项目工料机
  417. async insertGLJWhenIsAdd(glj,ration_glj,unitPriceFileId){
  418. //新增项目工料机
  419. let gljData = await this.add(glj);
  420. //查看是否是有配合比的工料机类型
  421. if(this.ownCompositionTypes.indexOf(glj.type)!=-1){
  422. // 配合比数据插入
  423. let key_array = [];
  424. if(ration_glj.subType){//定额类型的工料机和定额工料机的key有点不同subType
  425. key_array= ['code','name','specs','unit','subType'];
  426. }else {
  427. key_array= ['code','name','specs','unit','type'];
  428. }
  429. let connect_key =this.getIndex(ration_glj,key_array);
  430. let connect_key_n =this.getIndex(glj,['code','name','specs','unit','type']);
  431. //先查找配合比数据是否已经存在
  432. let mixRatioModel = new MixRatioModel();
  433. let mixRatios_o = await mixRatioModel.findDataByCondition({connect_key: connect_key_n, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  434. if(mixRatios_o&&mixRatios_o.length>0){//已经存在,不用再插入配合比数据,直接返回
  435. return gljData;
  436. }
  437. //不存在,则查找原始的配合比数据,并为新工料机增加配合比数据
  438. let mixRatios = await mixRatioModel.findDataByCondition({connect_key: connect_key, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  439. let mixInsertResult ={};
  440. if(mixRatios&&mixRatios.length>0){
  441. let newMixRatioData = [];
  442. for(let m of mixRatios){
  443. if(m.code == '80CCS') continue; //忽略原来的组成物信息,重新查看有没有匹配的
  444. let tem ={
  445. consumption: m.consumption,
  446. glj_id: m.glj_id,
  447. unit_price_file_id: m.unit_price_file_id,
  448. connect_key: connect_key_n,
  449. type: m.type,
  450. code: m.code,
  451. specs:m.specs,
  452. name:m.name,
  453. unit:m.unit
  454. };
  455. newMixRatioData.push(tem);
  456. }
  457. if(glj.type == 301){//机械台班添加车船税
  458. let newM = await this.getCCSData(glj,unitPriceFileId);
  459. if(newM) newMixRatioData.push(newM);
  460. }
  461. mixInsertResult= await mixRatioModel.add(newMixRatioData);
  462. }
  463. }
  464. return gljData;
  465. }
  466. /**
  467. * 根据工料机id修改市场单价
  468. *
  469. * @param {Object} updateData
  470. * @return {Promise}
  471. */
  472. async modifyMarketPrice(updateData) {
  473. let result = {};
  474. try {
  475. if (updateData.code === undefined || updateData.market_price === undefined ||
  476. updateData.name === undefined || updateData.project_id === undefined) {
  477. throw '参数有误!';
  478. }
  479. // 先查是否有对应code的数据
  480. let gljListData = await this.findDataByCondition({code: updateData.code,
  481. project_id: updateData.project_id}, {_id: 0}, false);
  482. if (!gljListData) {
  483. throw '不存在对应code数据';
  484. }
  485. // 获取标段对应的单价文件id
  486. let unitPriceFileModel = new UnitPriceFileModel();
  487. let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
  488. if (!unitPriceFile) {
  489. throw '没有对应的单价文件';
  490. }
  491. let unitPriceFileId = unitPriceFile.id;
  492. let unitPriceModel = new UnitPriceModel();
  493. let gljCount = gljListData.length;
  494. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId,"modify", gljCount);
  495. // 判断是否已存在对应数据
  496. let includeField = [
  497. {field: 'name', value: unitPriceData.name}
  498. ];
  499. let gljIndex = this.isIncluded(gljListData, includeField);
  500. let gljData = isAdd ? {} : gljListData[gljIndex];
  501. // 如果单价数据新增则工料机也需要新增
  502. if (isAdd) {
  503. // 如果没有对应的记录则新增一条工料机数据,并更改name
  504. let regular = /\(\d+\)/;
  505. let changeString = '(' + gljCount + ')';
  506. updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
  507. updateData.name + changeString;
  508. // 获取第一条数据作为数据源
  509. let originalData = gljListData[0];
  510. // 更改名称
  511. originalData.name = updateData.name;
  512. originalData = JSON.stringify(originalData);
  513. gljData = await this.add(JSON.parse(originalData));
  514. if (!gljData) {
  515. throw '新增工料机数据失败!';
  516. }
  517. }
  518. gljData.unit_price = unitPriceData;
  519. result = gljData;
  520. } catch (error) {
  521. console.log(error);
  522. result = {};
  523. }
  524. return result;
  525. }
  526. /**
  527. * 判断数据中是否包含某个数据
  528. *
  529. * @param {Array} data
  530. * @param {Array} includeField
  531. * @return {Number}
  532. */
  533. isIncluded(data, includeField) {
  534. let index = -1;
  535. if (data.length <= 0) {
  536. return index;
  537. }
  538. for(let tmp in data) {
  539. let counter = 0;
  540. for (let includeTmp of includeField) {
  541. if (data[tmp][includeTmp.field] === includeTmp.value) {
  542. counter++;
  543. }
  544. }
  545. if (counter === includeField.length) {
  546. index = tmp;
  547. break;
  548. }
  549. }
  550. return index;
  551. }
  552. //
  553. async getCCSData(data,unitFileId){
  554. let unitPriceFileModel = new UnitPriceFileModel();
  555. let unitFileInfo = await unitPriceFileModel.findDataByCondition({id: unitFileId});
  556. if(unitFileInfo.vvTaxFileID && unitFileInfo.vvTaxFileID!=""){
  557. let condition = {libID:unitFileInfo.vvTaxFileID,code:data.code,name:data.name,type:data.type,unit:data.unit,specs:data.specs};
  558. let item = await vvTaxModel.findOne(condition);
  559. if(item){
  560. return gljUtil.getBaseCCSMixRatio(unitFileId,item.vehicleVesselTax,gljUtil.getIndex(data))
  561. }
  562. }
  563. return null;
  564. }
  565. /**
  566. * 工料机中组成物操作
  567. * 该方法只在确保没有对应项目工料机的时候才会调用
  568. *
  569. * @param {Object} data
  570. * @param {Number} projectId
  571. * @return {void}
  572. */
  573. async compositionInit(data, unitPriceFileId,ext) {
  574. let gljId = data.glj_id === undefined ? 0 : data.glj_id;
  575. let projectId = data.project_id === undefined ? 0 : data.project_id;
  576. let mixRatioModel = new MixRatioModel();
  577. if (gljId === 0 || projectId === 0) {
  578. throw '参数错误';
  579. }
  580. let fromTable = data.from === undefined ? 'std' : data.from;
  581. var connect_key =this.getIndex(data,['code','name','specs','unit','type']);
  582. let e_mList = await this.getCompositionGLJListFromMixRatio(projectId,unitPriceFileId,connect_key,fromTable);
  583. //如果组成物数据已经存在,则以单价文件中组成物的数据为准,不用再往下做判断了(项目工料机中需要插入的已经在上一步中生成插入)
  584. if(e_mList.length > 0) return;
  585. // 查找对应组成物的项目工料机数据
  586. let indexs=['code','name','specs','unit','type'];
  587. let [projectGljList, compositionGljList] = await this.getCompositionGLJList(gljId, projectId, indexs, fromTable);
  588. // 整理配合比待插入数据,单价文件中配合比没有数据,则以库中的组成物信息为准
  589. let mixRatioInsertData = [];
  590. for (let tmp of compositionGljList) {
  591. let consumpiton = tmp.consumption;
  592. //只有标准的工料机的组成物才会有多单价、多组成物消耗量的情况 fromTable
  593. if(fromTable == 'std' && ext && ext.quantityField &&( tmp.consumption[ext.quantityField]!= undefined && tmp.consumption[ext.quantityField]!=null)){
  594. consumpiton = tmp.consumption[ext.quantityField];
  595. }
  596. // 配合比数据插入
  597. let mixRatioData = {
  598. consumption: consumpiton,
  599. glj_id: tmp.ID,
  600. unit_price_file_id: unitPriceFileId,
  601. connect_key: connect_key,
  602. type: tmp.gljType,
  603. code: tmp.code,
  604. specs:tmp.specs?tmp.specs:"",
  605. name:tmp.name,
  606. unit:tmp.unit?tmp.unit:''
  607. };
  608. mixRatioInsertData.push(mixRatioData);
  609. }
  610. //如果有车船税则加入到配合比表中 -- 先判断机械类型吧,所有都插入影响性能
  611. if(data.type == 301){//如果是机械台班,看有没有车船税 - modifyGLJ方法里也会有添加组成物,修改这里逻辑时记得同步修改。
  612. let newM = await this.getCCSData(data,unitPriceFileId);
  613. if(newM) mixRatioInsertData.push(newM);
  614. }
  615. let unitPriceModel = new UnitPriceModel();
  616. let unitPriceList = await unitPriceModel.model.find({unit_price_file_id:unitPriceFileId});//查找按文件id查找单价文件数据,用来判断单价文件信息是否已存在
  617. // 整理插入的数据
  618. let gljInsertData = [];
  619. let unitPriceInsertData = [];
  620. for(let tmp of compositionGljList) {
  621. let key = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  622. if (projectGljList[key] === undefined) {
  623. // 项目工料机插入的数据
  624. let gljData = this.getProjectGLJNewData(tmp,projectId);
  625. gljInsertData.push(gljData);
  626. }
  627. let basePrice = tmp.basePrice;
  628. //只有标准的工料机的组成物才会有多单价、多组成物消耗量的情况 fromTable
  629. if(fromTable == 'std' && ext && ext.priceField &&( tmp.priceProperty[ext.priceField]!= undefined && tmp.priceProperty[ext.priceField]!=null)){
  630. basePrice = tmp.priceProperty[ext.priceField];
  631. }
  632. basePrice = scMathUtil.roundTo(basePrice,-6);
  633. // 单价文件插入的数据
  634. let unitPriceData = {
  635. base_price: basePrice,
  636. // 初始市场价=基价
  637. market_price: basePrice,
  638. code: tmp.code,
  639. name: tmp.name,
  640. unit_price_file_id: unitPriceFileId,
  641. type: tmp.gljType,
  642. short_name: tmp.shortName === undefined ? '' : tmp.shortName,
  643. glj_id: tmp.ID,
  644. specs: tmp.specs?tmp.specs:'',
  645. unit: tmp.unit === undefined ? '' : tmp.unit,
  646. original_code:tmp.code,
  647. grossWeightCoe:tmp.grossWeightCoe,
  648. purchaseStorageRate:tmp.purchaseStorageRate,
  649. offSiteTransportLossRate:tmp.offSiteTransportLossRate,
  650. handlingLossRate:tmp.handlingLossRate
  651. };
  652. if(tmp.from=='cpt'){
  653. unitPriceData.is_add = 1;
  654. }
  655. if(!unitPriceModel.isPropertyInclude(unitPriceList,['code','name','specs','unit','type'],unitPriceData)){
  656. unitPriceInsertData.push(unitPriceData);
  657. }
  658. }
  659. // 整理完后开始插入数据
  660. // 插入单价数据表
  661. if(unitPriceInsertData.length >0) await unitPriceModel.add(unitPriceInsertData);
  662. //插入配合比数据
  663. if(mixRatioInsertData.length>0){
  664. let addMixRatioResult = await mixRatioModel.add(mixRatioInsertData);
  665. if (!addMixRatioResult) {
  666. throw '组成物插入单价数据失败!';
  667. }
  668. }
  669. //插入项目工料机
  670. if(gljInsertData.length > 0) await this.add(gljInsertData);
  671. return
  672. }
  673. getProjectGLJNewData(tmp,projectId){
  674. let gljData = {
  675. glj_id: tmp.ID,
  676. project_id: projectId,
  677. code: tmp.code,
  678. name: tmp.name,
  679. specs: tmp.specs?tmp.specs:'',
  680. unit: tmp.unit === undefined ? '' : tmp.unit,
  681. type: tmp.gljType,
  682. adjCoe:tmp.adjCoe,
  683. original_code:tmp.code,
  684. materialType: tmp.materialType, //三材类别
  685. materialCoe: tmp.materialCoe,
  686. from:tmp.from?tmp.from:"std"
  687. };
  688. return gljData;
  689. }
  690. /**
  691. * 获取组成物具体数据
  692. *
  693. * @param {Number} projectGLJId
  694. * @param {Number} unitPriceFileId
  695. * @return {Promise}
  696. */
  697. async getCompositionList(projectGLJId, unitPriceFileId) {
  698. let result = [];
  699. try {
  700. // 查找对应的项目工料机数据
  701. let projectGLJData = await this.getDataById(projectGLJId,unitPriceFileId);
  702. let allowType = this.ownCompositionTypes;
  703. if (projectGLJData.unit_price === null || allowType.indexOf(projectGLJData.unit_price.type) < 0) {
  704. throw '找不到相关项目工料机';
  705. }
  706. // 查找对应的项目工料机数据配合比,单价数据
  707. let [gljData, mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(projectGLJData, unitPriceFileId);
  708. if (gljData.length <= 0) {
  709. throw '没有对应的组成物项目工料机';
  710. }
  711. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  712. // 排序
  713. gljData.sort(function (a, b) {
  714. return parseInt(a.code) - parseInt(b.code);
  715. });
  716. result = gljData;
  717. } catch (error) {
  718. console.log(error);
  719. result = [];
  720. }
  721. return result;
  722. }
  723. //从配合比表中取组成物的信息以及对应的项目工料机信息,再从配合比数据中反查工料机库中的信息
  724. async getCompositionGLJListFromMixRatio (projectId,unitPriceFileId,connect_key,fromTable = 'std'){
  725. let mixRatioModel = new MixRatioModel();
  726. let e_mList = await mixRatioModel.model.find({unit_price_file_id: unitPriceFileId,connect_key: connect_key}).lean();
  727. if (e_mList.length <= 0) return [];
  728. let codeList = [];
  729. let ratioMap = {};
  730. for(let tmp of e_mList) {
  731. codeList.push(tmp.code);
  732. let ckey = this.getIndex(tmp);
  733. ratioMap[ckey] = tmp
  734. }
  735. // 查找对应的项目工料机数据
  736. let condition = {project_id: projectId,code: {"$in": codeList} };
  737. let gljData = await this.model.find(condition, {_id: 0}).lean();
  738. let matchGljs = {};
  739. for(let g of gljData){
  740. let gkey = this.getIndex(g);
  741. if(ratioMap[gkey]) matchGljs[gkey] = g;
  742. }
  743. //如果有组成物信息存在,但项目工料机信息不全的情况,应从数据库中查存询原始信息
  744. // (只有在父工料机是来自标准的情况下做这个操作,因为组成物表中没有存具体的工料机是来自补充的还是标准库,而父工料机是补充库的,组成物有可能来自两个库,而父来自标准,子只会来自标准)
  745. let lessIDList = [],lessMix=[];//lessMix只有在父工料机是来自补充工料机的时候才要用到
  746. let componentGljData = [];
  747. let gljInsertData = [];
  748. for(let mk in ratioMap){//找出缺少的工料机
  749. if(!matchGljs[mk]){
  750. lessMix.push(ratioMap[mk]);
  751. lessIDList.push(ratioMap[mk].glj_id);
  752. }
  753. }
  754. if(lessIDList.length > 0){
  755. if(fromTable == "std"){
  756. let gljListModel = new STDGLJLibGLJListModel();
  757. let condition = {ID: {$in: lessIDList}};
  758. componentGljData = await gljListModel.model.find(condition).lean();
  759. for(let tmp of componentGljData){
  760. let newProjctData = this.getProjectGLJNewData(tmp,projectId);
  761. gljInsertData.push(newProjctData)
  762. }
  763. }else {
  764. for(let m of lessMix){
  765. m.from = 'cpt';
  766. let newProjctData = this.getProjectGLJNewData(m,projectId);
  767. gljInsertData.push(newProjctData);
  768. }
  769. }
  770. }
  771. //这边只插入项目工料机数据,对于组成物文件中有的数据,理论上单价文件也是应该要有的,如果没有,那只能是mongodb数据库不支持事务的情况的问题
  772. if(gljInsertData.length > 0) await this.add(gljInsertData);
  773. return e_mList;
  774. }
  775. /**
  776. * 获取混凝土等有组成物相关工料机对应的组成物项目工料机数据
  777. *
  778. * @param {Number} gljId
  779. * @param {Number} projectId
  780. * @param {String} indexBy
  781. * @param {String} fromTable
  782. * @return {Promise} 返回组成物工料机数据和组成物列表数据
  783. */
  784. async getCompositionGLJList(gljId, projectId, indexBy = null, fromTable = 'std') {
  785. // 获取对应的组成物数据
  786. let gljListModel = fromTable === 'std' ? new STDGLJLibGLJListModel() : new GljModel();
  787. let componentGljList = await gljListModel.getComponent(gljId);
  788. if (componentGljList.length <= 0) {
  789. return [{},[]];
  790. }
  791. //2019-11-15 优化,用5个属性条件去查意义不大,用编号就能过滤得差不多了,最后再用5个属性去匹配。 同时可以提高查询效率
  792. let codeList = [];
  793. let componentMap = {};
  794. for(let tmp of componentGljList) {
  795. codeList.push(tmp.code);
  796. let ckey = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  797. componentMap[ckey] = tmp
  798. }
  799. // 查找对应的项目工料机数据
  800. let condition = {project_id: projectId,code: {"$in": codeList} };
  801. let gljData = await this.model.find(condition, {_id: 0}).lean();
  802. let matchGljs = {};
  803. for(let g of gljData){
  804. let gkey = this.getIndex(g);
  805. if(componentMap[gkey]) matchGljs[gkey] = g;
  806. }
  807. return [matchGljs, componentGljList];
  808. }
  809. async getCompositionGLJByData(glj,unitPriceFileId){
  810. let [gljData,mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(glj,unitPriceFileId);
  811. let priceNum = unitPriceData?Object.getOwnPropertyNames(unitPriceData).length:0;
  812. let mixNum = mixRatioData?Object.getOwnPropertyNames(mixRatioData).length:0;
  813. if(priceNum !==mixNum){//检查组成物数据是否一致
  814. console.log("组成物数据有误,组成物单价文件个数:"+priceNum+", 组成物个数:"+mixNum);
  815. return []
  816. }
  817. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  818. return gljData;
  819. }
  820. /**
  821. * 反回组成物工料机和配合比数据
  822. * @param glj
  823. * @param unitPriceFileId
  824. * @returns {Promise.<void>}
  825. */
  826. async getCompositionListByGLJ(glj,unitPriceFileId){
  827. let t_index = this.getIndex(glj,['code','name','specs','unit','type']);
  828. // 查找对应的配合比数据
  829. let mixRatioModel = new MixRatioModel();
  830. let condition = {connect_key: t_index, unit_price_file_id: unitPriceFileId};
  831. let mixRatios = await mixRatioModel.model.find(condition, {_id: 0}).lean();
  832. //2019-11-15 优化,用5个属性条件去查意义不大,用编号就能过滤得差不多了,最后再用5个属性去匹配。 同时可以提高查询效率
  833. let codeList = [];
  834. if(mixRatios.length<=0){
  835. return [[],[],[]];
  836. }
  837. let mixRatioData={};
  838. for(let tmp of mixRatios) {
  839. codeList.push(tmp.code);
  840. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  841. mixRatioData[m_index]=tmp;
  842. }
  843. //数组去重
  844. codeList = _.uniq(codeList);
  845. // 查找对应的项目工料机数据
  846. let gcondition = {project_id: glj.project_id?glj.project_id:glj.projectID,code: {"$in": codeList}};
  847. let gljData = await this.model.find(gcondition, {_id: 0}).lean();
  848. // 查找对应的单价数据
  849. let unitPriceModel = new UnitPriceModel();
  850. let ucondition = { unit_price_file_id: unitPriceFileId,code: {"$in": codeList}};
  851. let unitPriceList = await unitPriceModel.model.find(ucondition, {_id: 0}).lean();
  852. // 整理数据
  853. let unitPriceData = {};
  854. for(let tmp of unitPriceList) {
  855. let u_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  856. if(mixRatioData[u_index]) unitPriceData[u_index] = tmp;//过滤掉不完全匹配的工料机单价
  857. }
  858. let c_gljData = [];
  859. for(let g of gljData){//过滤掉不是组成物的工料机
  860. let g_index = this.getIndex(g,['code','name','specs','unit','type']);
  861. if(mixRatioData[g_index]) c_gljData.push(g);
  862. }
  863. return [c_gljData,mixRatioData,unitPriceData];
  864. }
  865. /**
  866. * 根据条件获取对应项目工料机数据
  867. *
  868. * @param {Number} id
  869. * @return {Promise}
  870. */
  871. async getDataById(id,unitPriceFileId) {
  872. // 查找对应的项目工料机数据
  873. let projectGLJData = await this.findDataByCondition({id: id});
  874. if (projectGLJData === null) {
  875. throw '没有找到对应数据';
  876. }
  877. // 查找对应的单价数据
  878. let unitPriceModel = new UnitPriceModel();
  879. let condition={
  880. unit_price_file_id:unitPriceFileId,
  881. code: projectGLJData.code,
  882. name:projectGLJData.name,
  883. specs:projectGLJData.specs,
  884. type:projectGLJData.type,
  885. unit:projectGLJData.unit
  886. }
  887. let unitPrice = await unitPriceModel.findDataByCondition(condition);
  888. projectGLJData.unit_price = unitPrice;
  889. return projectGLJData;
  890. }
  891. }
  892. export default GLJListModel;