deal_bills.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/5/8
  7. * @version
  8. */
  9. module.exports = app => {
  10. class DealBills extends app.BaseService {
  11. /**
  12. * 构造函数
  13. * @param ctx
  14. */
  15. constructor(ctx) {
  16. super(ctx);
  17. this.tableName = 'deal_bills';
  18. }
  19. async getSum(tenderId) {
  20. const sql = 'SELECT Sum(`total_price`) As `total_price` ' +
  21. ' From ' + this.tableName +
  22. ' WHERE `tender_id` = ? ';
  23. const sqlParam = [tenderId];
  24. return await this.db.queryOne(sql, sqlParam);
  25. }
  26. /**
  27. * 导入Excel数据(node-xlsx)
  28. *
  29. * @param {Array} sheet - Excel文件中的全部工作表
  30. * @param {Number} tenderId - 所属标段Id
  31. * @returns {Promise<boolean>}
  32. */
  33. async importData(sheet, tenderId) {
  34. let result = false;
  35. const transaction = await this.db.beginTransaction();
  36. try {
  37. const bills = [];
  38. // 识别表头导入
  39. let iCode = -1, iName = -1, iUnit = -1, iUp = -1, iQty = -1, iTp = -1, bCheckCol = false;
  40. for (let iRow = 0; iRow < sheet.data.length; iRow++) {
  41. const row = sheet.data[iRow];
  42. if (!bCheckCol) {
  43. for (let iCol = 0; iCol < row.length; iCol++) {
  44. let value = row[iCol];
  45. if (typeof value !== "string") { continue }
  46. value = this.ctx.helper._.trim(value);
  47. if (['清单编号', '子目号', '子目编号', '编号', '清单号'].indexOf(value) >= 0) iCode = iCol;
  48. if (['清单名称', '名称', '子目名称'].indexOf(value) >= 0) iName = iCol;
  49. if (value.indexOf('单位') >= 0) iUnit = iCol;
  50. if (value.indexOf('单价') >= 0) iUp = iCol;
  51. if (value.indexOf('数量') >= 0) iQty = iCol;
  52. if (value.indexOf('金额') >= 0 || value.indexOf('合价') >= 0) iTp = iCol;
  53. }
  54. bCheckCol = (iCode >= 0 && iName >= 0 && iUnit >= 0 && iUp >= 0 && iQty >= 0);
  55. if (!bCheckCol) {
  56. iCode = -1;
  57. iName = -1;
  58. iUnit = -1;
  59. iUp = -1;
  60. iQty = -1;
  61. }
  62. } else {
  63. const code = this.ctx.helper._.trim(row[iCode]);
  64. if (this.ctx.helper.validBillsCode(code)) {
  65. const data = {
  66. deal_id: bills.length + 1,
  67. tender_id: tenderId,
  68. code: code,
  69. name: row[iName],
  70. unit: row[iUnit],
  71. unit_price: row[iUp],
  72. quantity: row[iQty],
  73. };
  74. if ((data.unit_price && !this._.isNumber(data.unit_price)) || (data.quantity && !this._.isNumber(data.quantity))) {
  75. throw '导入的Excel的数据类型有误,请检查第' + (iRow + 1) + '行';
  76. }
  77. bills.push(data);
  78. }
  79. }
  80. }
  81. if (!bCheckCol) {
  82. throw '导入的Excel表头定义有误,请下载示例检查';
  83. }
  84. // 固定列,从第一行开始导入
  85. // let iCode = 0, iName = 1, iUnit = 2, iUp = 4, iQty = 3;
  86. // for (let iRow = 1; iRow < sheet.data.length; iRow++) {
  87. // const data = {
  88. // deal_id: bills.length + 1,
  89. // tender_id: tenderId,
  90. // code: row[iCode],
  91. // name: row[iName],
  92. // unit: row[iUnit],
  93. // unit_price: row[iUp],
  94. // quantity: row[iQty],
  95. // };
  96. // if (!data.code || data.code === '' || !data.name || data.name === '') continue;
  97. // if ((data.unit_price && !this._.isNumber(data.unit_price)) || (data.quantity && !this._.isNumber(data.quantity))) {
  98. // throw '导入的Excel的数据类型有误,请检查第' + (iRow + 1) + '行';
  99. // }
  100. // bills.push(data);
  101. // }
  102. if (bills.length > 0) {
  103. await transaction.delete(this.tableName, {tender_id: tenderId});
  104. const billsResult = await transaction.insert(this.tableName, bills);
  105. if (billsResult.affectedRows !== bills.length) {
  106. throw '导入签约清单数据出错';
  107. }
  108. } else {
  109. throw 'Excel文件中无签约清单数据';
  110. }
  111. await transaction.commit();
  112. result = true;
  113. } catch (err) {
  114. await transaction.rollback();
  115. throw err;
  116. }
  117. return result;
  118. }
  119. /**
  120. * 导入Excel数据(js-xlsx)
  121. *
  122. * @param {Array} sheet - Excel文件中的全部工作表
  123. * @param {Number} tenderId - 所属标段Id
  124. * @returns {Promise<boolean>}
  125. */
  126. async importDataJsXlsx(sheet, tenderId) {
  127. let result = false;
  128. // 整理数据
  129. const bills = [];
  130. let iCode = -1, iName = -1, iUnit = -1, iUp = -1, iQty = -1, iTp = -1, bCheckCol = false;
  131. for (let iRow = 0; iRow < sheet.rows.length; iRow++) {
  132. const row = sheet.rows[iRow];
  133. if (!bCheckCol) {
  134. for (let iCol = 0; iCol < row.length; iCol++) {
  135. let value = row[iCol];
  136. if (typeof value !== "string") { continue }
  137. value = this.ctx.helper._.trim(value);
  138. if (value === '子目号' || value === '清单编号') iCode = iCol;
  139. if (value.indexOf('名称') >= 0) iName = iCol;
  140. if (value.indexOf('单位') >= 0) iUnit = iCol;
  141. if (value.indexOf('单价') >= 0) iUp = iCol;
  142. if (value.indexOf('数量') >= 0) iQty = iCol;
  143. if (value.indexOf('金额') >= 0 || value.indexOf('合价') >= 0) iTp = iCol;
  144. }
  145. bCheckCol = (iCode >= 0 && iName >= 0 && iUnit >= 0 && iUp >= 0 && iQty >= 0 && iTp >= 0);
  146. if (!bCheckCol) {
  147. iCode = -1;
  148. iName = -1;
  149. iUnit = -1;
  150. iUp = -1;
  151. iQty = -1;
  152. iTp = -1;
  153. }
  154. } else {
  155. const code = this.ctx.helper.replaceReturn(this.ctx.helper._.trim(row[iCode]));
  156. if (!code) continue;
  157. // if (!this.ctx.helper.validBillsCode(code)) continue;
  158. const data = {
  159. id: this.uuid.v4(),
  160. order: bills.length + 1,
  161. tender_id: tenderId,
  162. code: code,
  163. name: this.ctx.helper.replaceReturn(row[iName]),
  164. unit: this.ctx.helper.replaceReturn(row[iUnit]),
  165. unit_price: (row[iUp] === undefined || row[iUp] === null) ? 0 : this._.toNumber(row[iUp]),
  166. quantity: (row[iQty] === undefined || row[iQty] === null) ? 0 : this._.toNumber(row[iQty]),
  167. total_price: (row[iTp] === undefined || row[iTp] === null) ? 0 : this._.toNumber(row[iTp]),
  168. };
  169. if (this._.isNaN(data.unit_price) || this._.isNaN(data.quantity) || this._.isNaN(data.total_price)) {
  170. throw '导入的Excel的数据类型有误,请检查第' + (iRow + 1) + '行';
  171. }
  172. bills.push(data);
  173. }
  174. }
  175. if (!bCheckCol) {
  176. throw '导入的Excel表头定义有误,请下载示例检查';
  177. }
  178. // 写入数据
  179. const transaction = await this.db.beginTransaction();
  180. try {
  181. if (bills.length > 0) {
  182. await transaction.delete(this.tableName, {tender_id: tenderId});
  183. const billsResult = await transaction.insert(this.tableName, bills);
  184. if (billsResult.affectedRows !== bills.length) throw '导入签约清单数据出错';
  185. } else {
  186. throw 'Excel文件中无签约清单数据';
  187. }
  188. await transaction.commit();
  189. result = true;
  190. } catch (err) {
  191. await transaction.rollback();
  192. throw err;
  193. }
  194. return result;
  195. }
  196. /*
  197. * 报表用
  198. * @param {Number} tenderId - 所属标段Id
  199. */
  200. async getDataByTenderId(tenderId) {
  201. const sql = 'SELECT Bills.* FROM ' + this.tableName + ' As Bills WHERE tender_id = ? ORDER BY `order` ASC';
  202. const sqlParam = [tenderId];
  203. return await this.db.query(sql, sqlParam);
  204. // let rst = await this.getDataByCondition({tender_id: tenderId});
  205. // return rst;
  206. }
  207. async _addDatas(data) {
  208. const info = this.ctx.tender.info;
  209. const datas = data instanceof Array ? data : [data];
  210. const insertData = [];
  211. for (const d of datas) {
  212. if (!d.code || !d.order) throw '新增签约清单,提交的数据错误';
  213. const nd = { id: this.uuid.v4(), tender_id: this.ctx.tender.id };
  214. nd.code = d.code;
  215. nd.order = d.order;
  216. if (d.name) nd.name = d.name;
  217. if (d.unit) nd.unit = d.unit;
  218. if (d.unit_price) nd.unit_price = this.ctx.helper.round(d.unit_price, info.decimal.up);
  219. const precision = this.ctx.helper.findPrecision(info.precision, d.unit);
  220. if (d.quantity) {
  221. nd.quantity = this.ctx.helper.round(d.quantity, precision.value);
  222. nd.total_price = this.ctx.helper.mul(nd.unit_price, nd.quantity, info.decimal.tp);
  223. }
  224. insertData.push(nd);
  225. }
  226. const result = await this.db.insert(this.tableName, insertData);
  227. return insertData;
  228. }
  229. async _delDatas (data) {
  230. await this.db.delete(this.tableName, {id: data});
  231. return data;
  232. }
  233. async _updateDatas (data) {
  234. const info = this.ctx.tender.info;
  235. const datas = data instanceof Array ? data : [data];
  236. const orgDatas = await this.getAllDataByCondition({where: {id: this.ctx.helper._.map(datas, 'id')}});
  237. const uDatas = [];
  238. for (const d of datas) {
  239. const od = this.ctx.helper._.find(orgDatas, {id: d.id});
  240. if (!od) continue;
  241. const nd = {id: od.id};
  242. if (d.code !== undefined) nd.code = d.code;
  243. if (d.order !== undefined) nd.order = d.order;
  244. if (d.name !== undefined) nd.name = d.name;
  245. if (d.unit !== undefined) nd.unit = d.unit;
  246. nd.unit_price = d.unit_price !== undefined ? this.ctx.helper.round(d.unit_price, info.decimal.up) : od.unit_price;
  247. const precision = this.ctx.helper.findPrecision(info.precision, d.unit);
  248. if (d.quantity !== undefined) {
  249. nd.quantity = this.ctx.helper.round(d.quantity, precision.value);
  250. nd.total_price = this.ctx.helper.mul(nd.unit_price, nd.quantity, info.decimal.tp);
  251. } else {
  252. nd.quantity = this.ctx.helper.round(od.quantity, precision.value);
  253. nd.total_price = this.ctx.helper.mul(nd.unit_price, nd.quantity, info.decimal.tp);
  254. }
  255. uDatas.push(nd);
  256. }
  257. if (uDatas.length > 0) {
  258. await this.db.updateRows(this.tableName, uDatas);
  259. return uDatas;
  260. } else {
  261. return [];
  262. }
  263. }
  264. async updateDatas(data) {
  265. const result = {add: [], del: [], update: []};
  266. try {
  267. if (data.add) {
  268. result.add = await this._addDatas(data.add);
  269. }
  270. if (data.update) {
  271. result.update = await this._updateDatas(data.update);
  272. }
  273. if (data.del) {
  274. result.del = await this._delDatas(data.del);
  275. }
  276. return result;
  277. } catch (err) {
  278. if (err) result.err = err;
  279. return result;
  280. }
  281. }
  282. }
  283. return DealBills;
  284. }