<?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.XmMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="ResultMap" type="org.sky.scientific.pojo.entity.XmEntity">
        <result column="id" property="id"/>
        <result column="tenant_id" property="tenantId"/>
        <result column="XMBH" property="xmbh"/>
        <result column="XMMC" property="xmmc"/>
        <result column="XMFZR" property="xmfzr"/>
        <result column="XMSQDWID" property="xmsqdwid"/>
        <result column="XMSQDW" property="xmsqdw"/>
        <result column="YJLX" property="yjlx"/>
        <result column="XMKSSJ" property="xmkssj"/>
        <result column="XMJSSJ" property="xmjssj"/>
        <result column="XMLY" property="xmly"/>
        <result column="XMKZXS" property="xmkzxs"/>
        <result column="YQCGXS" property="yqcgxs"/>
        <result column="XMJSJJMB" property="xmjsjjmb"/>
        <result column="XMYSZE" property="xmysze"/>
        <result column="XMYSZEMX" property="xmyszemx"/>
        <result column="LXPW" property="lxpw"/>
        <result column="LXBG" property="lxbg"/>
        <result column="QTZL" property="qtzl"/>
        <result column="remark" property="remark"/>
        <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>

    <resultMap id="ResultVoMap" type="org.sky.scientific.pojo.vo.XmVO" extends="ResultMap"/>

    <update id="updateXm" parameterType="org.sky.scientific.pojo.dto.XmUpdateDto">
        update kd_xm set yjlx = #{yjlx},xmly = #{xmly},xmkzxs = #{xmkzxs},yqcgxs = #{yqcgxs},xmjsjjmb = #{xmjsjjmb},
        lxpw = #{lxpw},lxbg = #{lxbg},qtzl = #{qtzl},remark = #{remark} where id = #{xmId}
    </update>

    <select id="selectXmPage" resultMap="ResultMap">
        SELECT
            xm.id,
            xm.XMBH,
            COALESCE(get_xm_info(xm.id,'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC,
            COALESCE(get_xm_info(xm.id,'STATUS', #{ew.yearAndMonth}),status) AS status,
            xm.XMKSSJ,
            COALESCE(get_xm_info(xm.id,'XMJSSJ', #{ew.yearAndMonth}),XMJSSJ) AS XMJSSJ
        FROM kd_xm xm
        WHERE xm.status != 0
          <if test = "ew.use==1">
              and #{ew.yearAndMonth} between DATE_FORMAT(xm.XMKSSJ, '%Y%m') and date_format(COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{ew.yearAndMonth}),xm.XMJSSJ), '%Y%m')
          </if>
    </select>

    <select id="selectXmListByYear" resultMap="ResultMap">
        SELECT
            xm.id,
            xm.XMBH,
            COALESCE(get_xm_info(xm.id,'XMMC', #{year}),XMMC) AS XMMC,
            xm.XMKSSJ,
            COALESCE(get_xm_info(xm.id,'XMJSSJ', #{year}),XMJSSJ) AS XMJSSJ,
            COALESCE(get_xm_info(xm.id,'STATUS', #{year}),STATUS) AS status
        FROM kd_xm xm
        WHERE xm.status != 0
            and #{year} between DATE_FORMAT(XMKSSJ, '%Y') and DATE_FORMAT(COALESCE(get_xm_info(xm.id,'XMJSSJ', #{year}),XMJSSJ), '%Y')
    </select>

    <select id="summaryPage" resultType="org.sky.scientific.pojo.vo.XmSummary">
        SELECT
        xm.id, xm.xmbh,xm.xmkssj,
        COALESCE(get_xm_info(xm.id, 'XMMC', #{ew.yearAndMonth}), xm.XMMC) AS xmmc,
        COALESCE(get_xm_info(xm.id, 'XMFZR', #{ew.yearAndMonth}), xm.XMFZR) AS xmfzr,
        tech.name as xmfzrxm,
        COALESCE(get_xm_info(xm.id, 'XMSQDWID', #{ew.yearAndMonth}), xm.XMSQDWID) AS xmsqdwid,
        CONCAT_WS('/',(SELECT name FROM kd_department d2 WHERE d2.id = d.parent_id), d.`name`) as xmsqdw,
        COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{ew.yearAndMonth}), xm.XMJSSJ) AS xmjssj,
        COALESCE(get_xm_info(xm.id, 'XMYSZEMX', #{ew.yearAndMonth}), xm.XMYSZEMX) AS xmyszemx,
        COALESCE(get_xm_info(xm.id, 'STATUS', #{ew.yearAndMonth}), xm.STATUS) AS status,
        LXBG as lxbg,
        (select count(*) from kd_xm_jdxbg jdxbg where jdxbg.xm_id = xm.id and date_format(jdxbg.fill_date,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as jdxbgCount,
        (select count(*) from kd_xm_jtb jtb where jtb.xm_id = xm.id and date_format(jtb.tbsj,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as ysbgCount,
        (select count(*) from kd_cg_zl zl where zl.xm_id = xm.id and zl.ZLLX = '发明专利' and date_format(zl.SHOUQUAN_RQ,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as fmzlCount,
        (select count(*) from kd_cg_zl zl where zl.xm_id = xm.id and zl.ZLLX = '实用新型专利' and date_format(zl.SHOUQUAN_RQ,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as syxxzlCount,
        (select count(*) from kd_cg_rjzzq rjzzq where rjzzq.xm_id = xm.id and date_format(rjzzq.DJPZRQ,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as rzCount,
        (select count(*) from kd_cg_jsbz jsbz where jsbz.xm_id = xm.id  and date_format(jsbz.SSRQ,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as bzCount,
        (select count(*) from kd_cg_kylw kylw where kylw.xm_id = xm.id  and date_format(kylw.FBRQ,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as lwCount,
        (select count(*) from kd_cg_qt qt where qt.xm_id = xm.id  and date_format(qt.SHOUQUAN_RQ,'%Y%m') between concat(left(#{ew.yearAndMonth},4),'01') and #{ew.yearAndMonth}) as qtCount,
        (select ifnull(sum(WWFYJE), 0) from kd_xm_wwxm ww where ww.XM_ID = xm.id and ww.year_and_month = left(#{ew.yearAndMonth}, 4)) as wwAmount
        FROM kd_xm xm
        LEFT JOIN kd_technician tech ON tech.id = COALESCE(get_xm_info(xm.id, 'XMFZR', #{ew.yearAndMonth}), xm.XMFZR) and tech.tenant_id = #{ew.tenantId}
        LEFT JOIN kd_department d on d.id = COALESCE(get_xm_info(xm.id, 'XMSQDWID', #{ew.yearAndMonth}), xm.XMSQDWID)
        WHERE xm.tenant_id = #{ew.tenantId}
              and
                (
                    #{ew.yearAndMonth} between DATE_FORMAT(xm.XMKSSJ, '%Y%m') and date_format(COALESCE(get_xm_info(xm.id, 'XMJSSJ', #{ew.yearAndMonth}),xm.XMJSSJ), '%Y%m')
                    or (xm.XMKSSJ is null and xm.status=0)
                )
        <if test="ew.xmbh!=null and ew.xmbh!=''">and xm.XMBH like concat('%',#{ew.xmbh},'%')</if>
        <if test="ew.xmmc!=null and ew.xmmc!=''">and COALESCE(get_xm_info(xm.id, 'XMMC', #{ew.yearAndMonth}), xm.XMMC) like concat('%',#{ew.xmmc},'%')</if>
        <if test="ew.xmfzrxm!=null and ew.xmfzrxm!=''">and tech.name like concat('%', #{ew.xmfzrxm},'%')</if>
        <if test="ew.xmsqdw!=null and ew.xmsqdw!=''">and CONCAT_WS('/',(SELECT name FROM kd_department d2 WHERE d2.id = d.parent_id), d.`name`) like concat('%',#{ew.xmsqdw},'%')</if>
        <if test="ew.status!=null">and COALESCE(get_xm_info(xm.id, 'STATUS', #{ew.yearAndMonth}), xm.STATUS) = #{ew.status}</if>
    </select>

    <select id="selectYszb" resultType="org.sky.scientific.pojo.vo.XmYszbVO">
        SELECT xm.id as xmId,xm.XMKSSJ,
            COALESCE(get_xm_info(xm.id, 'XMMC', null), xm.XMMC) AS xmmc,
            COALESCE(get_xm_info(xm.id, 'XMSQDWID', null),xm.XMSQDWID) AS xmsqdwid,
            COALESCE(get_xm_info(xm.id, 'XMJSSJ', null),xm.XMJSSJ) AS xmjssj,
            COALESCE(get_xm_info(xm.id, 'XMYSZEMX', null),xm.XMYSZEMX) AS XMYSZEMX,
            (select ifnull(sum(ZZSC*PJRGFY),0) from kd_ys_ryrgfy where XM_ID = #{xmId}) as ryrgfy,
            (select ifnull(sum(yong_liang*dan_jia+YSJE+ZLFYYS),0) from kd_ys_zjtrfy where XM_ID = #{xmId}) as zjtrfy,
            (select ifnull(sum(ZZSC*(select ifnull(YZJE,0) from kd_asset where id = asset_id)+CQDTFYYSJE),0) from kd_ys_zjfyycqdtfy t where XM_ID = #{xmId}) as zjfyycqdtfy,
            (select ifnull(sum(ZZSC*(select ifnull(YZJE,0) from kd_asset where id = asset_id)),0) from kd_ys_wxzctxfy where XM_ID = #{xmId}) as wxzctxfy,
            (select ifnull(sum(YSJE),0) from kd_ys_sjfy where XM_ID = #{xmId}) as sjfy,
            (select ifnull(sum(YSJE),0) from kd_ys_zbtsysyfy where XM_ID = #{xmId}) as zbtsysyfy,
            (select ifnull(sum(WWFY),0) from kd_ys_wtwbyjkffy where XM_ID = #{xmId}) as wtwbyjkffy,
            (select ifnull(sum(YSJE),0) from kd_ys_qtfy where XM_ID = #{xmId}) as qtfy
        FROM kd_xm xm
        WHERE xm.is_deleted = 0 and xm.id = #{xmId}
    </select>

    <select id="selectByXmIdAndYearMonth" resultMap="ResultVoMap" parameterType="java.util.Map">
        SELECT
            COALESCE(get_xm_info(xm.id,'XMMC', #{param2}), XMMC) AS XMMC,
            XMBH,
            COALESCE(get_xm_info(xm.id,'XMSQDWID', #{param2}),XMSQDWID) AS XMSQDWID,
            xm.XMKSSJ,
            COALESCE(get_xm_info(xm.id,'XMJSSJ', #{param2}),XMJSSJ) AS XMJSSJ,
            COALESCE(get_xm_info(xm.id,'XMFZR', #{param2}),XMFZR) AS XMFZR,
            XMLY,
            XMKZXS,
            YQCGXS,
            XMJSJJMB,
            LXBG
        FROM kd_xm xm
        where id = #{param1}
    </select>

    <select id="selectByXmId" resultMap="ResultVoMap" parameterType="java.lang.Long">
        SELECT
            id, XMBH,xm.XMKSSJ,
            COALESCE(get_xm_info(xm.id,'XMMC', null), XMMC) AS XMMC,
            COALESCE(get_xm_info(xm.id,'XMFZR', null),XMFZR) AS XMFZR,
            COALESCE(get_xm_info(xm.id,'XMSQDWID', null),xm.XMSQDWID) AS XMSQDWID,
            COALESCE(get_xm_info(xm.id,'XMJSSJ', null),XMJSSJ) AS XMJSSJ,
            COALESCE(get_xm_info(xm.id,'STATUS', null),STATUS) AS status,
            YJLX,
            XMLY,
            XMKZXS,
            YQCGXS,
            XMJSJJMB,
            XMYSZE,
            XMYSZEMX,
            LXPW,
            LXBG,
            QTZL,
            remark
        FROM kd_xm xm
        where id = #{xmId}
    </select>

    <select id="selectByXmIdList" resultMap="ResultVoMap">
        SELECT
            id, XMBH,XMKSSJ,create_time,
            COALESCE(get_xm_info(xm.id,'XMMC', null), XMMC ) AS XMMC,
            COALESCE(get_xm_info(xm.id,'XMFZR', null),XMFZR) AS XMFZR,
            COALESCE(get_xm_info(xm.id,'XMSQDWID', null),xm.XMSQDWID) AS XMSQDWID,
            COALESCE(get_xm_info(xm.id,'XMJSSJ', null),XMJSSJ) AS XMJSSJ,
            COALESCE(get_xm_info(xm.id,'STATUS', null),STATUS) AS STATUS
        FROM kd_xm xm
        where id in <foreach item="xmId" collection="xmIdList" separator="," open="(" close=")" index="index">#{xmId}</foreach>
    </select>

    <select id="getXmStatusByXmId" parameterType="java.lang.Long" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            xmbh,
            COALESCE(get_xm_info(id,'STATUS', null),STATUS) as status,
            xmkssj,
            COALESCE(get_xm_info(id,'XMJSSJ', null),XMJSSJ) as xmjssj
        FROM kd_xm where id = #{xmId}
    </select>

    <select id="getXmIdByXmmcOrXmbh" resultType="java.lang.Long">
        SELECT id
        FROM kd_xm xm
        where xm.is_deleted = 0
        <if test="xmmc!=null and xmmc!='' ">
          <if test="yearAndMonth!=null and yearAndMonth!=''">
              and COALESCE(get_xm_info(xm.id,'XMMC', #{yearAndMonth}),XMMC) = #{xmmc}
          </if>
            <if test="yearAndMonth==null or yearAndMonth!=''">
                and COALESCE(get_xm_info(xm.id,'XMMC', #{yearAndMonth}),XMMC) = #{xmmc}
            </if>
        </if>
        <if test="xmbh!=null and xmbh!=''">and xmbh = #{xmbh}</if>
    </select>

    <select id="listByYearAndMonth" resultMap="ResultMap">
        SELECT
            id,
            XMBH,
            COALESCE(get_xm_info(xm.id,'XMMC', #{yearAndMonth}),XMMC) AS XMMC,
            xm.XMKSSJ,
            COALESCE(get_xm_info(xm.id,'XMJSSJ', #{yearAndMonth}),XMJSSJ) AS XMJSSJ,
            COALESCE(get_xm_info(xm.id,'STATUS', #{yearAndMonth}),STATUS) AS status
        FROM kd_xm xm
        where #{yearAndMonth} between DATE_FORMAT(XMKSSJ, '%Y%m') and DATE_FORMAT(COALESCE(get_xm_info(xm.id,'XMJSSJ', #{yearAndMonth}),XMJSSJ),'%Y%m')
    </select>

    <select id="selectAssistList" resultType="com.alibaba.fastjson.JSONObject">
             SELECT
                xm.id,
                xm.XMBH,
                COALESCE(get_xm_info(xm.id,'XMMC', #{ew.yearAndMonth}),XMMC) AS XMMC,
                xm.XMKSSJ,
                COALESCE(get_xm_info(xm.id,'XMJSSJ', #{ew.yearAndMonth}),XMJSSJ) AS XMJSSJ
            FROM kd_xm xm
            WHERE
            xm.status = 1
            and #{ew.yearAndMonth} between DATE_FORMAT(XMKSSJ, '%Y') and DATE_FORMAT(COALESCE(get_xm_info(xm.id,'XMJSSJ', #{ew.yearAndMonth}),XMJSSJ),'%Y')
            <if test="ew.xmmc != null and ew.xmmc != ''">and COALESCE(get_xm_info(xm.id,'XMMC', #{ew.yearAndMonth}),XMMC) like concat('%',#{ew.xmmc},'%')</if>
            <if test="ew.xmbh!=null and ew.xmbh!=''">and xm.xmbh like concat('%',#{ew.xmbh},'%')</if>
     </select>

    <select id="getXmmcAndXmbhByXmId" resultType="com.alibaba.fastjson.JSONObject">
        SELECT
            id as xmId,
            COALESCE(get_xm_info(xm.id,'XMMC',null), XMMC ) AS xmmc,
            xmbh
        FROM kd_xm xm
        where id = #{xmId}
    </select>

    <select id="countXmfzrByUnicodeAndYearMonth" resultType="java.lang.Long">
        select
            COUNT(*)
        from kd_xm xm
        left join kd_technician t on t.id = COALESCE(get_xm_info(xm.id, 'XMFZR', #{yearAndMonth}), xm.XMFZR)
        where t.unicode = #{unicode} and t.year_and_month <![CDATA[>=]]>  #{yearAndMonth}
    </select>

</mapper>
