unit_price_model.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /**
  2. * 单价业务模型
  3. *
  4. * @author CaiAoLin
  5. * @date 2017/6/30
  6. * @version
  7. */
  8. import BaseModel from "../../common/base/base_model"
  9. import GLJTypeConst from "../../common/const/glj_type_const"
  10. import CounterModel from "./counter_model"
  11. import MixRatioModel from "./mix_ratio_model";
  12. import {default as UnitPriceSchema, collectionName as collectionName} from "./schemas/unit_price";
  13. import _ from "lodash";
  14. const scMathUtil = require('../../../public/scMathUtil').getUtil();
  15. class UnitPriceModel extends BaseModel {
  16. /**
  17. * 构造函数
  18. *
  19. * @return {void}
  20. */
  21. constructor() {
  22. let parent = super();
  23. parent.model = UnitPriceSchema;
  24. parent.init();
  25. }
  26. /**
  27. * 根据单价文件id获取单价数据
  28. *
  29. * @param {Number} fileId
  30. * @return {Promise}
  31. */
  32. async getDataByFileId(fileId) {
  33. fileId = parseInt(fileId);
  34. if (isNaN(fileId) || fileId <= 0) {
  35. return null;
  36. }
  37. let unitPriceList = await this.db.model.find({unit_price_file_id: fileId});
  38. if (unitPriceList.length <= 0) {
  39. return null;
  40. }
  41. // 整理数据
  42. let result = {};
  43. for(let tmp of unitPriceList) {
  44. let index = this.getIndex(tmp,['code','name','specs','unit','type'])
  45. result[index] = tmp;
  46. }
  47. return result;
  48. }
  49. /**
  50. * 设置场景
  51. *
  52. * @param {string} scene
  53. * @return {void}
  54. */
  55. setScene(scene = '') {
  56. switch (scene) {
  57. // 新增数据的验证规则
  58. case 'add':
  59. this.model.schema.path('base_price').required(true);
  60. this.model.schema.path('market_price').required(true);
  61. this.model.schema.path('name').required(true);
  62. this.model.schema.path('code').required(true);
  63. // this.model.schema.path('unit').required(true);
  64. this.model.schema.path('type').required(true);
  65. this.model.schema.path('unit_price_file_id').required(true);
  66. }
  67. }
  68. /**
  69. * 新增单价数据
  70. *
  71. * @param {Object} data
  72. * @param {Number} unitPriceFileId
  73. * @param {Number} gljCount
  74. * @return {Promise} 返回数据以及是否新增
  75. */
  76. async addUnitPrice(data, unitPriceFileId,operation='add', gljCount = 0) {
  77. if (data.original_code===undefined||data.code === undefined || data.project_id === undefined || data.name === undefined
  78. || data.market_price === undefined) {
  79. return [null, false];
  80. }
  81. // 先查找是否有原始code相同的记录
  82. let unitPriceData = await this.findDataByCondition({original_code: data.original_code, unit_price_file_id: unitPriceFileId}, null, false);
  83. // 如果有记录,判断是否存在一样的名称,单位...等,有则直接返回数据
  84. let unitPrice=null;
  85. if(operation=='add'){//新增操作时,要把code也一起判断,是否完全一样
  86. unitPrice = this.isPropertyInclude(unitPriceData,['code','name','specs','unit','type'],data);
  87. }else {//修改操作时,code不用加入判断,因为code是需要改变的
  88. unitPrice = this.isPropertyInclude(unitPriceData,['name','specs','unit','type'],data);
  89. }
  90. if(unitPrice){
  91. return [unitPrice, false];
  92. }
  93. // 如果不存在基价单价,则在数据源中获取
  94. if (data.base_price === undefined) {
  95. let firstUnitPrice = unitPriceData[0] !== undefined ? unitPriceData[0] : [];
  96. data.base_price = firstUnitPrice.base_price !== undefined ? firstUnitPrice.base_price : 0;
  97. data.type = firstUnitPrice.type !== undefined ? firstUnitPrice.type : 0;
  98. }
  99. let insertData = {
  100. code: data.code,
  101. base_price: data.base_price,
  102. market_price: data.market_price,
  103. unit_price_file_id: unitPriceFileId,
  104. name: data.name,
  105. specs:data.specs,
  106. original_code:data.original_code,
  107. unit:data.unit,
  108. type: data.type,
  109. short_name: data.shortName !== undefined ? data.shortName : '',
  110. glj_id: data.glj_id,
  111. is_add:0
  112. };
  113. if(data.from=='cpt'){//如果是来自补充工料机,则都添加新增标记
  114. insertData.is_add=1;
  115. }
  116. if (unitPriceData&&unitPriceData.length>0) {// 如果原始编码能找到,但不存在一样的编号,名称,单位.型号等,更改code和添加新增标记
  117. insertData.code = data.original_code+"-"+unitPriceData.length;
  118. insertData.is_add=1;
  119. }
  120. let addPriceResult = await this.add(insertData);
  121. return [addPriceResult, true];
  122. }
  123. /**
  124. * 新增记录
  125. *
  126. * @param {object} data
  127. * @return {Promise}
  128. */
  129. async add(data) {
  130. let counterModel = new CounterModel();
  131. if (data instanceof Array) {
  132. // 如果是批量新增
  133. for(let tmp in data) {
  134. data[tmp].id = await counterModel.getId(collectionName);
  135. }
  136. } else {
  137. data.id = await counterModel.getId(collectionName);
  138. }
  139. this.setScene('add');
  140. return this.db.model.create(data);
  141. }
  142. /**
  143. * 判断数据中是否包含某个市场价格的记录
  144. *
  145. * @param {Array} data
  146. * @param {Number} price
  147. * @return {Number}
  148. */
  149. isPriceIncluded(data, price) {
  150. let index = -1;
  151. if (data.length <= 0) {
  152. return index;
  153. }
  154. for(let tmp in data) {
  155. if (data[tmp].market_price === price) {
  156. index = tmp;
  157. break;
  158. }
  159. }
  160. return index;
  161. }
  162. isPropertyInclude(data,pops,obj){
  163. let condition={};
  164. if (data.length <= 0) {
  165. return null;
  166. }
  167. if(pops instanceof Array){
  168. for(let p of pops){
  169. if(obj[p]){
  170. condition[p]=obj[p]
  171. }
  172. }
  173. }else {
  174. condition[pops]=obj[pops]
  175. }
  176. return _.find(data,condition);
  177. }
  178. /**
  179. * 更新市场单价
  180. *
  181. * @param {Object} condition
  182. * @param {Object} updateData
  183. * @param {String} extend
  184. * @return {Promise}
  185. */
  186. async updatePrice(condition, updateData, extend = '') {
  187. if (Object.keys(condition).length <= 0 || Object.keys(updateData).length <= 0) {
  188. return false;
  189. }
  190. // 首先查找相应的数据判断工料机类型
  191. let unitPriceData = await this.findDataByCondition(condition);
  192. if (!unitPriceData) {
  193. throw '找不到对应的单价数据';
  194. }
  195. /* // 基价单价的计算-----先不考虑同步
  196. switch (unitPriceData.type) {
  197. // 主材、设备自动赋值基价单价=市场单价
  198. case GLJTypeConst.MAIN_MATERIAL:
  199. case GLJTypeConst.EQUIPMENT:
  200. updateData.base_price = updateData.market_price;
  201. break;
  202. }*/
  203. // 额外更新数据
  204. if (extend !== '') {
  205. extend = JSON.parse(extend);
  206. for (let code in extend) {
  207. let extendUpdateData = {
  208. market_price: extend[code].market_price,
  209. };
  210. let tmpCondition = {
  211. unit_price_file_id: unitPriceData.unit_price_file_id,
  212. code: code
  213. };
  214. let extendResult = await this.db.update(tmpCondition, extendUpdateData);
  215. if (!extendResult) {
  216. throw '更新额外数据,编码为' + code + '的数据失败!';
  217. }
  218. }
  219. }
  220. let result = await this.db.update(condition, updateData);
  221. return result.ok !== undefined && result.ok === 1;
  222. }
  223. async updateUnitPrice(data){
  224. //查找并更新单价
  225. let doc={};
  226. doc[data.field]=data.newval;
  227. let unitPrice = await this.db.findAndModify({id:data.id},doc);
  228. if(!unitPrice){
  229. throw "没有找到对应的单价";
  230. }
  231. //查找是否是属于某个项目工料机的组成物
  232. let mixRatioModel = new MixRatioModel();
  233. let condition = {unit_price_file_id:unitPrice.unit_price_file_id, code:unitPrice.code,name: unitPrice.name, specs: unitPrice.specs,unit:unitPrice.unit,type:unitPrice.type};
  234. let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
  235. let connectKeyMap={};
  236. //找到则计算项目工料机组成物的价格并更新
  237. let rList= [];
  238. if(mixRatioList&&mixRatioList.length>0){
  239. for(let m of mixRatioList){
  240. if(!connectKeyMap.hasOwnProperty(m.connect_key)){//为了去重复,组成物会与其它项目同步,所以有可能重复。
  241. rList.push(await this.updateParentUnitPrice(m,data.field));
  242. connectKeyMap[m.connect_key]=true;
  243. }
  244. }
  245. }
  246. return rList;
  247. }
  248. async updateParentUnitPrice(mixRatio,fieid){
  249. //查找该工料机所有组成物
  250. let indexList = ['code','name','specs','unit','type'];
  251. let mixRatioModel = new MixRatioModel();
  252. let mixRatioMap = await mixRatioModel.findDataByCondition({unit_price_file_id:mixRatio.unit_price_file_id,connect_key:mixRatio.connect_key}, null, false,indexList);
  253. //查找对应的价格
  254. let codeList = [];
  255. let nameList = [];
  256. let specsList= [];
  257. let typeList = [];
  258. let unitList = [];
  259. for(let mk in mixRatioMap){
  260. codeList.push(mixRatioMap[mk].code);
  261. nameList.push(mixRatioMap[mk].name);
  262. specsList.push(mixRatioMap[mk].specs);
  263. typeList.push(mixRatioMap[mk].type);
  264. unitList.push(mixRatioMap[mk].unit);
  265. }
  266. let condition = {unit_price_file_id: mixRatio.unit_price_file_id,code: {"$in": codeList}, name: {"$in": nameList},specs:{"$in": specsList},type:{"$in": typeList},unit:{"$in": unitList}};
  267. let priceMap = await this.findDataByCondition(condition, {_id: 0}, false, indexList);
  268. let sumPrice=0;
  269. for(let pk in priceMap){
  270. let price = parseFloat(priceMap[pk][fieid]);
  271. let consumption = parseFloat(mixRatioMap[pk].consumption);
  272. sumPrice +=scMathUtil.roundTo(price*consumption,-6);
  273. }
  274. sumPrice= scMathUtil.roundTo(sumPrice,-6);
  275. if(sumPrice<=0){
  276. return null;
  277. }
  278. //更新父价格
  279. let keyList = mixRatio.connect_key.split("|-|");
  280. let pcondition = {
  281. unit_price_file_id:mixRatio.unit_price_file_id,
  282. code:keyList[0]
  283. };
  284. for(let i = 1;i<keyList.length;i++){
  285. if(keyList[i]!='null'){
  286. pcondition[indexList[i]]=keyList[i];
  287. }
  288. }
  289. let doc={};
  290. doc[fieid]=sumPrice;
  291. let uprice = await this.db.findAndModify(pcondition,doc);
  292. uprice[fieid]=sumPrice;
  293. return uprice;
  294. }
  295. /**
  296. * 复制单价文件数据
  297. *
  298. * @param {Number} currentUnitPriceId
  299. * @param {Number} changeUnitPriceId
  300. * @return {Promise}
  301. */
  302. async copyNotExist(currentUnitPriceId, changeUnitPriceId) {
  303. let result = false;
  304. // 首先查找原单价文件id下的数据
  305. let currentUnitList = await this.findDataByCondition({unit_price_file_id: currentUnitPriceId}, null, false);
  306. if (currentUnitList === null) {
  307. return result;
  308. }
  309. // 过滤mongoose格式
  310. currentUnitList = JSON.stringify(currentUnitList);
  311. currentUnitList = JSON.parse(currentUnitList);
  312. let codeList = [];
  313. let nameList =[];
  314. for (let tmp of currentUnitList) {
  315. if (codeList.indexOf(tmp.code) >= 0) {
  316. continue;
  317. }
  318. codeList.push(tmp.code);
  319. if(nameList.indexOf(tmp.name)>=0){
  320. continue
  321. }
  322. nameList.push(tmp.name);
  323. }
  324. // 查找即将更替的单价文件是否存在对应的工料机数据 -- (这里只根据code和名称初步过滤,因为其它的几项更改的概率不大,在下一步的比较中再精确匹配)
  325. let condition = {unit_price_file_id: changeUnitPriceId, code: {"$in": codeList},name:{"$in": nameList}};
  326. let targetUnitList = await this.findDataByCondition(condition, null, false, ['code','name','specs','unit','type']);
  327. // 如果没有重叠的数据则原有的数据都复制一份
  328. let insertData = [];
  329. for (let tmp of currentUnitList) {
  330. let t_index = this.getIndex(tmp,['code','name','specs','unit','type']);
  331. if (targetUnitList !== null && targetUnitList[t_index] !== undefined) {
  332. continue;
  333. }
  334. // 删除原有id信息
  335. delete tmp._id;
  336. delete tmp.id;
  337. tmp.unit_price_file_id = changeUnitPriceId;
  338. insertData.push(tmp);
  339. }
  340. return insertData.length > 0 ? this.add(insertData) : true;
  341. }
  342. }
  343. export default UnitPriceModel;