<?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="org.sky.scientific.mapper.CgZlMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.CgZlEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="ZLH" property="zlh"/>
        <result column="ZLMC" property="zlmc"/>
        <result column="ZLLX" property="zllx"/>
        <result column="SFGFZL" property="sfgfzl"/>
        <result column="GB" property="gb"/>
        <result column="SQH_ZLH" property="sqhZlh"/>
        <result column="SHENQING_RQ" property="shenqingRq"/>
        <result column="SHOUQUAN_RQ" property="shouquanRq"/>
        <result column="SQDW" property="sqdw"/>
        <result column="ZLQR" property="zlqr"/>
        <result column="GYQR" property="gyqr"/>
        <result column="FMR" property="fmr"/>
        <result column="FLZT" property="flzt"/>
        <result column="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>
        <result column="XM_ID" property="xmId"/>
        <result column="SJRYX" property="sjryx"/>
        <result column="ZLTXGN" property="zltxgn"/>
        <result column="create_user" property="createUser"/>
        <result column="create_dept" property="createDept"/>
        <result column="create_time" property="createTime"/>
        <result column="update_user" property="updateUser"/>
        <result column="update_time" property="updateTime"/>
        <result column="status" property="status"/>
        <result column="is_deleted" property="isDeleted"/>
    </resultMap>

    <select id="selectPageByCondition" resultMap="ResultMap" parameterType="org.sky.scientific.pojo.entity.CgZlEntity">
        SELECT
            z.id,z.tenant_id,z.ZLH,z.ZLMC,z.ZLLX,z.SFGFZL,z.GB,z.SQH_ZLH,z.SHENQING_RQ,z.SHOUQUAN_RQ,z.SQDW,z.ZLQR,z.GYQR,z.FMR,z.FLZT,z.SJRYX,z.ZLTXGN,z.XMBH,z.XM_ID,z.attachment,
            xm.XMMC
        FROM kd_cg_zl z
        left join
        (
            select
                m.id,
                COALESCE(get_xm_info(m.id,'XMMC',null), XMMC) as XMMC
            from kd_xm m
        ) xm on xm.id = z.xm_id
        where z.tenant_id = #{ew.tenantId}
        <if test="ew.zlmc!=null and ew.zlmc!=''">and z.ZLMC like concat('%',#{ew.zlmc},'%')</if>
        <if test="ew.zllx!=null and ew.zllx!=''">and z.ZLLX = #{ew.zllx}</if>
        <if test="ew.sfgfzl!=null and ew.sfgfzl!=''">and z.sfgfzl = #{ew.sfgfzl}</if>
        <if test="ew.gb!=null and ew.gb!=''">and z.GB = #{ew.gb}</if>
        <if test="ew.sqhZlh != null and ew.sqhZlh!=''">and z.SQH_ZLH like concat('%',#{ew.sqhZlh},'%')</if>
        <if test="ew.sqdw != null and ew.sqdw!=''">and z.SQDW like concat('%',#{ew.sqdw},'%')</if>
        <if test="ew.zlqr != null and ew.zlqr!=''">and z.ZLQR like concat('%',#{ew.zlqr},'%')</if>
        <if test="ew.flzt != null and ew.flzt!=''">and z.FLZT = #{ew.flzt}</if>
        <if test="ew.xmmc != null and ew.xmmc!=''">and xm.XMMC like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.xmbh != null and ew.xmbh!=''">and z.XMBH like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.xmIdList!=null and ew.xmIdList.size()>0">and z.xm_id in <foreach item="xmId" collection="ew.xmIdList" separator="," open="(" close=")">#{xmId}</foreach></if>
    </select>

    <delete id="physicalDeleteByIds">
        DELETE FROM kd_cg_zl WHERE id IN
        <foreach item="id" collection="list" separator="," open="(" close=")">
            #{id}
        </foreach>
    </delete>

    <select id="zlDashboard" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            (SELECT count(*) from kd_cg_zl where tenant_id = #{tenantId} and DATE_FORMAT(SHENQING_RQ,'%Y') = #{year}) as total,
            (SELECT count(*) from kd_cg_zl where tenant_id = #{tenantId} and DATE_FORMAT(SHENQING_RQ,'%Y') = #{year} and FLZT in ('申请中','授权有效')) as shenqingshu,
            (SELECT count(*) from kd_cg_zl where tenant_id = #{tenantId} and DATE_FORMAT(SHOUQUAN_RQ,'%Y') = #{year} and FLZT = '授权有效') as shouquanshu
    </select>

</mapper>
