import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateArticleManageDto } from './dto/create-article_manage.dto';
import { UpdateArticleManageDto } from './dto/update-article_manage.dto';
import { ArticleManage } from './entities/article_manage.entity';
import { getRepository } from 'typeorm';
import * as jwt from 'jsonwebtoken';
import { TokenDataEntity } from 'src/common/entities/token-data.entity';
import { ManualsManage } from 'src/manuals_manage/entities/manuals_manage.entity';

const enum lookOrEditor {
  A = 'canView',
  B = 'canEditor',
}

@Injectable()
export class ArticleManageService {
  constructor(
    @InjectRepository(ArticleManage)
    private articleSql: Repository<ArticleManage>,
    @InjectRepository(ManualsManage)
    private manualsSql: Repository<ManualsManage>,
  ) {}
  async create(articeData: CreateArticleManageDto, req) {
    for (const k in articeData) {
      const type = typeof articeData[k];
      if (type == 'object') {
        articeData[k] = JSON.stringify(articeData[k]);
      }
    }
    const artice: any = articeData;
    this.manualsSql.update(
      { id: articeData.manuals },
      { updateTime: new Date() },
    );
    return await this.articleSql.save(artice);
  }
  // 查询所有文章
  async findAll(req) {
    const result = await this.articleSql.findBy({ delFlag: 0 });
    return result;
  }
  async findManualsAll(id, token: TokenDataEntity) {
    const result = await this.articleSql.findBy({ delFlag: 0, manuals: id });
    return result;
  }
  async findOne(id: number, req) {
    const result = await this.articleSql.findOneBy({ id, delFlag: 0 });
    if (!result) {
      throw '未查询到此文章';
    }

    return result;
  }

  async update(updateDto: UpdateArticleManageDto, req) {
    const updateData: any = updateDto;
    for (const k in updateData) {
      const type = typeof updateData[k];
      if (type == 'object') {
        updateData[k] = JSON.stringify(updateData[k]);
      }
    }
    const res = await this.articleSql.save(updateData);
    this.manualsSql.update(
      { id: updateDto.manuals },
      { updateTime: new Date() },
    );
    this.manualsSql.update({ id: res.manuals }, { updateTime: new Date() });
    console.log(res);
    return res;
  }
  // 排序
  async sort(sortIdArray) {
    const formatData = sortIdArray.map((id, index) => {
      return {
        id,
        index: index + 1,
      };
    });
    // this.articleSql.findBy({ id: sortIdArray });
    const result = await this.articleSql.save(formatData);
    return result;
  }
  // 批量更新
  async updatePortion(data, req) {
    console.log(data);
    let result: any = await this.articleSql.update(data.id, data.data);
    result = await this.articleSql.findBy({
      manuals: data.manuals,
      delFlag: 0,
    });
    this.manualsSql.update({ id: data.manuals }, { updateTime: new Date() });
    return result;
  }
  remove(id: number, req) {
    const result = this.articleSql.update(id, { delFlag: 1 });
    return result;
  }
}
