tender.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. 'use strict';
  2. /**
  3. * 标段数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/30
  7. * @version
  8. */
  9. const tenderConst = require('../const/tender');
  10. const auditConst = require('../const/audit');
  11. const commonQueryColumns = ['id', 'project_id', 'name', 'status', 'category', 'ledger_times', 'ledger_status', 'measure_type', 'user_id'];
  12. module.exports = app => {
  13. class Tender extends app.BaseService {
  14. /**
  15. * 构造函数
  16. *
  17. * @param {Object} ctx - egg全局变量
  18. * @return {void}
  19. */
  20. constructor(ctx) {
  21. super(ctx);
  22. this.tableName = 'tender';
  23. // 状态相关
  24. this.status = {
  25. TRY: 1,
  26. NORMAL: 2,
  27. DISABLE: 3,
  28. };
  29. this.displayStatus = [];
  30. this.displayStatus[this.status.TRY] = '试用';
  31. this.displayStatus[this.status.NORMAL] = '正常';
  32. this.displayStatus[this.status.DISABLE] = '禁用';
  33. this.statusClass = [];
  34. this.statusClass[this.status.TRY] = 'warning';
  35. this.statusClass[this.status.NORMAL] = 'success';
  36. this.statusClass[this.status.DISABLE] = 'danger';
  37. }
  38. /**
  39. * 数据规则
  40. *
  41. * @param {String} scene - 场景
  42. * @return {Object} - 返回数据规则
  43. */
  44. rule(scene) {
  45. let rule = {};
  46. switch (scene) {
  47. case 'add':
  48. rule = {
  49. name: { type: 'string', required: true, min: 2 },
  50. type: { type: 'string', required: true, min: 1 },
  51. };
  52. break;
  53. case 'save':
  54. rule = {
  55. name: { type: 'string', required: true, min: 2 },
  56. type: { type: 'string', required: true, min: 1 },
  57. };
  58. default:
  59. break;
  60. }
  61. return rule;
  62. }
  63. /**
  64. * 获取你所参与的标段的列表
  65. *
  66. * @return {Array} - 返回标段数据
  67. */
  68. async getList() {
  69. // 获取当前项目信息
  70. const session = this.ctx.session;
  71. const sql = 'SELECT t.`id`, t.`project_id`, t.`name`, t.`status`, t.`category`, t.`ledger_times`, t.`ledger_status`, t.`measure_type`, t.`user_id` '+
  72. ' FROM ?? As t ' +
  73. ' WHERE t.`project_id` = ? AND (' +
  74. // 创建的标段
  75. ' t.`user_id` = ?' +
  76. // 参与审批 台账 的标段
  77. ' OR (t.`ledger_status` != ' + auditConst.ledger.status.uncheck + ' AND ' +
  78. ' t.id IN ( SELECT la.`tender_id` FROM ?? As la WHERE la.`audit_id` = ? GROUP BY la.`tender_id`))' +
  79. // 参与审批 计量期 的标段
  80. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  81. ' t.id IN ( SELECT sa.`tid` FROM ?? As sa WHERE sa.`aid` = ? GROUP BY sa.`tid`))' +
  82. // 参与审批 变更令 的标段
  83. ' OR (t.`ledger_status` = ' + auditConst.ledger.status.checked + ' AND ' +
  84. ' t.id IN ( SELECT ca.`tid` FROM ?? AS ca WHERE ca.`uid` = ? GROUP BY ca.`tid`))' +
  85. // 未参与,但可见的标段
  86. ')';
  87. const sqlParam = [this.tableName, session.sessionProject.id, session.sessionUser.accountId,
  88. this.ctx.service.ledgerAudit.tableName, session.sessionUser.accountId,
  89. this.ctx.service.stageAudit.tableName, session.sessionUser.accountId,
  90. this.ctx.service.changeAudit.tableName, session.sessionUser.accountId];
  91. const list = await this.db.query(sql, sqlParam);
  92. for (const l of list) {
  93. l.category = l.category && l.category !== '' ? JSON.parse(l.category) : null;
  94. }
  95. return list;
  96. }
  97. async getTender (id) {
  98. this.initSqlBuilder();
  99. this.sqlBuilder.setAndWhere('id', {
  100. value: id,
  101. operate: '=',
  102. });
  103. this.sqlBuilder.columns = commonQueryColumns;
  104. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  105. const tender = await this.db.queryOne(sql, sqlParam);
  106. if (tender) {
  107. tender.category = tender.category && tender.category !== '' ? JSON.parse(tender.category) : null;
  108. }
  109. return tender;
  110. }
  111. /**
  112. * 新增标段
  113. *
  114. * @param {Object} data - 提交的数据
  115. * @return {Boolean} - 返回新增结果
  116. */
  117. async add(data) {
  118. let result = false;
  119. this.transaction = await this.db.beginTransaction();
  120. try {
  121. // 获取当前用户信息
  122. const sessionUser = this.ctx.session.sessionUser;
  123. // 获取当前项目信息
  124. const sessionProject = this.ctx.session.sessionProject;
  125. const insertData = {
  126. name: data.name,
  127. status: tenderConst.status.APPROVAL,
  128. project_id: sessionProject.id,
  129. user_id: sessionUser.accountId,
  130. create_time: new Date(),
  131. category: JSON.stringify(data.category),
  132. };
  133. const operate = await this.transaction.insert(this.tableName, insertData);
  134. result = operate.insertId > 0;
  135. if (!result) {
  136. throw '新增标段数据失败';
  137. }
  138. // 获取标段项目节点模板
  139. const tenderNodeTemplateData = await this.ctx.service.tenderNodeTemplate.getData();
  140. // 复制模板数据到标段数据表
  141. result = await this.ctx.service.ledger.innerAdd(tenderNodeTemplateData, operate.insertId, this.transaction);
  142. if (!result) {
  143. throw '新增标段项目节点失败';
  144. }
  145. // 获取合同支付模板 并添加到标段
  146. result = await this.ctx.service.pay.addDefaultPayData(operate.insertId, this.transaction);
  147. if (!result) {
  148. throw '新增合同支付数据失败';
  149. }
  150. await this.transaction.commit();
  151. return await this.getTender(operate.insertId);
  152. } catch (error) {
  153. await this.transaction.rollback();
  154. throw error;
  155. }
  156. }
  157. /**
  158. * 保存标段
  159. *
  160. * @param {Object} postData - 表单post过来的数据
  161. * @param {Number} id - 用于判断修改还是新增的id
  162. * @return {Boolean} - 返回执行结果
  163. */
  164. async save(postData, id = 0) {
  165. id = parseInt(id);
  166. const rowData = {
  167. id: id,
  168. name: postData.name,
  169. type: postData.type,
  170. category: JSON.stringify(postData.category),
  171. };
  172. const result = await this.db.update(this.tableName, rowData);
  173. return result.affectedRows > 0;
  174. }
  175. /**
  176. * 假删除
  177. *
  178. * @param {Number} id - 删除的id
  179. * @return {Boolean} - 删除结果
  180. */
  181. async deleteTenderById(id) {
  182. const updateData = {
  183. status: this.status.DISABLE,
  184. id,
  185. };
  186. const result = this.db.update(this.tableName, updateData);
  187. return result.affectedRows > 0;
  188. }
  189. /**
  190. * 真删除
  191. * @param {Number} id - 删除的标段id
  192. * @returns {Promise<boolean>} - 结果
  193. */
  194. async deleteTenderNoBackup(id) {
  195. const transaction = await this.db.beginTransaction();
  196. try {
  197. await transaction.delete(this.tableName, {id: id});
  198. await transaction.delete(this.ctx.service.tenderInfo.tableName, {tid: id});
  199. await transaction.delete(this.ctx.service.ledger.tableName, {tender_id: id});
  200. await transaction.delete(this.ctx.service.ledgerAudit.tableName, {tender_id: id});
  201. await transaction.delete(this.ctx.service.ledgerAudit.tableName + '_copy', {tender_id: id});
  202. await transaction.delete(this.ctx.service.pos.tableName, {tid: id});
  203. //await transaction.delete(this.ctx.service.pay.tableName, {tid: id});
  204. await transaction.delete(this.ctx.service.stage.tableName, {tid: id});
  205. await transaction.delete(this.ctx.service.stageAudit.tableName, {tid: id});
  206. await transaction.delete(this.ctx.service.stageBills.tableName, {tid: id});
  207. await transaction.delete(this.ctx.service.stagePos.tableName, {tid: id});
  208. await transaction.delete(this.ctx.service.stageDetail.tableName, {tid: id});
  209. //await transaction.delete(this.ctx.service.stagePay.tableName, {tid: id});
  210. await transaction.commit();
  211. return true;
  212. } catch (err) {
  213. console.log(err);
  214. await transaction.rollback();
  215. return false;
  216. }
  217. }
  218. /**
  219. * 切换标段
  220. *
  221. * @param {Number} tenderId - 标段id
  222. * @return {Boolean} - 返回切换结果
  223. */
  224. async switchTender(tenderId) {
  225. // 获取该用户拥有的项目数据
  226. const sessionUser = this.ctx.session.sessionUser;
  227. const tenderInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  228. let result = false;
  229. // 判断切换的标段是否属于对应用户
  230. if (tenderInfo.length < 0) {
  231. return result;
  232. }
  233. let targetTender = {};
  234. for (const tmp of tenderInfo) {
  235. if (tmp.id === tenderId) {
  236. result = true;
  237. targetTender = tmp;
  238. }
  239. }
  240. // 成功后更改session
  241. if (result) {
  242. this.ctx.session.sessionTender = {
  243. id: targetTender.id,
  244. name: targetTender.name,
  245. userAccount: targetTender.user_account,
  246. };
  247. }
  248. return result;
  249. }
  250. }
  251. return Tender;
  252. };