<?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.dk.mdm.mapper.ivt.IntoReturnMapper">

    <!-- 通用设置 -->
    <!-- 通用查询列 -->
    <sql id="Base_Column_List">
        return_id, return_no, return_type,  pur_id,  pur_no, org_id, staff_id, sup_id, contact_name, contact_phone, delivery_date, sum_quantity, sum_standard, sum_amount, sale_discount, return_status, out_status, outing_qty, outing_amt, out_qty, out_amt, amt_payable, amt_handle, amt_residue, remarks, annex_paths, make_staff, make_time, flg_valid, cp_id, op_create_time, op_create_user_id, op_update_time, op_update_user_id, op_app_code, op_timestamp, op_db_user
    </sql>

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.dk.mdm.model.pojo.ivt.IntoReturn">
        <id column="return_id" property="returnId"/>
                <result column="return_no" property="returnNo"/>
                <result column="return_type" property="returnType"/>

                <result column="pur_id" property="purId" typeHandler="UuidTypeHandler"/>
                <result column="pur_no" property="purNo"/>
                <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
                <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
                <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
                <result column="contact_name" property="contactName"/>
                <result column="contact_phone" property="contactPhone"/>
            <result column="delivery_date" property="deliveryDate" typeHandler="TimestampTypeHandler"/>
                <result column="sum_quantity" property="sumQuantity"/>
                <result column="sum_standard" property="sumStandard"/>
                <result column="sum_amount" property="sumAmount"/>
                <result column="sale_discount" property="saleDiscount"/>
                <result column="return_status" property="returnStatus"/>
                <result column="out_status" property="outStatus"/>
                <result column="outing_qty" property="outingQty"/>
                <result column="outing_amt" property="outingAmt"/>
                <result column="out_qty" property="outQty"/>
                <result column="out_amt" property="outAmt"/>
                <result column="amt_payable" property="amtPayable"/>
                <result column="amt_handle" property="amtHandle"/>
                <result column="amt_residue" property="amtResidue"/>
                <result column="remarks" property="remarks"/>
                <result column="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
                <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
            <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
                <result column="flg_valid" property="flgValid"/>
                <result column="cp_id" property="cpId"/>
            <result column="op_create_time" property="opCreateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_create_user_id" property="opCreateUserId" typeHandler="UuidTypeHandler"/>
            <result column="op_update_time" property="opUpdateTime" typeHandler="TimestampTypeHandler"/>
                <result column="op_update_user_id" property="opUpdateUserId" typeHandler="UuidTypeHandler"/>
                <result column="op_app_code" property="opAppCode"/>
            <result column="op_timestamp" property="opTimestamp" typeHandler="TimestampTypeHandler"/>
                <result column="op_db_user" property="opDbUser"/>
    </resultMap>




    <sql id="Base_Column_List_Response">
        tpir.return_id, tpir.return_no, tpir.return_type,  tpir.pur_id, tpir.pur_no, tpir.org_id,
        tpir.staff_id, tpir.sup_id, tpir.contact_name, tpir.contact_phone, tpir.delivery_date, tpir.sum_quantity,
        tpir.sum_standard, tpir.sum_amount, tpir.sale_discount, tpir.return_status, tpir.out_status,
        tpir.outing_qty, tpir.outing_amt, tpir.out_qty, tpir.out_amt, tpir.amt_payable, tpir.amt_handle, tpir.amt_residue,
        tpir.remarks, tpir.annex_paths, tpir.make_staff, tpir.make_time, tpir.flg_valid, tpir.cp_id
    </sql>


    <resultMap id="BaseResultMapResponse" type="com.dk.mdm.model.response.ivt.IntoReturnResponse">
        <id column="return_id" property="returnId"/>
        <result column="return_no" property="returnNo"/>
        <result column="return_type" property="returnType"/>
        <result column="pur_id" property="purId" typeHandler="UuidTypeHandler"/>
        <result column="pur_no" property="purNo"/>
        <result column="org_id" property="orgId" typeHandler="UuidTypeHandler"/>
        <result column="staff_id" property="staffId" typeHandler="UuidTypeHandler"/>
        <result column="sup_id" property="supId" typeHandler="UuidTypeHandler"/>
        <result column="contact_name" property="contactName"/>
        <result column="contact_phone" property="contactPhone"/>
        <result column="delivery_date" property="deliveryDate" typeHandler="TimestampTypeHandler"/>
        <result column="sum_quantity" property="sumQuantity"/>
        <result column="sum_standard" property="sumStandard"/>
        <result column="sum_amount" property="sumAmount"/>
        <result column="sale_discount" property="saleDiscount"/>
        <result column="return_status" property="returnStatus"/>
        <result column="out_status" property="outStatus"/>
        <result column="outing_qty" property="outingQty"/>
        <result column="outing_amt" property="outingAmt"/>
        <result column="out_qty" property="outQty"/>
        <result column="out_amt" property="outAmt"/>
        <result column="amt_payable" property="amtPayable"/>
        <result column="amt_handle" property="amtHandle"/>
        <result column="amt_residue" property="amtResidue"/>
        <result column="remarks" property="remarks"/>
        <result column="annex_paths" property="annexPaths" typeHandler="JsonTypeHandler"/>
        <result column="make_staff" property="makeStaff" typeHandler="UuidTypeHandler"/>
        <result column="make_time" property="makeTime" typeHandler="TimestampTypeHandler"/>
        <result column="flg_valid" property="flgValid"/>
        <result column="cp_id" property="cpId"/>

        <result column="sup_name" property="supName"/>
        <result column="sup_code" property="supCode"/>

        <result column="staff_name" property="staffName"/>
        <result column="staff_code" property="staffCode"/>

        <result column="org_name" property="orgName"/>
        <result column="org_code" property="orgCode"/>
        <result column="return_address" property="returnAddress"/>
        <result column="out_status_name" property="outStatusName"/>
        <result column="return_status_name" property="returnStatusName"/>
        <result column="into_id" property="intoId"/>

    </resultMap>


    <!-- 通用条件列 -->
    <sql id="Condition">
        <where>
            <if test="returnNo != null and returnNo != ''">
                AND return_no = #{returnNo}
            </if>
            <if test="returnType != null and returnType != ''">
                AND return_type = #{returnType}
            </if>
            <if test="purId != null and purId != ''">
                AND pur_id = #{purId}
            </if>
            <if test="purNo != null and purNo != ''">
                AND pur_no = #{purNo}
            </if>
            <if test="orgId != null and orgId != ''">
                AND org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND staff_id = #{staffId}
            </if>
            <if test="supId != null and supId != ''">
                AND sup_id = #{supId}
            </if>
            <if test="contactName != null and contactName != ''">
                AND contact_name = #{contactName}
            </if>
            <if test="contactPhone != null and contactPhone != ''">
                AND contact_phone = #{contactPhone}
            </if>
            <if test="deliveryDate != null">
                AND delivery_date = #{deliveryDate}
            </if>
            <if test="sumQuantity != null">
                AND sum_quantity = #{sumQuantity}
            </if>
            <if test="sumStandard != null">
                AND sum_standard = #{sumStandard}
            </if>
            <if test="sumAmount != null">
                AND sum_amount = #{sumAmount}
            </if>
            <if test="saleDiscount != null">
                AND sale_discount = #{saleDiscount}
            </if>
            <if test="returnStatus != null and returnStatus != ''">
                AND return_status = #{returnStatus}
            </if>
            <if test="outStatus != null and outStatus != ''">
                AND out_status = #{outStatus}
            </if>
            <if test="outingQty != null">
                AND outing_qty = #{outingQty}
            </if>
            <if test="outingAmt != null">
                AND outing_amt = #{outingAmt}
            </if>
            <if test="outQty != null">
                AND out_qty = #{outQty}
            </if>
            <if test="outAmt != null">
                AND out_amt = #{outAmt}
            </if>
            <if test="amtPayable != null">
                AND amt_payable = #{amtPayable}
            </if>
            <if test="amtHandle != null">
                AND amt_handle = #{amtHandle}
            </if>
            <if test="amtResidue != null">
                AND amt_residue = #{amtResidue}
            </if>
            <if test="remarks != null and remarks != ''">
                AND remarks = #{remarks}
            </if>
            <if test="annexPaths != null and annexPaths != ''">
                AND annex_paths = #{annexPaths}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND make_staff = #{makeStaff}
            </if>
            <if test="makeTime != null">
                AND make_time = #{makeTime}
            </if>
            <if test="flgValid != null">
                AND flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND cp_id = #{cpId}
            </if>
        </where>
    </sql>

    <sql id="Condition_Response">
        <where>
            <if test="isShowCancel == null">
             AND tpir.return_status != '退货状态-作废'
            </if>
            <if test="returnNo != null and returnNo != ''">
                AND tpir.return_no = #{returnNo}
            </if>
            <if test="returnId != null and returnId != ''">
                AND tpir.return_id = #{returnId}::uuid
            </if>
            <if test="returnType != null and returnType != ''">
                AND tpir.return_type = #{returnType}
            </if>

            <if test="purId != null and purId != ''">
                AND tpir.pur_id = #{purId}
            </if>

            <if test="purNo != null and purNo != ''">
                AND tpir.pur_no = #{purNo}
            </if>
            <if test="orgId != null and orgId != ''">
                AND tpir.org_id = #{orgId}
            </if>
            <if test="staffId != null and staffId != ''">
                AND tpir.staff_id = #{staffId}
            </if>
            <if test="supId != null and supId != ''">
                AND tpir.sup_id = #{supId}
            </if>
            <if test="contactName != null and contactName != ''">
                AND tpir.contact_name = #{contactName}
            </if>
            <if test="contactPhone != null and contactPhone != ''">
                AND tpir.contact_phone = #{contactPhone}
            </if>
            <if test="deliveryDate != null">
                AND tpir.delivery_date = #{deliveryDate}
            </if>
            <if test="sumQuantity != null">
                AND tpir.sum_quantity = #{sumQuantity}
            </if>
            <if test="sumStandard != null">
                AND tpir.sum_standard = #{sumStandard}
            </if>
            <if test="sumAmount != null">
                AND tpir.sum_amount = #{sumAmount}
            </if>
            <if test="saleDiscount != null">
                AND tpir.sale_discount = #{saleDiscount}
            </if>
            <if test="returnStatus != null and returnStatus != ''">
                AND tpir.return_status = #{returnStatus}
            </if>
            <if test="outStatus != null and outStatus != ''">
                AND tpir.out_status = #{outStatus}
            </if>
            <if test="outingQty != null">
                AND tpir.outing_qty = #{outingQty}
            </if>
            <if test="outingAmt != null">
                AND tpir.outing_amt = #{outingAmt}
            </if>
            <if test="outQty != null">
                AND tpir.out_qty = #{outQty}
            </if>
            <if test="outAmt != null">
                AND tpir.out_amt = #{outAmt}
            </if>
            <if test="amtPayable != null">
                AND tpir.amt_payable = #{amtPayable}
            </if>
            <if test="amtHandle != null">
                AND tpir.amt_handle = #{amtHandle}
            </if>
            <if test="amtResidue != null">
                AND tpir.amt_residue = #{amtResidue}
            </if>
            <if test="remarks != null and remarks != ''">
                AND tpir.remarks = #{remarks}
            </if>
            <if test="annexPaths != null and annexPaths != ''">
                AND tpir.annex_paths = #{annexPaths}
            </if>
            <if test="makeStaff != null and makeStaff != ''">
                AND tpir.make_staff = #{makeStaff}
            </if>
            <if test="makeTime != null">
                AND tpir.make_time = #{makeTime}
            </if>
            <if test="flgValid != null">
                AND tpir.flg_valid = #{flgValid}
            </if>
            <if test="cpId != null">
                AND tpir.cp_id = #{cpId}
            </if>
            <!--退货单id集合-->
            <if test="intoReturnIdList != null and intoReturnIdList.size() > 0">
                AND tpir.return_id IN
                <foreach collection="intoReturnIdList" index="index" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if  test="searchText != null">
                AND (tpir.return_no  like concat('%', my_ex.likequery(#{searchText}) , '%')
                OR  tpir.pur_no  like concat('%', my_ex.likequery(#{searchText}) , '%')
                 OR  tms.sup_name like concat('%', my_ex.likequery(#{searchText}) , '%')
                 OR  tms.sup_code like concat('%', my_ex.likequery(#{searchText}) , '%')
                 )
            </if>
            <if test="outStatusList != null and outStatusList.size()>0">
                AND tpir.out_status =
                any(#{outStatusList, typeHandler= StringListTypeHandler})
            </if>
        </where>
    </sql>

    <sql id="idsForeach">
        <!-- 根据主键returnId批量操作 -->
        WHERE return_id in
        <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
            #{item}
        </foreach>
    </sql>

    <sql id="Base_Column_List_Join">
        tms.sup_name AS sup_name,
        tms.sup_code AS sup_code,
        tms.return_address AS return_address,
        tmsf.staff_name AS staff_name,
        tmsf.staff_code AS staff_code,
        tmo.org_name AS org_name,
        tmo.org_code AS org_code,
        tdk1.kind_name_i18n ->>  #{i18n} AS out_status_name,
        tdk2.kind_name_i18n ->>  #{i18n} AS return_status_name
    </sql>

    <!-- 查询表t_psi_into_return，（条件查询+分页）列表 -->
    <select id="selectByCond" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>,
        <include refid="Base_Column_List_Join"/>
        ,tpi.into_id as into_id
        FROM dkic_b.t_psi_into_return  tpir
        left join  dkic_b.t_mst_supplier tms  on  tms.sup_id = tpir.sup_id
        left join  dkic_b.t_mst_staff tmsf  on  tmsf.staff_id = tpir.staff_id
        left join  dkic_b.t_mst_org tmo  on  tmo.org_id = tpir.org_id
        left join  sys.t_data_kind tdk1 on  tdk1.kind_code = tpir.out_status
        left join  sys.t_data_kind tdk2 on  tdk2.kind_code = tpir.return_status
        left join  dkic_b.t_psi_inbound tpi on tpi.from_id = tpir.return_id
        <include refid="Condition_Response"/>
            order by tpir.op_create_time desc
        <if test="pageSize != null and currentPage != null and pageSize != 0 and currentPage != 0">
            limit #{end} offset #{start}
        </if>
    </select>

    <!-- 查询表t_psi_into_return，（条件查询）个数 -->
    <select id="countByCond" resultType="Long">
        SELECT
        count(1)
        FROM dkic_b.t_psi_into_return tpir
        left join  dkic_b.t_mst_supplier tms  on  tms.sup_id = tpir.sup_id
        <include refid="Condition_Response"/>
    </select>

    <!-- 根据主键查询表t_psi_into_return的一行数据 -->
    <select id="selectById" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>
        FROM dkic_b.t_psi_into_return tpir
        WHERE tpir.return_id = #{id}::uuid
    </select>

    <!-- 根据主键锁定表t_psi_into_return的一行数据 -->
    <select id="selectByIdForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_into_return
        WHERE return_id = #{id}
        for update
    </select>

    <!-- 根据主键锁定表t_psi_into_return的多行数据 -->
    <select id="selectByIdsForUpdate" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_into_return
        <include refid="idsForeach"/>
        for update
    </select>

    <insert id="insertBatch">
        insert into dkic_b.t_psi_into_return
        (
        <trim suffixOverrides=",">
            return_no,
            return_type,
            pur_id,
            pur_no,
            org_id,
            staff_id,
            sup_id,
            contact_name,
            contact_phone,
            delivery_date,
            sum_quantity,
            sum_standard,
            sum_amount,
            sale_discount,
            return_status,
            out_status,
            outing_qty,
            outing_amt,
            out_qty,
            out_amt,
            amt_payable,
            amt_handle,
            amt_residue,
            remarks,
            annex_paths,
            make_staff,
            make_time,
            cp_id,
            op_app_code,
        </trim>
        )
        values
        <foreach collection="list" index="index" item="item" separator=",">
            (
            <trim suffixOverrides=",">
                #{item.returnNo},
                #{item.returnType},
                #{item.purId}::uuid,
                #{item.purNo},
                #{item.orgId}::uuid,
                #{item.staffId}::uuid,
                #{item.supId}::uuid,
                #{item.contactName},
                #{item.contactPhone},
                #{item.deliveryDate},
                #{item.sumQuantity},
                #{item.sumStandard},
                #{item.sumAmount},
                #{item.saleDiscount},
                #{item.returnStatus},
                #{item.outStatus},
                #{item.outingQty},
                #{item.outingAmt},
                #{item.outQty},
                #{item.outAmt},
                #{item.amtPayable},
                #{item.amtHandle},
                #{item.amtResidue},
                #{item.remarks},
                #{item.annexPaths},
                #{item.makeStaff}::uuid,
                #{item.makeTime},
                #{item.cpId},
                #{item.opAppCode},
            </trim>
            )
        </foreach>
    </insert>


    <!--查询采购退货数据List -->
    <select id="selectReturnList" resultMap="BaseResultMapResponse" >
        SELECT
        <include refid="Base_Column_List_Join"/>
        FROM dkic_b.t_psi_into_return  tpir
        where tpir.return_id = any(#{returnId,typeHandler= UuidListTypeHandler})
    </select>


    <!-- 查询原始数据 冲正用-->
    <select id="selectReturnByRighting" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM dkic_b.t_psi_into_return
        where return_id =#{returnId}::uuid
    </select>

    <!-- 根据id修改-->
    <update id="updateById" parameterType="com.dk.mdm.model.pojo.ivt.IntoReturn">
        update dkic_b.t_psi_into_return
        <set>
            <if test=" et!=null and et.returnType != null">
                return_type = #{et.returnType,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.orgId != null">
                org_id = #{et.orgId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.staffId != null">
                staff_id = #{et.staffId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.supId != null">
                sup_id = #{et.supId,typeHandler=UuidTypeHandler},
            </if>
            <if test=" et!=null and et.contactName != null">
                contact_name = #{et.contactName,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.contactPhone != null">
                contact_phone = #{et.contactPhone,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.deliveryDate != null">
                delivery_date = #{et.deliveryDate,jdbcType=DATE},
            </if>
            <if test=" et!=null and et.sumQuantity != null">
                sum_quantity = #{et.sumQuantity,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.sumStandard != null">
                sum_standard = #{et.sumStandard,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.sumAmount != null">
                sum_amount = #{et.sumAmount,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.saleDiscount != null">
                sale_discount = #{et.saleDiscount,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.returnStatus != null">
                return_status = #{et.returnStatus,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.outStatus != null">
                out_status = #{et.outStatus,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.outingQty != null">
                outing_qty = outing_qty + #{et.outingQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outingAmt != null">
                outing_amt = outing_amt + #{et.outingAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outQty != null">
                out_qty = out_qty + #{et.outQty,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.outAmt != null">
                out_amt = out_amt + #{et.outAmt,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtPayable != null">
                amt_payable = #{et.amtPayable,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtHandle != null">
                amt_handle = #{et.amtHandle,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.amtResidue != null">
                amt_residue = #{et.amtResidue,jdbcType=NUMERIC},
            </if>
            <if test=" et!=null and et.remarks != null">
                remarks = #{et.remarks,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.annexPaths != null">
                annex_paths = #{et.annexPaths,typeHandler=JsonTypeHandler},
            </if>
            <if test=" et!=null and et.makeStaff != null">
                make_staff = #{et.makeStaff,jdbcType=OTHER},
            </if>
            <if test=" et!=null and et.makeTime != null">
                make_time = #{et.makeTime,jdbcType=TIMESTAMP},
            </if>
            <if test=" et!=null and et.flgValid != null">
                flg_valid = #{et.flgValid,jdbcType=BOOLEAN},
            </if>
            <if test=" et!=null and et.cpId != null">
                cp_id = #{et.cpId,jdbcType=INTEGER},
            </if>
        </set>
        where return_id = #{et.returnId,typeHandler=UuidTypeHandler}
        and  sum_quantity >= (out_qty + outing_qty)
    </update>

<!--    查询总单 编辑用-->
    <select id="selectByIntoReturnQuery" resultMap="BaseResultMapResponse">
        SELECT
        <include refid="Base_Column_List_Response"/>,
        <include refid="Base_Column_List_Join"/>
        FROM dkic_b.t_psi_into_return  tpir
        left join  dkic_b.t_mst_supplier tms  on  tms.sup_id = tpir.sup_id
        left join  dkic_b.t_mst_staff tmsf  on  tmsf.staff_id = tpir.staff_id
        left join  dkic_b.t_mst_org tmo  on  tmo.org_id = tpir.org_id
        left join  sys.t_data_kind tdk1 on  tdk1.kind_code = tpir.out_status
        left join  sys.t_data_kind tdk2 on  tdk2.kind_code = tpir.return_status
        <include refid="Condition_Response"/>
    </select>



    <update id="updateByUuId" parameterType="com.dk.mdm.model.pojo.ivt.IntoReturn">
        update dkic_b.t_psi_into_return
        <set>
            <if test=" et!=null and et.remarks != null">
                remarks = #{et.remarks,jdbcType=VARCHAR},
            </if>
            <if test=" et!=null and et.annexPaths != null">
                annex_paths = #{et.annexPaths,typeHandler=JsonTypeHandler},
            </if>

        </set>
        where return_id = #{et.returnId} ::uuid

    </update>

</mapper>
