<?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="com.unis.crk.common.mapper.BizflowtrailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.unis.crk.common.entity.Bizflowtrail">
        <id column="TrailID" property="TrailID" />
        <result column="BillNo" property="BillNo" />
        <result column="UnitID" property="UnitID" />
        <result column="CurBizNodeID" property="CurBizNodeID" />
        <result column="NextBizNodeID" property="NextBizNodeID" />
        <result column="BizNodeId" property="BizNodeId" />
        <result column="TrailOrder" property="TrailOrder" />
        <result column="BuildDate" property="BuildDate" />
        <result column="Builder" property="Builder" />
        <result column="ShowOrder" property="ShowOrder" />
        <result column="status" property="status" />
        <result column="FlowID" property="FlowID" />
        <result column="IsLocked" property="IsLocked" />
        <result column="LockedUser" property="LockedUser" />
        <result column="LockedDate" property="LockedDate" />
        <result column="note" property="note" />
        <result column="UPDATE_TIME" property="updateTime" />
    </resultMap>
    <select id="getCrkPzInfo" resultType="java.util.Map">
        <choose>
            <when test="direction==1">
                SELECT
                a.lspz,
                e.enumname as lspzName,
                a.unitid as unitId,
                b.org_name AS orgName,
                DATE_FORMAT(a.ywdjsj,'%Y-%m-%d') as ywrq,
                a.slr,a.cph
                FROM
                data_crkyw_rmdj_default a
                INNER JOIN data_crkyw_qyxx_default c on a.BizNo = c.BizNo
                INNER JOIN org_info b ON a.UnitID = b.org_id
                LEFT JOIN basic_enum e  on e.enumId = a.lspz
                WHERE
                a.bizno = #{bizNo}
            </when>
            <otherwise>
                SELECT
                a.lspz,
                e.enumname as lspzName,
                a.unitid as unitId,
                b.org_name AS orgName,
                DATE_FORMAT(a.rmsj,'%Y-%m-%d') as ywrq,
                a.slr,a.cph
                FROM
                data_crkyw_ckrmdj_default a
                INNER JOIN data_crkyw_ckcpz_default c on a.BizNo = c.BizNo
                INNER JOIN org_info b ON a.UnitID = b.org_id
                LEFT JOIN basic_enum e  on e.enumId = a.lspz
                WHERE
                a.bizno = #{bizNo}
            </otherwise>
        </choose>
    </select>
    <select id="getCircleBusinessInfo" resultType="java.util.Map">
        <choose>
            <when test="direction==1">
                select
                DATE_FORMAT(a.jmsj,'%Y-%m-%d') as "ywrq",
                a.klhjz as "sl",
                f.storehouse_name as "cfmc",
                e.enumname as "lsdjName"

                from data_crkyw_ckcmz_default a
                INNER JOIN data_crkyw_ckrmdj_default c on a.UnitID=c.UnitID and a.BizNo=c.BizNo
                LEFT JOIN basic_enum e on  c.lydj=e.enumId
                LEFT JOIN basic_storehouse f on a.sjckch = f.storehouse_id
                where
                a.UnitID != #{unitId}
                and c.lspz= #{lspz} and c.cph = #{cph}
                and DATE_FORMAT(a.jmsj,'%Y-%m-%d') between #{beginDay} and  #{endDay}
                order by a.jmsj desc limit 1
            </when>
            <otherwise>
                select
                DATE_FORMAT(a.cpzsj,'%Y-%m-%d') as "ywrq",
                a.xzklhjz as "sl",
                f.storehouse_name as "cfmc",
                e.enumname as "lsdjName"
                from data_crkyw_cpz_default a
                INNER JOIN data_crkyw_jyxx_default b on a.UnitID=b.UnitID and a.BizNo=b.BizNo
                INNER JOIN data_crkyw_rmdj_default c on a.UnitID=c.UnitID and a.BizNo=c.BizNo
                LEFT JOIN basic_enum e on  b.lsdj=e.enumId
                LEFT JOIN basic_storehouse f on a.sjchID = f.storehouse_id
                where
                a.UnitID != #{unitId}
                and c.lspz= #{lspz} and c.cph = #{cph}
                and DATE_FORMAT(a.cpzsj,'%Y-%m-%d') between #{beginDay} and  #{endDay}
                order by a.cpzsj desc limit 1
            </otherwise>
        </choose>
    </select>

</mapper>
