
'use strict';

const Service = require('egg').Service;
const R = require('ramda');
const _ = require('lodash');
const moment = require('moment');

class BackService extends Service {
  // 登录
  async login(input) {
    const { ctx } = this;
    const { account, password } = input;

    const userInfo = await ctx.classModel.CourseBackUser.findOne({ where: { account, is_deleted: 0 } });
    if (ctx.isEmpty(userInfo)) {
      ctx.failed('用户不存在');
    }

    console.log(ctx.helper.md5(password + 'course'));
    if (userInfo.password !== ctx.helper.md5(password + 'course')) {
      ctx.failed('密码错误');
    }

    const authToken = await this.service.jwt.apply({ user_uuid: userInfo.id });
    await this.app.memcache.set('course_back_auth_token_' + userInfo.id, authToken, 3600);

    return { auth_token: authToken };
  }

  // 获取菜单
  async getMenuList() {
    const { ctx } = this;
    const userUuid = ctx.userUuid;

    const userInfo = await ctx.classModel.CourseBackUser.findOne({ where: { id: userUuid, is_deleted: 0 } });
    if (ctx.isEmpty(userInfo)) {
      ctx.failed('无效用户');
    }

    let menuList = [];
    if (userInfo.is_admin > 0) {
      menuList = await ctx.classModel.CourseBackMenu.findAll({ where: { is_deleted: 0 }, attributes: [ 'id', 'title', 'path', 'icon', 'parent_id' ], raw: true });
    } else {
      const userToMenuList = await ctx.classModel.CourseBackUserToMenu.findAll({ where: { user_uuid: userUuid, is_deleted: 0 } });
      menuList = await ctx.classModel.CourseBackMenu.findAll({ where: { id: { $in: R.pluck('menu_id', userToMenuList) }, is_deleted: 0 }, attributes: [ 'id', 'title', 'path', 'icon', 'parent_id' ], raw: true });
    }

    for (const i in menuList) {
      menuList[i].subs = [];
    }
    const menu = _.groupBy(menuList, 'id');

    for (const v of menuList) {
      if (v.parent_id > 0 && !ctx.isEmpty(menu[v.parent_id])) {
        menu[v.parent_id][0].subs.push(v);
      }
    }

    const results = [];
    for (const i in menu) {
      if (menu[i][0].parent_id === 0) {
        results.push(menu[i][0]);
      }
    }

    return { list: results };
  }


