123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- 'use strict';
- /**
- * Created by EllisRan on 2020/3/3.
- */
- const BaseService = require('../base/base_service');
- const accountGroup = require('../const/account_group');
- module.exports = app => {
- class ConstructionUnit extends BaseService {
- /**
- * 构造函数
- *
- * @param {Object} ctx - egg全局变量
- * @return {void}
- */
- constructor(ctx) {
- super(ctx);
- this.tableName = 'construction_unit';
- this.dataId = 'id';
- }
- /**
- * 数据验证规则
- *
- * @param {String} scene - 场景
- * @return {Object} - 返回数据
- */
- rule(scene) {
- let rule = {};
- switch (scene) {
- case 'add':
- rule = {
- name: { type: 'string', required: true },
- type: { type: 'string', required: true },
- };
- break;
- default:
- break;
- }
- return rule;
- }
- /**
- * 修改单位数据
- *
- * @param {Object} data - post过来的数据
- * @return {Boolean} - 返回修改结果
- */
- async save(data) {
- if (data._csrf_j !== undefined) {
- delete data._csrf_j;
- }
- const id = data.id !== undefined ? parseInt(data.id) : 0;
- const transaction = await this.db.beginTransaction();
- try {
- if (id > 0) {
- // 修改操作时
- delete data.create_time;
- data.id = id;
- } else {
- // 重名检测
- const accountData = await this.db.select(this.tableName, {
- where: {
- name: data.name,
- pid: data.pid,
- },
- });
- if (accountData.length > 0) {
- throw '已存在对应的单位名';
- }
- data.create_time = new Date();
- }
- const operate = id === 0 ? await transaction.insert(this.tableName, data) :
- await transaction.update(this.tableName, data);
- if (id === 0 && operate.affectedRows === 1) data.id = operate.insertId;
- // 并修改用户的用户组及单位名称
- const updateRow = {};
- if (data.name) {
- updateRow.company = data.name;
- updateRow.company_id = data.id;
- }
- if (data.type) updateRow.account_group = data.type;
- if (!this._.isEmpty(updateRow)) {
- const info = id !== 0 ? await this.getDataById(id) : null;
- await this.ctx.service.projectAccount.update(updateRow, {
- project_id: data.pid,
- company: info ? info.name : data.name,
- }, transaction);
- // egg 自带的update方法无法使用UPDATE zh_project_account SET company = ? WHERE project_id = ? AND company = ? ,未知原因
- // await transaction.update(this.ctx.service.projectAccount.tableName, updateRow,
- // {
- // where: {
- // project_id: data.pid,
- // company: info ? info.name : data.name,
- // },
- // }
- // );
- }
- await transaction.commit();
- const result = operate.affectedRows > 0;
- return result;
- } catch (e) {
- await transaction.rollback();
- }
- }
- async del(id) {
- // 先删除签章oss地址
- const info = await this.getDataById(id);
- if (info.sign_path) {
- await this.ctx.app.fujianOss.delete(this.ctx.app.config.fujianOssFolder + info.sign_path);
- }
- // 删除单位
- return await this.db.delete(this.tableName, { id });
- }
- async getReportData(pid) {
- const data = await this.getAllDataByCondition({ where: { pid } });
- data.forEach(x => {
- x.type_str = accountGroup.group[x.type];
- });
- return data;
- }
- }
- return ConstructionUnit;
- };
|