glj_list_model.js 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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 = await this.findDataByCondition(condition);
  304. let isAddProjectGLJ = false;
  305. // 获取标段对应的单价文件id
  306. let unitPriceFileId = unitFileId?unitFileId:await ProjectModel.getUnitPriceFileId(data.project_id);
  307. if (unitPriceFileId <= 0) {
  308. throw '没有对应的单价文件';
  309. }
  310. let unitPriceModel = new UnitPriceModel();
  311. // 新增单条记录 (两个操作本来应该是事务操作,然而mongodb事务支持比较弱,就当作是都可以顺利执行)
  312. // 如果找不到数据则新增
  313. if (!projectGljData) {
  314. let gljInsertData = await this.add(data);
  315. if (!gljInsertData) {
  316. throw '新增项目工料机失败!';
  317. }
  318. isAddProjectGLJ = true;
  319. projectGljData = gljInsertData;
  320. }
  321. let CompositionGLJ=[];
  322. // 判断类型,如果是混凝土、砂浆、配合比或者主材则查找对应的组成物(前提是没有对应的项目工料机数据)
  323. if(this.ownCompositionTypes.indexOf(data.type)!=-1) {
  324. //如果是新增
  325. if(isAddProjectGLJ ){
  326. await this.compositionInit(data, unitPriceFileId,ext);
  327. }
  328. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  329. if(isAddProjectGLJ==false&&CompositionGLJ.length==0){//如果不是新增,并且是有组成物的类型但又没有发现组成物的情况下,有可能是错误数据,重新在库中查找一下组成物,有则插入
  330. await this.compositionInit(data, unitPriceFileId,ext);
  331. CompositionGLJ=await this.getCompositionGLJByData(data,unitPriceFileId);
  332. if(CompositionGLJ.length>0){//如果这次发现又有组成物了,则把旧的单价数据删除,在后面的操作中会重新增加
  333. let de_condition ={unit_price_file_id: unitPriceFileId,code:data.code,name:data.name,unit:data.unit,type:data.type};
  334. data.specs!=null&&data.specs!=undefined&&data.specs!=""?de_condition.specs = data.specs:de_condition;
  335. await unitPriceModel.db.delete(de_condition);
  336. }
  337. }
  338. }
  339. projectGljData._doc.subList=CompositionGLJ;
  340. // 新增单价文件
  341. let [unitPriceInsertData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId);
  342. if (!unitPriceInsertData) {
  343. throw '新增单价失败!';
  344. }
  345. projectGljData.unit_price = unitPriceInsertData;
  346. result = projectGljData;
  347. } catch (error) {
  348. console.log(error);
  349. result = null;
  350. }
  351. return result;
  352. }
  353. /**
  354. * 新增单条工料机数据
  355. *
  356. * @param {object} data
  357. * @return {Promise}
  358. */
  359. async add(data) {
  360. if (Object.keys(data).length <= 0) {
  361. throw '新增数据为空';
  362. }
  363. let counterModel = new CounterModel();
  364. if (data instanceof Array) {
  365. // 如果是批量新增
  366. for(let tmp in data) {
  367. data[tmp].id = await counterModel.getId(gljCollectionName);
  368. }
  369. } else {
  370. data.id = await counterModel.getId(gljCollectionName);
  371. }
  372. let result = await this.db.create(data);
  373. return result;
  374. }
  375. /**
  376. * 修改名称、规格型号、单位、市场单价等
  377. * @param data
  378. * @returns {Promise.<void>}
  379. */
  380. async modifyGLJ(data,ration_glj){
  381. let unitPriceFileModel = new UnitPriceFileModel();
  382. let unitPriceFile = await unitPriceFileModel.getDataByProject(data.project_id);
  383. if (!unitPriceFile) {
  384. throw '没有对应的单价文件';
  385. }
  386. //查找单价信息,有则返回,没有则新增并返回
  387. let unitPriceFileId = unitPriceFile.id;
  388. let unitPriceModel = new UnitPriceModel();
  389. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(data, unitPriceFileId,"modify");
  390. let gljData=null;
  391. if(isAdd){ //如果是新增,则新增一条新的项目工料机
  392. data.code = unitPriceData.code;
  393. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  394. }else { //如果不是新增,则查找是否有对应的项目工料机,有则返回,没有则新增
  395. let condition = {
  396. project_id:data.project_id,
  397. original_code: data.original_code,
  398. name:data.name,
  399. specs:data.specs,
  400. type:data.type,
  401. unit:data.unit
  402. }
  403. let gljList = await this.findDataByCondition(condition,{_id: 0},false);
  404. if(gljList&&gljList.length>0){
  405. for(let tem of gljList){
  406. if(tem.code == unitPriceData.code){
  407. gljData = tem;
  408. }
  409. }
  410. }
  411. if(gljData==null){
  412. data.code = unitPriceData.code;
  413. gljData = await this.insertGLJWhenIsAdd(data,ration_glj,unitPriceFileId);
  414. }
  415. }
  416. gljData.unit_price = unitPriceData;
  417. return gljData
  418. }
  419. //修改属性后插入项目工料机
  420. async insertGLJWhenIsAdd(glj,ration_glj,unitPriceFileId){
  421. //新增项目工料机
  422. let gljData = await this.add(glj);
  423. //查看是否是有配合比的工料机类型
  424. if(this.ownCompositionTypes.indexOf(glj.type)!=-1){
  425. // 配合比数据插入
  426. let key_array = [];
  427. if(ration_glj.subType){//定额类型的工料机和定额工料机的key有点不同subType
  428. key_array= ['code','name','specs','unit','subType'];
  429. }else {
  430. key_array= ['code','name','specs','unit','type'];
  431. }
  432. let connect_key =this.getIndex(ration_glj,key_array);
  433. let connect_key_n =this.getIndex(glj,['code','name','specs','unit','type']);
  434. //先查找配合比数据是否已经存在
  435. let mixRatioModel = new MixRatioModel();
  436. let mixRatios_o = await mixRatioModel.findDataByCondition({connect_key: connect_key_n, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  437. if(mixRatios_o&&mixRatios_o.length>0){//已经存在,不用再插入配合比数据,直接返回
  438. return gljData;
  439. }
  440. //不存在,则查找原始的配合比数据,并为新工料机增加配合比数据
  441. let mixRatios = await mixRatioModel.findDataByCondition({connect_key: connect_key, unit_price_file_id: unitPriceFileId}, {_id: 0}, false);
  442. let mixInsertResult ={};
  443. if(mixRatios&&mixRatios.length>0){
  444. let newMixRatioData = [];
  445. for(let m of mixRatios){
  446. if(m.code == '80CCS') continue; //忽略原来的组成物信息,重新查看有没有匹配的
  447. let tem ={
  448. consumption: m.consumption,
  449. glj_id: m.glj_id,
  450. unit_price_file_id: m.unit_price_file_id,
  451. connect_key: connect_key_n,
  452. type: m.type,
  453. code: m.code,
  454. specs:m.specs,
  455. name:m.name,
  456. unit:m.unit
  457. };
  458. newMixRatioData.push(tem);
  459. }
  460. if(glj.type == 301){//机械台班添加车船税
  461. let newM = await this.getCCSData(glj,unitPriceFileId);
  462. if(newM) newMixRatioData.push(newM);
  463. }
  464. mixInsertResult= await mixRatioModel.add(newMixRatioData);
  465. }
  466. }
  467. return gljData;
  468. }
  469. /**
  470. * 根据工料机id修改市场单价
  471. *
  472. * @param {Object} updateData
  473. * @return {Promise}
  474. */
  475. async modifyMarketPrice(updateData) {
  476. let result = {};
  477. try {
  478. if (updateData.code === undefined || updateData.market_price === undefined ||
  479. updateData.name === undefined || updateData.project_id === undefined) {
  480. throw '参数有误!';
  481. }
  482. // 先查是否有对应code的数据
  483. let gljListData = await this.findDataByCondition({code: updateData.code,
  484. project_id: updateData.project_id}, {_id: 0}, false);
  485. if (!gljListData) {
  486. throw '不存在对应code数据';
  487. }
  488. // 获取标段对应的单价文件id
  489. let unitPriceFileModel = new UnitPriceFileModel();
  490. let unitPriceFile = await unitPriceFileModel.getDataByProject(updateData.project_id);
  491. if (!unitPriceFile) {
  492. throw '没有对应的单价文件';
  493. }
  494. let unitPriceFileId = unitPriceFile.id;
  495. let unitPriceModel = new UnitPriceModel();
  496. let gljCount = gljListData.length;
  497. let [unitPriceData, isAdd] = await unitPriceModel.addUnitPrice(updateData, unitPriceFileId,"modify", gljCount);
  498. // 判断是否已存在对应数据
  499. let includeField = [
  500. {field: 'name', value: unitPriceData.name}
  501. ];
  502. let gljIndex = this.isIncluded(gljListData, includeField);
  503. let gljData = isAdd ? {} : gljListData[gljIndex];
  504. // 如果单价数据新增则工料机也需要新增
  505. if (isAdd) {
  506. // 如果没有对应的记录则新增一条工料机数据,并更改name
  507. let regular = /\(\d+\)/;
  508. let changeString = '(' + gljCount + ')';
  509. updateData.name = regular.test(updateData.name) ? updateData.name.replace(regular, changeString) :
  510. updateData.name + changeString;
  511. // 获取第一条数据作为数据源
  512. let originalData = gljListData[0];
  513. // 更改名称
  514. originalData.name = updateData.name;
  515. originalData = JSON.stringify(originalData);
  516. gljData = await this.add(JSON.parse(originalData));
  517. if (!gljData) {
  518. throw '新增工料机数据失败!';
  519. }
  520. }
  521. gljData.unit_price = unitPriceData;
  522. result = gljData;
  523. } catch (error) {
  524. console.log(error);
  525. result = {};
  526. }
  527. return result;
  528. }
  529. /**
  530. * 判断数据中是否包含某个数据
  531. *
  532. * @param {Array} data
  533. * @param {Array} includeField
  534. * @return {Number}
  535. */
  536. isIncluded(data, includeField) {
  537. let index = -1;
  538. if (data.length <= 0) {
  539. return index;
  540. }
  541. for(let tmp in data) {
  542. let counter = 0;
  543. for (let includeTmp of includeField) {
  544. if (data[tmp][includeTmp.field] === includeTmp.value) {
  545. counter++;
  546. }
  547. }
  548. if (counter === includeField.length) {
  549. index = tmp;
  550. break;
  551. }
  552. }
  553. return index;
  554. }
  555. //
  556. async getCCSData(data,unitFileId){
  557. let unitPriceFileModel = new UnitPriceFileModel();
  558. let unitFileInfo = await unitPriceFileModel.findDataByCondition({id: unitFileId});
  559. if(unitFileInfo.vvTaxFileID && unitFileInfo.vvTaxFileID!=""){
  560. let condition = {libID:unitFileInfo.vvTaxFileID,code:data.code,name:data.name,type:data.type,unit:data.unit,specs:data.specs};
  561. let item = await vvTaxModel.findOne(condition);
  562. if(item){
  563. return gljUtil.getBaseCCSMixRatio(unitFileId,item.vehicleVesselTax,gljUtil.getIndex(data))
  564. }
  565. }
  566. return null;
  567. }
  568. /**
  569. * 工料机中组成物操作
  570. * 该方法只在确保没有对应项目工料机的时候才会调用
  571. *
  572. * @param {Object} data
  573. * @param {Number} projectId
  574. * @return {void}
  575. */
  576. async compositionInit(data, unitPriceFileId,ext) {
  577. let gljId = data.glj_id === undefined ? 0 : data.glj_id;
  578. let projectId = data.project_id === undefined ? 0 : data.project_id;
  579. let mixRatioModel = new MixRatioModel();
  580. if (gljId === 0 || projectId === 0) {
  581. throw '参数错误';
  582. }
  583. let fromTable = data.from === undefined ? 'std' : data.from;
  584. var connect_key =this.getIndex(data,['code','name','specs','unit','type']);
  585. let e_mList = await this.getCompositionGLJListFromMixRatio(projectId,unitPriceFileId,connect_key,fromTable);
  586. //如果组成物数据已经存在,则以单价文件中组成物的数据为准,不用再往下做判断了(项目工料机中需要插入的已经在上一步中生成插入)
  587. if(e_mList.length > 0) return;
  588. // 查找对应组成物的项目工料机数据
  589. let indexs=['code','name','specs','unit','type'];
  590. let [projectGljList, compositionGljList] = await this.getCompositionGLJList(gljId, projectId, indexs, fromTable);
  591. // 整理配合比待插入数据,单价文件中配合比没有数据,则以库中的组成物信息为准
  592. let mixRatioInsertData = [];
  593. for (let tmp of compositionGljList) {
  594. let consumpiton = tmp.consumption;
  595. //只有标准的工料机的组成物才会有多单价、多组成物消耗量的情况 fromTable
  596. if(fromTable == 'std' && ext && ext.quantityField &&( tmp.consumption[ext.quantityField]!= undefined && tmp.consumption[ext.quantityField]!=null)){
  597. consumpiton = tmp.consumption[ext.quantityField];
  598. }
  599. // 配合比数据插入
  600. let mixRatioData = {
  601. consumption: consumpiton,
  602. glj_id: tmp.ID,
  603. unit_price_file_id: unitPriceFileId,
  604. connect_key: connect_key,
  605. type: tmp.gljType,
  606. code: tmp.code,
  607. specs:tmp.specs?tmp.specs:"",
  608. name:tmp.name,
  609. unit:tmp.unit?tmp.unit:''
  610. };
  611. mixRatioInsertData.push(mixRatioData);
  612. }
  613. //如果有车船税则加入到配合比表中 -- 先判断机械类型吧,所有都插入影响性能
  614. if(data.type == 301){//如果是机械台班,看有没有车船税 - modifyGLJ方法里也会有添加组成物,修改这里逻辑时记得同步修改。
  615. let newM = await this.getCCSData(data,unitPriceFileId);
  616. if(newM) mixRatioInsertData.push(newM);
  617. }
  618. let unitPriceModel = new UnitPriceModel();
  619. let unitPriceList = await unitPriceModel.model.find({unit_price_file_id:unitPriceFileId});//查找按文件id查找单价文件数据,用来判断单价文件信息是否已存在
  620. // 整理插入的数据
  621. let gljInsertData = [];
  622. let unitPriceInsertData = [];
  623. for(let tmp of compositionGljList) {
  624. let key = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  625. if (projectGljList[key] === undefined) {
  626. // 项目工料机插入的数据
  627. let gljData = this.getProjectGLJNewData(tmp,projectId);
  628. gljInsertData.push(gljData);
  629. }
  630. let basePrice = tmp.basePrice;
  631. //只有标准的工料机的组成物才会有多单价、多组成物消耗量的情况 fromTable
  632. if(fromTable == 'std' && ext && ext.priceField &&( tmp.priceProperty[ext.priceField]!= undefined && tmp.priceProperty[ext.priceField]!=null)){
  633. basePrice = tmp.priceProperty[ext.priceField];
  634. }
  635. basePrice = scMathUtil.roundTo(basePrice,-6);
  636. // 单价文件插入的数据
  637. let unitPriceData = {
  638. base_price: basePrice,
  639. // 初始市场价=基价
  640. market_price: basePrice,
  641. code: tmp.code,
  642. name: tmp.name,
  643. unit_price_file_id: unitPriceFileId,
  644. type: tmp.gljType,
  645. short_name: tmp.shortName === undefined ? '' : tmp.shortName,
  646. glj_id: tmp.ID,
  647. specs: tmp.specs?tmp.specs:'',
  648. unit: tmp.unit === undefined ? '' : tmp.unit,
  649. original_code:tmp.code,
  650. grossWeightCoe:tmp.grossWeightCoe,
  651. purchaseStorageRate:tmp.purchaseStorageRate,
  652. offSiteTransportLossRate:tmp.offSiteTransportLossRate,
  653. handlingLossRate:tmp.handlingLossRate
  654. };
  655. if(tmp.from=='cpt'){
  656. unitPriceData.is_add = 1;
  657. }
  658. if(!unitPriceModel.isPropertyInclude(unitPriceList,['code','name','specs','unit','type'],unitPriceData)){
  659. unitPriceInsertData.push(unitPriceData);
  660. }
  661. }
  662. // 整理完后开始插入数据
  663. // 插入单价数据表
  664. if(unitPriceInsertData.length >0) await unitPriceModel.add(unitPriceInsertData);
  665. //插入配合比数据
  666. if(mixRatioInsertData.length>0){
  667. let addMixRatioResult = await mixRatioModel.add(mixRatioInsertData);
  668. if (!addMixRatioResult) {
  669. throw '组成物插入单价数据失败!';
  670. }
  671. }
  672. //插入项目工料机
  673. if(gljInsertData.length > 0) await this.add(gljInsertData);
  674. return
  675. }
  676. getProjectGLJNewData(tmp,projectId){
  677. let gljData = {
  678. glj_id: tmp.ID,
  679. project_id: projectId,
  680. code: tmp.code,
  681. name: tmp.name,
  682. specs: tmp.specs?tmp.specs:'',
  683. unit: tmp.unit === undefined ? '' : tmp.unit,
  684. type: tmp.gljType,
  685. adjCoe:tmp.adjCoe,
  686. original_code:tmp.code,
  687. materialType: tmp.materialType, //三材类别
  688. materialCoe: tmp.materialCoe,
  689. from:tmp.from?tmp.from:"std"
  690. };
  691. return gljData;
  692. }
  693. /**
  694. * 获取组成物具体数据
  695. *
  696. * @param {Number} projectGLJId
  697. * @param {Number} unitPriceFileId
  698. * @return {Promise}
  699. */
  700. async getCompositionList(projectGLJId, unitPriceFileId) {
  701. let result = [];
  702. try {
  703. // 查找对应的项目工料机数据
  704. let projectGLJData = await this.getDataById(projectGLJId,unitPriceFileId);
  705. let allowType = this.ownCompositionTypes;
  706. if (projectGLJData.unit_price === null || allowType.indexOf(projectGLJData.unit_price.type) < 0) {
  707. throw '找不到相关项目工料机';
  708. }
  709. // 查找对应的项目工料机数据配合比,单价数据
  710. let [gljData, mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(projectGLJData, unitPriceFileId);
  711. if (gljData.length <= 0) {
  712. throw '没有对应的组成物项目工料机';
  713. }
  714. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  715. // 排序
  716. gljData.sort(function (a, b) {
  717. return parseInt(a.code) - parseInt(b.code);
  718. });
  719. result = gljData;
  720. } catch (error) {
  721. console.log(error);
  722. result = [];
  723. }
  724. return result;
  725. }
  726. //从配合比表中取组成物的信息以及对应的项目工料机信息,再从配合比数据中反查工料机库中的信息
  727. async getCompositionGLJListFromMixRatio (projectId,unitPriceFileId,connect_key,fromTable = 'std'){
  728. let mixRatioModel = new MixRatioModel();
  729. let e_mList = await mixRatioModel.model.find({unit_price_file_id: unitPriceFileId,connect_key: connect_key}).lean();
  730. if (e_mList.length <= 0) return [];
  731. let codeList = [];
  732. let ratioMap = {};
  733. for(let tmp of e_mList) {
  734. codeList.push(tmp.code);
  735. let ckey = this.getIndex(tmp);
  736. ratioMap[ckey] = tmp
  737. }
  738. // 查找对应的项目工料机数据
  739. let condition = {project_id: projectId,code: {"$in": codeList} };
  740. let gljData = await this.model.find(condition, {_id: 0}).lean();
  741. let matchGljs = {};
  742. for(let g of gljData){
  743. let gkey = this.getIndex(g);
  744. if(ratioMap[gkey]) matchGljs[gkey] = g;
  745. }
  746. //如果有组成物信息存在,但项目工料机信息不全的情况,应从数据库中查存询原始信息
  747. // (只有在父工料机是来自标准的情况下做这个操作,因为组成物表中没有存具体的工料机是来自补充的还是标准库,而父工料机是补充库的,组成物有可能来自两个库,而父来自标准,子只会来自标准)
  748. let lessIDList = [],lessMix=[];//lessMix只有在父工料机是来自补充工料机的时候才要用到
  749. let componentGljData = [];
  750. let gljInsertData = [];
  751. for(let mk in ratioMap){//找出缺少的工料机
  752. if(!matchGljs[mk]){
  753. lessMix.push(ratioMap[mk]);
  754. lessIDList.push(ratioMap[mk].glj_id);
  755. }
  756. }
  757. if(lessIDList.length > 0){
  758. if(fromTable == "std"){
  759. let gljListModel = new STDGLJLibGLJListModel();
  760. let condition = {ID: {$in: lessIDList}};
  761. componentGljData = await gljListModel.model.find(condition).lean();
  762. for(let tmp of componentGljData){
  763. let newProjctData = this.getProjectGLJNewData(tmp,projectId);
  764. gljInsertData.push(newProjctData)
  765. }
  766. }else {
  767. for(let m of lessMix){
  768. m.from = 'cpt';
  769. let newProjctData = this.getProjectGLJNewData(m,projectId);
  770. gljInsertData.push(newProjctData);
  771. }
  772. }
  773. }
  774. //这边只插入项目工料机数据,对于组成物文件中有的数据,理论上单价文件也是应该要有的,如果没有,那只能是mongodb数据库不支持事务的情况的问题
  775. if(gljInsertData.length > 0) await this.add(gljInsertData);
  776. return e_mList;
  777. }
  778. /**
  779. * 获取混凝土等有组成物相关工料机对应的组成物项目工料机数据
  780. *
  781. * @param {Number} gljId
  782. * @param {Number} projectId
  783. * @param {String} indexBy
  784. * @param {String} fromTable
  785. * @return {Promise} 返回组成物工料机数据和组成物列表数据
  786. */
  787. async getCompositionGLJList(gljId, projectId, indexBy = null, fromTable = 'std') {
  788. // 获取对应的组成物数据
  789. let gljListModel = fromTable === 'std' ? new STDGLJLibGLJListModel() : new GljModel();
  790. let componentGljList = await gljListModel.getComponent(gljId);
  791. if (componentGljList.length <= 0) {
  792. return [{},[]];
  793. }
  794. //2019-11-15 优化,用5个属性条件去查意义不大,用编号就能过滤得差不多了,最后再用5个属性去匹配。 同时可以提高查询效率
  795. let codeList = [];
  796. let componentMap = {};
  797. for(let tmp of componentGljList) {
  798. codeList.push(tmp.code);
  799. let ckey = this.getIndex(tmp,['code','name','specs','unit','gljType']);
  800. componentMap[ckey] = tmp
  801. }
  802. // 查找对应的项目工料机数据
  803. let condition = {project_id: projectId,code: {"$in": codeList} };
  804. let gljData = await this.model.find(condition, {_id: 0}).lean();
  805. let matchGljs = {};
  806. for(let g of gljData){
  807. let gkey = this.getIndex(g);
  808. if(componentMap[gkey]) matchGljs[gkey] = g;
  809. }
  810. return [matchGljs, componentGljList];
  811. }
  812. async getCompositionGLJByData(glj,unitPriceFileId){
  813. let [gljData,mixRatioData,unitPriceData] = await this.getCompositionListByGLJ(glj,unitPriceFileId);
  814. let priceNum = unitPriceData?Object.getOwnPropertyNames(unitPriceData).length:0;
  815. let mixNum = mixRatioData?Object.getOwnPropertyNames(mixRatioData).length:0;
  816. if(priceNum !==mixNum){//检查组成物数据是否一致
  817. console.log("组成物数据有误,组成物单价文件个数:"+priceNum+", 组成物个数:"+mixNum);
  818. return []
  819. }
  820. gljData = this.combineData(gljData, unitPriceData, [], mixRatioData);
  821. return gljData;
  822. }
  823. /**
  824. * 反回组成物工料机和配合比数据
  825. * @param glj
  826. * @param unitPriceFileId
  827. * @returns {Promise.<void>}
  828. */
  829. async getCompositionListByGLJ(glj,unitPriceFileId){
  830. let t_index = this.getIndex(glj,['code','name','specs','unit','type']);
  831. // 查找对应的配合比数据
  832. let mixRatioModel = new MixRatioModel();
  833. let condition = {connect_key: t_index, unit_price_file_id: unitPriceFileId};
  834. let mixRatios = await mixRatioModel.model.find(condition, {_id: 0}).lean();
  835. //2019-11-15 优化,用5个属性条件去查意义不大,用编号就能过滤得差不多了,最后再用5个属性去匹配。 同时可以提高查询效率
  836. let codeList = [];
  837. if(mixRatios.length<=0){
  838. return [[],[],[]];
  839. }
  840. let mixRatioData={};
  841. for(let tmp of mixRatios) {
  842. codeList.push(tmp.code);
  843. let m_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  844. mixRatioData[m_index]=tmp;
  845. }
  846. //数组去重
  847. codeList = _.uniq(codeList);
  848. // 查找对应的项目工料机数据
  849. let gcondition = {project_id: glj.project_id?glj.project_id:glj.projectID,code: {"$in": codeList}};
  850. let gljData = await this.model.find(gcondition, {_id: 0}).lean();
  851. // 查找对应的单价数据
  852. let unitPriceModel = new UnitPriceModel();
  853. let ucondition = { unit_price_file_id: unitPriceFileId,code: {"$in": codeList}};
  854. let unitPriceList = await unitPriceModel.model.find(ucondition, {_id: 0}).lean();
  855. // 整理数据
  856. let unitPriceData = {};
  857. for(let tmp of unitPriceList) {
  858. let u_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  859. if(mixRatioData[u_index]) unitPriceData[u_index] = tmp;//过滤掉不完全匹配的工料机单价
  860. }
  861. let c_gljData = [];
  862. for(let g of gljData){//过滤掉不是组成物的工料机
  863. let g_index = this.getIndex(g,['code','name','specs','unit','type']);
  864. if(mixRatioData[g_index]) c_gljData.push(g);
  865. }
  866. return [c_gljData,mixRatioData,unitPriceData];
  867. }
  868. /**
  869. * 根据条件获取对应项目工料机数据
  870. *
  871. * @param {Number} id
  872. * @return {Promise}
  873. */
  874. async getDataById(id,unitPriceFileId) {
  875. // 查找对应的项目工料机数据
  876. let projectGLJData = await this.findDataByCondition({id: id});
  877. if (projectGLJData === null) {
  878. throw '没有找到对应数据';
  879. }
  880. // 查找对应的单价数据
  881. let unitPriceModel = new UnitPriceModel();
  882. let condition={
  883. unit_price_file_id:unitPriceFileId,
  884. code: projectGLJData.code,
  885. name:projectGLJData.name,
  886. specs:projectGLJData.specs,
  887. type:projectGLJData.type,
  888. unit:projectGLJData.unit
  889. }
  890. let unitPrice = await unitPriceModel.findDataByCondition(condition);
  891. projectGLJData.unit_price = unitPrice;
  892. return projectGLJData;
  893. }
  894. }
  895. export default GLJListModel;