project.js 1.71 KB
'use strict';

const Service = require('egg').Service;
const objExclude = require('../util/common').objExclude;
const moment = require('moment');

class ProjectService extends Service {
  async count() {
    const result = await this.app.mysql.query('select count(*) as total from project');
    return result;
  }
  async add(data) {
    const value = data;
    value.create_time = moment().format('YYYY-MM-DD HH:mm:ss');
    const todayCountResult = await this.app.mysql.query('select count(*) as count from project where create_time like ?', [ `%${moment().format('YYYY-MM-DD')}%` ]);
    const todayCount = todayCountResult && todayCountResult.length > 0 ? todayCountResult[0].count : 0;
    const count = Number(todayCount) + 1;
    const number = `SF${moment().format('YYYYMMDD')}${count > 9 ? count : `0${count}`}`;
    value.number = number;
    const result = await this.app.mysql.insert('project', value);
    return result;
  }
  async update(data) {
    const value = data;
    value.update_time = moment().format('YYYY-MM-DD HH:mm:ss');
    const result = await this.app.mysql.update('project', value);
    return result;
  }
  async find(id) {
    const user = await this.app.mysql.get('project', { id });
    return user;
  }
  async list(data) {
    const { current = 1, pageSize = 10 } = data;
    const other = objExclude(data, [ 'current', 'pageSize' ]);
    const limit = Number(pageSize);
    const offset = Number((current - 1) * pageSize);
    const list = await this.app.mysql.select('project', { where: other, limit, offset, orders: [[ 'number', 'asc' ]] });
    return list;
  }
  async remove(data) {
    const result = await this.app.mysql.delete('project', data);
    return result;
  }
}

module.exports = ProjectService;