project.js 9.8 KB

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