glj_list_model.js 41 KB

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