project.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. /**
  148. * 功能设置
  149. * @param id
  150. * @returns {Promise<null>}
  151. */
  152. async getFunRela(id) {
  153. const projectData = await this.db.get(this.tableName, { id });
  154. const result = projectData.fun_rela ? JSON.parse(projectData.fun_rela) : {};
  155. this.ctx.helper._.defaults(result, defaultFunRela);
  156. return result;
  157. }
  158. async updateFunRela(id, data) {
  159. const result = await this.db.update(this.tableName, {
  160. id: id, fun_rela: JSON.stringify({
  161. banOver: data.banOver, hintOver: data.hintOver, banMinusChangeBills: data.banMinusChangeBills,
  162. imType: data.imType, needGcl: data.needGcl, minusNoValue: data.minusNoValue,
  163. lockPayExpr: data.lockPayExpr, showMinusCol: data.showMinusCol,
  164. }),
  165. });
  166. return result.affectedRows === 1;
  167. }
  168. async getSjsRela(id) {
  169. const projectData = await this.db.get(this.tableName, { id });
  170. const result = projectData.sjs_rela ? JSON.parse(projectData.sjs_rela) : {};
  171. this.ctx.helper._.defaults(result, sjsRelaConst);
  172. return result;
  173. }
  174. async getTenderSjsRela(id, show) {
  175. const projectData = await this.db.get(this.tableName, { id });
  176. const result = projectData.sjs_rela ? JSON.parse(projectData.sjs_rela) : {};
  177. this.ctx.helper._.defaults(result, sjsRelaConst);
  178. if (!show) result.ledgerCol.forEach(x => { x.show = 0 });
  179. return result;
  180. }
  181. async updateSjsRela(id, sub, field, key, value) {
  182. const sjsRela = await this.getSjsRela(id);
  183. if (!sjsRela[sub]) throw '数据异常';
  184. const sjsField = sjsRela[sub].find(x => { return x.field === field; });
  185. if (!sjsField) throw '数据异常';
  186. sjsField[key] = value;
  187. await this.db.update(this.tableName, { id: id, sjs_rela: JSON.stringify(sjsRela) });
  188. return sjsField;
  189. }
  190. async updatePageshow(id) {
  191. const result = await this.db.update(this.tableName, {
  192. id, page_show: JSON.stringify(this.ctx.session.sessionProject.page_show),
  193. });
  194. return result.affectedRows === 1;
  195. }
  196. /**
  197. * 校验项目是否存在(项目管理)
  198. * @param {String} token - token
  199. * @return {Promise} Promise
  200. */
  201. verifyManagementProject(token) {
  202. return new Promise((resolve, reject) => {
  203. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/calibration`, {
  204. method: 'POST',
  205. // dateType: 'json',
  206. encoding: 'utf8',
  207. data: {
  208. token,
  209. },
  210. // timeout: 2000,
  211. }).then(({ status, data }) => {
  212. if (status === 200) {
  213. const result = JSON.parse(data.toString()).data;
  214. return resolve(result);
  215. }
  216. return reject(new Error('校验失败'));
  217. }).catch(err => {
  218. console.log(err);
  219. return reject(err);
  220. });
  221. });
  222. }
  223. /**
  224. * 创建项目和账号(项目管理)
  225. * @return {Promise} Promise
  226. */
  227. async addProjectFromManagement() {
  228. const token = this.ctx.helper.createJWT({ account: this.ctx.session.sessionUser.account, code: this.ctx.session.sessionProject.code });
  229. return new Promise((resolve, reject) => {
  230. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/project/add`, {
  231. method: 'POST',
  232. encoding: 'utf8',
  233. data: { token },
  234. }).then(({ status, data }) => {
  235. if (status === 200) {
  236. const result = JSON.parse(data.toString());
  237. return resolve(result);
  238. }
  239. return reject(new Error('添加失败'));
  240. }).catch(err => {
  241. console.log(err);
  242. return reject(err);
  243. });
  244. });
  245. }
  246. async setPmDealCache(pid, data) {
  247. const pmDeal = data.filter(x => { return x.selected; }).map(x => { return x.id; });
  248. await this.cache.set('pmDeal-' + pid, pmDeal.join(','), 'EX', this.ctx.app.config.cacheTime);
  249. }
  250. async getPmDealCache(pid) {
  251. const result = await this.cache.get('pmDeal-' + pid);
  252. return result ? result.split(',') : [];
  253. }
  254. async getFunSet(fun_set = null) {
  255. const result = fun_set ? JSON.parse(fun_set) : {};
  256. this.ctx.helper._.defaults(result, defaultFunSet);
  257. return result;
  258. }
  259. async updateFunSet(id, funSet) {
  260. const result = await this.db.update(this.tableName, {
  261. id, fun_set: JSON.stringify(funSet),
  262. });
  263. return result.affectedRows === 1;
  264. }
  265. async updateProjectSet(id, field, datas) {
  266. const updateData = {
  267. id,
  268. };
  269. updateData[field] = JSON.stringify(datas);
  270. const result = await this.db.update(this.tableName, updateData);
  271. return result.affectedRows === 1;
  272. }
  273. async saveCommonJson(id, field, datas) {
  274. const projectData = await this.getDataById(id);
  275. projectData.common_json = projectData.common_json ? JSON.parse(projectData.common_json) : {};
  276. const updateData = {
  277. id,
  278. };
  279. projectData.common_json[field] = datas;
  280. updateData.common_json = JSON.stringify(projectData.common_json);
  281. const result = await this.db.update(this.tableName, updateData);
  282. return result.affectedRows === 1;
  283. }
  284. }
  285. return Project;
  286. };