<?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.grainReserves.modular.grReserveScale.mapper.GrReserveScaleMapper">


    <select id="selectMyPage" resultType="com.unis.grainReserves.modular.grReserveScale.vo.GrReserveScaleVO">

        SELECT t1.id,
        t2.id unitInfoId,
        t2.dwmc unitInfoName,
        t1.grain_variety_id grainVarietyId,
        t3.enumname grainVarietyName,
        t1.grain_nature_id grainNatureId,
        t4.enumname grainNatureName,
        t1.reserve_plan_quantity,
        t1.status
        FROM biz_gr_reserve_scale t1
        LEFT JOIN biz_unit_info t2 ON t1.unit_info_id = t2.id
        LEFT JOIN depot_qh.basic_enum t3 ON t3.enumId = t1.grain_variety_id
        LEFT JOIN depot_qh.basic_enum t4 ON t1.grain_nature_id = t4.enumId

        <where>
            and t2.delete_flag='NOT_DELETE'
            <if test="unitInfoName!=null and unitInfoName!=''">
                and t2.dwmc like concat("%",#{unitInfoName},"%")
            </if>
            <if test="grainNatureName!=null and grainNatureName!=''">
                and t4.enumname like concat("%",#{grainNatureName},"%")
            </if>
            <if test="grainVarietyName!=null and grainVarietyName!=''">
                and t3.enumname like concat("%",#{grainVarietyName},"%")
            </if>
        </where>
        order by t1.create_time desc
    </select>


    <select id="detail" resultType="com.unis.grainReserves.modular.grReserveScale.vo.GrReserveScaleVO">

        SELECT DISTINCT t1.id,
               t2.id unitInfoId,
               t2.dwmc unitInfoName,
               t1.grain_variety_id grainVarietyId,
               t3.enumname grainVarietyName,
               t4.grain_nature_id grainNatureId,
               t4.grainNatureName grainNatureName,
               t1.reserve_plan_quantity,
               t1.org_info_id,
               t5.org_name orgInfoName,
               t1.plan_no,
               t1.remark,
               t1.status,
               t1.scale_year,
               t1.scale_month
        FROM biz_gr_reserve_scale t1
                 LEFT JOIN biz_unit_info t2 ON t1.unit_info_id = t2.id
                 LEFT JOIN depot_qh.basic_enum t3 ON t3.enumId = t1.grain_variety_id
                 LEFT JOIN
             (SELECT t1.grain_nature_id, t2.enumname grainNatureName
              FROM biz_gr_reserve_scale t1
                       LEFT JOIN depot_qh.basic_enum t2 ON t1.grain_nature_id = t2.enumId) t4
             ON t1.grain_nature_id = t4.grain_nature_id
        left join depot_qh.org_info t5 on t5.org_id=t1.org_info_id
        where t1.id=#{id}

    </select>

    <select id="getCbgmGjjDate" parameterType="java.lang.String" resultType="java.util.Map">
        SELECT
            a.id,
            CONCAT(DATE_FORMAT(a.create_time, '%Y'),'年') nf,
            b.xzqhdm,
            b.dwdm ccqy,
            c.enumCode ylpz,
            d.enumCode ylxz,
            a.reserve_plan_quantity ylcbgmjhsl,
            a.cbgmjhwh,
            a.remarks,
            DATE_FORMAT(a.update_time, '%Y-%m-%d %H:%i:%s') zhgxsj
        FROM
            biz_gr_reserve_scale a
                LEFT JOIN  biz_unit_info b on a.unit_info_id = b.id
                LEFT JOIN depot_qh.basic_enum c on c.enumId = a.grain_variety_id
                LEFT JOIN depot_qh.basic_enum d on d.enumId = a.grain_nature_id
        where a.`status` = 2 and a.id = #{id}
    </select>

</mapper>