<?xml version="1.0" encoding="utf-8" ?>
<select>
  <selectstr>
SELECT u_mattress.mattressid
		,mattressname
		,deptid
		,mattresstypeid
		,mattresscode
		,mattress_width
		,mattress_length
		,mattress_height
		,packtype
		,packqty
		,woodpallettype
		,total_hr_cost
		,total_material_cost
		,fees_dscrp
		,total_fees_cost
		,total_cost
		,taxrate
		,taxes
		,commissionrate
		,commission
		,fob
		,profitrate
		,nottax_factory_cost
		,nottax_dept_cost
		,dept_cost
		,foreign_cost
		,extras_cost
		,dept_profitrate
		,moneyrate
		,mattressrelcode
		,flag
		,auditingrep
		,auditingdate
		,createtime
		,createby
		,yw_flag
		,yw_auditingrep
		,yw_auditingdate
		,js1_flag
		,js1_auditingrep
		,js1_auditingdate
		,js2_flag
		,js2_auditingrep
		,js2_auditingdate
		,creatmtrl_flag
		,creatmtrl_auditingrep
		,creatmtrl_auditingdate
		,creatmtrlqd_flag
		,creatmtrlqd_auditingrep
		,creatmtrlqd_auditingdate
		,erp_mtrlcode AS erp_mtrlcode
		,erp_mtrlid
		,nottax_factory_cost / (dept_profitrate + dept_profitrate_rangli / 100) AS dijia_cost
		,erp_mtrlengname
		,erp_configcodetype
		,0 AS yongjin
		,ISNULL(parentid, 0) AS parentid
		,vv_mattress.child_count
    FROM u_mattress
	LEFT JOIN (
		SELECT
			um.mattressid,
			COUNT(um2.mattressid) AS child_count
		FROM
			u_mattress um
		LEFT JOIN
			u_mattress um2 ON um.mattressid = um2.parentid
		GROUP BY um.mattressid
	) AS vv_mattress on vv_mattress.mattressid = u_mattress.mattressid
  </selectstr>
  <where>
	<when>u_mattress.xd_flag = 1</when>
	<when notnull="@arg_deptid">
		u_mattress.deptid=@arg_deptid
	</when>
	<when notnull="@creatmtrlqd_flag" creatmtrlqd_flag!="-1">
		u_mattress.creatmtrlqd_flag = @creatmtrlqd_flag
	</when>
	<when notnull="@creatmtrl_flag" creatmtrl_flag!="-1">
		u_mattress.creatmtrl_flag = @creatmtrl_flag
	</when>
	<when notnull="@js2_flag" js2_flag!="-1">
		u_mattress.js2_flag = @js2_flag
	</when>
	<when notnull="@js1_flag" js1_flag!="-1">
		u_mattress.js1_flag = @js1_flag
	</when>
	<when notnull="@yw_flag" yw_flag!="-1">
		u_mattress.yw_flag = @yw_flag
	</when>
	<when $user_outrepstr!="-1">
		u_mattress.createby in ($$user_outrepstr$$)
	</when>
	<when $user_deptstr!="-1">
		u_mattress.deptid in ($$user_deptstr$$)
	</when>
	<when notnull="@arg_search">
		u_mattress.mattresscode like '%'+ @arg_search + '%' OR u_mattress.mattressrelcode like '%'+ @arg_search + '%'
		OR u_mattress.mattressname like '%'+ @arg_search + '%' OR u_mattress.erp_mtrlengname like '%'+ @arg_search + '%'
		OR u_mattress.erp_mtrlcode like '%'+ @arg_search + '%'
	</when>
  </where>
  <orderstr>mattressid DESC</orderstr>
  <displayfields>
    <field field="yw_flag" datatype="checkbox">业务审核</field>
    <field field="js1_flag" datatype="checkbox">产品补充审核</field>
    <field field="js2_flag" datatype="checkbox">清单补充审核</field>
    <field field="creatmtrl_flag" datatype="checkbox">生成L1产品</field>
    <field field="creatmtrlqd_flag" datatype="checkbox">生成L1清单</field>
    <field field="erp_mtrlcode">L1物料编码</field>
    <field field="deptid" mapper="">部门</field>
    <field field="mattresscode">报价唯一码</field>
    <field field="mattressrelcode">床垫编码</field>
    <field field="mattressname">床垫名称</field>
    <field field="mattresstypeid" mapper="">床垫类别</field>
    <field field="mattress_width" datatype="number">床垫宽/CM</field>
    <field field="mattress_length" datatype="number">床垫长/CM</field>
    <field field="mattress_height" datatype="number">床垫高/CM</field>
    <field field="createby">登记人</field>
    <field field="createtime" datatype="datetime">登记时间</field>
    <field field="yw_auditingrep">业务审核人</field>
    <field field="yw_auditingdate" datatype="datetime">业务审核时间</field>
    <field field="js1_auditingrep">产品补充审核人</field>
    <field field="js1_auditingdate" datatype="datetime">产品补充审核时间</field>
    <field field="js2_auditingrep">清单补充审核人</field>
    <field field="js2_auditingdate" datatype="datetime">清单补充审核时间</field>
    <field field="creatmtrl_auditingrep">生成L1产品人</field>
    <field field="creatmtrl_auditingdate" datatype="datetime">生成L1产品时间</field>
    <field field="creatmtrlqd_auditingrep">生成L1清单人</field>
    <field field="creatmtrlqd_auditingdate" datatype="datetime">生成L1清单时间</field>
    <field field="dijia_cost" datatype="number">底价</field>
    <field field="yongjin" datatype="number">佣金</field>
    <field field="taxrate" datatype="number">税率</field>
    <field field="fob" datatype="number">Fob</field>
    <field field="taxes" datatype="number">税金</field>
    <field field="erp_mtrlengname">原物料编码</field>
    <field field="erp_configcodetype">ERP配置类型</field>
  </displayfields>
</select>
