<?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.DepotHeadMapper">

    <resultMap extends="BaseResultMap" id="ResultMapEx" type="com.jsh.erp.datasource.vo.DepotHeadVo4List">
        <result column="ProjectName" jdbcType="VARCHAR" property="projectName" />
        <result column="OrganName" jdbcType="VARCHAR" property="organName" />
        <result column="HandsPersonName" jdbcType="VARCHAR" property="handsPersonName" />
        <result column="AccountName" jdbcType="VARCHAR" property="accountName" />
        <result column="AllocationProjectName" jdbcType="VARCHAR" property="allocationProjectName" />
    </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="DOUBLE" property="UnitPrice" />
        <result column="OperNumber" jdbcType="DOUBLE" property="OperNumber" />
        <result column="AllPrice" jdbcType="DOUBLE" 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="DOUBLE" property="numSum" />
        <result column="priceSum" jdbcType="DOUBLE" 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="DOUBLE" property="discountLastMoney" />
        <result column="ChangeAmount" jdbcType="DOUBLE" 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 dh.*, d.name ProjectName, s.supplier OrganName, p.name HandsPersonName, a.name AccountName, dd.name AllocationProjectName
        from jsh_depothead dh
        left join jsh_depot d on dh.ProjectId=d.id
        left join jsh_supplier s on dh.OrganId=s.id
        left join jsh_person p on dh.HandsPersonId=p.id
        left join jsh_account a on dh.AccountId=a.id
        left join jsh_depot dd on dh.AllocationProjectId=dd.id
        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="dhIds != null">
            and dh.Id in (${dhIds})
        </if>
        <if test="beginTime != null">
            and dh.OperTime >= (${beginTime})
        </if>
        <if test="endTime != null">
            and dh.OperTime &lt;= (${endTime})
        </if>
        order by dh.Id desc
        <if test="offset != null and rows != null">
            limit #{offset},#{rows}
        </if>
    </select>
    <select id="countsByDepotHead" resultType="java.lang.Integer">
        SELECT
        COUNT(id)
        FROM jsh_depothead
        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="dhIds != null">
            and Id in (${dhIds})
        </if>
        <if test="beginTime != null">
            and OperTime >= (${beginTime})
        </if>
        <if test="endTime != null">
            and OperTime &lt;= (${endTime})
        </if>
    </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 where jsh_depotitem.HeaderId = ${id}
    </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
        inner join jsh_material m on m.id=di.MaterialId
        inner join jsh_supplier s on s.id=dh.OrganId
        inner join (select id,name as dName from jsh_depot) d on d.id=di.DepotId
        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>
        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
        inner join jsh_material m on m.id=di.MaterialId
        inner join jsh_supplier s on s.id=dh.OrganId
        inner join (select id,name as dName from jsh_depot) d on d.id=di.DepotId
        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>
        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 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>
        ) numSum,

        (select sum(jdi.AllPrice) priceSum from jsh_depothead jdh INNER JOIN jsh_depotitem jdi
        on jdh.id=jdi.HeaderId 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>
        ) priceSum
        from jsh_depothead dh INNER JOIN jsh_depotitem di on dh.id=di.HeaderId
        INNER JOIN (SELECT jsh_material.id,jsh_material.name mName, Model,jsh_materialcategory.`Name` categoryName
        from jsh_material INNER JOIN jsh_materialcategory on jsh_material.CategoryId=jsh_materialcategory.Id) 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>
        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 jsh_depothead dh INNER JOIN jsh_depotitem di on dh.id=di.HeaderId
        INNER JOIN (SELECT jsh_material.id,jsh_material.name mName, Model,jsh_materialcategory.`Name` categoryName
        from jsh_material INNER JOIN jsh_materialcategory on jsh_material.CategoryId=jsh_materialcategory.Id) 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>
    </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 where s.type='${supType}' and dh.SubType!='其它'
        and dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
        <if test="organId != null">
            and dh.OrganId=${organId}
        </if>
        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 where s.type='${supType}'
        and ah.BillTime >='${beginTime}' and ah.BillTime &lt;='${endTime}'
        <if test="organId != null">
            and ah.OrganId=${organId}
        </if>
        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 where s.type='${supType}' and dh.SubType!='其它'
            and dh.OperTime >='${beginTime}' and dh.OperTime &lt;='${endTime}'
            <if test="organId != null">
                and dh.OrganId=${organId}
            </if>
            UNION ALL
            select count(1) a from jsh_accounthead ah
            inner join jsh_supplier s on s.id=ah.OrganId where s.type='${supType}'
            and ah.BillTime >='${beginTime}' and ah.BillTime &lt;='${endTime}'
            <if test="organId != null">
                and ah.OrganId=${organId}
            </if>
        ) cc
    </select>

    <select id="findAllMoney" resultType="java.lang.Double">
        select sum(${modeName}) as allMoney from jsh_depothead where Type='${type}' and SubType = '${subType}'
        and OrganId =${supplierId} and OperTime &lt;='${endTime}'
    </select>

    <select id="getDetailByNumber" parameterType="com.jsh.erp.datasource.entities.DepotHeadExample" resultMap="ResultMapEx">
        select dh.*, d.name ProjectName, s.supplier OrganName, p.name HandsPersonName, a.name AccountName, dd.name AllocationProjectName
        from jsh_depothead dh
        left join jsh_depot d on dh.ProjectId=d.id
        left join jsh_supplier s on dh.OrganId=s.id
        left join jsh_person p on dh.HandsPersonId=p.id
        left join jsh_account a on dh.AccountId=a.id
        left join jsh_depot dd on dh.AllocationProjectId=dd.id
        where 1=1
        <if test="number != null">
            and dh.Number='${number}'
        </if>
    </select>

</mapper>