<?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="HandsPersonName" jdbcType="VARCHAR" property="handsPersonName" />
        <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="UnitPrice" jdbcType="DECIMAL" property="UnitPrice" />
        <result column="OperNumber" jdbcType="DECIMAL" property="OperNumber" />
        <result column="AllPrice" 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="DiscountLastMoney" jdbcType="DECIMAL" property="discountLastMoney" />
        <result column="ChangeAmount" 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, p.name HandsPersonName, a.name AccountName
        from jsh_depothead dh
        left join jsh_supplier s on dh.OrganId=s.id and ifnull(s.delete_Flag,'0') !='1'
        left join jsh_person p on dh.HandsPersonId=p.id and ifnull(p.delete_Flag,'0') !='1'
        left join jsh_account a on dh.AccountId=a.id and ifnull(a.delete_Flag,'0') !='1'
        left join jsh_depotitem di on dh.Id = di.HeaderId and ifnull(di.delete_Flag,'0') !='1'
        left join jsh_material m on di.MaterialId = 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.SubType='${subType}'
        </if>
        <if test="number != null">
            and dh.Number like '%${number}%'
        </if>
        <if test="beginTime != null">
            and dh.OperTime >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and dh.OperTime &lt;= '${endTime}'
        </if>
        <if test="materialParam != null">
            and (m.`Name` like '%${materialParam}%' or m.Model like '%${materialParam}%')
        </if>
        <if test="depotIds != null">
            and di.DepotId in (${depotIds})
        </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 jsh_depothead.* FROM jsh_depothead
        left join jsh_depotitem di on jsh_depothead.Id = di.HeaderId and ifnull(di.delete_Flag,'0') !='1'
        left join jsh_material m on di.MaterialId = m.Id and ifnull(m.delete_Flag,'0') !='1'
        WHERE 1=1
        <if test="type != null">
            and Type='${type}'
        </if>
        <if test="subType != null">
            and SubType='${subType}'
        </if>
        <if test="number != null">
            and Number like '%${number}%'
        </if>
        <if test="beginTime != null">
            and OperTime >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and OperTime &lt;= '${endTime}'
        </if>
        <if test="materialParam != null">
            and (m.`Name` like '%${materialParam}%' or m.Model like '%${materialParam}%')
        </if>
        <if test="depotIds != null">
            and di.DepotId in (${depotIds})
        </if>
        and ifnull(jsh_depothead.delete_Flag,'0') !='1') tb
    </select>

    <select id="getMaxId" resultType="java.lang.Long">
        select max(Id) as Id from jsh_depothead
    </select>

    <select id="findMaterialsListByHeaderId" resultType="java.lang.String">
        select group_concat(concat(jsh_material.`Name`,' ',jsh_material.Model)) as mName
        from jsh_depotitem
        inner join jsh_material on jsh_depotitem.MaterialId = jsh_material.Id and ifnull(jsh_material.delete_Flag,'0') !='1'
        where jsh_depotitem.HeaderId = ${id}
        and ifnull(jsh_depotitem.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.UnitPrice,di.OperNumber,di.AllPrice,s.supplier SName,d.dName DName,
        date_format(dh.OperTime, '%Y-%m-%d') OperTime, concat(dh.SubType,dh.Type) as NewType
        from jsh_depothead dh
        inner join jsh_depotitem di on di.HeaderId=dh.id and ifnull(di.delete_Flag,'0') !='1'
        inner join jsh_material m on m.id=di.MaterialId and ifnull(m.delete_Flag,'0') !='1'
        inner join jsh_supplier s on s.id=dh.OrganId and ifnull(s.delete_Flag,'0') !='1'
        inner join (select id,name as dName,delete_Flag from jsh_depot ) d on d.id=di.DepotId and ifnull(d.delete_Flag,'0') !='1'
        where dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
        <if test="oId != null">
            and dh.OrganId = ${oId}
        </if>
        <if test="pid != null">
            and di.DepotId = ${pid}
        </if>
        <if test="pid == null">
            and di.DepotId in (${dids})
        </if>
        <if test="type != null">
            and dh.Type='${type}'
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
        ORDER BY OperTime 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_depothead dh
        inner join jsh_depotitem di on di.HeaderId=dh.id and ifnull(di.delete_Flag,'0') !='1'
        inner join jsh_material m on m.id=di.MaterialId and ifnull(m.delete_Flag,'0') !='1'
        inner join jsh_supplier s on s.id=dh.OrganId and ifnull(s.delete_Flag,'0') !='1'
        inner join (select id,name as dName,delete_Flag from jsh_depot) d on d.id=di.DepotId and ifnull(d.delete_Flag,'0') !='1'
        where dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
        <if test="oId != null">
            and dh.OrganId = ${oId}
        </if>
        <if test="pid != null">
            and di.DepotId = ${pid}
        </if>
        <if test="pid == null">
            and di.DepotId in (${dids})
        </if>
        <if test="type != null">
            and dh.Type='${type}'
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
        ORDER BY OperTime DESC,Number desc
    </select>

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

        (select sum(jdi.BasicNumber) numSum from jsh_depothead jdh
        INNER JOIN jsh_depotitem jdi on jdh.id=jdi.HeaderId and ifnull(jdi.delete_Flag,'0') !='1'
        where jdi.MaterialId=di.MaterialId
        and jdh.type='${type}' and jdh.OperTime >='${beginTime}' and jdh.OperTime &lt;='${endTime}'
        <if test="oId != null">
            and jdh.OrganId = ${oId}
        </if>
        <if test="pid != null">
            and jdi.DepotId= ${pid}
        </if>
        <if test="pid == null">
            and jdi.DepotId in (${dids})
        </if>
        and ifnull(jdh.delete_Flag,'0') !='1'
        ) numSum,

        (select sum(jdi.AllPrice) priceSum from jsh_depothead jdh
        INNER JOIN jsh_depotitem jdi on jdh.id=jdi.HeaderId  and ifnull(jdi.delete_Flag,'0') !='1'
        where jdi.MaterialId=di.MaterialId
        and jdh.type='${type}' and jdh.OperTime >='${beginTime}' and jdh.OperTime &lt;='${endTime}'
        <if test="oId != null">
            and jdh.OrganId = ${oId}
        </if>
        <if test="pid != null">
            and jdi.DepotId= ${pid}
        </if>
        <if test="pid == null">
            and jdi.DepotId in (${dids})
        </if>
        and ifnull(jdh.delete_Flag,'0') !='1'
        ) priceSum
        from jsh_depothead dh
        INNER JOIN jsh_depotitem di on dh.id=di.HeaderId and ifnull(di.delete_Flag,'0') !='1'
        INNER JOIN (SELECT jsh_material.id,jsh_material.name mName, Model,jsh_materialcategory.`Name` categoryName
        from jsh_material
        LEFT JOIN jsh_materialcategory on jsh_material.CategoryId=jsh_materialcategory.Id and ifnull(jsh_materialcategory.status,'0') !='2'
        where ifnull(jsh_material.delete_Flag,'0') !='1'
        ) m
        on m.Id=di.MaterialId where dh.type='${type}' and dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
        <if test="oId != null">
            and dh.OrganId = ${oId}
        </if>
        <if test="pid != null">
            and di.DepotId= ${pid}
        </if>
        <if test="pid == null">
            and di.DepotId in (${dids})
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
        GROUP BY di.MaterialId,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.MaterialId, m.mName,m.Model,m.categoryName from jsh_depothead dh
        INNER JOIN jsh_depotitem di on dh.id=di.HeaderId  and ifnull(di.delete_Flag,'0') !='1'
        INNER JOIN (SELECT jsh_material.id,jsh_material.name mName, Model,jsh_materialcategory.`Name` categoryName
        from jsh_material
        LEFT JOIN jsh_materialcategory on jsh_material.CategoryId=jsh_materialcategory.Id and ifnull(jsh_materialcategory.status,'0') !='2'
        where ifnull(jsh_material.delete_Flag,'0') !='1'
        ) m on m.Id=di.MaterialId where dh.type='${type}' and dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
        <if test="oId != null">
            and dh.OrganId = ${oId}
        </if>
        <if test="pid != null">
            and di.DepotId= ${pid}
        </if>
        <if test="pid == null">
            and di.DepotId in (${dids})
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
        GROUP BY di.MaterialId,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.SubType,dh.Type) as type,dh.DiscountLastMoney,dh.ChangeAmount,s.supplier supplierName,
        date_format(dh.OperTime,'%Y-%m-%d %H:%i:%S') as oTime from jsh_depothead dh
        inner join jsh_supplier s on s.id=dh.OrganId and ifnull(s.delete_Flag,'0') !='1'
        where s.type='${supType}' and (dh.SubType!='其它' and dh.SubType!='采购订单' and dh.SubType!='销售订单')
        and dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
        <if test="organId != null">
            and dh.OrganId=${organId}
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
        UNION ALL
        select ah.BillNo Number,ah.Type as newType,ah.TotalPrice DiscountLastMoney,ah.ChangeAmount,s.supplier supplierName,
        date_format(ah.BillTime,'%Y-%m-%d %H:%i:%S') as oTime from jsh_accounthead ah
        inner join jsh_supplier s on s.id=ah.OrganId and ifnull(s.delete_Flag,'0') !='1'
        where s.type='${supType}'
        and ah.BillTime >='${beginTime}' and ah.BillTime &lt;='${endTime}'
        <if test="organId != null">
            and ah.OrganId=${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_depothead dh
            inner join jsh_supplier s on s.id=dh.OrganId and ifnull(s.delete_Flag,'0') !='1'
            where s.type='${supType}' and (dh.SubType!='其它' and dh.SubType!='采购订单' and dh.SubType!='销售订单')
            and dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
            <if test="organId != null">
                and dh.OrganId=${organId}
            </if>
            and ifnull(dh.delete_Flag,'0') !='1'
            UNION ALL
            select count(1) a from jsh_accounthead ah
            inner join jsh_supplier s on s.id=ah.OrganId and ifnull(s.delete_Flag,'0') !='1'
            where s.type='${supType}'
            and ah.BillTime >='${beginTime}' and ah.BillTime &lt;='${endTime}'
            <if test="organId != null">
                and ah.OrganId=${organId}
            </if>
            and ifnull(ah.delete_Flag,'0') !='1'
        ) cc
    </select>

    <select id="findAllMoney" resultType="java.math.BigDecimal">
        select sum(${modeName}) as allMoney from jsh_depothead where Type='${type}' and SubType = '${subType}'
        and OrganId =${supplierId} and OperTime &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, p.name HandsPersonName, a.name AccountName
        from jsh_depothead dh
        left join jsh_supplier s on dh.OrganId=s.id and ifnull(s.delete_Flag,'0') !='1'
        left join jsh_person p on dh.HandsPersonId=p.id and ifnull(p.delete_Flag,'0') !='1'
        left join jsh_account a on dh.AccountId=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_depothead
        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_depothead
        where 1=1
        and accountId 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_depothead
        where 1=1
        and organId in (
        <foreach collection="organIds" item="organId" separator=",">
            #{organId}
        </foreach>
        )
        and ifnull(delete_Flag,'0') !='1'
    </select>
    <select id="getDepotHeadListByHandsPersonIds" resultMap="com.jsh.erp.datasource.mappers.DepotHeadMapper.BaseResultMap">
        select
        <include refid="com.jsh.erp.datasource.mappers.DepotHeadMapper.Base_Column_List" />
        from jsh_depothead
        where 1=1
        and handsPersonId in (
        <foreach collection="handsPersonIds" item="handsPersonId" separator=",">
            #{handsPersonId}
        </foreach>
        )
        and ifnull(delete_Flag,'0') !='1'
    </select>

    <select id="getBuyAndSaleStatistics" resultType="java.math.BigDecimal">
        SELECT
        ifnull(sum(DiscountLastMoney),0)
        FROM jsh_depothead
        WHERE 1=1
        <if test="type != null">
            and Type='${type}'
        </if>
        <if test="subType != null">
            and SubType='${subType}'
        </if>
        <if test="hasSupplier == 1">
            and OrganId is not null
        </if>
        <if test="hasSupplier == 0">
            and OrganId is null
        </if>
        <if test="beginTime != null">
            and OperTime >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and OperTime &lt;= '${endTime}'
        </if>
        and ifnull(delete_Flag,'0') !='1'
    </select>

    <select id="getBuyAndSaleRetailStatistics" resultType="java.math.BigDecimal">
        SELECT
        ifnull(sum(TotalPrice),0)
        FROM jsh_depothead
        WHERE 1=1
        <if test="type != null">
            and Type='${type}'
        </if>
        <if test="subType != null">
            and SubType='${subType}'
        </if>
        <if test="hasSupplier == 1">
            and OrganId is not null
        </if>
        <if test="hasSupplier == 0">
            and OrganId is null
        </if>
        <if test="beginTime != null">
            and OperTime >= '${beginTime}'
        </if>
        <if test="endTime != null">
            and OperTime &lt;= '${endTime}'
        </if>
        and ifnull(delete_Flag,'0') !='1'
    </select>
</mapper>