project.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. 'use strict';
  2. /**
  3. * 项目数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. const imType = require('../const/tender').imType;
  10. const defaultFunRela = {
  11. banOver: true,
  12. hintOver: true,
  13. minusNoValue: true,
  14. imType: imType.zl.value,
  15. needGcl: false,
  16. };
  17. const sjsRelaConst = require('../const/setting').sjsRela;
  18. module.exports = app => {
  19. class Project extends app.BaseService {
  20. /**
  21. * 构造函数
  22. *
  23. * @param {Object} ctx - egg全局变量
  24. * @return {void}
  25. */
  26. constructor(ctx) {
  27. super(ctx);
  28. this.tableName = 'project';
  29. // 状态相关
  30. this.status = {
  31. TRY: 1,
  32. NORMAL: 2,
  33. DISABLE: 3,
  34. };
  35. }
  36. /**
  37. * 数据规则
  38. *
  39. * @param {String} scene - 场景
  40. * @return {Object} - 返回数据规则
  41. */
  42. rule(scene) {
  43. let rule = {};
  44. switch (scene) {
  45. case 'saveInfo':
  46. rule = {
  47. name: { type: 'string', required: true, min: 2 },
  48. };
  49. break;
  50. case 'fun':
  51. rule = {
  52. imType: {type: 'enum', values: [imType.tz.value, imType.zl.value, imType.bb.value, imType.bw.value], required: true},
  53. banOver: {type: 'bool', required: true,},
  54. hintOver: {type: 'bool', required: true,},
  55. minusNoValue: {type: 'bool', required: true,}
  56. };
  57. break;
  58. default:
  59. break;
  60. }
  61. return rule;
  62. }
  63. /**
  64. * 根据项目code获取项目数据
  65. *
  66. * @param {String} code - 项目code
  67. * @return {Object} - 返回项目数据
  68. */
  69. async getProjectByCode(code) {
  70. // 获取项目状态为非禁止的项目
  71. this.initSqlBuilder();
  72. this.sqlBuilder.setAndWhere('code', {
  73. value: this.db.escape(code),
  74. operate: '=',
  75. });
  76. this.sqlBuilder.setAndWhere('status', {
  77. value: this.status.DISABLE,
  78. operate: '<',
  79. });
  80. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  81. const projectData = await this.db.queryOne(sql, sqlParam);
  82. return projectData;
  83. }
  84. /**
  85. * 根据项目code获取项目数据
  86. *
  87. * @param {int} prjId - id
  88. * @return {Object} - 返回项目数据
  89. */
  90. async getProjectById(prjId) {
  91. // 获取项目状态为非禁止的项目
  92. this.initSqlBuilder();
  93. this.sqlBuilder.setAndWhere('id', {
  94. value: this.db.escape(prjId),
  95. operate: '=',
  96. });
  97. this.sqlBuilder.setAndWhere('status', {
  98. value: this.status.DISABLE,
  99. operate: '<',
  100. });
  101. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  102. const projectData = await this.db.queryOne(sql, sqlParam);
  103. return projectData;
  104. }
  105. /**
  106. * 切换项目
  107. *
  108. * @param {Number} projectId - 项目id
  109. * @return {Boolean} - 返回切换结果
  110. */
  111. async switchProject(projectId) {
  112. // 获取该用户拥有的项目数据
  113. const sessionUser = this.ctx.session.sessionUser;
  114. const projectInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  115. let result = false;
  116. // 判断切换的项目是否属于对应用户
  117. if (projectInfo.length < 0) {
  118. return result;
  119. }
  120. let targetProject = {};
  121. for (const tmp of projectInfo) {
  122. if (tmp.id === projectId) {
  123. result = true;
  124. targetProject = tmp;
  125. }
  126. }
  127. // 成功后更改session
  128. if (result) {
  129. this.ctx.session.sessionProject = {
  130. id: targetProject.id,
  131. name: targetProject.name,
  132. userAccount: targetProject.user_account,
  133. };
  134. }
  135. return result;
  136. }
  137. /**
  138. * 功能设置
  139. * @param id
  140. * @returns {Promise<null>}
  141. */
  142. async getFunRela(id) {
  143. const projectData = await this.db.get(this.tableName, { id });
  144. const result = projectData.fun_rela ? JSON.parse(projectData.fun_rela) : {};
  145. this.ctx.helper._.defaults(result, defaultFunRela);
  146. return result;
  147. }
  148. async updateFunRela(id, data) {
  149. const result = await this.db.update(this.tableName, {
  150. id: id, fun_rela: JSON.stringify({
  151. banOver: data.banOver, hintOver: data.hintOver,
  152. imType: data.imType, needGcl: data.needGcl, minusNoValue: data.minusNoValue,
  153. }),
  154. });
  155. return result.affectedRows === 1;
  156. }
  157. async getSjsRela(id) {
  158. const projectData = await this.db.get(this.tableName, { id });
  159. const result = projectData.sjs_rela ? JSON.parse(projectData.sjs_rela) : {};
  160. this.ctx.helper._.defaults(result, sjsRelaConst);
  161. return result;
  162. }
  163. async getTenderSjsRela(id, show) {
  164. const projectData = await this.db.get(this.tableName, { id });
  165. const result = projectData.sjs_rela ? JSON.parse(projectData.sjs_rela) : {};
  166. this.ctx.helper._.defaults(result, sjsRelaConst);
  167. if (!show) result.ledgerCol.forEach(x => { x.show = 0 });
  168. return result;
  169. }
  170. async updateSjsRela(id, sub, field, key, value) {
  171. const sjsRela = await this.getSjsRela(id);
  172. if (!sjsRela[sub]) throw '数据异常';
  173. const sjsField = sjsRela[sub].find(x => { return x.field === field; });
  174. if (!sjsField) throw '数据异常';
  175. sjsField[key] = value;
  176. await this.db.update(this.tableName, { id: id, sjs_rela: JSON.stringify(sjsRela) });
  177. return sjsField;
  178. }
  179. async updatePageshow(id) {
  180. const result = await this.db.update(this.tableName, {
  181. id, page_show: JSON.stringify(this.ctx.session.sessionProject.page_show),
  182. });
  183. return result.affectedRows === 1;
  184. }
  185. /**
  186. * 校验项目是否存在(项目管理)
  187. * @param {String} token - token
  188. * @return {Promise} Promise
  189. */
  190. verifyManagementProject(token) {
  191. return new Promise((resolve, reject) => {
  192. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/calibration`, {
  193. method: 'POST',
  194. // dateType: 'json',
  195. encoding: 'utf8',
  196. data: {
  197. token,
  198. },
  199. // timeout: 2000,
  200. }).then(({ status, data }) => {
  201. if (status === 200) {
  202. const result = JSON.parse(data.toString()).data;
  203. return resolve(result);
  204. }
  205. return reject(new Error('校验失败'));
  206. }).catch(err => {
  207. console.log(err);
  208. return reject(err);
  209. });
  210. });
  211. }
  212. /**
  213. * 创建项目和账号(项目管理)
  214. * @return {Promise} Promise
  215. */
  216. async addProjectFromManagement() {
  217. const token = this.ctx.helper.createJWT({ account: this.ctx.session.sessionUser.account, code: this.ctx.session.sessionProject.code });
  218. return new Promise((resolve, reject) => {
  219. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/project/add`, {
  220. method: 'POST',
  221. encoding: 'utf8',
  222. data: { token },
  223. }).then(({ status, data }) => {
  224. if (status === 200) {
  225. const result = JSON.parse(data.toString());
  226. return resolve(result);
  227. }
  228. return reject(new Error('添加失败'));
  229. }).catch(err => {
  230. console.log(err);
  231. return reject(err);
  232. });
  233. });
  234. }
  235. async setPmDealCache(pid, data) {
  236. const pmDeal = data.filter(x => { return x.selected; }).map(x => { return x.id; });
  237. await this.cache.set('pmDeal-' + pid, pmDeal.join(','), 'EX', this.ctx.app.config.cacheTime);
  238. }
  239. async getPmDealCache(pid) {
  240. const result = await this.cache.get('pmDeal-' + pid);
  241. return result ? result.split(',') : [];
  242. }
  243. }
  244. return Project;
  245. };