import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { EntityRepository, Repository } from 'typeorm';
import { CreateManualsManageDto } from './dto/create-manuals_manage.dto';
import { UpdateManualsManageDto } from './dto/update-manuals_manage.dto';
import { ManualsManage } from './entities/manuals_manage.entity';
import * as jwt from 'jsonwebtoken';
import { getRepository } from 'typeorm';
import { User } from '../system/entities/user.entity';
import { Role } from '../system/entities/role.entity';
import { MnaualsManageClassify } from './entities/manuals_manage_classify.entity';
const enum lookOrEditor {
  A = 'canView',
  B = 'canEditor',
}
@Injectable()
export class ManualsManageService {
  constructor(
    // 注入手册管理实例
    @InjectRepository(ManualsManage)
    private manualsSql: Repository<ManualsManage>,
    // 注入用户管理实例
    @InjectRepository(User)
    private userServe: Repository<User>,

    // 注入手册分类实例
    @InjectRepository(MnaualsManageClassify)
    private manualsClassify: Repository<MnaualsManageClassify>,
  ) {}
  async create(manualsInfo: CreateManualsManageDto, req) {
    const saveData: any = manualsInfo;
    saveData.canEditor = JSON.stringify(manualsInfo.canEditor);
    saveData.canView = JSON.stringify(manualsInfo.canView);
    const result = await this.manualsSql.save(saveData);
    if (!result) {
      throw '新建手册失败';
    }
    return result;
  }

  async findAll(token, classifyId) {
    let result;
    if (classifyId) {
      result = await this.manualsSql.findBy({
        delFlag: 0,
        classify: classifyId,
      });
    } else {
      result = await this.manualsSql.findBy({ delFlag: 0 });
    }
    result.forEach((item) => {
      item.canEditor = JSON.parse(item.canEditor ? item.canEditor : '[]');
      item.canView = JSON.parse(item.canView ? item.canView : '[]');
    });

    return result;
  }

  async findOne(id: number) {
    // 此时说明用户未登录   我们就需要查看他所查询的手册是否需要权限

    const result = await this.manualsSql.findOneBy({ id, delFlag: 0 });
    if (!result) {
      throw '此手册不存在，或已经被删除！';
    }
    result.canView = JSON.parse(result.canView ? result.canView : '[]');
    result.canEditor = JSON.parse(result.canEditor ? result.canEditor : '[]');
    return result;
  }

  async update(
    id: number,
    updateManualsManageDto: UpdateManualsManageDto,
    req,
  ) {
    const manualsInfo = await this.manualsSql.findOneBy({ id, delFlag: 0 });
    if (!manualsInfo) {
      throw '手册不存在，无法编辑！';
    }
    const saveData: any = updateManualsManageDto;
    saveData.canEditor = JSON.stringify(updateManualsManageDto.canEditor);
    saveData.canView = JSON.stringify(updateManualsManageDto.canView);
    const result = await this.manualsSql.save({ ...saveData, id });
    return result;
  }
  async portionUpdate(idArray: Array<number>, data, req) {
    const result = await this.manualsSql.update(idArray, data);
    if (result.affected < 0) {
      throw '数据修改失败';
    }
    return await this.findAll(req, null);
  }

  async remove(id: number) {
    const result = await this.manualsSql.update(id, { delFlag: 1 });
    if (result.affected < 1) {
      throw '未知原因';
    }
    return result;
  }

  async findAllClassify() {
    const res = await this.manualsClassify.findBy({ delFlag: 0 });
    return res;
  }
  async createClassify(data) {
    const res = await this.manualsClassify.save(data);
    return res;
  }
  async deleteClassify(id) {
    const result = await this.manualsClassify.update(id, { delFlag: 1 });
    return result;
  }
}
