unit_price_model.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. }else if (unitPriceData&&unitPriceData.length>0) {// 如果原始编码能找到,但不存在一样的编号,名称,单位.型号等,更改code和添加新增标记
  116. insertData.code = data.original_code+"-"+unitPriceData.length;
  117. insertData.is_add=1;
  118. }
  119. let addPriceResult = await this.add(insertData);
  120. return [addPriceResult, true];
  121. }
  122. /**
  123. * 新增记录
  124. *
  125. * @param {object} data
  126. * @return {Promise}
  127. */
  128. async add(data) {
  129. let counterModel = new CounterModel();
  130. if (data instanceof Array) {
  131. // 如果是批量新增
  132. for(let tmp in data) {
  133. data[tmp].id = await counterModel.getId(collectionName);
  134. }
  135. } else {
  136. data.id = await counterModel.getId(collectionName);
  137. }
  138. this.setScene('add');
  139. return this.db.model.create(data);
  140. }
  141. /**
  142. * 判断数据中是否包含某个市场价格的记录
  143. *
  144. * @param {Array} data
  145. * @param {Number} price
  146. * @return {Number}
  147. */
  148. isPriceIncluded(data, price) {
  149. let index = -1;
  150. if (data.length <= 0) {
  151. return index;
  152. }
  153. for(let tmp in data) {
  154. if (data[tmp].market_price === price) {
  155. index = tmp;
  156. break;
  157. }
  158. }
  159. return index;
  160. }
  161. isPropertyInclude(data,pops,obj){
  162. let condition={};
  163. if (data.length <= 0) {
  164. return null;
  165. }
  166. if(pops instanceof Array){
  167. for(let p of pops){
  168. if(obj[p]){
  169. condition[p]=obj[p]
  170. }
  171. }
  172. }else {
  173. condition[pops]=obj[pops]
  174. }
  175. return _.find(data,condition);
  176. }
  177. /**
  178. * 更新市场单价
  179. *
  180. * @param {Object} condition
  181. * @param {Object} updateData
  182. * @param {String} extend
  183. * @return {Promise}
  184. */
  185. async updatePrice(condition, updateData, extend = '') {
  186. if (Object.keys(condition).length <= 0 || Object.keys(updateData).length <= 0) {
  187. return false;
  188. }
  189. // 首先查找相应的数据判断工料机类型
  190. let unitPriceData = await this.findDataByCondition(condition);
  191. if (!unitPriceData) {
  192. throw '找不到对应的单价数据';
  193. }
  194. /* // 基价单价的计算-----先不考虑同步
  195. switch (unitPriceData.type) {
  196. // 主材、设备自动赋值基价单价=市场单价
  197. case GLJTypeConst.MAIN_MATERIAL:
  198. case GLJTypeConst.EQUIPMENT:
  199. updateData.base_price = updateData.market_price;
  200. break;
  201. }*/
  202. // 额外更新数据
  203. if (extend !== '') {
  204. extend = JSON.parse(extend);
  205. for (let code in extend) {
  206. let extendUpdateData = {
  207. market_price: extend[code].market_price,
  208. };
  209. let tmpCondition = {
  210. unit_price_file_id: unitPriceData.unit_price_file_id,
  211. code: code
  212. };
  213. let extendResult = await this.db.update(tmpCondition, extendUpdateData);
  214. if (!extendResult) {
  215. throw '更新额外数据,编码为' + code + '的数据失败!';
  216. }
  217. }
  218. }
  219. let result = await this.db.update(condition, updateData);
  220. return result.ok !== undefined && result.ok === 1;
  221. }
  222. async updateUnitPrice(data){
  223. //查找并更新单价
  224. let doc={};
  225. doc[data.field]=data.newval;
  226. let unitPrice = await this.db.findAndModify({id:data.id},doc);
  227. if(!unitPrice){
  228. throw "没有找到对应的单价";
  229. }
  230. //查找是否是属于某个项目工料机的组成物
  231. let mixRatioModel = new MixRatioModel();
  232. 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};
  233. let mixRatioList = await mixRatioModel.findDataByCondition(condition, null, false);
  234. let connectKeyMap={};
  235. //找到则计算项目工料机组成物的价格并更新
  236. let rList= [];
  237. if(mixRatioList&&mixRatioList.length>0){
  238. for(let m of mixRatioList){
  239. if(!connectKeyMap.hasOwnProperty(m.connect_key)){//为了去重复,组成物会与其它项目同步,所以有可能重复。
  240. rList.push(await this.updateParentUnitPrice(m,data.field));
  241. connectKeyMap[m.connect_key]=true;
  242. }
  243. }
  244. }
  245. return rList;
  246. }
  247. async updateParentUnitPrice(mixRatio,fieid){
  248. //查找该工料机所有组成物
  249. let indexList = ['code','name','specs','unit','type'];
  250. let mixRatioModel = new MixRatioModel();
  251. let mixRatioMap = await mixRatioModel.findDataByCondition({unit_price_file_id:mixRatio.unit_price_file_id,connect_key:mixRatio.connect_key}, null, false,indexList);
  252. //查找对应的价格
  253. let codeList = [];
  254. let nameList = [];
  255. let specsList= [];
  256. let typeList = [];
  257. let unitList = [];
  258. for(let mk in mixRatioMap){
  259. codeList.push(mixRatioMap[mk].code);
  260. nameList.push(mixRatioMap[mk].name);
  261. specsList.push(mixRatioMap[mk].specs);
  262. typeList.push(mixRatioMap[mk].type);
  263. unitList.push(mixRatioMap[mk].unit);
  264. }
  265. 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}};
  266. let priceMap = await this.findDataByCondition(condition, {_id: 0}, false, indexList);
  267. let sumPrice=0;
  268. for(let pk in priceMap){
  269. let price = parseFloat(priceMap[pk][fieid]);
  270. let consumption = parseFloat(mixRatioMap[pk].consumption);
  271. sumPrice +=scMathUtil.roundTo(price*consumption,-6);
  272. }
  273. sumPrice= scMathUtil.roundTo(sumPrice,-6);
  274. if(sumPrice<=0){
  275. return null;
  276. }
  277. //更新父价格
  278. let keyList = mixRatio.connect_key.split("|-|");
  279. let pcondition = {
  280. unit_price_file_id:mixRatio.unit_price_file_id,
  281. code:keyList[0]
  282. };
  283. for(let i = 1;i<keyList.length;i++){
  284. if(keyList[i]!='null'){
  285. pcondition[indexList[i]]=keyList[i];
  286. }
  287. }
  288. let doc={};
  289. doc[fieid]=sumPrice;
  290. let uprice = await this.db.findAndModify(pcondition,doc);
  291. uprice[fieid]=sumPrice;
  292. return uprice;
  293. }
  294. /**
  295. * 复制单价文件数据
  296. *
  297. * @param {Number} currentUnitPriceId
  298. * @param {Number} changeUnitPriceId
  299. * @return {Promise}
  300. */
  301. async copyNotExist(currentUnitPriceId, changeUnitPriceId) {
  302. let result = false;
  303. // 首先查找原单价文件id下的数据
  304. let currentUnitList = await this.findDataByCondition({unit_price_file_id: currentUnitPriceId}, null, false);
  305. if (currentUnitList === null) {
  306. return result;
  307. }
  308. // 过滤mongoose格式
  309. currentUnitList = JSON.stringify(currentUnitList);
  310. currentUnitList = JSON.parse(currentUnitList);
  311. let codeList = [];
  312. for (let tmp of currentUnitList) {
  313. if (codeList.indexOf(tmp.code) >= 0) {
  314. continue;
  315. }
  316. codeList.push(tmp.code);
  317. }
  318. // 查找即将更替的单价文件是否存在对应的工料机数据
  319. let condition = {unit_price_file_id: changeUnitPriceId, code: {"$in": codeList}};
  320. let targetUnitList = await this.findDataByCondition(condition, null, false, 'code');
  321. // 如果没有重叠的数据则原有的数据都复制一份
  322. let insertData = [];
  323. for (let tmp of currentUnitList) {
  324. if (targetUnitList !== null && targetUnitList[tmp.code] !== undefined) {
  325. continue;
  326. }
  327. // 删除原有id信息
  328. delete tmp._id;
  329. delete tmp.id;
  330. tmp.unit_price_file_id = changeUnitPriceId;
  331. insertData.push(tmp);
  332. }
  333. return insertData.length > 0 ? this.add(currentUnitList) : true;
  334. }
  335. }
  336. export default UnitPriceModel;