import HelperUtil from '../../utils/HelperUtil'
import UserLogAPI from "../../api/restApis/UserLogAPI";
import {USER_LOGIN_LOG_KEY, USER_OPTION_LOG_KEY} from "../../utils/constantCollection";
import {USER_LOGIN_LOG_FILE_NAME, USER_OPTION_LOG_FILE_NAME} from "../../utils/constantCollection";

// 静态方法
let staticMethods = {
  // 条件分页查询登录日志
  queryLoginLogSplitByCondition: function (beginItem,endItem,selectList) {
    return UserLogAPI.putQueryLoginLogSplitByCondition(beginItem,endItem,selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,USER_LOGIN_LOG_KEY);
      } else {
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 条件分页查询操作日志
  queryOptionLogSplitByCondition: function (beginItem, endItem, selectList) {
    return UserLogAPI.putQueryOptionLogSplitByCondition(beginItem, endItem, selectList).then(result => {
      if (result.data.result) {
        return HelperUtil.arrayToObject(result.data.returnValue,USER_OPTION_LOG_KEY);
      } else {
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 添加查询登录日志总条数
  queryLoginLogCountsByCondition: function (selectList) {
    return UserLogAPI.putQueryLoginLogCountsByCondition(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue;
      } else {
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 添加查询操作日志总条数
  queryOptionLogCountsByCondition: function (selectList) {
    return UserLogAPI.putQueryOLogCountsByCondition(selectList).then(result => {
      if (result.data.result) {
        return result.data.returnValue;
      } else {
        return HelperUtil.returnPromiseRejectObj(result);
      }
    });
  },

  // 导出用户登录日志
  exportLoginLogToExcel: function (selectList) {
    return UserLogAPI.putExportLoginLogToExcel(selectList).then(result => {
      return HelperUtil.downFile(result,USER_LOGIN_LOG_FILE_NAME);
    });
  },

  // 导出用户操作日志
  exportOptionLogToExcel: function (selectList) {
    return UserLogAPI.putExportOptionLogToExcel(selectList).then(result => {
      return HelperUtil.downFile(result, USER_OPTION_LOG_FILE_NAME);
    });
  }
};

function UserLogService (websocketService = null) {

  return Object.freeze(Object.assign(
    {
      // object methods comes here
    },
    staticMethods
  ))
}
// inject static methods
Object.assign(UserLogService, staticMethods);
export default UserLogService
