king
2019-12-16 c0930736f5b5955efecdac4c0ca85957d4f7b574
src/utils/utils.js
@@ -25,13 +25,14 @@
   */
  static getguid () {
    // 产生一个新的GUID值
    let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
      let r = Math.random() * 16 | 0
      // eslint-disable-next-line
      let v = (c === 'x') ? r : (r & 0x3 | 0x8)
      return v.toString(16)
    })
    return uuid
    let uuid = []
    let d = new Date()
    let options = '0123456789abcdefghigklmnopqrstuv'
    for (let i = 0; i < 19; i++) {
      uuid.push(options.substr(Math.floor(Math.random() * 0x20), 1))
    }
    uuid = moment().format('YYYYMMDDHHmmss') + d.getMilliseconds() + uuid.join('')
    return uuid.toUpperCase()
  }
  /**
@@ -189,37 +190,45 @@
  }
  /**
   * @description 初始化搜索条件
   * @param {Array}   searches     搜索条件
   * @return {String}  searches    格式化后结果
   */
  static initMainSearch (searches) {
    if (!searches || searches.length === 0) return []
    let newsearches = []
    searches.forEach(search => {
      let item = {
        key: search.field,
        match: search.match,
        type: search.type,
        value: search.initval
      }
      if (item.type === 'date') {
        item.value = item.value ? moment().subtract(item.value, 'days').format('YYYY-MM-DD') : ''
      } else if (item.type === 'datemonth') {
        item.value = item.value ? moment().subtract(item.value, 'month').format('YYYY-MM') : ''
      } else if (item.type === 'dateweek') {
        item.value = item.value ? [moment().subtract(item.value * 7, 'days').startOf('week').format('YYYY-MM-DD'),
          moment().subtract(item.value * 7, 'days').endOf('week').format('YYYY-MM-DD')] : ''
      } else if (item.type === 'daterange') {
        item.value = item.value ? [moment().subtract(item.value, 'days').format('YYYY-MM-DD'),
          moment().subtract(item.value === 1 ? 1 : 0, 'days').format('YYYY-MM-DD')] : ''
      }
      newsearches.push(item)
    })
    return newsearches
  }
  /**
   * @description 拼接搜索条件
   * @param {Array}   searches     搜索条件
   * @return {String}  searchText  拼接结果
   */
  static mainjointsearchkey (searches) {
  static joinMainSearchkey (searches) {
    if (!searches || searches.length === 0) return ''
    if (searches[0].hasOwnProperty('initval')) {
      let newsearches = []
      searches.forEach(search => {
        let item = {
          key: search.field,
          match: search.match,
          type: search.type,
          value: search.initval
        }
        if (item.type === 'date') {
          item.value = item.value ? moment().subtract(item.value, 'days').format('YYYY-MM-DD') : ''
        } else if (item.type === 'datemonth') {
          item.value = item.value ? moment().subtract(item.value, 'month').format('YYYY-MM') : ''
        } else if (item.type === 'dateweek') {
          item.value = item.value ? [moment().subtract(item.value * 7, 'days').startOf('week').format('YYYY-MM-DD'),
            moment().subtract(item.value * 7, 'days').endOf('week').format('YYYY-MM-DD')] : ''
        } else if (item.type === 'daterange') {
          item.value = item.value ? [moment().subtract(item.value, 'days').format('YYYY-MM-DD'),
            moment().subtract(item.value === 1 ? 1 : 0, 'days').format('YYYY-MM-DD')] : ''
        }
        newsearches.push(item)
      })
      searches = newsearches
    }
    let searchText = ''
    searches.forEach(item => {
@@ -231,15 +240,26 @@
        let str = item.match === '=' ? '' : '%'
        // eslint-disable-next-line
        searchText += item.key + ' ' + item.match + ' ' + '\'' + str + item.value + str + '\''
      } else if (item.type === 'date' || item.type === 'datemonth') {
      } else if (item.type === 'date') {
        let timetail = ' 00:00:00.000'
        if (item.match === '<' || item.match === '<=') {
          timetail = ' 23:59:59.999'
        }
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + timetail + '\')'
      } else if (item.type === 'datemonth') {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + '\')'
      } else if (item.type === 'dateweek') {
        let stimetail = ' 00:00:00.000'
        let etimetail = ' 23:59:59.999'
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + '\' AND \'' + item.value[1] + '\')'
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + stimetail + '\' AND \'' + item.value[1] + etimetail + '\')'
      } else if (item.type === 'daterange') {
        let stimetail = ' 00:00:00.000'
        let etimetail = ' 23:59:59.999'
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + '\' AND \'' + item.value[1] + '\')'
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value[0] + stimetail + '\' AND \'' + item.value[1] + etimetail + '\')'
      } else {
        // eslint-disable-next-line
        searchText += '(' + item.key + ' ' + item.match + ' ' + '\'' + item.value + '\')'
@@ -296,4 +316,182 @@
    let realurl = url.match(/^http/) || url.match(/^\/\//) ? url : baseurl + url
    return realurl
  }
  /**
   * @description 使用系统函数时(sPC_TableData_InUpDe ),生成sql语句
   * @return {String} type   执行类型
   * @return {String} table  表名
   */
  static getSysDefaultSql (btn, setting, formdata) {
    console.log(btn)
    console.log(formdata)
    let _sql = ''
    if (btn.OpenType === 'pop' && btn.sqlType === 'insert') {
      let keys = []
      let values = []
      formdata.forEach(item => {
        if (!item.readonly && item.type === 'number') {
          keys.push(item.key)
          values.push(item.value)
        } else if (!item.readonly) {
          keys.push(item.key)
          values.push('\'' + item.value + '\'')
        }
      })
      keys = keys.join(',')
      values = values.join(',')
      _sql = `insert into ${btn.sql} (${keys}, createuserid) select ${values},@userid`
    } else if (btn.OpenType === 'pop' && btn.sqlType === 'update') {
      let _form = []
      formdata.forEach(item => {
        if (!item.readonly && item.type === 'number') {
          _form.push(item.key + '=' + item.value)
        } else if (!item.readonly) {
          _form.push(item.key + '=\'' + item.value + '\'')
        }
      })
      _form = _form.join(',')
      _sql = `update ${btn.sql} set ${_form},modifydate=getdate(),modifyuserid=@userid where ${setting.primaryKey}=@id`
    } else if ((btn.OpenType === 'prompt' || btn.OpenType === 'exec') && btn.sqlType === 'LogicDelete') { // 逻辑删除
      _sql = `update ${btn.sql} set deleted=1,modifydate=getdate(),modifyuserid=@userid where ${setting.primaryKey}=@id`
    } else if ((btn.OpenType === 'prompt' || btn.OpenType === 'exec') && btn.sqlType === 'delete') {
      _sql = `insert into snote (remark,createuserid) select '删除表:${btn.sql} 数据: id='+@id,@userid delete ${btn.sql} where ${setting.primaryKey}=@id`
    }
    //insert into 表名 (表单字段,逗号分隔,如果主键为可编辑则传填写内容,只读或没有配置表单,则传前端生成id,+createuserid)
    // +select 表单字段值+@userid
    // 修改:update 表名 set 表单非只读字段=表单值,modifydate=getdate(),modifyuserid=@userid where 主键=@id行id
    // 逻辑删除:update 表名 set deleted=1,modifydate=getdate(),modifyuserid=@userid where 主键=@id行id
    // 物理删除:insert into snote (remark,createuserid) select '删除表 '+表名+'数据 '+id=@id delete 表名 where  主键=@id行id
    console.log(_sql)
    return _sql
  }
  /**
   * @description 删除存储过程sql
   * @return {String} name 存储过程名称
   */
  static dropfunc (name) {
    return `IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID('${name}') AND type in (N'P', N'PC'))  mdrpk PROCEDURE ${name}`
  }
  /**
   * @description 创建存储过程
   * @return {String}
   */
  static getfunc (param) {
    let form = ''
    let formParam = ''
    if (param.fields && param.fields.length > 0) {
      let _fields = []
      param.fields.forEach(item => {
        if (item.field) {
          let type = ''
          if (item.type.match(/date/ig)) {
            type = 'datetime=null'
          } else if (item.type === 'number') {
            type = `decimal(18,${item.decimal})=0`
          } else {
            type = 'nvarchar(50)=\'\''
          }
          formParam = formParam + `mchr13k@${item.field} ${type},`
          _fields.push(item.field)
        }
      })
      let field1 = _fields.join(',')
      let field2 = _fields.join(',@')
      let field3 = _fields.map(cell => {
        return cell + '=@' + cell
      })
      field2 = field2 ? '@' + field2 : ''
      field3 = field3.join(',')
      form = `
        insert into ${param.name} (${field1},createuserid) select ${field2},@UserID
        update ${param.name} set ${field3},modifydate=getdate(),modifyuserid=@UserID
      `
    }
    let Ltext = `create proc ${param.funcName}
    (
    @BID nvarchar(50)='',
    @ID nvarchar(50)='',${formParam}
    @sEPTMenuNo nvarchar(50)='${param.menuNo}',
    @lang nvarchar(50)='',
    @debug nvarchar(50)='',
    @LoginUID nvarchar(50)='',
    @SessionUid nvarchar(50)='',
    @UserID nvarchar(50),
    @ErrorCode nvarchar(50) out,
    @retmsg nvarchar(4000) out
    )
    as
    begin
    declare  @BegindateTest datetime,@EnddateTest datetime
    select  @BegindateTest=getdate()
    set @ErrorCode=''
    set @retmsg=''
    BEGIN TRY
      /*事务操作*/
      BEGIN TRAN
        /*具体业务操作*/
         /*
        select top 10 * from sProcExcep order by id desc
        declare @UserName  nvarchar(50),@FullName nvarchar(50)
        select @UserName=UserName,@FullName=FullName from SUsers where UID=@UserID
        ${form}
        if 1=2
        begin
          set @ErrorCode='E'
          set @retmsg='在此写报错'
          goto GOTO_RETURN
        end
        insert into sNote (remark,createuserid,CreateUser,CreateStaff)
        select '在此写日志',@UserID,@UserName,@FullName
        */
      COMMIT TRAN
      SET NOCOUNT ON
      RETURN
    END TRY
    BEGIN CATCH
      /*错误处理*/
      ROLLBACK TRAN
      DECLARE @ErrorMessage NVARCHAR(4000);
      DECLARE @ErrorSeverity INT;
      DECLARE @ErrorState INT;
      /*把自定义的友好的错误信息提示加上*/
      set @ErrorCode=cast(ERROR_NUMBER() as nvarchar(50))
      SET @retmsg=ERROR_MESSAGE();
      SELECT @ErrorMessage=ERROR_MESSAGE(),
        @ErrorSeverity=ERROR_SEVERITY(),
        @ErrorState=ERROR_STATE();
      RAISERROR(@ErrorMessage, /*-- Message text.*/
        @ErrorSeverity, /*-- Severity.*/
        @ErrorState  /*-- State.*/
        );
    END CATCH
    GOTO_RETURN:
      ROLLBACK TRAN
    END`
    Ltext = Ltext.replace(/\n\s{4}/ig, 'mchr13k')
    console.log(Ltext)
    return Ltext
  }
}