user.js 10 KB
/*
 * @Author: limin
 * @Date: 2019-11-21 11:18:37
 * @Last Modified by: limin
 * @Last Modified time: 2020-01-04 12:53:16
 */

const Service = require("egg").Service;
// var moment = require('moment');/
class UserService extends Service {
  constructor(ctx) {
    super(ctx);
    this.helper = ctx.helper;
    this._reg_models_alias();
    this._associate();
  }
  /**
   * 绑定model 在 this 上的 别名
   */
  _reg_models_alias() {
    const {
      TUserCityGender,
      TUserCityCarbrand,
      TUserCityDaily,
      TUserCityDriveInfo,
      TUserCityRealtime,
      TUserNewActiveDetail
    } = this.app.model.Screen.User;

    const { TProvince, TCity } = this.app.model;

    this.MUserRealTime = TUserCityRealtime;
    this.MUserGender = TUserCityGender;
    this.MUserCarBrand = TUserCityCarbrand;
    this.MUserDaily = TUserCityDaily;
    this.MUserDriveInfo = TUserCityDriveInfo;
    this.MUserActive = TUserNewActiveDetail;
    this.MProvice = TProvince;
    this.MCity = TCity;
  }

  /**
   * 绑定表关系
   */
  _associate() {
    this.MUserDaily.belongsTo(this.MProvice, {
      foreignKey: "prov_code",
      targetKey: "prov_code"
    });
    this.MUserDaily.belongsTo(this.MCity, {
      foreignKey: "city_code",
      targetKey: "city_code"
    });
    this.MUserRealTime.belongsTo(this.MProvice, {
      foreignKey: "prov_code",
      targetKey: "prov_code"
    });
    this.MUserRealTime.belongsTo(this.MCity, {
      foreignKey: "city_code",
      targetKey: "city_code"
    });
    this.MUserActive.belongsTo(this.MProvice, {
      foreignKey: "prov_code",
      targetKey: "prov_code"
    });
    this.MUserActive.belongsTo(this.MCity, {
      foreignKey: "city_code",
      targetKey: "city_code"
    });
  }

