<?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="vip.xiaonuo.production.mapper.ProductionWorkstopPlanMapper">

    <select id="getTarget" resultType="vip.xiaonuo.production.entity.vo.ProductionPlanVo2">

        SELECT
        COALESCE(curr.produceName, prev.produceName) AS produceName,
        convert(IFNULL(curr.actualProduction,'0'),decimal(10,2)) AS monthValue,
        convert(IFNULL(prev.actualProduction,'0'),decimal(10,2)) AS lastMonthValue
        FROM
        (SELECT produce_name produceName, IFNULL(sum(actual_production),'0') actualProduction
        FROM production_workstop_plan
        WHERE create_time between #{start} AND #{end}
        <if test="orgId!=null and orgId!=''">
            and  org_id = #{orgId}
        </if>
        AND produce_name LIKE '%${name}%') AS curr
        LEFT JOIN
        (SELECT produce_name produceName, IFNULL(sum(actual_production),'0') actualProduction
        FROM production_workstop_plan
        WHERE create_time between #{lastStart} AND #{lastEnd}
        <if test="orgId!=null and orgId!=''">
            and org_id = #{orgId}
        </if>
        AND produce_name LIKE '%${name}%') AS prev
        ON curr.produceName = prev.produceName;

    </select>

    <select id="getTop" resultType="vip.xiaonuo.production.entity.ProductionWorkstopPlan">
        SELECT @rownum := @rownum + 1 AS id,convert(IFNULL(sum(actual_production),'0'),decimal(10,2)) actualProduction,location_name locationName
         FROM production_workstop_plan, (SELECT @rownum := 0) r
         ${ew.customSqlSegment}
    </select>
</mapper>