  // 订单列表
  async getOrderList(input) {
    const { ctx } = this;
    const userUuid = ctx.userUuid;
    const page = Number(input.page) || 1;
    const limit = Number(input.limit) || 10;
    const offset = (page - 1) * limit;

    const userInfo = await ctx.classModel.CourseBackUser.findOne({ where: { id: userUuid, is_deleted: 0 } });
    if (ctx.isEmpty(userInfo)) {
      ctx.failed('用户异常');
    }

    const filter = { where: { status: 1, is_deleted: 0 }, limit, offset, attributes: [ 'id', 'order_no', 'class_id', 'pay', 'pay_time', 'type', 'redeem', 'address' ], order: [[ 'pay_time', 'desc' ]] };
    let filterClassIds = [];
    if (!ctx.isEmpty(input.class_name)) {
      const classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { name: { $like: `%${input.class_name}%` } }, attributes: [ 'id' ] });
      filterClassIds = R.pluck('id', classList);
      filter.where.class_id = { $in: filterClassIds };
    }

    if (!ctx.isEmpty(input.user_name)) {
      filter.where.address = { $like: `%${input.user_name}%` };
    }

    if (!ctx.isEmpty(input.start_time)) {
      if (!ctx.isEmpty(input.end_time)) {
        filter.where.pay_time = { $gte: input.start_time, $lte: input.end_time };
      } else {
        filter.where.pay_time = { $gte: input.start_time };
      }
    } else if (!ctx.isEmpty(input.end_time)) {
      filter.where.pay_time = { $lte: input.end_time };
    }

    if (userInfo.is_admin === 0) {
      const classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { institution_id: { $in: userInfo.institution_id } }, attributes: [ 'id' ] });
      const classIds = ctx.isEmpty(input.class_name) ? R.pluck('id', classList) : _.intersection(filterClassIds, R.pluck('id', classList));
      filter.where.class_id = { $in: classIds };
    }

    const orderList = await ctx.classModel.V5.CourseUserOrder.findAndCountAll(filter);
    if (ctx.isEmpty(orderList.rows)) {
      return {
        list: [],
        page_size: limit,
        total_count: orderList.count,
        page,
      };
    }

    let classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { id: { $in: _.uniq(R.pluck('class_id', orderList.rows)) } }, attributes: [ 'id', 'name' ], raw: true });
    classList = _.groupBy(classList, 'id');

    const results = [];
    for (const v of orderList.rows) {
      results.push({
        id: v.id,
        order_no: v.order_no,
        class_id: v.class_id,
        class_name: ctx.isEmpty(classList[v.class_id]) ? '' : classList[v.class_id][0].name,
        pay: v.pay,
        pay_time: v.pay_time,
        type: v.type,
        redeem: v.redeem,
        address: ctx.isEmpty(v.address) ? {} : JSON.parse(v.address),
      });
    }

    const ret = {
      list: results,
      page_size: limit,
      total_count: orderList.count,
      page,
    };

    return ret;
  }


  // 获取兑换码列表
  async getRedeemCodeList(input) {
    const { ctx } = this;
    const userUuid = ctx.userUuid;
    const page = Number(input.page) || 1;
    const limit = Number(input.limit) || 10;
    const offset = (page - 1) * limit;

    const userInfo = await ctx.classModel.CourseBackUser.findOne({ where: { id: userUuid, is_deleted: 0 } });
    if (ctx.isEmpty(userInfo) || userInfo.is_admin === 0) {
      ctx.failed('用户异常');
    }

    const filter = { where: { status: 1, is_deleted: 0 }, limit, offset, order: [[ 'id', 'desc' ]] };
    if (!ctx.isEmpty(input.class_name)) {
      const classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { name: { $like: `%${input.class_name}%` } }, attributes: [ 'id' ] });
      filter.where.class_id = { $in: R.pluck('id', classList) };
    }

    const redeemCodeList = await ctx.classModel.V5.CourseRedeemCode.findAndCountAll(filter);
    if (ctx.isEmpty(redeemCodeList.rows)) {
      return {
        list: [],
        page_size: limit,
        total_count: redeemCodeList.count,
        page,
      };
    }

    let classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { id: { $in: _.uniq(R.pluck('class_id', redeemCodeList.rows)) } }, attributes: [ 'id', 'name' ], raw: true });
    classList = _.groupBy(classList, 'id');

    const results = [];
    for (const v of redeemCodeList.rows) {
      results.push({
        id: v.id,
        class_id: v.class_id,
        class_name: ctx.isEmpty(classList[v.class_id]) ? '' : classList[v.class_id][0].name,
        code: v.code,
        channel: v.channel,
        is_used: v.is_used,
        used_time: v.used_time,
        created_time: v.created_time,
      });
    }

    const ret = {
      list: results,
      page_size: limit,
      total_count: redeemCodeList.count,
      page,
    };

    return ret;
  }


  // 生成兑换码
  async addRedeemCode(input) {
    const { ctx } = this;
    const num = Number(input.num) || 0;

    const data = [];
    for (let i = 0; i < num; i++) {
      const tmp = {
        code: (ctx.helper.md5(moment().unix()).slice(-2) + await this.getRandomString(8)).toUpperCase(),
        channel: input.channel || '',
        class_id: input.class_id || 0,
      };
      data.push(tmp);
    }
    await ctx.classModel.V5.CourseRedeemCode.bulkCreate(data);

    return { result: true };
  }


  async getRandomString(n) {
    const chars = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
    let ret = '';
    for (let i = 0; i < n; i++) {
      const id = Math.floor(Math.random() * 36);
      ret += chars[id];
    }
    return ret;
  }


  // 获取机构和课程
  async getClassList(input) {
    const { ctx } = this;
    const userUuid = ctx.userUuid;
    const page = Number(input.page) || 1;
    const limit = Number(input.limit) || 10;
    const offset = (page - 1) * limit;

    const userInfo = await ctx.classModel.CourseBackUser.findOne({ where: { id: userUuid, is_deleted: 0 } });
    if (ctx.isEmpty(userInfo)) {
      ctx.failed('用户异常');
    }

    const filter = userInfo.is_admin === 0 ? { where: { id: userInfo.institution_id, status: 1, is_deleted: 0 }, attributes: [ 'id', 'name' ], limit, offset, raw: true } : { where: { status: 1, is_deleted: 0 }, limit, offset, attributes: [ 'id', 'name' ] };
    if (!ctx.isEmpty(input.institution_name)) {
      filter.where.name = { $like: `%${input.institution_name}%` };
    }

    const institutionList = await ctx.classModel.V5.CourseV5Institution.findAll(filter);
    if (ctx.isEmpty(institutionList)) {
      return {
        list: [],
      };
    }

    let classList = await ctx.classModel.V5.CourseV5Class.findAll({ where: { institution_id: { $in: R.pluck('id', institutionList) }, status: 1, is_deleted: 0 }, attributes: [ 'id', 'institution_id', 'name' ] });
    classList = _.groupBy(classList, 'institution_id');

    const results = [];
    for (const v of institutionList) {
      results.push({
        id: v.id,
        name: v.name,
        class: ctx.isEmpty(classList[v.id]) ? [] : classList[v.id],
      });
    }

    return {
      list: results,
    };
  }


  // 导出订单
  async exportOrder(input) {
    const { ctx } = this;
    input.page = 1;
    input.limit = 9999;

    let orderList = await this.getOrderList(input);
    orderList = orderList.list;

    if (ctx.isEmpty(orderList)) {
      ctx.failed('无有效数据');
    }

    const results = [];
    for (const v of orderList) {
      const address = ctx.isEmpty(v.address) ? { province: '', city: '', area: '', address: '', name: '', phone: '' } : v.address;
      results.push({
        order_no: v.order_no,
        class_id: v.class_id,
        class_name: v.class_name,
        pay: v.pay,
        pay_time: v.pay_time,
        type: v.type === 1 ? '微信支付' : '兑换码',
        redeem: v.redeem,
        province: address.province,
        city: address.city,
        area: address.area,
        address: address.address,
        name: address.name,
        phone: address.phone,
      });
    }

    await ctx.service.excel
      .newExport()
      .newSheet(results, '订单列表')
      .newColumn('order_no', '订单编号')
      .newColumn('class_name', '课程')
      .newColumn('pay', '支付金额')
      .newColumn('pay_time', '支付时间')
      .newColumn('type', '支付类型')
      .newColumn('redeem', '兑换码')
      .newColumn('province', '省')
      .newColumn('city', '市')
      .newColumn('area', '区')
      .newColumn('address', '详细地址')
      .newColumn('name', '姓名')
      .newColumn('phone', '手机')
      .setFileName('订单列表')
      .get();

    return true;

  }


  // 导出兑换码
  async exportRedeemCode(input) {
    const { ctx } = this;
    input.page = 1;
    input.limit = 9999;

    let orderList = await this.getRedeemCodeList(input);
    orderList = orderList.list;

    if (ctx.isEmpty(orderList)) {
      ctx.failed('无有效数据');
    }

    const results = [];
    for (const v of orderList) {
      results.push({
        class_name: v.class_name,
        code: v.code,
        channel: v.channel,
        is_used: v.is_used === 1 ? '是' : '否',
        used_time: v.used_time,
        created_time: v.created_time,
      });
    }

    await ctx.service.excel
      .newExport()
      .newSheet(results, '兑换码列表')
      .newColumn('class_name', '课程')
      .newColumn('code', '兑换码')
      .newColumn('channel', '渠道')
      .newColumn('is_used', '是否已用')
      .newColumn('used_time', '使用时间')
      .newColumn('created_time', '生成时间')
      .setFileName('兑换码列表')
      .get();

    return true;

  }
}

module.exports = BackService;
