project.js 9.5 KB

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