<?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.jeeplus.psimanage.collect.mapper.PsiCollectDetailedMapper">
    <select id="getByBasicId" resultType="com.jeeplus.psimanage.collect.domain.PsiCollectDetailed">
        SELECT a.id,
               a.create_by_id,
               a.create_time,
               a.update_by_id,
               a.update_time,
               a.del_flag,
               a.remarks,
               a.basic_id,
               a.recipient_agent,
        a.collect_type,
        a.goods_name,
        a.collect_number,
        a.company,
        a.current_inventory,
               c.spec,
        su.name AS recipientAgent, su.id AS recipientAgentId, so.name AS recipientOffice,
        d.id AS collectTypeId, d.name AS collectType,
               wd.current_inventory AS surplusNumber,
        a.is_return
             FROM psi_management_collect_detailed a
             LEFT JOIN sys_user su
             ON a.recipient_agent = su.id
             LEFT JOIN sys_office so
             ON su.office_id = so.id

             LEFT JOIN (
                SELECT
                    sum( collect_number ) AS collect_number,
                    goods_name,
                STATUS
                FROM
                    psi_management_collect_detailed e
                    LEFT JOIN psi_management_collect_basics f ON e.basic_id = f.id
                    AND f.del_flag = 0
                WHERE
                    f.`status` = 5
                    AND e.del_flag = 0
                GROUP BY
                    e.goods_name
                ) b ON a.goods_name = b.goods_name

             LEFT JOIN (
                 SELECT
                     sum( current_inventory ) AS current_inventory,
                     trade_name
                 FROM
                     psi_management_warehouse_detailed e
                 WHERE  e.del_flag = 0
                 GROUP BY
                     e.trade_name
             ) wd ON a.goods_name = wd.trade_name

             LEFT JOIN psi_management_warehouse_detailed c on a.goods_name = c.trade_name and a.collect_type = c.ware_house_type
             LEFT JOIN psi_management_warehouse_basics bas ON c.basic_id = bas.id

             LEFT JOIN psi_management_type d ON a.collect_type = d.id
             WHERE a.basic_id = #{id} AND a.del_flag = '0' and c.del_flag = '0' AND ( bas.`status` = '0' OR bas.`status` = '5')
             group by a.goods_name;
    </select>
    <select id="getRecordsByBasicId" resultType="com.jeeplus.psimanage.collect.domain.PsiCollectDetailedRecord">
        select
            a.id,
            su.name AS recipientAgent,
            su.id AS recipientAgentId,
            so.name AS recipientOffice,
            d.id AS collectTypeId,
            d.name AS collectType,
            a.goods_name,
            pmwd.current_inventory as surplusNumber,
            a.collect_number,
            a.company,
            a.current_inventory,
            pmwd.produce_date,
            pmwd.spec,
            a.detail_id,
            a.inventory_id,
            pmcd.is_return,
            a.surplus_stock,
            a.not_surplus_stock,
            a.returned_num,
            pmwd.warn_num,
            pmwd.warn_flag,
            a.remarks
        from psi_management_collect_detailed_record a
            LEFT JOIN sys_user su  ON a.recipient_agent = su.id
            LEFT JOIN sys_office so ON su.office_id = so.id
            LEFT JOIN psi_management_type d ON a.collect_type = d.id
            LEFT JOIN psi_management_collect_detailed pmcd ON a.detail_id = pmcd.id
            LEFT JOIN psi_management_warehouse_detailed pmwd ON a.inventory_id = pmwd.id

        where a.del_flag = '0' and
        a.basic_id = #{id}
    </select>

    <select id="getByAttachmentId" resultType="com.jeeplus.psimanage.oss.service.dto.WorkAttachmentDto">
        SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by_id AS `by`,
			create_time
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
    </select>
    <select id="getIdByBasicId" resultType="java.lang.String">
        select a.id from psi_management_collect_detailed a where a.basic_id = #{id} and a.del_flag = 0
    </select>

    <update id="updateReturnFlagById">
        update psi_management_collect_detailed set is_return = #{isReturn} where id = #{id}
    </update>

    <update id="updateAuditReturnFlagById">
        update psi_management_collect_detailed_record
        set is_return = #{isReturn},
            surplus_stock = #{surplusStock},
            not_surplus_stock = #{notSurplusStock},
            returned_num = #{returnedNum}
        where id = #{id}
    </update>

    <update id="updateStatusByIdReturn">
        update psi_management_collect_basics_return set status = #{status} where id = #{id}
    </update>

    <update id="updateByIdReturn">
        update psi_management_collect_basics_return set status = #{status},request_user = #{userId},remarks = #{cause} where id = #{id}
    </update>

    <update id="updateNoReturnByBasicId">
        update psi_management_collect_detailed set is_return = '0' where basic_id = #{id}
    </update>


    <update id="updateAutditNoReturnByBasicId">
        update psi_management_collect_detailed_record
        set is_return = '0',
            returned_num = '0'
        where basic_id = #{id}
    </update>
</mapper>
