<?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.test.materialManagement.collect.mapper.CollectDetailedMapper">
    <select id="getByBasicId" resultType="com.jeeplus.test.materialManagement.collect.domain.CollectDetailed">
        SELECT a.id,a.create_by,a.create_date,a.update_by,a.update_date,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,
        su.name AS recipientAgent, su.id AS recipientAgentId, so.name AS recipientOffice,
        d.id AS collectTypeId, d.name AS collectType,
            IFNULL((
                        sum( trade_number ) - b.collect_number
                        ),
                sum( trade_number )) AS surplusNumber,
                a.is_return
             FROM material_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
                    material_management_collect_detailed e
                    LEFT JOIN material_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 material_management_warehouse_detailed c on a.goods_name = c.trade_name and a.collect_type = c.ware_house_type
             LEFT JOIN material_management_warehouse_basics bas ON c.basic_id = bas.id

             LEFT JOIN material_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="getByAttachmentId" resultType="com.jeeplus.test.workContract.service.dto.WorkAttachmentDto">
        SELECT
			id,
			url,
			attachment_name AS `name`,
			create_by AS `by`,
			create_date
		FROM
			work_attachment
		WHERE
			del_flag = 0
			AND attachment_id = #{id}
    </select>
    <select id="getIdByBasicId" resultType="java.lang.String">
        select a.id from material_management_collect_detailed a where a.basic_id = #{id} and a.del_flag = 0
    </select>

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

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

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

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