change.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. 'use strict';
  2. /**
  3. *
  4. *
  5. * @author Mai
  6. * @date 2018/8/14
  7. * @version
  8. */
  9. const audit = require('../const/audit');
  10. module.exports = app => {
  11. class Change extends app.BaseService {
  12. /**
  13. * 构造函数
  14. *
  15. * @param {Object} ctx - egg全局变量
  16. * @return {void}
  17. */
  18. constructor(ctx) {
  19. super(ctx);
  20. this.tableName = 'change';
  21. }
  22. /**
  23. * 查找数据
  24. *
  25. * @param {Object} data - 筛选表单中的get数据
  26. * @return {void}
  27. */
  28. searchFilter(data) {
  29. this.initSqlBuilder();
  30. // this.sqlBuilder.columns = ['id', 'username', 'real_name', 'create_time', 'last_login', 'login_ip',
  31. // 'group_id', 'token', 'can_login'];
  32. data.type = parseInt(data.status);
  33. if (data.keyword !== undefined) {
  34. switch (data.type) {
  35. // 用户名
  36. case 1:
  37. this.sqlBuilder.setAndWhere('username', {
  38. value: this.db.escape(data.keyword + '%'),
  39. operate: 'like',
  40. });
  41. break;
  42. // 姓名
  43. case 2:
  44. this.sqlBuilder.setAndWhere('real_name', {
  45. value: this.db.escape(data.keyword + '%'),
  46. operate: 'like',
  47. });
  48. break;
  49. // 联系电话
  50. case 3:
  51. this.sqlBuilder.setAndWhere('telephone', {
  52. value: this.db.escape(data.keyword + '%'),
  53. operate: 'like',
  54. });
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. // 办事处筛选
  61. if (data.office !== undefined && data.office !== '') {
  62. this.sqlBuilder.setAndWhere('office', {
  63. value: this.db.escape(data.office),
  64. operate: '=',
  65. });
  66. }
  67. }
  68. async add(tenderId, userId, code, name) {
  69. const count = await this.count({ tid: tenderId, code });
  70. if (count > 0) {
  71. throw '变更令号重复';
  72. }
  73. // 初始化事务
  74. this.transaction = await this.db.beginTransaction();
  75. let result = false;
  76. try {
  77. const cid = this.uuid.v4();
  78. const change = {
  79. cid,
  80. tid: tenderId,
  81. uid: userId,
  82. status: audit.flow.status.uncheck,
  83. times: 1,
  84. valid: true,
  85. in_time: new Date(),
  86. code,
  87. name,
  88. };
  89. const operate = await this.transaction.insert(this.tableName, change);
  90. if (operate.affectedRows <= 0) {
  91. throw '新建变更令数据失败';
  92. }
  93. // 把提交人信息添加到zh_change_audit
  94. const userInfo = await this.ctx.service.projectAccount.getDataById(userId);
  95. const changeaudit = {
  96. tid: tenderId,
  97. cid,
  98. uid: userId,
  99. name: userInfo.name,
  100. jobs: userInfo.role,
  101. company: userInfo.company,
  102. times: 1,
  103. usite: 0,
  104. usort: 0,
  105. status: 2,
  106. };
  107. await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit);
  108. result = change;
  109. this.transaction.commit();
  110. } catch (error) {
  111. console.log(error);
  112. // 回滚
  113. await this.transaction.rollback();
  114. }
  115. return result;
  116. }
  117. async pendingDatas(tenderId, userId) {
  118. return await this.getAllDataByCondition({
  119. tid: tenderId,
  120. uid: userId,
  121. status: audit.flow.status.checking,
  122. });
  123. }
  124. async uncheckDatas(tenderId, userId) {
  125. return await this.getAllDataByCondition({
  126. tid: tenderId,
  127. uid: userId,
  128. status: audit.flow.status.uncheck,
  129. });
  130. }
  131. async checkingDatas(tenderId, userId) {
  132. return await this.getAllDataByCondition({
  133. tid: tenderId,
  134. uid: userId,
  135. status: audit.flow.status.checking,
  136. });
  137. }
  138. async checkedDatas(tenderId, userId) {
  139. return await this.getAllDataByCondition({
  140. tid: tenderId,
  141. uid: userId,
  142. status: audit.flow.status.checked,
  143. });
  144. }
  145. async checkNoDatas(tenderId, userId) {
  146. return await this.getAllDataByCondition({
  147. tid: tenderId,
  148. uid: userId,
  149. status: audit.flow.status.checkNo,
  150. });
  151. }
  152. async checkNoCount(tenderId, userId) {
  153. return await this.count({
  154. tid: tenderId,
  155. uid: userId,
  156. status: audit.flow.status.checkNo,
  157. });
  158. }
  159. /**
  160. * 获取变更令列表
  161. * @param {int} tenderId - 标段id
  162. * @param {int} status - 状态
  163. * @return {object} list - 列表
  164. */
  165. async getListByStatus(tenderId, status = 0) {
  166. let sql = '';
  167. let sqlParam = '';
  168. switch (status) {
  169. case 0:// 包含你的所有变更令
  170. sql = 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
  171. '(a.uid = ? OR (a.status != 1 AND a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid))) ORDER BY a.in_time DESC';
  172. sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  173. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  174. break;
  175. case 1:// 待处理(你的)
  176. sql = 'SELECT a.* FROM ?? as a WHERE cid in(SELECT b.cid FROM ?? as b WHERE tid = ? AND uid = ? AND status = ?) ORDER BY in_time DESC';
  177. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName, tenderId, this.ctx.session.sessionUser.accountId, 2];
  178. break;
  179. case 5:// 待上报(所有的)PS:取未上报和退回的变更令
  180. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  181. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  182. '(a.status = 1 OR a.status = 5) AND a.tid = ? ORDER BY a.in_time DESC';
  183. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  184. this.ctx.session.sessionUser.accountId, tenderId];
  185. break;
  186. case 2:// 进行中(所有的)
  187. case 3:// 已完成(所有的)
  188. case 4:// 终止(所有的)
  189. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  190. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  191. 'a.status = ? AND a.tid = ? ORDER BY a.in_time DESC';
  192. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  193. this.ctx.session.sessionUser.accountId, status, tenderId];
  194. break;
  195. default:
  196. break;
  197. }
  198. const limit = this.app.config.pageSize;
  199. const offset = limit * (this.ctx.page - 1);
  200. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  201. sql += ' LIMIT ' + limitString;
  202. const list = await this.db.query(sql, sqlParam);
  203. return list;
  204. }
  205. /**
  206. * 获取变更令个数
  207. * @param {int} tenderId - 标段id
  208. * @param {int} status - 状态
  209. * @return {void}
  210. */
  211. async getCountByStatus(tenderId, status) {
  212. switch (status) {
  213. case 0:// 包含你的所有变更令
  214. const sql = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  215. '(a.uid = ? OR a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid))';
  216. const sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  217. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  218. const result = await this.db.query(sql, sqlParam);
  219. return result[0].count;
  220. case 1:// 待处理(你的)
  221. return await this.ctx.service.changeAudit.count({
  222. tid: tenderId,
  223. uid: this.ctx.session.sessionUser.accountId,
  224. status: 2,
  225. });
  226. case 5:// 待上报(所有的)PS:取未上报和退回的变更令
  227. const sql2 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  228. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) ' +
  229. 'AND (a.status = 1 OR a.status = 5) AND a.tid = ?';
  230. const sqlParam2 = [this.tableName, this.ctx.service.changeAudit.tableName,
  231. this.ctx.session.sessionUser.accountId, tenderId];
  232. const result2 = await this.db.query(sql2, sqlParam2);
  233. return result2[0].count;
  234. case 2:// 进行中(所有的)
  235. case 3:// 已完成(所有的)
  236. case 4:// 终止(所有的)
  237. const sql3 = 'SELECT count(*) AS count FROM ?? AS a WHERE ' +
  238. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND a.status = ? AND a.tid = ?';
  239. const sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName,
  240. this.ctx.session.sessionUser.accountId, status, tenderId];
  241. const result3 = await this.db.query(sql3, sqlParam3);
  242. return result3[0].count;
  243. default:
  244. break;
  245. }
  246. }
  247. /**
  248. * 上报或重新上报或保存修改功能
  249. * @param {int} postData - 表单提交的数据
  250. * @param {int} tenderId - 标段id
  251. * @return {void}
  252. */
  253. async save(postData, tenderId) {
  254. // 初始化事务
  255. this.transaction = await this.db.beginTransaction();
  256. let result = false;
  257. try {
  258. // 变更令信息
  259. const changeInfo = await this.getDataByCondition({ cid: postData.cid });
  260. // 该变更令原报人信息
  261. const lastUser = await this.ctx.service.changeAudit.getLastUser(changeInfo.cid, changeInfo.times, 0);
  262. // 先删除本次原有的变更审批人和清单
  263. await this.ctx.service.changeAudit.deleteAuditData(this.transaction, changeInfo.cid, changeInfo.times);
  264. await this.transaction.delete(this.ctx.service.changeAuditList.tableName, { cid: changeInfo.cid });
  265. let change_status = false;
  266. // 获取变更令提交状态
  267. if (postData.changestatus !== undefined && parseInt(postData.changestatus) === 1) {
  268. change_status = true;
  269. // 更新原报人审批状态
  270. await this.transaction.update(this.ctx.service.changeAudit.tableName, { id: lastUser.id, status: 3 });
  271. }
  272. // 再插入postData里的变更审批人和清单
  273. if (postData.changeaudit !== undefined && postData.changeaudit !== '') {
  274. const changeAudit = postData.changeaudit.split(',');
  275. const insertCA = [];
  276. let uSite = 1;
  277. let uSort = parseInt(lastUser.usort) + 1;
  278. for (const [index, ca] of changeAudit.entries()) {
  279. const auditInfo = ca.split('/%/');
  280. const uStatus = change_status && index === 0 ? 2 : 1;
  281. const sin_time = change_status && index === 0 ? new Date() : null;
  282. const caArray = {
  283. tid: tenderId,
  284. cid: changeInfo.cid,
  285. uid: auditInfo[0],
  286. name: auditInfo[1],
  287. jobs: auditInfo[2],
  288. company: auditInfo[3],
  289. times: changeInfo.times,
  290. usite: uSite,
  291. usort: uSort,
  292. status: uStatus,
  293. sin_time,
  294. };
  295. uSite++;
  296. uSort++;
  297. insertCA.push(caArray);
  298. }
  299. await this.transaction.insert(this.ctx.service.changeAudit.tableName, insertCA);
  300. }
  301. let changeList = [];
  302. if (postData.changelist !== undefined && postData.changelist !== '') {
  303. changeList = postData.changelist.split('^_^');
  304. }
  305. let changeWhiteList = [];
  306. if (postData.changewhitelist !== undefined && postData.changewhitelist !== '') {
  307. changeWhiteList = postData.changewhitelist.split('^_^');
  308. }
  309. changeList.push.apply(changeList, changeWhiteList);
  310. const insertCL = [];
  311. let total_price = 0;
  312. for (const cl of changeList) {
  313. const clInfo = cl.split(';');
  314. const clArray = {
  315. tid: tenderId,
  316. cid: changeInfo.cid,
  317. lid: clInfo[7],
  318. code: clInfo[0],
  319. name: clInfo[1],
  320. unit: clInfo[2],
  321. unit_price: clInfo[3],
  322. oamount: clInfo[4],
  323. camount: clInfo[5],
  324. samount: '',
  325. detail: clInfo[6],
  326. };
  327. insertCL.push(clArray);
  328. total_price += this.ctx.helper.accMul(clArray.unit_price, clArray.camount);
  329. }
  330. await this.transaction.insert(this.ctx.service.changeAuditList.tableName, insertCL);
  331. // 修改变更令基本数据
  332. const cArray = {
  333. code: postData.code,
  334. name: postData.name,
  335. peg: postData.peg,
  336. org_name: postData.org_name,
  337. org_code: postData.org_code,
  338. new_name: postData.new_name,
  339. new_code: postData.new_code,
  340. content: postData.content,
  341. basis: postData.basis,
  342. memo: postData.memo,
  343. type: postData.type.join(','),
  344. class: postData.class,
  345. quality: postData.quality,
  346. company: postData.company,
  347. charge: postData.charge,
  348. total_price,
  349. };
  350. const options = {
  351. where: {
  352. cid: changeInfo.cid,
  353. },
  354. };
  355. if (change_status) {
  356. cArray.status = 2;
  357. cArray.cin_time = Date.parse(new Date()) / 1000;
  358. }
  359. await this.transaction.update(this.tableName, cArray, options);
  360. await this.transaction.commit();
  361. result = true;
  362. } catch (error) {
  363. await this.transaction.rollback();
  364. result = false;
  365. }
  366. return result;
  367. }
  368. }
  369. return Change;
  370. };