????

Your IP : 3.139.103.13


Current Path : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/quantri/
Upload File :
Current File : C:/inetpub/vhost/sdoc.gdtsolutions.vn/package/app/controllers/quantri/phong.js

var Database = require('../../models/Database');
const queryBuilder = require('../../repositories/QueryBuilder');
class PhongController extends Database {
  constructor(params) {
    super(params);
  }
  static create(params) {
    return new PhongController(params);
  }
  async selectFonds() {
    var fonds = await queryBuilder('Fond').select('*').orderBy('FondNumber')
    for (var i = 0; i < fonds.length; i++) {
      fonds[i].KeyGroups = await queryBuilder('File').leftJoin('FileGroup', 'FileGroup.Id', 'File.GroupId')
        .column('File.GroupId', 'FileGroup.GroupName', 
          { startYear: queryBuilder.raw('MIN(YEAR(StartDate))')},
          { endYear: queryBuilder.raw('MAX(YEAR(EndDate))')})
        .select()
        .where('FondCode', fonds[i].FondCode)
        .groupBy('File.GroupId', 'FileGroup.GroupName');
      const tools = (fonds[i].LookupTools || '').split(',').filter(f => f);
      if (tools.length > 0) 
        fonds[i].Tools = await queryBuilder('LookupTool').select('ToolName').whereIn('Id', tools);
    }
    return fonds;
  }
  async addFond(fond) {
    const checkExist = await queryBuilder('Fond').select('UUID').where('FondCode', fond.code).first();
    if (checkExist) throw new Error(`Đã tồn tại phông có mã ${fond.code}`);
    return await queryBuilder('Fond').insert({
      OrganId: fond.organ,
      FondCode: fond.code,
      FondNumber: fond.number,
      FondName: fond.name,
      FondHistory: fond.history,
      ArchivesTime: fond.archivestime,
      PaperTotal: fond.papertotal,
      TotalCatalog: fond.totalcatalog,
      Language: fond.language ? fond.language.toString() : null,
      LookupTools: fond.lookuptool ? fond.lookuptool.toString() : null,
      Description: fond.description,
      State: parseInt(fond.state) || 0,
    });
  }
  async editFond(fond) {
    const checkExist = await queryBuilder('Fond').select('UUID').where('FondCode', fond.code)
      .andWhereNot('UUID', fond.id).first();
    if (checkExist) throw new Error(`Đã tồn tại phông khác có mã ${fond.code}`);
    return await queryBuilder('Fond').update({
      FondCode: fond.code,
      FondNumber: fond.number,
      FondName: fond.name,
      FondHistory: fond.history,
      ArchivesTime: fond.archivestime,
      PaperTotal: fond.papertotal,
      PaperDigital: fond.paperdigital,
      TotalCatalog: fond.totalcatalog,
      Description: fond.description,
      Language: fond.language ? fond.language.toString() : null,
      LookupTools: fond.lookuptool ? fond.lookuptool.toString() : null,
      IsStatisticsVisible: typeof fond.isvisible !== 'undefined' ? 1 : 0,
    }).where('UUID', fond.id);
  }
  async deleteFond({FondCode, UUID}) {
    const checkExist = await queryBuilder('File').select('UUID').where({ FondCode, State: 1}).first();
    if (checkExist) throw new Error('Trong phông hiện đang tồn tại tài liệu!\nCần xóa hoặc chuyển hết hồ sơ sang phông khác trước khi xóa phông!')
    return await queryBuilder('Fond').where({UUID}).del();
  }
}
module.exports = PhongController;