<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.timer.api.dao.zcgl.ZcglAssoZcdaMapper">

	<resultMap id="BaseResultMap"
		type="cn.timer.api.bean.zcgl.ZcglAssoZcda">
		<id column="id" property="id" />
		<result column="zcbh" property="zcbh" />
		<result column="mc" property="mc" />
		<result column="ggxh" property="ggxh" />
		<result column="jldw" property="jldw" />
		<result column="cs" property="cs" />
		<result column="tulj" property="tulj" />
		<result column="bzxx" property="bzxx" />
		<result column="lrsj_time" property="lrsjTime" />
		<result column="lrrid" property="lrrid" />
	</resultMap>

	<sql id="Base_Column_List">
		id,
		zcbh,
		mc,
		ggxh,
		lb,
		jldw,
		zt_type,
		cs,
		tulj,
		bzxx,
		lrsj_time,
		lrrid
	</sql>

	<sql id="Base_Column_List_Alias">
		id ZcglAssoZcda_id,
		zcbh ZcglAssoZcda_zcbh,
		mc ZcglAssoZcda_mc,
		ggxh ZcglAssoZcda_ggxh,
		lb ZcglAssoZcda_lb,
		jldw ZcglAssoZcda_jldw,
		zt_type ZcglAssoZcda_zt_type,
		cs ZcglAssoZcda_cs,
		tulj ZcglAssoZcda_tulj,
		bzxx ZcglAssoZcda_bzxx,
		lrsj_time ZcglAssoZcda_lrsj_time,
		lrrid ZcglAssoZcda_lrrid
	</sql>
	
	<select id="queryCategory" parameterType="java.lang.Integer" resultType="cn.timer.api.dto.zcgl.ZcglCartogramDto">
		SELECT
			COUNT(b.lbmc) sl
			,b.lbmc mc
		FROM
			zcgl_asso_zcda a
		LEFT JOIN
			zcgl_asso_zcgx b
		ON
			a.lbid = b.id
		WHERE
			b.org_code = #{orgCode}
		AND
		 	FIND_IN_SET(b.id, queryDetails(#{id}
		))
	</select>

	<!-- 根据类别选择查询资产 -->
	<select id="queryDetails" parameterType="java.lang.Integer"  resultType="cn.timer.api.dto.zcgl.ZcglDetailsDto">
		SELECT
			a.id id,
			a.zdyid zdyid,
			a.mc mc,
			a.zcbh zcbh,
			a.ggxh ggxh,
			a.jldw jldw,
			a.cs cs,
			a.tulj tulj,
			a.bzxx bzxx,
			a.lbid lbid,
			b.lbmc lbmc,
			a.xzsl xzsl,
			a.lysl lysl,
			a.bfsl bfsl,
			a.zcdj zcdj
		FROM
			zcgl_asso_zcda a
		LEFT JOIN 
			zcgl_asso_zcgx b 
		ON 
			a.lbid = b.id
		WHERE
			a.org_code = #{orgCode}
		AND FIND_IN_SET(b.id, queryDetails(#{id}
		))
	</select>
	
	<!-- 固定资产/消耗资产(手机端) -->
	<select id="queryGdzc" parameterType="java.lang.Integer"  resultType="cn.timer.api.dto.zcgl.ZcglDetailsDto">
		SELECT
		    a.zdyid zdyid,
			a.mc mc,
			a.zcbh zcbh,
			a.ggxh ggxh,
			a.jldw jldw,
			a.lbid lbid,
			a.xzsl xzsl,
			a.lysl lysl,
			a.bfsl bfsl,
			a.zcdj zcdj
		FROM
			zcgl_asso_zcda a
		LEFT JOIN 
			zcgl_asso_zcgx b 
		ON 
			a.lbid = b.id
		WHERE
			a.org_code = #{orgCode}
		AND FIND_IN_SET(b.id, queryDetails(1
		))
		limit #{currPage},#{pageSize}
	</select>
	<select id="queryXhzc" parameterType="java.lang.Integer"  resultType="cn.timer.api.dto.zcgl.ZcglDetailsDto">
		SELECT
			a.zdyid zdyid,
			a.mc mc,
			a.zcbh zcbh,
			a.ggxh ggxh,
			a.jldw jldw,
			a.lbid lbid,
			a.xzsl xzsl,
			a.lysl lysl,
			a.bfsl bfsl,
			a.zcdj zcdj
		FROM
			zcgl_asso_zcda a
		LEFT JOIN 
			zcgl_asso_zcgx b 
		ON 
			a.lbid = b.id
		WHERE
			a.org_code = #{orgCode}
		AND FIND_IN_SET(b.id, queryDetails(2
		))
		limit #{currPage},#{pageSize}
	</select>
	
	<!-- 根据类别选择删除资产 -->
	<delete id="deleteDetails" parameterType="java.lang.Integer">
		DELETE FROM 
			zcgl_asso_zcda 
		WHERE
			org_code = #{orgCode}
		AND FIND_IN_SET(lbid, queryDetails(#{id}
			))
	</delete>

	<!-- 个人资产 -->
	<select id="personalassets"
		resultType="cn.timer.api.dto.zcgl.ZcglPersonalAssetsDto">
		SELECT
			a.id zcid,
			a.mc zcmc,
			b.lyrq_time lyrq,
			b.ghrq_time ghrq,
			IFNULL(a.zcbh, 'null') bh,
			IFNULL(d.lbmc, 'null') lb,
			IFNULL(a.ggxh, 'null') xh,
			IFNULL(b.lysl, 0) sl,
			IFNULL(a.jldw, 'null') dw,
			IFNULL(a.zcdj, 0.0) je,
			b.lyhcfd cfd
		FROM
			zcgl_asso_zcda a
		LEFT JOIN zcgl_asso_zcly b ON a.id = b.zcid
		LEFT JOIN zcgl_asso_zcgx d ON a.lbid = d.id
		WHERE
			b.lysl>0
		AND	a.org_code = #{orgCode}
		AND b.lyrid = #{empNum}
		
		<if test="query != null">
		and (
			a.mc like CONCAT('%',#{query},'%')
            or 
            a.ggxh like CONCAT('%',#{query},'%')
		) 
        </if>
        <if test="startTime != null and startTime != ''">
        	and b.lyrq_time > #{startTime}
        </if>
        <if test="endTime != null and endTime != ''">
        	and b.lyrq_time <![CDATA[ < ]]> #{endTime}
        </if>
        <!-- limit #{currPage},#{pageSize} -->
	</select>
	
	
	<select id="queryZczj" resultType="cn.timer.api.dto.zcgl.ZcglDetailsDto">
		
		SELECT
			zcdj,xzsl,lysl,bfsl,mc
		FROM
			zcgl_asso_zcda a
		LEFT JOIN
			zcgl_asso_zcgx b
		ON
			a.lbid = b.id
		WHERE
			a.org_code = #{orgCode}
		AND
			FIND_IN_SET(a.lbid, queryDetails(#{id}
		))
		
	</select>


	<!-- <insert id="insert" useGeneratedKeys="true" keyColumn="id" parameterType="cn.timer.api.bean.zcgl.ZcglAssoZcda"> 
		INSERT INTO zcgl_asso_zcda <trim prefix="(" suffix=")" suffixOverrides=","> 
		<if test ='null != zcbh'> zcbh, </if> <if test ='null != mc'> mc, </if> <if 
		test ='null != ggxh'> ggxh, </if> <if test ='null != lb'> lb, </if> <if test 
		='null != jldw'> jldw, </if> <if test ='null != ztType'> zt_type, </if> <if 
		test ='null != cs'> cs, </if> <if test ='null != tulj'> tulj, </if> <if test 
		='null != bzxx'> bzxx, </if> <if test ='null != lrsjTime'> lrsj_time, </if> 
		<if test ='null != lrrid'> lrrid </if> </trim> <trim prefix="values (" suffix=")" 
		suffixOverrides=","> <if test ='null != zcbh'> #{zcbh}, </if> <if test ='null 
		!= mc'> #{mc}, </if> <if test ='null != ggxh'> #{ggxh}, </if> <if test ='null 
		!= lb'> #{lb}, </if> <if test ='null != jldw'> #{jldw}, </if> <if test ='null 
		!= ztType'> #{ztType}, </if> <if test ='null != cs'> #{cs}, </if> <if test 
		='null != tulj'> #{tulj}, </if> <if test ='null != bzxx'> #{bzxx}, </if> 
		<if test ='null != lrsjTime'> #{lrsjTime}, </if> <if test ='null != lrrid'> 
		#{lrrid} </if> </trim> </insert> <delete id="delete" > DELETE FROM zcgl_asso_zcda 
		WHERE id = #{id} </delete> <update id="update" parameterType="cn.timer.api.bean.zcgl.ZcglAssoZcda"> 
		UPDATE zcgl_asso_zcda <set> <if test ='null != zcbh'>zcbh = #{zcbh},</if> 
		<if test ='null != mc'>mc = #{mc},</if> <if test ='null != ggxh'>ggxh = #{ggxh},</if> 
		<if test ='null != lb'>lb = #{lb},</if> <if test ='null != jldw'>jldw = #{jldw},</if> 
		<if test ='null != ztType'>zt_type = #{ztType},</if> <if test ='null != cs'>cs 
		= #{cs},</if> <if test ='null != tulj'>tulj = #{tulj},</if> <if test ='null 
		!= bzxx'>bzxx = #{bzxx},</if> <if test ='null != lrsjTime'>lrsj_time = #{lrsjTime},</if> 
		<if test ='null != lrrid'>lrrid = #{lrrid}</if> </set> WHERE id = #{id} </update> 
		<select id="load" resultMap="BaseResultMap"> SELECT <include refid="Base_Column_List" 
		/> FROM zcgl_asso_zcda WHERE id = #{id} </select> <select id="pageList" resultMap="BaseResultMap"> 
		SELECT <include refid="Base_Column_List" /> FROM zcgl_asso_zcda LIMIT #{offset}, 
		#{pageSize} </select> <select id="pageListCount" resultType="java.lang.Integer"> 
		SELECT count(1) FROM zcgl_asso_zcda </select> -->

</mapper>