supplychain/wwwroot/supply_contracts_list/get_supply_contracts.dspy

157 lines
3.5 KiB
Plaintext

ns = params_kw.copy()
userorgid = await get_userorgid()
if not userorgid:
return {
"widgettype":"Error",
"options":{
"title":"Authorization Error",
"timeout":3,
"cwidth":16,
"cheight":9,
"message":"Please login"
}
}
ns['resellerid'] = userorgid
ns['userorgid'] = userorgid
debug(f'get_supply_contracts.dspy:{ns=}')
if not ns.get('page'):
ns['page'] = 1
if not ns.get('sort'):
ns['sort'] = ["created_at desc"]
sql = '''select a.*, b.supplier_id_text, c.resellerid_text
from (select * from supply_contracts where 1=1 [[filterstr]]) a left join (select id as supplier_id,
supplier_name as supplier_id_text from suppliers where 1 = 1) b on a.supplier_id = b.supplier_id left join (select id as resellerid,
orgname as resellerid_text from organization where 1 = 1) c on a.resellerid = c.resellerid'''
filterjson = params_kw.get('data_filter')
fields_str=r'''[
{
"name": "id",
"title": "主键ID",
"type": "str",
"length": 32,
"nullable": "no"
},
{
"name": "resellerid",
"title": "所属分销商机构ID",
"type": "str",
"length": 32,
"nullable": "no"
},
{
"name": "supplier_id",
"title": "供应商ID",
"type": "str",
"length": 32,
"nullable": "no"
},
{
"name": "contract_code",
"title": "合同编号",
"type": "str",
"length": 64,
"nullable": "no"
},
{
"name": "contract_name",
"title": "合同名称",
"type": "str",
"length": 255,
"nullable": "no"
},
{
"name": "sign_date",
"title": "签署日期",
"type": "date"
},
{
"name": "start_date",
"title": "生效日期",
"type": "date",
"nullable": "no"
},
{
"name": "end_date",
"title": "到期日期",
"type": "date"
},
{
"name": "status",
"title": "状态",
"type": "char",
"length": 1,
"nullable": "no",
"default": "1"
},
{
"name": "default_discount",
"title": "默认折扣",
"type": "double",
"length": 5,
"dec": 4,
"default": "1.0000"
},
{
"name": "remark",
"title": "备注",
"type": "text"
},
{
"name": "created_by",
"title": "创建人",
"type": "str",
"length": 32
},
{
"name": "created_at",
"title": "创建时间",
"type": "datetime",
"nullable": "no"
},
{
"name": "updated_at",
"title": "更新时间",
"type": "datetime"
}
]'''
ori_fields = json.loads(fields_str)
if not filterjson:
fields = [ f['name'] for f in ori_fields ]
filterjson = default_filterjson(fields, ns)
filterdic = ns.copy()
filterdic['filterstr'] = ''
filterdic['userorgid'] = '${userorgid}$'
filterdic['userid'] = '${userid}$'
if filterjson:
dbf = DBFilter(filterjson)
conds = dbf.gen(ns)
if conds:
ns.update(dbf.consts)
conds = f' and {conds}'
filterdic['filterstr'] = conds
ac = ArgsConvert('[[', ']]')
vars = ac.findAllVariables(sql)
NameSpace = {v:'${' + v + '}$' for v in vars if v != 'filterstr' }
filterdic.update(NameSpace)
sql = ac.convert(sql, filterdic)
debug(f'{sql=}')
db = DBPools()
dbname = get_module_dbname('supplychain')
async with db.sqlorContext(dbname) as sor:
r = await sor.sqlPaging(sql, ns)
return r
return {
"total":0,
"rows":[]
}