/*
 * @Author: YKH
 * @Date: 2021-12-08 10:26:27
 * @LastEditTime: 2022-09-29 15:24:19
 * @Description:
 * @FilePath: \cool-admin\cool-admin-midway-master\src\app\modules\shuyou\controller\admin\rent_computer.ts
 */
import { ALL, Body, Inject, Post, Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from 'midwayjs-cool-core';
import { ShuyouRentComputerEntity } from '../../entity/rent_computer';
import { ShuyouRentComputerService } from '../../service/rent_computer';
import { ILogger } from '@midwayjs/logger';
// import xlsx from 'node-xlsx';
// import fs from 'fs';

/**
 * 租机
 */
@Provide()
@CoolController({
  // 添加通用CRUD接口
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  // 设置表实体
  entity: ShuyouRentComputerEntity,
  service: ShuyouRentComputerService,
  // // 向表插入当前登录用户ID
  // insertParam: async (ctx: Context) => {
  //   return {
  //     userId: ctx.admin.userId,
  //   };
  // },
  // // info接口忽略价格字段
  // infoIgnoreProperty: ['price'],

  before: ctx => {
    //前段数据转JSON格式
    const data = ctx.request.body;
    if (data.pc_num == '') {
      data.pc_num = null;
    }
    ctx.request.body.data = JSON.stringify(data);
  },
  // 分页查询配置
  pageQueryOp: {
    // 让字段支持模糊查询
    keyWordLikeFields: ['pc_num'],
    // 让字段支持筛选
    fieldEq: ['pc_num'],
    // 添加排序
    addOrderBy: {
      // 排序字段及排序方式
      pc_num: 'asc',
    },
  },
})
export class ShuyouRentComputerController extends BaseController {
  @Inject()
  logger: ILogger;

  @Inject()
  ShuyouRentComputerService: ShuyouRentComputerService;

  // 换编
  @Post('/replaceNum')
  async replaceNum(@Body(ALL) queryObject) {
    // this.logger.info(queryObject);
    let reqmsg = await this.ShuyouRentComputerService.replaceNum(queryObject);
    if (reqmsg.code == 1000) {
      return await this.ok({ code: 1, message: reqmsg.data });
    } else {
      return await this.fail(reqmsg.data, -1);
    }
  }

  // 退租
  @Post('/rentingOut')
  async rentingOut(@Body(ALL) queryObject) {
    // this.logger.info(queryObject);
    let reqmsg = await this.ShuyouRentComputerService.rentingOut(queryObject);
    if (reqmsg.code == 1000) {
      return await this.ok({ code: 1, message: reqmsg.data });
    } else {
      return await this.fail(reqmsg.data, -1);
    }
  }

  // 获取租机列表
  @Post('/rentComputerList')
  async rentComputerList() {
    let list = await this.ShuyouRentComputerService.rentComputerList();
    return await this.ok({ code: 1, data: list });
  }
  // 租机续费
  @Post('/renew')
  async renew(@Body(ALL) queryObject) {
    // this.logger.info(queryObject);
    let reqmsg = await this.ShuyouRentComputerService.renew(queryObject);
    if (reqmsg.code == 1000) {
      return await this.ok({ code: 1, message: reqmsg.data });
    } else {
      return await this.fail(reqmsg.data, -1);
    }
  }
  // 租机下架
  @Post('/offShelf')
  async offShelf(@Body(ALL) queryObject) {
    this.logger.info(queryObject);
    let reqmsg = await this.ShuyouRentComputerService.offShelf(queryObject);
    if (reqmsg.code == 1000) {
      return await this.ok({ code: 1, message: reqmsg.data });
    } else {
      return await this.fail(reqmsg.data, -1);
    }
  }
  @Post('/batchImport')
  async batchImport(@Body(ALL) queryObject) {
    console.log(queryObject);

    // const { fileaddress } = queryObject;
    // console.log(fileaddress._value);
    // const filename = fileaddress._value.split('uploads')[1];
    // console.log(filename);

    // const filename = fileaddress.split('uploads')[1];
    // 解析文件
    // const data = xlsx.parse(filename);
    // console.log(data);

    // 以 buffer 形式导入
    // const workSheetsFromBuffer = xlsx.parse(fs.readFileSync(filename));
    // console.log(JSON.stringify(workSheetsFromBuffer, null, 2));

    // 以文件形式导入
    // const workSheetsFromFile = xlsx.parse(
    //   'F:/project/cool-admin-3.x/cool-admin-midway-master/public/uploads/20220928/187a94f0-3f00-11ed-b595-3fe20740ebd9.xls'
    // );
    // console.log(JSON.stringify(workSheetsFromFile, null, 2));

    // try {
    //   // 解析文件
    //   const data = xlsx.parse(file.filepath);
    //   console.log(data);

    //   // xlsx包使用方式
    //   //  const data = XLSX.readFile(file.filepath, {});
    //   //  const json = XLSX.utils.sheet_to_json(data.Sheets['Sheet1']);
    // } finally {
    //   fs.unlinkSync(file.filepath);
    // }
  }
}
