郑永安
2023-06-19 2fcd97552d16718cc7997629fd637a73a5a4483f
src/main/java/com/gk/firework/Mapper/mybatis/StockInfoMapper.xml
对比新文件
@@ -0,0 +1,1829 @@
<?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.gk.firework.Mapper.StockInfoMapper">
    <resultMap id="BaseResultMap" type="com.gk.firework.Domain.StockInfo">
        <!--          -->
        <id column="id" property="id" jdbcType="BIGINT"/>
        <result column="productid" property="productid" jdbcType="BIGINT"/>
        <result column="directioncode" property="directioncode" jdbcType="VARCHAR"/>
        <result column="directionboxcode" property="directionboxcode" jdbcType="VARCHAR"/>
        <result column="num" property="num" jdbcType="INTEGER"/>
        <result column="owner" property="owner" jdbcType="VARCHAR"/>
        <result column="status" property="status" jdbcType="VARCHAR"/>
        <result column="indate" property="indate" jdbcType="TIMESTAMP"/>
        <result column="outdate" property="outdate" jdbcType="TIMESTAMP"/>
        <result column="operator" property="operator" jdbcType="VARCHAR"/>
        <result column="type" property="type" jdbcType="TINYINT"/>
        <result column="remark" property="remark" jdbcType="VARCHAR"/>
        <result column="createdby" property="createdby" jdbcType="VARCHAR"/>
        <result column="createddate" property="createddate" jdbcType="TIMESTAMP"/>
        <result column="modifiedby" property="modifiedby" jdbcType="VARCHAR"/>
        <result column="modifieddate" property="modifieddate" jdbcType="TIMESTAMP"/>
        <result column="flag" property="flag" jdbcType="TINYINT"/>
    </resultMap>
    <resultMap id="StockVoResultMap" type="com.gk.firework.Domain.Vo.StockVo"/>
    <sql id="Base_Column_List">
        <!--          -->
        id, productid, directioncode, directionboxcode, num, owner, status, indate, outdate,
        operator, type, remark, createdby, createddate, modifiedby, modifieddate, flag
    </sql>
    <select id="selectStockByDirection" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from stock${slice}
        <where>
            directioncode = #{directionCode}
        </where>
        for update
    </select>
    <select id="selectStockByDireAndUser" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from stock${slice}
        <where>
            directioncode = #{directionCode}
            and (owner = #{companyId} or status = '已售出')
        </where>
    </select>
    <select id="selectEarlyWarn" resultType="com.gk.firework.Domain.StockInfo">
        SELECT stock.`owner`,
        SUM(stock.stocknum) as num
        FROM(
        select stock.`owner`,sum(stock.num/product.boxnumber) stocknum
        from stock as stock,product as product
        where stock.productid = product.id
        and stock.`status` =''
        GROUP BY stock.`owner`
        <foreach collection="slices" item="item" index="index">
            UNION ALL
            select stock.`owner`,sum(stock.num/product.boxnumber) stocknum
            from stock_slice${item} as stock,product as product
            where stock.productid = product.id
            and stock.`status` =''
            GROUP BY stock.`owner`
        </foreach>
        ) as stock
        LEFT JOIN enterprise on enterprise.id = stock.`owner`
        where enterprise.safetysupervision = #{enterprisetype}
        GROUP BY stock.`owner`
        HAVING SUM(stock.stocknum) &gt;= #{min} and SUM(stock.stocknum) &lt; #{max}
    </select>
    <select id="selectAlarm" resultType="com.gk.firework.Domain.StockInfo">
        SELECT stock.`owner`,
        SUM(stock.stocknum) as num
        FROM(
        select stock.`owner`,sum(stock.num/product.boxnumber) stocknum
        from stock as stock,product as product
        where stock.productid = product.id
        and stock.`status` =''
        GROUP BY stock.`owner`
        <foreach collection="slices" item="item" index="index">
            UNION ALL
            select stock.`owner`,sum(stock.num/product.boxnumber) stocknum
            from stock_slice${item} as stock,product as product
            where stock.productid = product.id
            and stock.`status` =''
            GROUP BY stock.`owner`
        </foreach>
        ) as stock
        LEFT JOIN enterprise on enterprise.id = stock.`owner`
        where enterprise.safetysupervision = #{enterprisetype}
        GROUP BY stock.`owner`
        HAVING SUM(stock.stocknum) &gt;= #{max}
    </select>
    <insert id="insertStockInfo">
        insert into stock${slice}
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="stockInfo.id != null">
                id,
            </if>
            <if test="stockInfo.productid != null">
                productid,
            </if>
            <if test="stockInfo.directioncode != null">
                directioncode,
            </if>
            <if test="stockInfo.directionboxcode != null">
                directionboxcode,
            </if>
            <if test="stockInfo.num != null">
                num,
            </if>
            <if test="stockInfo.owner != null">
                owner,
            </if>
            <if test="stockInfo.status != null">
                status,
            </if>
            <if test="stockInfo.indate != null">
                indate,
            </if>
            <if test="stockInfo.outdate != null">
                outdate,
            </if>
            <if test="stockInfo.operator != null">
                operator,
            </if>
            <if test="stockInfo.type != null">
                type,
            </if>
            <if test="stockInfo.remark != null">
                remark,
            </if>
            <if test="stockInfo.createdby != null">
                createdby,
            </if>
            <if test="stockInfo.createddate != null">
                createddate,
            </if>
            <if test="stockInfo.modifiedby != null">
                modifiedby,
            </if>
            <if test="stockInfo.modifieddate != null">
                modifieddate,
            </if>
            <if test="stockInfo.flag != null">
                flag,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="stockInfo.id != null">
                #{stockInfo.id,jdbcType=BIGINT},
            </if>
            <if test="stockInfo.productid != null">
                #{stockInfo.productid,jdbcType=BIGINT},
            </if>
            <if test="stockInfo.directioncode != null">
                #{stockInfo.directioncode,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.directionboxcode != null">
                #{stockInfo.directionboxcode,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.num != null">
                #{stockInfo.num,jdbcType=INTEGER},
            </if>
            <if test="stockInfo.owner != null">
                #{stockInfo.owner,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.status != null">
                #{stockInfo.status,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.indate != null">
                #{stockInfo.indate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.outdate != null">
                #{stockInfo.outdate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.operator != null">
                #{stockInfo.operator,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.type != null">
                #{stockInfo.type,jdbcType=TINYINT},
            </if>
            <if test="stockInfo.remark != null">
                #{stockInfo.remark,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.createdby != null">
                #{stockInfo.createdby,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.createddate != null">
                #{stockInfo.createddate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.modifiedby != null">
                #{stockInfo.modifiedby,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.modifieddate != null">
                #{stockInfo.modifieddate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.flag != null">
                #{stockInfo.flag,jdbcType=TINYINT},
            </if>
        </trim>
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </insert>
    <update id="updateStockInfo" parameterType="com.gk.firework.Domain.StockInfo">
        <!--          -->
        update stock${slice}
        <set>
            <if test="stockInfo.productid != null">
                productid = #{stockInfo.productid,jdbcType=BIGINT},
            </if>
            <if test="stockInfo.directioncode != null">
                directioncode = #{stockInfo.directioncode,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.directionboxcode != null">
                directionboxcode = #{stockInfo.directionboxcode,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.num != null">
                num = #{stockInfo.num,jdbcType=INTEGER},
            </if>
            <if test="stockInfo.owner != null">
                owner = #{stockInfo.owner,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.status != null">
                status = #{stockInfo.status,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.indate != null">
                indate = #{stockInfo.indate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.outdate != null">
                outdate = #{stockInfo.outdate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.operator != null">
                operator = #{stockInfo.operator,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.type != null">
                type = #{stockInfo.type,jdbcType=TINYINT},
            </if>
            <if test="stockInfo.remark != null">
                remark = #{stockInfo.remark,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.createdby != null">
                createdby = #{stockInfo.createdby,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.createddate != null">
                createddate = #{stockInfo.createddate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.modifiedby != null">
                modifiedby = #{stockInfo.modifiedby,jdbcType=VARCHAR},
            </if>
            <if test="stockInfo.modifieddate != null">
                modifieddate = #{stockInfo.modifieddate,jdbcType=TIMESTAMP},
            </if>
            <if test="stockInfo.flag != null">
                flag = #{stockInfo.flag,jdbcType=TINYINT},
            </if>
        </set>
        where id = #{stockInfo.id,jdbcType=BIGINT}
    </update>
    <update id="clearStock">
        UPDATE stock${slice} AS stock
        LEFT JOIN enterprise ON enterprise.id = stock.`owner`
        LEFT JOIN product ON product.id = stock.productid
        AND product.isdel = 0
        <set>
            stock.owner = '',
            stock.modifieddate = #{date},
            stock.remark = #{remark}
        </set>
        where
        `status` = ''
        AND stock.`owner` = #{owner}
        AND enterprise.validflag = 1
        AND enterprise.enterprisestatus = 'ON'
    </update>
    <update id="clearStockByItem">
        UPDATE stock${slice} AS stock
        LEFT JOIN enterprise ON enterprise.id = stock.`owner`
        LEFT JOIN product ON product.id = stock.productid
        AND product.isdel = 0
        <set>
            stock.owner = '',
            stock.modifieddate = #{date},
            stock.remark = #{remark}
        </set>
        where
        `status` = ''
        AND stock.`owner` = #{owner}
        AND enterprise.validflag = 1
        AND enterprise.enterprisestatus = 'ON'
        AND product.directioncode IN
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <select id="selectEnterpriseStockDataGrid" resultMap="StockVoResultMap">
        SELECT
        enterprise.id as `owner`,
        IFNULL(SUM( stockNum ),0) AS stockNum,
        enterprise.enterprisename,
        enterprise.enterprisenumber,
        enterprise.province,
        enterprise.city,
        enterprise.district
        FROM(
        SELECT
        stock.`owner`,
        count(1)/product.boxnumber as stockNum,
        enterprise.enterprisename as enterpriseName,
        enterprise.enterprisenumber as enterpriseNumber
        FROM
        stock as stock
        LEFT JOIN enterprise on enterprise.id = stock.`owner`
        left join product on product.id = stock.productid and product.isdel = 0
        <where>
            `status` = ''
            and stock.`owner` != ''
            and enterprise.validflag = 1
            and enterprise.enterprisestatus = 'ON'
            <if test="condition.name != null and condition.name != ''">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY `owner`,productid
        <foreach collection="list" item="item" index="index">
            union all
            (
            SELECT
            stock.`owner`,
            count(1)/product.boxnumber as stockNum,
            enterprise.enterprisename as enterpriseName,
            enterprise.enterprisenumber as enterpriseNumber
            FROM
            stock_slice${item} as stock
            LEFT JOIN enterprise on enterprise.id = stock.`owner`
            LEFT JOIN product on product.id = stock.productid and product.isdel = 0
            <where>
                `status` = ''
                and stock.`owner` != ''
                and enterprise.validflag = 1
                and enterprise.enterprisestatus = 'ON'
                <if test="condition.name != null and condition.name != ''">
                    and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
                </if>
                <if test="condition.operator != null and condition.operator != ''">
                    and (enterprise.enterprisenumber =#{condition.operator}
                    <if test="condition.subList != null and condition.subList.size() > 0">
                        <foreach collection="condition.subList" item="item" index="index" >
                            or  enterprise.enterprisenumber = #{item}
                        </foreach>
                    </if>
                    )
                </if>
                <if test="condition.type != null  and condition.type != ''">
                    and enterprise.safetysupervision = #{condition.type}
                </if>
                <if test="condition.province != null and condition.province != ''">
                    and enterprise.province = #{condition.province}
                </if>
                <if test="condition.city != null and condition.city != ''">
                    and enterprise.city = #{condition.city}
                </if>
                <if test="condition.district != null and condition.district != ''">
                    and enterprise.district = #{condition.district}
                </if>
                <if test="condition.street != null and condition.street != ''">
                    and enterprise.street = #{condition.street}
                </if>
                <if test="condition.community != null and condition.community != ''">
                    and enterprise.committee = #{condition.community}
                </if>
            </where>
            GROUP BY `owner`,productid
            )
        </foreach>
        )as temp
        RIGHT JOIN enterprise on temp.enterpriseName = enterprise.enterprisename
        <where>
            1=1
            and enterprise.validflag = 1
            and enterprise.enterprisestatus = 'ON'
            <if test="condition.name != null  and condition.name != ''">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null  and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null  and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null  and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null  and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null  and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null  and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null  and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisename
    </select>
    <select id="selectStockDataGrid" resultMap="StockVoResultMap">
        SELECT
        stock.productid,
        product.directioncode as directioncode,
        product.`name`,
        product.explosivecontent as explosiveContent,
        product.specification,
        product.createddate,
        product.boxnumber,
        product.type AS productType,
        product.secondarytype AS secondaryType,
        count(1)/product.boxnumber as countNum,
        enterprise.enterprisenumber
        FROM
        stock as stock
        LEFT JOIN product on product.id = stock.productid and product.isdel = 0
        LEFT JOIN enterprise on enterprise.id = stock.owner
        <where>
            stock.`status` = ''
            and stock.`owner` = #{condition.owner}
            <if test="condition.name != null and condition.name != ''">
                and product.name = #{condition.name}
            </if>
            <if test="condition.directionCode != null and condition.directionCode != ''">
                and product.directioncode = #{condition.directionCode}
            </if>
            <if test="condition.manufacturer != null and condition.manufacturer != ''">
                and product.manufacturer = #{condition.manufacturer}
            </if>
            <if test="condition.type != null and condition.type != ''">
                and product.type = #{condition.type}
            </if>
            <if test="condition.secondaryType != null and condition.secondaryType != ''">
                and product.secondarytype = #{condition.secondaryType}
            </if>
        </where>
        GROUP BY stock.productid
        <foreach collection="list" item="item" index="index">
            union all
            (
            select
            stock.productid,
            product.directioncode,
            product.`name`,
            product.explosivecontent,
            product.specification,
            product.createddate,
            product.boxnumber,
            product.type AS productType,
            product.secondarytype AS secondaryType,
            count(1)/product.boxnumber as countNum,
            enterprise.enterprisenumber
            from stock_slice${item} as stock
            LEFT JOIN product on product.id = stock.productid and product.isdel = 0
            LEFT JOIN enterprise on enterprise.id = stock.owner
            <where>
                stock.`status` = ''
                and stock.`owner` = #{condition.owner}
                <if test="condition.name != null and  condition.name != ''">
                    and product.name = #{condition.name}
                </if>
                <if test="condition.directionCode != null and condition.directionCode != ''">
                    and product.directioncode = #{condition.directionCode}
                </if>
                <if test="condition.manufacturer != null and condition.manufacturer != ''">
                    and product.manufacturer = #{condition.manufacturer}
                </if>
                <if test="condition.type != null and condition.type != ''">
                    and product.type = #{condition.type}
                </if>
                <if test="condition.secondaryType != null and condition.secondaryType != ''">
                    and product.secondarytype = #{condition.secondaryType}
                </if>
            </where>
            GROUP BY stock.productid
            )
        </foreach>
    </select>
    <select id="selectNoEntryCount" resultType="com.gk.firework.Domain.Vo.NoEntryVo">
        SELECT
        temp.enterprisename AS name,
        COUNT(1) AS number
        FROM(
        SELECT
        stock.directioncode,
        enterprise.enterprisename
        FROM
        stock as stock
        LEFT JOIN `user` on `user`.username = stock.modifiedby
        LEFT JOIN enterprise ON enterprise.id = `user`.companyid
        <where>
            `owner` = ''
            and DATE(modifieddate) = DATE_SUB(DATE(#{time}),INTERVAL 30 day)
        </where>
        <foreach collection="list" item="item" index="index">
            union all
            (SELECT
            stock.directioncode,
            enterprise.enterprisename
            FROM
            stock_slice${item} as stock
            LEFT JOIN `user` on `user`.username = stock.modifiedby
            LEFT JOIN enterprise ON enterprise.id = `user`.companyid
            <where>
                `owner` = ''
                and DATE(modifieddate) = DATE_SUB(DATE(#{time}),INTERVAL #{period} day)
            </where>)
        </foreach>
        )temp
        GROUP BY temp.enterprisename
    </select>
    <select id="selectNoEntryDetail" resultType="com.gk.firework.Domain.Vo.StockVo">
        SELECT
        *
        FROM
        (
        SELECT
        stock.directioncode,
        enterprise.enterprisename AS enterpriseName,
        product.name,
        stock.modifieddate
        FROM
        stock as stock
        LEFT JOIN `user` on `user`.username = stock.modifiedby
        LEFT JOIN enterprise ON enterprise.id = `user`.companyid
        LEFT JOIN product on product.id = stock.productid and product.isdel = 0
        <where>
            `owner` = ''
            and enterprise.id = #{condition.enterpriseid}
            and DATE(stock.modifieddate) = DATE(#{condition.starttime})
        </where>
        <foreach collection="condition.list" item="item" index="index">
            union all
            (SELECT
            stock.directioncode,
            enterprise.enterprisename AS enterpriseName,
            product.name,
            stock.modifieddate
            FROM
            stock_slice${item} as stock
            LEFT JOIN `user` on `user`.username = stock.modifiedby
            LEFT JOIN enterprise ON enterprise.id = `user`.companyid
            LEFT JOIN product on product.id = stock.productid and product.isdel = 0
            <where>
                `owner` = ''
                and enterprise.id = #{condition.enterpriseid}
                and DATE(stock.modifieddate) = DATE(#{condition.starttime})
            </where>)
        </foreach>
        )temp
    </select>
    <select id="selectStockByProductId" resultType="com.gk.firework.Domain.StockInfo">
        select
        <include refid="Base_Column_List"/>
        from stock${slice}
        <where>
            productid = #{id}
        </where>
        for update
    </select>
    <select id="selectStockVoByItemCode" resultType="com.gk.firework.Domain.Vo.StockVo">
        select
        stock.directioncode,
        stock.owner,
        stock.status,
        stock.createdby,
        stock.createddate,
        stock.modifiedby,
        stock.modifieddate,
        product.name
        from stock${condition.slice} as stock
        left join product on product.id = stock.productid and product.isdel = 0
        <where>
            product.directioncode = #{condition.itemCode}
            and stock.status = ''
            <if test="condition.directionCode != null ">
                and stock.directioncode = #{condition.directionCode}
            </if>
            <if test="condition.owner != null">
                and stock.owner = #{condition.owner}
            </if>
        </where>
    </select>
    <select id="selectEnterpriseSaleDataGrid" resultType="com.gk.firework.Domain.Vo.StockVo">
        SELECT
        SUM( entrynum ) AS entryNum,
        SUM( returnnum ) AS returnNum,
        SUM( salenum ) AS saleNum,
        SUM( deliverynum ) AS deliveryNum,
        SUM( returndeliverynum ) AS returnDeliveryNum,
        companynumber as enterpriseNumber,
        enterprisename
        FROM
        (
        SELECT
        IFNULL(SUM( num / boxnumber ),0) AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN entryorder AS entry ON entry.companynumber = enterprise.enterprisenumber
        and entry.type = 1
        and entry.entrydate &gt;= #{condition.beginDate}
        and entry.entrydate &lt;= #{condition.endDate}
        LEFT JOIN entrydetail AS detail ON detail.entryorder = entry.`code`
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null  and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null  and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null  and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null  and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null  and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null  and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null  and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,product.directioncode)entrytemp
        GROUP BY companynumber
        UNION ALL
        (
        SELECT
        0 AS entrynum,
        IFNULL(SUM( num / boxnumber ),0) AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN entryorder AS entry ON entry.companynumber = enterprise.enterprisenumber
        and entry.type = 2
        and entry.entrydate &gt;= #{condition.beginDate}
        and entry.entrydate &lt;= #{condition.endDate}
        LEFT JOIN entrydetail AS detail ON detail.entryorder = entry.`code`
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,product.directioncode)entrytemp
        GROUP BY companynumber
        ) UNION ALL
        (
        SELECT
        0 AS entrynum,
        0 AS returnnum,
        IFNULL(SUM( salenum ),0),
        0 AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        ( count( 1 )/ product.boxnumber ) AS salenum,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename
        FROM
        enterprise
        LEFT JOIN saleorder AS sale ON sale.companynumber = enterprise.enterprisenumber
        AND sale.createdat &gt;= #{condition.beginDate}
        AND sale.createdat &lt;= #{condition.endDate}
        LEFT JOIN saleorderdetail AS detail ON sale.`code` = detail.ordercode
        LEFT JOIN product ON detail.itemcode = product.directioncode
        AND product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,
        product.directioncode
        ) stemp
        GROUP BY
        companynumber
        ) UNION ALL
        (
        SELECT
        0 AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        IFNULL(SUM( num / boxnumber ),0) AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN deliveryorder AS delivery ON delivery.companynumber = enterprise.enterprisenumber
        AND delivery.type = 1
        AND delivery.operatat &gt;= #{condition.beginDate}
        AND delivery.operatat &lt;= #{condition.endDate}
        LEFT JOIN deliverydetail AS detail ON delivery.ordercode = detail.deliverycode
        LEFT JOIN product ON detail.itemcode = product.directioncode
        AND product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,
        product.directioncode)deliverytemp
        GROUP BY
        companynumber
        ) UNION ALL
        (
        SELECT
        0 AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        IFNULL(SUM( num / boxnumber ),0) AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN deliveryorder AS delivery ON delivery.companynumber = enterprise.enterprisenumber
        AND delivery.type = 2
        AND delivery.operatat &gt;= #{condition.beginDate}
        AND delivery.operatat &lt;= #{condition.endDate}
        LEFT JOIN deliverydetail AS detail ON delivery.ordercode = detail.deliverycode
        LEFT JOIN product ON detail.itemcode = product.directioncode
        AND product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,
        product.directioncode )deliverytemp
        GROUP BY
        companynumber
        )
        ) temp
        GROUP BY
        companynumber
    </select>
    <select id="selectSaleDetailDataGrid" resultType="com.gk.firework.Domain.Vo.StockVo">
        SELECT
        name,
        directioncode,
        manufacturer,
        boxnumber,
        SUM( entrynum ) AS entryNum,
        SUM( returnnum ) AS returnNum,
        SUM( salenum ) AS saleNum,
        SUM( deliverynum ) AS deliveryNum,
        SUM( returndeliverynum ) AS returnDeliveryNum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        product.name,
        product.directioncode,
        product.manufacturer,
        product.boxnumber,
        sum( detail.num)/ product.boxnumber AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        0 AS returndeliverynum,
        entry.companynumber,
        enterprise.enterprisename
        FROM
        entrydetail AS detail
        LEFT JOIN entryorder AS entry ON entry.`code` = detail.entryorder
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        LEFT JOIN enterprise ON enterprise.enterprisenumber = entry.companynumber
        <where>
            entry.type = 1
            and entry.entrydate &gt;= #{condition.beginDate}
            and entry.entrydate &lt;= #{condition.endDate}
            <if test="condition.name != null">
                and product.name like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.manufacturer != null">
                and product.manufacturer = #{condition.manufacturer}
            </if>
            <if test="condition.directionCode != null">
                and product.directioncode = #{condition.directionCode}
            </if>
            <if test="condition.companyNumber != null">
                and enterprise.enterprisenumber = #{condition.companyNumber}
            </if>
        </where>
        GROUP BY
        entry.companynumber,product.directioncode
        UNION ALL
        (
        SELECT
        product.name,
        product.directioncode,
        product.manufacturer,
        product.boxnumber,
        0 AS entrynum,
        sum( detail.num)/ product.boxnumber AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        0 AS returndeliverynum,
        entry.companynumber,
        enterprise.enterprisename
        FROM
        entrydetail AS detail
        LEFT JOIN entryorder AS entry ON entry.`code` = detail.entryorder
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        LEFT JOIN enterprise ON enterprise.enterprisenumber = entry.companynumber
        <where>
            entry.type = 2
            and entry.entrydate &gt;= #{condition.beginDate}
            and entry.entrydate &lt;= #{condition.endDate}
            <if test="condition.name != null">
                and product.name like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.manufacturer != null">
                and product.manufacturer = #{condition.manufacturer}
            </if>
            <if test="condition.directionCode != null">
                and product.directioncode = #{condition.directionCode}
            </if>
            <if test="condition.companyNumber != null">
                and enterprise.enterprisenumber = #{condition.companyNumber}
            </if>
        </where>
        GROUP BY
        entry.companynumber,product.directioncode
        ) UNION ALL
        (
        SELECT
        product.name,
        product.directioncode,
        product.manufacturer,
        product.boxnumber,
        0 AS entrynum,
        0 AS returnnum,
        count( 1 ) / product.boxnumber AS salenum,
        0 AS deliverynum,
        0 AS returndeliverynum,
        sale.companynumber,
        enterprise.enterprisename
        FROM
        saleorderdetail AS detail
        LEFT JOIN saleorder AS sale ON sale.`code` = detail.ordercode
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        LEFT JOIN enterprise on sale.companynumber = enterprise.enterprisenumber
        <where>
            1=1
            AND sale.createdat &gt;= #{condition.beginDate}
            AND sale.createdat &lt;= #{condition.endDate}
            <if test="condition.name != null">
                and product.name like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.manufacturer != null">
                and product.manufacturer = #{condition.manufacturer}
            </if>
            <if test="condition.directionCode != null">
                and product.directioncode = #{condition.directionCode}
            </if>
            <if test="condition.companyNumber != null">
                and enterprise.enterprisenumber = #{condition.companyNumber}
            </if>
        </where>
        GROUP BY
        sale.companynumber,
        product.directioncode
        )
        UNION ALL
        (
        SELECT
        product.name,
        product.directioncode,
        product.manufacturer,
        product.boxnumber,
        0 AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        SUM( detail.num )/product.boxnumber AS deliverynum,
        0 AS returndeliverynum,
        delivery.companynumber,
        enterprise.enterprisename
        FROM
        deliverydetail AS detail
        LEFT JOIN deliveryorder AS delivery ON delivery.ordercode = detail.deliverycode
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        LEFT JOIN enterprise on delivery.companynumber = enterprise.enterprisenumber
        <where>
            delivery.type = 1
            AND delivery.operatat &gt;= #{condition.beginDate}
            AND delivery.operatat &lt;= #{condition.endDate}
            <if test="condition.name != null">
                and product.name like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.manufacturer != null">
                and product.manufacturer = #{condition.manufacturer}
            </if>
            <if test="condition.directionCode != null">
                and product.directioncode = #{condition.directionCode}
            </if>
            <if test="condition.companyNumber != null">
                and enterprise.enterprisenumber = #{condition.companyNumber}
            </if>
        </where>
        GROUP BY
        delivery.companynumber,
        product.directioncode
        ) UNION ALL
        (
        SELECT
        product.name,
        product.directioncode,
        product.manufacturer,
        product.boxnumber,
        0 AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        SUM( detail.num )/product.boxnumber AS returndeliverynum,
        delivery.companynumber,
        enterprise.enterprisename
        FROM
        deliverydetail AS detail
        LEFT JOIN deliveryorder AS delivery ON delivery.ordercode = detail.deliverycode
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        LEFT JOIN enterprise on delivery.companynumber = enterprise.enterprisenumber
        <where>
            delivery.type = 2
            AND delivery.operatat &gt;= #{condition.beginDate}
            AND delivery.operatat &lt;= #{condition.endDate}
            <if test="condition.name != null">
                and product.name like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.manufacturer != null">
                and product.manufacturer = #{condition.manufacturer}
            </if>
            <if test="condition.directionCode != null">
                and product.directioncode = #{condition.directionCode}
            </if>
            <if test="condition.companyNumber != null">
                and enterprise.enterprisenumber = #{condition.companyNumber}
            </if>
        </where>
        GROUP BY
        delivery.companynumber,
        product.directioncode
        )
        ) temp
        GROUP BY
        companynumber,
        directioncode
    </select>
    <select id="selectCurrentFireStatus" resultType="com.gk.firework.Domain.StockInfo">
     select stock.*
     from (
        <foreach collection="codes" index="index" item="item"
                 separator="union all" >
        <trim suffixOverrides="union all">
            select #{item}
            <if test="index == 0">
                code
            </if>
        </trim>
        </foreach>
        ) as code
      , stock${slice}  as stock
       where stock.directioncode = code.code
       <if test="params.status != null">
          and stock.status = #{params.status}
       </if>
       <if test="params.owner != null and params.owner != ''">
          and stock.owner != ''
       </if>
       <if test="params.owner != null and params.owner == ''">
          and stock.owner = ''
       </if>
    </select>
    <select id="selectEnterpriseStocks" resultType="com.gk.firework.Domain.Vo.StockVo">
        SELECT
        `owner`,
        SUM(stockNum) as stockNum,
        enterpriseName,
        enterpriseNumber,
        province,
        city,
        district,
        IFNULL(MAX(CASE type WHEN '爆竹类' THEN stockNum END ),0)firecracker ,
        IFNULL(MAX(CASE type WHEN '喷花类' THEN stockNum END ),0)spray ,
        IFNULL(MAX(CASE type WHEN '旋转类' THEN stockNum END ),0)rotation ,
        IFNULL(MAX(CASE type WHEN '吐珠类' THEN stockNum END ),0)bead ,
        IFNULL(MAX(CASE type WHEN '玩具类' THEN stockNum END ),0)toy ,
        IFNULL(MAX(CASE type WHEN '组合烟花类' THEN stockNum END ),0)combined
        FROM (
        SELECT
        enterprise.id as `owner`,
        temp.type,
        IFNULL(SUM( stockNum ),0) AS stockNum,
        enterprise.enterprisename,
        enterprise.enterprisenumber,
        enterprise.province,
        enterprise.city,
        enterprise.district
        FROM(
        SELECT
        stock.`owner`,
        count(1)/product.boxnumber as stockNum,
        enterprise.enterprisename as enterpriseName,
        enterprise.enterprisenumber as enterpriseNumber,
        product.type
        FROM
        stock as stock
        LEFT JOIN enterprise on enterprise.id = stock.`owner`
        left join product on product.id = stock.productid and product.isdel = 0
        <where>
            `status` = ''
            and stock.`owner` != ''
            and enterprise.validflag = 1
            and enterprise.enterprisestatus = 'ON'
            <if test="condition.name != null and condition.name != ''">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY `owner`,productid
        <foreach collection="list" item="item" index="index">
            union all
            (
            SELECT
            stock.`owner`,
            count(1)/product.boxnumber as stockNum,
            enterprise.enterprisename as enterpriseName,
            enterprise.enterprisenumber as enterpriseNumber,
            product.type
            FROM
            stock_slice${item} as stock
            LEFT JOIN enterprise on enterprise.id = stock.`owner`
            LEFT JOIN product on product.id = stock.productid and product.isdel = 0
            <where>
                `status` = ''
                and stock.`owner` != ''
                and enterprise.validflag = 1
                and enterprise.enterprisestatus = 'ON'
                <if test="condition.name != null and condition.name != ''">
                    and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
                </if>
                <if test="condition.operator != null and condition.operator != ''">
                    and (enterprise.enterprisenumber =#{condition.operator}
                    <if test="condition.subList != null and condition.subList.size() > 0">
                        <foreach collection="condition.subList" item="item" index="index" >
                            or  enterprise.enterprisenumber = #{item}
                        </foreach>
                    </if>
                    )
                </if>
                <if test="condition.type != null  and condition.type != ''">
                    and enterprise.safetysupervision = #{condition.type}
                </if>
                <if test="condition.province != null and condition.province != ''">
                    and enterprise.province = #{condition.province}
                </if>
                <if test="condition.city != null and condition.city != ''">
                    and enterprise.city = #{condition.city}
                </if>
                <if test="condition.district != null and condition.district != ''">
                    and enterprise.district = #{condition.district}
                </if>
                <if test="condition.street != null and condition.street != ''">
                    and enterprise.street = #{condition.street}
                </if>
                <if test="condition.community != null and condition.community != ''">
                    and enterprise.committee = #{condition.community}
                </if>
            </where>
            GROUP BY `owner`,productid
            )
        </foreach>
        )as temp
        RIGHT JOIN enterprise on temp.enterpriseName = enterprise.enterprisename
        <where>
            1=1
            and enterprise.validflag = 1
            and enterprise.enterprisestatus = 'ON'
            <if test="condition.name != null  and condition.name != ''">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null  and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null  and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null  and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null  and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null  and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null  and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null  and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisename,
        temp.type)temp1
        GROUP BY enterprisename
    </select>
    <select id="selectEnterpriseStock" resultType="com.gk.firework.Domain.Vo.StockVo">
        SELECT
        enterprise.id as `owner`,
        IFNULL(SUM( stockNum ),0) AS stockNum,
        enterprise.enterprisename,
        enterprise.enterprisenumber,
        enterprise.province,
        enterprise.city,
        enterprise.district
        FROM(
        SELECT
        stock.`owner`,
        count(1)/product.boxnumber as stockNum,
        enterprise.enterprisename as enterpriseName,
        enterprise.enterprisenumber as enterpriseNumber
        FROM
        stock as stock
        LEFT JOIN enterprise on enterprise.id = stock.`owner`
        left join product on product.id = stock.productid and product.isdel = 0
        <where>
            `status` = ''
            and stock.`owner` != ''
            and enterprise.id = #{id}
        </where>
        GROUP BY `owner`,productid
        <foreach collection="list" item="item" index="index">
            union all
            (
            SELECT
            stock.`owner`,
            count(1)/product.boxnumber as stockNum,
            enterprise.enterprisename as enterpriseName,
            enterprise.enterprisenumber as enterpriseNumber
            FROM
            stock_slice${item} as stock
            LEFT JOIN enterprise on enterprise.id = stock.`owner`
            LEFT JOIN product on product.id = stock.productid and product.isdel = 0
            <where>
                `status` = ''
                and stock.`owner` != ''
                and enterprise.id = #{id}
            </where>
            GROUP BY `owner`,productid
            )
        </foreach>
        )as temp
        LEFT JOIN enterprise on temp.enterpriseName = enterprise.enterprisename
        GROUP BY
        enterprise.enterprisename
    </select>
    <select id="selectEnterpriseSaleDataCount" resultType="java.util.Map">
        select
        sum(tmp.entryNum) entryNum,
        sum(tmp.returnNum) returnNum,
        sum(tmp.saleNum) saleNum,
        sum(tmp.deliveryNum) deliveryNum,
        sum(tmp.returnDeliveryNum) returnDeliveryNum
        from
        (
        SELECT
        SUM( entrynum ) AS entryNum,
        SUM( returnnum ) AS returnNum,
        SUM( salenum ) AS saleNum,
        SUM( deliverynum ) AS deliveryNum,
        SUM( returndeliverynum ) AS returnDeliveryNum,
        companynumber as enterpriseNumber,
        enterprisename
        FROM
        (
        SELECT
        IFNULL(SUM( num / boxnumber ),0) AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN entryorder AS entry ON entry.companynumber = enterprise.enterprisenumber
        and entry.type = 1
        and entry.entrydate &gt;= #{condition.beginDate}
        and entry.entrydate &lt;= #{condition.endDate}
        LEFT JOIN entrydetail AS detail ON detail.entryorder = entry.`code`
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null  and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null  and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null  and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null  and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null  and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null  and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null  and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,product.directioncode)entrytemp
        GROUP BY companynumber
        UNION ALL
        (
        SELECT
        0 AS entrynum,
        IFNULL(SUM( num / boxnumber ),0) AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN entryorder AS entry ON entry.companynumber = enterprise.enterprisenumber
        and entry.type = 2
        and entry.entrydate &gt;= #{condition.beginDate}
        and entry.entrydate &lt;= #{condition.endDate}
        LEFT JOIN entrydetail AS detail ON detail.entryorder = entry.`code`
        LEFT JOIN product ON detail.itemcode = product.directioncode and product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,product.directioncode)entrytemp
        GROUP BY companynumber
        ) UNION ALL
        (
        SELECT
        0 AS entrynum,
        0 AS returnnum,
        IFNULL(SUM( salenum ),0),
        0 AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        ( count( 1 )/ product.boxnumber ) AS salenum,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename
        FROM
        enterprise
        LEFT JOIN saleorder AS sale ON sale.companynumber = enterprise.enterprisenumber
        AND sale.createdat &gt;= #{condition.beginDate}
        AND sale.createdat &lt;= #{condition.endDate}
        LEFT JOIN saleorderdetail AS detail ON sale.`code` = detail.ordercode
        LEFT JOIN product ON detail.itemcode = product.directioncode
        AND product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,
        product.directioncode
        ) stemp
        GROUP BY
        companynumber
        ) UNION ALL
        (
        SELECT
        0 AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        IFNULL(SUM( num / boxnumber ),0) AS deliverynum,
        0 AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN deliveryorder AS delivery ON delivery.companynumber = enterprise.enterprisenumber
        AND delivery.type = 1
        AND delivery.operatat &gt;= #{condition.beginDate}
        AND delivery.operatat &lt;= #{condition.endDate}
        LEFT JOIN deliverydetail AS detail ON delivery.ordercode = detail.deliverycode
        LEFT JOIN product ON detail.itemcode = product.directioncode
        AND product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,
        product.directioncode)deliverytemp
        GROUP BY
        companynumber
        ) UNION ALL
        (
        SELECT
        0 AS entrynum,
        0 AS returnnum,
        0 AS salenum,
        0 AS deliverynum,
        IFNULL(SUM( num / boxnumber ),0) AS returndeliverynum,
        companynumber,
        enterprisename
        FROM
        (
        SELECT
        SUM( detail.num ) AS num,
        enterprise.enterprisenumber AS companynumber,
        enterprise.enterprisename,
        product.boxnumber
        FROM
        enterprise
        LEFT JOIN deliveryorder AS delivery ON delivery.companynumber = enterprise.enterprisenumber
        AND delivery.type = 2
        AND delivery.operatat &gt;= #{condition.beginDate}
        AND delivery.operatat &lt;= #{condition.endDate}
        LEFT JOIN deliverydetail AS detail ON delivery.ordercode = detail.deliverycode
        LEFT JOIN product ON detail.itemcode = product.directioncode
        AND product.isdel = 0
        <where>
            enterprise.enterprisestatus = 'ON'
            and enterprise.validflag = 1
            <if test="condition.name != null">
                and enterprise.enterprisename like CONCAT('%',#{condition.name},'%')
            </if>
            <if test="condition.operator != null and condition.operator != ''">
                and (enterprise.enterprisenumber =#{condition.operator}
                <if test="condition.subList != null and condition.subList.size() > 0">
                    <foreach collection="condition.subList" item="item" index="index" >
                        or  enterprise.enterprisenumber = #{item}
                    </foreach>
                </if>
                )
            </if>
            <if test="condition.type != null and condition.type != ''">
                and enterprise.safetysupervision = #{condition.type}
            </if>
            <if test="condition.province != null and condition.province != ''">
                and enterprise.province = #{condition.province}
            </if>
            <if test="condition.city != null and condition.city != ''">
                and enterprise.city = #{condition.city}
            </if>
            <if test="condition.district != null and condition.district != ''">
                and enterprise.district = #{condition.district}
            </if>
            <if test="condition.street != null and condition.street != ''">
                and enterprise.street = #{condition.street}
            </if>
            <if test="condition.community != null and condition.community != ''">
                and enterprise.committee = #{condition.community}
            </if>
        </where>
        GROUP BY
        enterprise.enterprisenumber,
        product.directioncode )deliverytemp
        GROUP BY
        companynumber
        )
        ) temp
        GROUP BY
        companynumber
        ) as tmp
    </select>
    <select id="selectEnterpriseAllStock" resultType="com.gk.firework.Domain.StockInfo">
        SELECT
               stock.*
        FROM stock
        LEFT JOIN enterprise on enterprise.id = stock.`owner`
        LEFT JOIN product on product.id = stock.productid and product.isdel = 0
        <where>
            `status` = ''
            and stock.`owner` != ''
            and enterprise.validflag = 1
            and enterprise.enterprisestatus = 'ON'
            and `owner` = #{owner}
        </where>
        <foreach collection="list" item="item" index="index">
            union all
            (
            SELECT
            stock.*
            FROM
            stock_slice${item} as stock
            LEFT JOIN enterprise on enterprise.id = stock.`owner`
            LEFT JOIN product on product.id = stock.productid and product.isdel = 0
            <where>
                `status` = ''
                and stock.`owner` != ''
                and enterprise.validflag = 1
                and enterprise.enterprisestatus = 'ON'
                and `owner` = #{owner}
            </where>
            )
        </foreach>
    </select>
</mapper>