$PBExportHeader$uo_string.sru
forward
global type uo_string from nonvisualobject
end type
end forward

global type uo_string from nonvisualobject
end type
global uo_string uo_string

forward prototypes
public function string uf_right (string arg_str, long arg_lena)
public subroutine uof_split (string arg_source, string arg_spliter, ref string arg_return[])
public function string uof_replace (string arg_str, string arg_old, string arg_new)
public function string uof_replace_sql (string arg_input)
public function string uof_replace_sql_like (string arg_input)
public function string uof_replace_sql_code (string arg_str)
public function string uof_replace_trim_name (string arg_str)
public function string uof_getitemstring (datastore arg_ds, long arg_row, string arg_colname)
public function string uof_getitemname (datastore arg_ds, long arg_row, string arg_colname)
public function string uof_getitemname (datawindow arg_ds, long arg_row, string arg_colname)
public function string uf_add_single_quotes (string arg_str)
public function string uof_getitemformattedstring (datawindow arg_dw, long arg_row, string arg_colname)
public function string uof_dw_getsortname (datawindow arg_dw)
public function integer uof_dw_append_filter (datawindow arg_dw, ref string arg_filter)
public function integer uof_dw_append_sort (datawindow arg_dw, ref string arg_sort)
end prototypes

public function string uf_right (string arg_str, long arg_lena);//====================================================================
// 事件: uo_string.uf_right()
//--------------------------------------------------------------------
// 描述: 返回字符串右边指定长度内容,不截断中文
//--------------------------------------------------------------------
// 参数:
// 	value	string	arg_str 	
// 	value	long  	arg_lena	
//--------------------------------------------------------------------
// 返回:  string
//--------------------------------------------------------------------
// 作者:	lwl		日期: 2018年03月29日
//--------------------------------------------------------------------
//	LONGJOE
//--------------------------------------------------------------------
// 修改历史:
//
//====================================================================

string ls_rslt
long ll_lena
long i = 0

ls_rslt = right(arg_str, arg_lena)
ll_lena = lena(ls_rslt)

do while ll_lena > arg_lena
	i ++
	ls_rslt = right(arg_str, arg_lena - i)
	ll_lena = lena(ls_rslt)
loop

return ls_rslt


end function

public subroutine uof_split (string arg_source, string arg_spliter, ref string arg_return[]);string arg_rslt[]
long ll_cnt = 0
long ll_pos = 0

do while len(arg_source) > 0
	ll_pos = pos(arg_source, arg_spliter)
	if ll_pos > 0 then
		ll_cnt++
		arg_rslt[ll_cnt] = mid(arg_source, 1, ll_pos - 1)
		arg_source = mid(arg_source, ll_pos + len(arg_spliter))
	else
		ll_cnt++
		arg_rslt[ll_cnt] = arg_source
		arg_source = ''
	end if
loop

arg_return = arg_rslt
end subroutine

public function string uof_replace (string arg_str, string arg_old, string arg_new);

long ll_pos = 1, ll_find
ll_find = pos(arg_str, arg_old, ll_pos)
do while ll_find > 0
	arg_str = Replace(arg_str, ll_find, len(arg_old), arg_new)
	ll_pos = ll_find + len(arg_new)
	ll_find = pos(arg_str, arg_old, ll_pos)
loop

return arg_str
end function

public function string uof_replace_sql (string arg_input);return uof_replace(arg_input, "'", "''")
end function

public function string uof_replace_sql_like (string arg_input);arg_input = uof_replace(arg_input, "'", "''")
arg_input = uof_replace(arg_input, "[", "[[]")
arg_input = uof_replace(arg_input, "_", "[_]")
arg_input = uof_replace(arg_input, "%", "[%]")
arg_input = uof_replace(arg_input, "^", "[^]")

return arg_input
end function

public function string uof_replace_sql_code (string arg_str);// 将 u_orderrqwp.empCN_QC 转换成 CASE WHEN CHARINDEX(':',u_orderrqwp.empCN_QC,1) > 0 THEN LEFT(u_orderrqwp.empCN_QC,CHARINDEX(':',u_orderrqwp.empCN_QC,1)-1) ELSE u_orderrqwp.empCN_QC END

string ls_sql

ls_sql = "CASE WHEN CHARINDEX(':',"+arg_str+",1) > 0  "&
+" THEN LEFT("+arg_str+",CHARINDEX(':',"+arg_str+",1)-1) "&
+" ELSE "+arg_str+" END"

RETURN ls_sql
end function

public function string uof_replace_trim_name (string arg_str);// 将'A:B','B:C','C:D'转换成'A','B','C'

Long ll_pos = 1, ll_find, ll_pos2
ll_find = Pos(arg_str, ":", ll_pos)
DO WHILE ll_find > 0
	ll_pos2 = Pos(arg_str, "'", ll_find)
	IF ll_pos2 > 0 THEN
		arg_str = Replace(arg_str, ll_find, ll_pos2 - ll_find, "")
		ll_pos = ll_find
	ELSE
		EXIT
	END IF
	
	ll_find = Pos(arg_str, ":", ll_pos)
LOOP

RETURN arg_str

end function

public function string uof_getitemstring (datastore arg_ds, long arg_row, string arg_colname);string ls_coltype

