project.js 9.6 KB

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