<?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.jsh.erp.datasource.mappers.DepotHeadMapperEx">

    <resultMap extends="com.jsh.erp.datasource.mappers.DepotHeadMapper.BaseResultMap" id="ResultMapEx" type="com.jsh.erp.datasource.vo.DepotHeadVo4List">
        <result column="OrganName" jdbcType="VARCHAR" property="organName" />
        <result column="userName" jdbcType="VARCHAR" property="userName" />
        <result column="AccountName" jdbcType="VARCHAR" property="accountName" />
    </resultMap>

    <resultMap id="ResultWithInfoExMap" type="com.jsh.erp.datasource.vo.DepotHeadVo4InDetail">
        <result column="Number" jdbcType="VARCHAR" property="Number" />
        <result column="MName" jdbcType="VARCHAR" property="MName" />
        <result column="Model" jdbcType="VARCHAR" property="Model" />
        <result column="unit_price" jdbcType="DECIMAL" property="UnitPrice" />
        <result column="oper_number" jdbcType="DECIMAL" property="OperNumber" />
        <result column="all_price" jdbcType="DECIMAL" property="AllPrice" />
        <result column="SName" jdbcType="VARCHAR" property="SName" />
        <result column="DName" jdbcType="VARCHAR" property="DName" />
        <result column="OperTime" jdbcType="VARCHAR" property="OperTime" />
        <result column="NewType" jdbcType="VARCHAR" property="NewType" />
    </resultMap>

    <resultMap id="ResultInOutMaterialCount" type="com.jsh.erp.datasource.vo.DepotHeadVo4InOutMCount">
        <result column="MaterialId" jdbcType="BIGINT" property="MaterialId" />
        <result column="mName" jdbcType="VARCHAR" property="mName" />
        <result column="Model" jdbcType="VARCHAR" property="Model" />
        <result column="categoryName" jdbcType="VARCHAR" property="categoryName" />
        <result column="numSum" jdbcType="DECIMAL" property="numSum" />
        <result column="priceSum" jdbcType="DECIMAL" property="priceSum" />
    </resultMap>

    <resultMap id="ResultStatementAccount" type="com.jsh.erp.datasource.vo.DepotHeadVo4StatementAccount">
        <result column="number" jdbcType="VARCHAR" property="number" />
        <result column="type" jdbcType="VARCHAR" property="type" />
        <result column="discount_last_money" jdbcType="DECIMAL" property="discountLastMoney" />
        <result column="other_money" jdbcType="DECIMAL" property="otherMoney" />
        <result column="change_amount" jdbcType="DECIMAL" property="changeAmount" />
        <result column="supplierName" jdbcType="VARCHAR" property="supplierName" />
        <result column="oTime" jdbcType="VARCHAR" property="oTime" />
    </resultMap>

    <select id="selectByConditionDepotHead" parameterType="com.jsh.erp.datasource.entities.DepotHeadExample" resultMap="ResultMapEx">
        select distinct dh.*, s.supplier OrganName, u.username userName, a.name AccountName
        from jsh_depot_head dh
        left join jsh_supplier s on dh.organ_id=s.id and ifnull(s.delete_Flag,'0') !='1'
        left join jsh_user u on dh.creator=u.id and ifnull(u.Status,'0') ='0'
        left join jsh_account a on dh.account_id=a.id and ifnull(a.delete_Flag,'0') !='1'
        left join jsh_depot_item di on dh.id = di.header_id and ifnull(di.delete_flag,'0') !='1'
        left join jsh_material m on di.material_id = m.id and ifnull(m.delete_flag,'0') !='1'
        where 1=1
        <if test="type != null">
            and dh.type='${type}'
        </if>
        <if test="subType != null">
            and dh.sub_type='${subType}'
        </if>
        <if test="status != null">
            and dh.status ='${status}'
        </if>
        <if test="number != null">
            and dh.number like '%${number}%'
        </if>
        <if test="beginTime != null">
            and dh.oper_time >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and dh.oper_time &lt;= '${endTime}'
        </if>
        <if test="materialParam != null">
            and (m.`Name` like '%${materialParam}%' or m.Model like '%${materialParam}%')
        </if>
        <if test="depotIds != null">
            and di.depot_id in (${depotIds})
        </if>
        <if test="creatorArray != null">
            and dh.creator in (
            <foreach collection="creatorArray" item="creator" separator=",">
                #{creator}
            </foreach>
            )
        </if>
        and ifnull(dh.delete_flag,'0') !='1'
        order by dh.id desc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>
    <select id="countsByDepotHead" resultType="java.lang.Long">
        SELECT
        COUNT(1) from
        (select distinct dh.* FROM jsh_depot_head dh
        left join jsh_depot_item di on dh.Id = di.header_id and ifnull(di.delete_flag,'0') !='1'
        left join jsh_material m on di.material_id = m.Id and ifnull(m.delete_Flag,'0') !='1'
        WHERE 1=1
        <if test="type != null">
            and dh.type='${type}'
        </if>
        <if test="subType != null">
            and dh.sub_type='${subType}'
        </if>
        <if test="status != null">
            and dh.status ='${status}'
        </if>
        <if test="number != null">
            and dh.number like '%${number}%'
        </if>
        <if test="beginTime != null">
            and dh.oper_time >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and dh.oper_time &lt;= '${endTime}'
        </if>
        <if test="materialParam != null">
            and (m.`Name` like '%${materialParam}%' or m.Model like '%${materialParam}%')
        </if>
        <if test="depotIds != null">
            and di.depot_id in (${depotIds})
        </if>
        <if test="creatorArray != null">
            and dh.creator in (
            <foreach collection="creatorArray" item="creator" separator=",">
                #{creator}
            </foreach>
            )
        </if>
        and ifnull(dh.delete_Flag,'0') !='1') tb
    </select>

    <select id="findMaterialsListByHeaderId" resultType="java.lang.String">
        select group_concat(concat(jsh_material.`Name`,' ',jsh_material.Model)) as mName
        from jsh_depot_item
        inner join jsh_material on jsh_depot_item.material_id = jsh_material.Id and ifnull(jsh_material.delete_Flag,'0') !='1'
        where jsh_depot_item.header_id = ${id}
        and ifnull(jsh_depot_item.delete_flag,'0') !='1'
    </select>

    <select id="findByAll" parameterType="com.jsh.erp.datasource.entities.DepotItemExample" resultMap="ResultWithInfoExMap">
        select dh.number,m.`name` MName,m.model,di.unit_price,di.oper_number,di.all_price,s.supplier SName,d.dName DName,
        date_format(dh.oper_time, '%Y-%m-%d') OperTime, concat(dh.sub_type,dh.type) as NewType
        from jsh_depot_head dh
        inner join jsh_depot_item di on di.header_id=dh.id and ifnull(di.delete_flag,'0') !='1'
        inner join jsh_material m on m.id=di.material_id and ifnull(m.delete_flag,'0') !='1'
        inner join jsh_supplier s on s.id=dh.organ_id and ifnull(s.delete_Flag,'0') !='1'
        inner join (select id,name as dName,delete_Flag from jsh_depot ) d on d.id=di.depot_id and ifnull(d.delete_Flag,'0') !='1'
        where dh.oper_time >='${beginTime}' and dh.oper_time &lt;='${endTime}'
        <if test="oId != null">
            and dh.organ_id = ${oId}
        </if>
        <if test="pid != null">
            and di.depot_id = ${pid}
        </if>
        <if test="pid == null">
            and di.depot_id in (${dids})
        </if>
        <if test="type != null">
            and dh.type='${type}'
        </if>
        and ifnull(dh.delete_flag,'0') !='1'
        ORDER BY oper_time DESC,number desc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>

    <select id="findByAllCount" resultType="java.lang.Integer">
        select count(1)
        from jsh_depot_head dh
        inner join jsh_depot_item di on di.header_id=dh.id and ifnull(di.delete_flag,'0') !='1'
        inner join jsh_material m on m.id=di.material_id and ifnull(m.delete_Flag,'0') !='1'
        inner join jsh_supplier s on s.id=dh.organ_id and ifnull(s.delete_Flag,'0') !='1'
        inner join (select id,name as dName,delete_Flag from jsh_depot) d on d.id=di.depot_id and ifnull(d.delete_Flag,'0') !='1'
        where dh.oper_time >='${beginTime}' and dh.oper_time &lt;='${endTime}'
        <if test="oId != null">
            and dh.organ_id = ${oId}
        </if>
        <if test="pid != null">
            and di.depot_id = ${pid}
        </if>
        <if test="pid == null">
            and di.depot_id in (${dids})
        </if>
        <if test="type != null">
            and dh.type='${type}'
        </if>
        and ifnull(dh.delete_flag,'0') !='1'
        ORDER BY oper_time DESC,number desc
    </select>

    <select id="findInOutMaterialCount" parameterType="com.jsh.erp.datasource.entities.DepotItemExample" resultMap="ResultInOutMaterialCount">
        select di.material_id, m.mName,m.Model,m.categoryName,

        (select sum(jdi.basic_number) numSum from jsh_depot_head jdh
        INNER JOIN jsh_depot_item jdi on jdh.id=jdi.header_id and ifnull(jdi.delete_flag,'0') !='1'
        where jdi.material_id=di.material_id
        and jdh.type='${type}' and jdh.oper_time >='${beginTime}' and jdh.oper_time &lt;='${endTime}'
        <if test="oId != null">
            and jdh.organ_id = ${oId}
        </if>
        <if test="pid != null">
            and jdi.depot_id= ${pid}
        </if>
        <if test="pid == null">
            and jdi.depot_id in (${dids})
        </if>
        and ifnull(jdh.delete_flag,'0') !='1'
        ) numSum,

        (select sum(jdi.all_price) priceSum from jsh_depot_head jdh
        INNER JOIN jsh_depot_item jdi on jdh.id=jdi.header_id  and ifnull(jdi.delete_flag,'0') !='1'
        where jdi.material_id=di.material_id
        and jdh.type='${type}' and jdh.oper_time >='${beginTime}' and jdh.oper_time &lt;='${endTime}'
        <if test="oId != null">
            and jdh.organ_id = ${oId}
        </if>
        <if test="pid != null">
            and jdi.depot_id= ${pid}
        </if>
        <if test="pid == null">
            and jdi.depot_id in (${dids})
        </if>
        and ifnull(jdh.delete_flag,'0') !='1'
        ) priceSum
        from jsh_depot_head dh
        INNER JOIN jsh_depot_item di on dh.id=di.header_id and ifnull(di.delete_flag,'0') !='1'
        INNER JOIN (SELECT jsh_material.id,jsh_material.name mName, Model,jsh_material_category.`Name` categoryName
        from jsh_material
        LEFT JOIN jsh_material_category on jsh_material.category_id=jsh_material_category.Id and ifnull(jsh_material_category.status,'0') !='2'
        where ifnull(jsh_material.delete_Flag,'0') !='1'
        ) m
        on m.Id=di.material_id where dh.type='${type}' and dh.oper_time >='${beginTime}' and dh.oper_time &lt;='${endTime}'
        <if test="oId != null">
            and dh.organ_id = ${oId}
        </if>
        <if test="pid != null">
            and di.depot_id= ${pid}
        </if>
        <if test="pid == null">
            and di.depot_id in (${dids})
        </if>
        and ifnull(dh.delete_flag,'0') !='1'
        GROUP BY di.material_id,m.mName,m.Model,m.categoryName
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>

    <select id="findInOutMaterialCountTotal" resultType="java.lang.Integer">
        select count(1) from
        (select di.material_id, m.mName,m.Model,m.categoryName from jsh_depot_head dh
        INNER JOIN jsh_depot_item di on dh.id=di.header_id  and ifnull(di.delete_flag,'0') !='1'
        INNER JOIN (SELECT jsh_material.id,jsh_material.name mName, Model,jsh_material_category.`Name` categoryName
        from jsh_material
        LEFT JOIN jsh_material_category on jsh_material.category_id=jsh_material_category.Id and ifnull(jsh_material_category.status,'0') !='2'
        where ifnull(jsh_material.delete_Flag,'0') !='1'
        ) m on m.Id=di.material_id where dh.type='${type}' and dh.oper_time >='${beginTime}' and dh.oper_time &lt;='${endTime}'
        <if test="oId != null">
            and dh.organ_id = ${oId}
        </if>
        <if test="pid != null">
            and di.depot_id= ${pid}
        </if>
        <if test="pid == null">
            and di.depot_id in (${dids})
        </if>
        and ifnull(dh.delete_flag,'0') !='1'
        GROUP BY di.material_id,m.mName,m.Model,m.categoryName) a
    </select>

    <select id="findStatementAccount" parameterType="com.jsh.erp.datasource.entities.DepotItemExample" resultMap="ResultStatementAccount">
        select dh.number,concat(dh.sub_type,dh.type) as type,dh.discount_last_money,dh.other_money,dh.change_amount,s.supplier supplierName,
        date_format(dh.oper_time,'%Y-%m-%d %H:%i:%S') as oTime from jsh_depot_head dh
        inner join jsh_supplier s on s.id=dh.organ_id and ifnull(s.delete_Flag,'0') !='1'
        where s.type='${supType}' and (dh.sub_type!='其它' and dh.sub_type!='采购订单' and dh.sub_type!='销售订单')
        and dh.oper_time >='${beginTime}' and dh.oper_time &lt;='${endTime}'
        <if test="organId != null">
            and dh.organ_id=${organId}
        </if>
        and ifnull(dh.delete_flag,'0') !='1'
        UNION ALL
        select ah.bill_no number,ah.type as newType,ah.total_price discount_last_money, 0 other_money,ah.change_amount,s.supplier supplierName,
        date_format(ah.bill_time,'%Y-%m-%d %H:%i:%S') as oTime from jsh_account_head ah
        inner join jsh_supplier s on s.id=ah.organ_id and ifnull(s.delete_Flag,'0') !='1'
        where s.type='${supType}'
        and ah.bill_time >='${beginTime}' and ah.bill_time &lt;='${endTime}'
        <if test="organId != null">
            and ah.organ_id=${organId}
        </if>
        and ifnull(ah.delete_flag,'0') !='1'
        ORDER BY oTime
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>

    <select id="findStatementAccountCount" resultType="java.lang.Integer">
        select sum(a) from
        (
            select count(1) a from jsh_depot_head dh
            inner join jsh_supplier s on s.id=dh.organ_id and ifnull(s.delete_Flag,'0') !='1'
            where s.type='${supType}' and (dh.sub_type!='其它' and dh.sub_type!='采购订单' and dh.sub_type!='销售订单')
            and dh.oper_time >='${beginTime}' and dh.oper_time &lt;='${endTime}'
            <if test="organId != null">
                and dh.organ_id=${organId}
            </if>
            and ifnull(dh.delete_flag,'0') !='1'
            UNION ALL
            select count(1) a from jsh_account_head ah
            inner join jsh_supplier s on s.id=ah.organ_id and ifnull(s.delete_Flag,'0') !='1'
            where s.type='${supType}'
            and ah.bill_time >='${beginTime}' and ah.bill_time &lt;='${endTime}'
            <if test="organId != null">
                and ah.organ_id=${organId}
            </if>
            and ifnull(ah.delete_flag,'0') !='1'
        ) cc
    </select>

    <select id="findAllMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(${modeName}),0) as allMoney from jsh_depot_head where type='${type}' and sub_type = '${subType}'
        and organ_id =${supplierId} and oper_time &lt;='${endTime}'
        and ifnull(delete_flag,'0') !='1'
    </select>

    <select id="findAllOtherMoney" resultType="java.math.BigDecimal">
        select ifnull(sum(other_money),0) as allOtherMoney from jsh_depot_head where type='${type}' and sub_type = '${subType}'
        and organ_id =${supplierId} and oper_time &lt;='${endTime}'
        and ifnull(delete_flag,'0') !='1'
    </select>

    <select id="getDetailByNumber" parameterType="com.jsh.erp.datasource.entities.DepotHeadExample" resultMap="ResultMapEx">
        select dh.*, s.supplier OrganName, a.name AccountName
        from jsh_depot_head dh
        left join jsh_supplier s on dh.organ_id=s.id and ifnull(s.delete_Flag,'0') !='1'
        left join jsh_account a on dh.account_id=a.id and ifnull(a.delete_Flag,'0') !='1'
        where 1=1
        <if test="number != null">
            and dh.number='${number}'
        </if>
        and ifnull(dh.delete_flag,'0') !='1'
    </select>

    <update id="updateBuildOnlyNumber">
        update jsh_sequence set current_val = current_val + 1 where seq_name = 'depot_number_seq'
    </update>

    <select id="getBuildOnlyNumber" resultType="java.lang.Long">
      select current_val from jsh_sequence where seq_name = 'depot_number_seq'
    </select>

    <update id="batchDeleteDepotHeadByIds">
        update jsh_depot_head
        set delete_flag='1'
        where 1=1
        and id in (
        <foreach collection="ids" item="id" separator=",">
            #{id}
        </foreach>
        )
    </update>
    <select id="getDepotHeadListByAccountIds" resultMap="com.jsh.erp.datasource.mappers.DepotHeadMapper.BaseResultMap">
        select
        <include refid="com.jsh.erp.datasource.mappers.DepotHeadMapper.Base_Column_List" />
        from jsh_depot_head
        where 1=1
        and account_id in (
        <foreach collection="accountIds" item="accountId" separator=",">
            #{accountId}
        </foreach>
        )
        and ifnull(delete_flag,'0') !='1'
    </select>
    <select id="getDepotHeadListByOrganIds" resultMap="com.jsh.erp.datasource.mappers.DepotHeadMapper.BaseResultMap">
        select
        <include refid="com.jsh.erp.datasource.mappers.DepotHeadMapper.Base_Column_List" />
        from jsh_depot_head
        where 1=1
        and organ_id in (
        <foreach collection="organIds" item="organId" separator=",">
            #{organId}
        </foreach>
        )
        and ifnull(delete_flag,'0') !='1'
    </select>
    <select id="getDepotHeadListByCreator" resultMap="com.jsh.erp.datasource.mappers.DepotHeadMapper.BaseResultMap">
        select
        <include refid="com.jsh.erp.datasource.mappers.DepotHeadMapper.Base_Column_List" />
        from jsh_depot_head
        where 1=1
        and creator in (
        <foreach collection="creatorArray" item="creator" separator=",">
            #{creator}
        </foreach>
        )
        and ifnull(delete_flag,'0') !='1'
    </select>

    <select id="getBuyAndSaleStatistics" resultType="java.math.BigDecimal">
        SELECT
        ifnull(sum(discount_last_money),0)
        FROM jsh_depot_head
        WHERE 1=1
        <if test="type != null">
            and type='${type}'
        </if>
        <if test="subType != null">
            and sub_type='${subType}'
        </if>
        <if test="hasSupplier == 1">
            and organ_id is not null
        </if>
        <if test="hasSupplier == 0">
            and organ_id is null
        </if>
        <if test="beginTime != null">
            and oper_time >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and oper_time &lt;= '${endTime}'
        </if>
        and ifnull(delete_flag,'0') !='1'
    </select>

    <select id="getBuyAndSaleRetailStatistics" resultType="java.math.BigDecimal">
        SELECT
        ifnull(sum(total_price),0)
        FROM jsh_depot_head
        WHERE 1=1
        <if test="type != null">
            and type='${type}'
        </if>
        <if test="subType != null">
            and sub_type='${subType}'
        </if>
        <if test="hasSupplier == 1">
            and organ_id is not null
        </if>
        <if test="hasSupplier == 0">
            and organ_id is null
        </if>
        <if test="beginTime != null">
            and oper_time >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and oper_time &lt;= '${endTime}'
        </if>
        and ifnull(delete_flag,'0') !='1'
    </select>
</mapper>