<?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.gopsPlan.mapper.GopsPlanMapper">
    <select id="pageList" parameterType="com.baomidou.mybatisplus.core.conditions.Wrapper" resultType="com.unis.grainReserves.modular.gopsPlan.entity.GopsPlan">
        SELECT
            bfp.*,
            oi.org_name orgName
        FROM
            biz_gops_plan bfp
        LEFT JOIN depot_qh.org_info oi ON bfp.org_id = oi.org_id
        ${ew.customSqlSegment}
    </select>
    <select id="getLastPlanOrderNo" resultType="string">
        SELECT
            plan_order_no
        FROM
            biz_gops_plan
        ORDER BY
            create_time DESC
        LIMIT 1
    </select>
    <select id="getListDetail" parameterType="com.unis.grainReserves.modular.gopsPlan.param.GopsPlanDetail" resultType="map">
        SELECT
            kkd.pz,
            be.enumname pzName,
            kkd.hwxz,
            be1.enumname hwxzName,
            kkd.dj,
            be2.enumname djName,
            ROUND(kkd.kcsl / 1000, 3) kcsl
        FROM
            depot_qh.data_kcgl_kcsw_default kkd
        LEFT JOIN depot_qh.basic_enum be ON be.enumId = kkd.pz
        LEFT JOIN depot_qh.basic_enum be1 ON be1.enumId = kkd.hwxz
        LEFT JOIN depot_qh.basic_enum be2 ON be2.enumId = kkd.dj
        WHERE
            kkd.UnitID = #{param.orgId}
        AND kkd.ch = #{param.basicStorehouseId}
        AND kkd.hwh = #{param.warehouseId}
    </select>
    <select id="getPlanRatePage" parameterType="com.baomidou.mybatisplus.core.conditions.Wrapper" resultType="map">
        SELECT
            t1.id,
            t1.orgId,
            oi.org_name orgName,
            t1.planType,
            CASE
        WHEN t1.planType = 1 THEN
            t2.planNum
        WHEN t2.planType = 2 THEN
            t1.planNum
        WHEN t1.planType = 2 THEN
            t1.planNum
        WHEN t2.planType = 1 THEN
            t2.planNum
        END AS planNum,
         IFNULL(
            ROUND(SUM(kkd.kcsl) / 1000, 3),
            0
        ) wcsl,
         IFNULL(
            ROUND(
                (
                    IFNULL(
                        ROUND(SUM(kkd.kcsl) / 1000, 3),
                        0
                    ) / CASE
                    WHEN t1.planType = 1 THEN
                        t2.planNum
                    WHEN t2.planType = 2 THEN
                        t1.planNum
                    WHEN t1.planType = 2 THEN
                        t1.planNum
                    WHEN t2.planType = 1 THEN
                        t2.planNum
                    ELSE
                        0
                    END * 100
                ),
                3
            ),
            0
        ) percentage
        FROM
            (
                SELECT
                    gp.id,
                    gp.org_id orgId,
                    gp.plan_type planType,
                    gpb.plan_quantity planNum
                FROM
                    biz_gops_plan gp
                LEFT JOIN biz_gops_plan_buy gpb ON gp.id = gpb.gops_plan_id
            ) t1
        LEFT JOIN (
            SELECT
                gp.id,
                gp.org_id orgId,
                gp.plan_type planType,
                gps.plan_quantity planNum
            FROM
                biz_gops_plan gp
            LEFT JOIN biz_gops_plan_sale gps ON gp.id = gps.gops_plan_id
        ) t2 ON t1.id = t2.id
        LEFT JOIN depot_qh.data_kcgl_kcsw_default kkd ON t1.orgId = kkd.UnitID
        LEFT JOIN depot_qh.org_info oi ON t1.orgId = oi.org_id
        ${ew.customSqlSegment}
    </select>
    <select id="getEnumTreeList" parameterType="integer" resultType="map">
        SELECT
            enumId,
            enumcode,
            enumname,
            gbcode,
            parentId
        FROM
            depot_qh.basic_enum
        WHERE
            parentId = #{parentId}
        AND delFlag = 1
      </select>
    <select id="getStoreHouseList" parameterType="integer" resultType="map">
        SELECT
            storehouse_id storehouseId,
            storehouse_name storehouseName
        FROM
            depot_qh.basic_storehouse
        WHERE
            org_id = #{orgId}
    </select>
    <select id="getWarehouseList" parameterType="integer" resultType="map">
        SELECT
            warehouse_id warehouseId,
            warehouse_name warehouseName
        FROM
            depot_qh.basic_warehouse
        WHERE
            storehouse_id = #{storehouseId}
    </select>
    <select id="getOrgIdList" resultType="map">
        SELECT
            org_id orgId,
            org_name orgName
        FROM
            depot_qh.org_info
    </select>
</mapper>