  __build__option__(opt = {}) {
    let { pid, cid, n, limit, order, day, beginDate, endDate } = opt;
    let where = {};
    const { between } = this.app.Sequelize.Op;
    const { date, format_end_date } = this.helper;
    if (cid) {
      where.city_code = cid;
    } else if (pid) {
      where.prov_code = pid;
    }
    if (day) {
      //如果有具体天数 就直接查询具体一天的数据(优先具体哪一天)
      where.day = day;
    } else if (n) {
      // 查询近N天的数据  不包括今天
      where.day = {
        [between]: [date(n), format_end_date(date(-1))]
      };
    } else if (n === 0) {
      // 查询
      where.day = {
        [between]: [date(0), format_end_date(date(0))]
      };
    } else if (beginDate) {
      if (!endDate) {
        endDate = beginDate;
      }
      if (!endDate.includes(":")) {
        endDate = format_end_date(endDate);
      }
      where.day = {
        [between]: [beginDate, endDate]
      };
    }
    const option = {
      where,
      raw: true
    };
    if (limit) {
      option.limit = +limit;
    }
    if (order) {
      option.order = order;
    }
    return option;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 用户城市排行
   */
  async city_range({ pid, limit }) {
    const { app, MCity, MUserDaily } = this;
    const { col, fn, Op } = app.Sequelize;
    const option = this.__build__option__({
      pid,
      limit,
      order: [["total_user", "DESC"]]
    });
    Object.assign(option.where, {
      city_code: {
        [Op.gt]: 0
      }
    });
    Object.assign(option, {
      attributes: [
        "city_code",
        [fn("SUM", col("total_user")), "total"],
        col("t_city.city_name", "city")
      ],
      include: { model: MCity, attributes: [] },
      group: "city_code"
    });
    const data = await MUserDaily.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 用户车辆品牌分布
   */
  async car_brand_range({ pid, cid, limit }) {
    const { app, MUserCarBrand } = this;
    const { fn, col } = app.Sequelize;
    const option = this.__build__option__({
      pid,
      cid,
      limit,
      order: [["total", "DESC"]]
    });
    Object.assign(option, {
      attributes: [
        col("brand_name", "brand"),
        [fn("SUM", col("brand_cars")), "total"],
        col("update_time", "date")
      ],
      group: "brand_name"
    });
    const data = await MUserCarBrand.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 用户日均里程分布
   */
  async drive_range({ pid, cid }) {
    const { app, MUserDriveInfo } = this;
    const option = this.__build__option__({ pid, cid });
    const { fn, col } = app.Sequelize;
    Object.assign(option, {
      attributes: [
        [fn("SUM", col("d_0_5")), "d_0_5"],
        [fn("SUM", col("d_5_10")), "d_5_10"],
        [fn("SUM", col("d_10_15")), "d_10_15"],
        [fn("SUM", col("d_15_20")), "d_15_20"],
        [fn("SUM", col("d_20_25")), "d_20_25"],
        [fn("SUM", col("d_25_31")), "d_25_31"],
        [fn("SUM", col("km_0_40")), "km_0_40"],
        [fn("SUM", col("km_40_70")), "km_40_70"],
        [fn("SUM", col("km_70_100")), "km_70_100"],
        [fn("SUM", col("km_100_")), "km_100_"],
        [fn("SUM", col("w_0_10")), "w_0_10"],
        [fn("SUM", col("w_10_20")), "w_10_20"],
        [fn("SUM", col("w_20_30")), "w_20_30"],
        [fn("SUM", col("w_30_50")), "w_30_50"],
        [fn("SUM", col("w_50_")), "w_50_"]
      ]
    });
    const data = await MUserDriveInfo.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 获取某天城市用户总数列表
   */
  async user_summary_city_by_day({ beginDate, endDate, pid, cid }) {
    const { app, MUserDaily: MUserCity, MCity, MProvice } = this;
    const { col } = app.Sequelize;
    var option = this.__build__option__({ beginDate, endDate, pid, cid });
    Object.assign(option, {
      attributes: [
        "city_code",
        "prov_code",
        "total_user",
        "renew",
        "recent_renew",
        "active",
        "increase",
        "journey",
        "miles",
        "recent_total",
        "recent_7_day_active",
        "recent_30_day_active",
        "online_time",
        "day",
        col("t_city.city_name"),
        col("t_province.prov_name")
      ],
      include: [
        { model: MCity, attributes: [] },
        { model: MProvice, attributes: [] }
      ]
    });
    const data = await MUserCity.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 获取实时数据(详细)
   */
  async user_realtime({ pid, cid }) {
    const { app, MUserRealTime, MCity, MProvice } = this;
    const { col } = app.Sequelize;
    var option = this.__build__option__({ pid, cid });
    Object.assign(option, {
      attributes: [
        "city_code",
        "prov_code",
        "online_total",
        "renew",
        "active",
        "increase",
        "online_time",
        col("t_city.city_name"),
        col("t_province.prov_name")
      ],
      include: [
        { model: MCity, attributes: [] },
        { model: MProvice, attributes: [] }
      ]
    });
    const data = await MUserRealTime.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 获取用户性别年龄数量占比
   */
  async user_gender({ pid, cid }) {
    const { app, MUserGender } = this;
    var option = this.__build__option__({ pid, cid });
    const { fn, col } = app.Sequelize;
    Object.assign(option, {
      attributes: [
        "gender_range",
        [fn("SUM", col("total")), "total"],
        [fn("SUM", col("male")), "male"],
        [fn("SUM", col("female")), "female"]
      ],
      group: "gender_range"
    });
    const data = await MUserGender.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 获取用户激活详情
   */
  async activation({ pid, limit, last_id }) {
    const { app, service } = this;
    const { Op } = app.Sequelize;
    var option = service.screen.user.__build__option__({ pid, limit });
    Object.assign(option.where, {
      id: {
        [Op.gt]: last_id
      }
    });
    Object.assign(option, {
      attributes: [
        "id",
        "sn",
        "active_time",
        "prov_code",
        "city_code",
        "device_type"
      ]
    });
    const data = await service.screen.user.MUserActive.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 根据id 获取 激活数据
   */
  async user_active({ limit }) {
    const { MUserActive } = this;
    var option = this.__build__option__({ limit, order: [["id", "DESC"]] });
    Object.assign(option, {
      attributes: [
        "id",
        "sn",
        "active_time",
        "prov_code",
        "city_code",
        "device_type"
      ]
    });
    const data = await MUserActive.findAll(option);
    return data;
  }

  /**
   * @param field {String}
   * @param value {String}
   * @return {Promise.<boolean>}
   * @Description 获取每个省的用户数
   */
  async total_group_by_province({ pid }) {
    const { app, MUserDaily, MProvice, MCity } = this;
    var option = this.__build__option__({ pid });
    const { fn, col } = app.Sequelize;
    const _opt = pid
      ? {
          attributes: [
            "city_code",
            [fn("SUM", col("total_user")), "total_user"],
            col("t_city.city_name")
          ],
          include: [{ model: MCity, attributes: [] }],
          group: "city_code"
        }
      : {
          attributes: [
            "prov_code",
            [fn("SUM", col("total_user")), "total_user"],
            col("t_province.prov_name")
          ],
          include: [{ model: MProvice, attributes: [] }],
          group: "prov_code"
        };
    Object.assign(option, _opt);
    const data = await MUserDaily.findAll(option);
    return data;
  }
}

module.exports = UserService;