project.js 11 KB

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