ls_coltype = lower(arg_ds.Describe(arg_colname+".ColType"))

IF ls_coltype = '!' OR ls_coltype = '' THEN
	return "''"
END IF

IF pos(ls_coltype, 'char') > 0 THEN
	return "'"+arg_ds.GetItemString(arg_row, arg_colname)+"'"
END IF

IF pos(ls_coltype, 'dec') > 0 THEN
	return string(arg_ds.GetItemDecimal(arg_row, arg_colname))
END IF

IF pos(ls_coltype, 'datetime') > 0 THEN
	return string(arg_ds.GetItemDateTime(arg_row, arg_colname), 'yyyy-MM-dd')
END IF

IF pos(ls_coltype, 'date') > 0 THEN
	return string(arg_ds.GetItemDate(arg_row, arg_colname), 'yyyy-MM-dd')
END IF

IF pos(ls_coltype, 'time') > 0 THEN
	return string(arg_ds.GetItemTime(arg_row, arg_colname), 'HH:mm:ss')
END IF

IF pos(ls_coltype, 'long') > 0 OR pos(ls_coltype, 'num') > 0 OR pos(ls_coltype, 'int') > 0 THEN
	return string(arg_ds.GetItemNumber(arg_row, arg_colname))
END IF

return "''"
end function

public function string uof_getitemname (datastore arg_ds, long arg_row, string arg_colname);string ls_style

ls_style = lower(arg_ds.Describe(arg_colname + ".Edit.Style"))

if ls_style = 'checkbox' then
	IF arg_ds.GetItemNumber(arg_row, arg_colname) = 1 THEN
		return '✔'
	ELSE
		return '❌'
	END IF
else
	return arg_ds.Describe("Evaluate('lookupdisplay("+arg_colname+")',"+String(arg_row)+")")
end if
end function

public function string uof_getitemname (datawindow arg_ds, long arg_row, string arg_colname);string ls_style

ls_style = lower(arg_ds.Describe(arg_colname + ".Edit.Style"))

if ls_style = 'checkbox' then
	IF arg_ds.GetItemNumber(arg_row, arg_colname) = 1 THEN
		return '✔'
	ELSE
		return '❌'
	END IF
else
	return arg_ds.Describe("Evaluate('lookupdisplay("+arg_colname+")',"+String(arg_row)+")")
end if
end function

public function string uf_add_single_quotes (string arg_str);string reslt = ''
string arr_ids[]
long i

uof_split(arg_str, ',', Ref arr_ids)
FOR i = 1 To UpperBound(arr_ids)
	IF reslt <> '' THEN reslt += ','
	reslt += "'"+arr_ids[i] + "'"
NEXT

return reslt
end function

public function string uof_getitemformattedstring (datawindow arg_dw, long arg_row, string arg_colname);return arg_dw.Describe("Evaluate('lookupdisplay("+ arg_colname+")',"+String(arg_row)+")")
end function

public function string uof_dw_getsortname (datawindow arg_dw);String ls_sort

ls_sort = arg_dw.Describe("DataWindow.Table.Sort")

IF Len(ls_sort) < 3 THEN
	RETURN ""
END IF

String arr_col[]

uof_split(ls_sort, ",", Ref arr_col)

Long i, ll_pos
String ls_colname, ls_s, ls_rslt = '', ls_tmp
FOR i = 1 To UpperBound(arr_col)
	ls_colname = Trim(Lower(arr_col[i]))
	ll_pos = Pos(ls_colname, ' ')
	IF ll_pos > 0 THEN
		ls_s = Trim(Mid(ls_colname, ll_pos + 1))
		ls_colname = Left(ls_colname, ll_pos - 1)
	ELSE
		ls_s = 'a'
	END IF
	ls_tmp = Trim(arg_dw.Describe(ls_colname+"_t.Text"))
	
	IF ls_tmp = '!' Or ls_tmp = '?' THEN
		ls_rslt += ls_colname
	ELSE
		ls_rslt += ls_tmp
	END IF
	
	IF mid(ls_s, 1, 1) = 'a' THEN
		ls_rslt += "▲"
	ELSE
		ls_rslt += "▼"
	END IF
	ls_rslt += ","
NEXT

return ls_rslt


end function

public function integer uof_dw_append_filter (datawindow arg_dw, ref string arg_filter);String ls_filter


ls_filter = arg_dw.Describe("DataWindow.Table.Filter")

ls_filter = uof_replace(ls_filter, "~~~~", "~~")
ls_filter = uof_replace(ls_filter, "~~~"", "~"")

arg_filter = "("+arg_filter+")"

IF len(ls_filter) > 1 THEN
	ls_filter += " AND " + arg_filter
ELSE
	ls_filter = arg_filter
END IF

arg_filter = ls_filter

RETURN arg_dw.SetFilter(ls_filter)

end function

public function integer uof_dw_append_sort (datawindow arg_dw, ref string arg_sort);String ls_sort

ls_sort = arg_dw.Describe("DataWindow.Table.Sort")

IF Len(ls_sort) < 3 THEN
	ls_sort = arg_sort
ELSE
	ls_sort += ","+arg_sort
END IF

arg_sort = ls_sort

return arg_dw.SetSort(ls_sort)

end function

on uo_string.create
call super::create
TriggerEvent( this, "constructor" )
end on

on uo_string.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

