project.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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 updateSjsRela(id, sub, field, key, value) {
  164. const sjsRela = await this.getSjsRela(id);
  165. if (!sjsRela[sub]) throw '数据异常';
  166. const sjsField = sjsRela[sub].find(x => { return x.field === field; });
  167. if (!sjsField) throw '数据异常';
  168. sjsField[key] = value;
  169. await this.db.update(this.tableName, { id: id, sjs_rela: JSON.stringify(sjsRela) });
  170. return sjsField;
  171. }
  172. async updatePageshow(id) {
  173. const result = await this.db.update(this.tableName, {
  174. id, page_show: JSON.stringify(this.ctx.session.sessionProject.page_show),
  175. });
  176. return result.affectedRows === 1;
  177. }
  178. /**
  179. * 校验项目是否存在(项目管理)
  180. * @param {String} token - token
  181. * @return {Promise} Promise
  182. */
  183. verifyManagementProject(token) {
  184. return new Promise((resolve, reject) => {
  185. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/calibration`, {
  186. method: 'POST',
  187. // dateType: 'json',
  188. encoding: 'utf8',
  189. data: {
  190. token,
  191. },
  192. // timeout: 2000,
  193. }).then(({ status, data }) => {
  194. if (status === 200) {
  195. const result = JSON.parse(data.toString()).data;
  196. return resolve(result);
  197. }
  198. return reject(new Error('校验失败'));
  199. }).catch(err => {
  200. console.log(err);
  201. return reject(err);
  202. });
  203. });
  204. }
  205. /**
  206. * 创建项目和账号(项目管理)
  207. * @return {Promise} Promise
  208. */
  209. async addProjectFromManagement() {
  210. const token = this.ctx.helper.createJWT({ account: this.ctx.session.sessionUser.account, code: this.ctx.session.sessionProject.code });
  211. return new Promise((resolve, reject) => {
  212. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/project/add`, {
  213. method: 'POST',
  214. encoding: 'utf8',
  215. data: { token },
  216. }).then(({ status, data }) => {
  217. if (status === 200) {
  218. const result = JSON.parse(data.toString());
  219. return resolve(result);
  220. }
  221. return reject(new Error('添加失败'));
  222. }).catch(err => {
  223. console.log(err);
  224. return reject(err);
  225. });
  226. });
  227. }
  228. async setPmDealCache(pid, data) {
  229. const pmDeal = data.filter(x => { return x.selected; }).map(x => { return x.id; });
  230. await this.cache.set('pmDeal-' + pid, pmDeal.join(','), 'EX', this.ctx.app.config.cacheTime);
  231. }
  232. async getPmDealCache(pid) {
  233. const result = await this.cache.get('pmDeal-' + pid);
  234. return result ? result.split(',') : [];
  235. }
  236. }
  237. return Project;
  238. };