change.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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: code});
  70. if (count > 0) {
  71. throw '变更令号重复';
  72. }
  73. // 初始化事务
  74. this.transaction = await this.db.beginTransaction();
  75. let result = false;
  76. try {
  77. let cid = this.uuid.v4();
  78. const change = {
  79. cid: 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: code,
  87. name: 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. this.ctx.service.changeAudit.transaction = this.transaction;
  95. const userInfo = await this.ctx.service.projectAccount.getDataById(userId);
  96. const changeaudit = {
  97. tid: tenderId,
  98. cid: cid,
  99. uid: userId,
  100. name: userInfo.name,
  101. jobs: userInfo.role,
  102. company: userInfo.company,
  103. times: 1,
  104. usite: 0,
  105. usort: 0,
  106. status: 2
  107. };
  108. await this.transaction.insert(this.ctx.service.changeAudit.tableName, changeaudit);
  109. result = change;
  110. this.transaction.commit();
  111. } catch (error) {
  112. console.log(error);
  113. // 回滚
  114. await this.transaction.rollback();
  115. }
  116. return result;
  117. }
  118. async pendingDatas(tenderId, userId) {
  119. return await this.getAllDataByCondition({
  120. tid: tenderId,
  121. uid: userId,
  122. status: audit.flow.status.checking,
  123. });
  124. }
  125. async uncheckDatas(tenderId, userId) {
  126. return await this.getAllDataByCondition({
  127. tid: tenderId,
  128. uid: userId,
  129. status: audit.flow.status.uncheck,
  130. });
  131. }
  132. async checkingDatas(tenderId, userId) {
  133. return await this.getAllDataByCondition({
  134. tid: tenderId,
  135. uid: userId,
  136. status: audit.flow.status.checking,
  137. });
  138. }
  139. async checkedDatas(tenderId, userId) {
  140. return await this.getAllDataByCondition({
  141. tid: tenderId,
  142. uid: userId,
  143. status: audit.flow.status.checked,
  144. });
  145. }
  146. async checkNoDatas(tenderId, userId) {
  147. return await this.getAllDataByCondition({
  148. tid: tenderId,
  149. uid: userId,
  150. status: audit.flow.status.checkNo,
  151. });
  152. }
  153. async checkNoCount(tenderId, userId) {
  154. return await this.count({
  155. tid: tenderId,
  156. uid: userId,
  157. status: audit.flow.status.checkNo,
  158. });
  159. }
  160. /**
  161. * 获取变更令列表
  162. * @param tenderId 标段id
  163. * @param userId 创建者id
  164. * @param status 状态
  165. * @return {Promise.<void>}
  166. */
  167. async getListByStatus(tenderId, status = 0) {
  168. let sql = '';
  169. let sqlParam = '';
  170. switch (status) {
  171. case 0:
  172. // 包含你的所有变更令
  173. sql = 'SELECT a.* FROM ?? AS a WHERE a.tid = ? AND ' +
  174. '(a.uid = ? OR (a.status != 1 AND a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid)))';
  175. sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  176. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  177. break;
  178. case 1:
  179. // 待处理(你的)
  180. sql = 'SELECT * FROM ?? WHERE tid = ? AND uid = ? AND status = ?';
  181. sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId, status];
  182. break;
  183. case 2:
  184. // 待上报(所有的)PS:取未上报和退回的变更令
  185. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  186. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  187. '(a.status = 1 OR a.status = 5) AND a.tid = ?';
  188. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  189. this.ctx.session.sessionUser.accountId, tenderId];
  190. break;
  191. case 3:
  192. // 进行中(所有的)
  193. case 4:
  194. // 已完成(所有的)
  195. case 5:
  196. // 终止(所有的)
  197. sql = 'SELECT a.* FROM ?? AS a WHERE ' +
  198. 'a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND ' +
  199. 'a.status = ? AND a.tid = ?';
  200. sqlParam = [this.tableName, this.ctx.service.changeAudit.tableName,
  201. this.ctx.session.sessionUser.accountId, status, tenderId];
  202. break;
  203. default:
  204. break;
  205. }
  206. const limit = this.app.config.pageSize;
  207. const offset = limit * (this.ctx.page - 1);
  208. const limitString = offset >= 0 ? offset + ',' + limit : limit;
  209. sql += ' LIMIT ' + limitString;
  210. const list = await this.db.query(sql, sqlParam);
  211. return list;
  212. }
  213. /**
  214. * 获取变更令个数
  215. * @param tenderId 标段id
  216. * @param userId 创建者id
  217. * @param status 状态
  218. * @return {Promise.<*>}
  219. */
  220. async getCountByStatus(tenderId, status) {
  221. switch (status) {
  222. case 0:
  223. // 包含你的所有变更令
  224. let sql = 'SELECT count(*) AS count FROM ?? AS a WHERE a.tid = ? AND ' +
  225. '(a.uid = ? OR a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid))';
  226. let sqlParam = [this.tableName, tenderId, this.ctx.session.sessionUser.accountId,
  227. this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId];
  228. let result = await this.db.query(sql, sqlParam);
  229. return result[0]['count'];
  230. break;
  231. case 1:
  232. // 待处理(你的)
  233. return await this.count({
  234. tid: tenderId,
  235. uid: this.ctx.session.sessionUser.accountId,
  236. status: status,
  237. });
  238. break;
  239. case 2:
  240. // 待上报(所有的)PS:取未上报和退回的变更令
  241. let sql2 = 'SELECT count(*) AS count FROM ?? AS a WHERE a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND (a.status = 1 OR a.status = 5) AND a.tid = ?';
  242. let sqlParam2 = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, tenderId];
  243. let result2 = await this.db.query(sql2, sqlParam2);
  244. return result2[0]['count'];
  245. break;
  246. case 3:
  247. // 进行中(所有的)
  248. case 4:
  249. // 已完成(所有的)
  250. case 5:
  251. // 终止(所有的)
  252. let sql3 = 'SELECT count(*) AS count FROM ?? AS a WHERE a.cid IN (SELECT b.cid FROM ?? AS b WHERE b.uid = ? GROUP BY b.cid) AND a.status = ? AND a.tid = ?';
  253. let sqlParam3 = [this.tableName, this.ctx.service.changeAudit.tableName, this.ctx.session.sessionUser.accountId, status, tenderId];
  254. let result3 = await this.db.query(sql3, sqlParam3);
  255. return result3[0]['count'];
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. }
  262. return Change;
  263. };