<?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="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="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 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 and ifnull(d.delete_Flag,'0') !='1'
        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_depot dd on dh.AllocationProjectId=dd.id and ifnull(dd.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="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>
        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(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>
        and ifnull(delete_Flag,'0') !='1'
    </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
        INNER 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
        INNER 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.*, 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 and ifnull(d.delete_Flag,'0') !='1'
        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_depot dd on dh.AllocationProjectId=dd.id and ifnull(dd.delete_Flag,'0') !='1'
        where 1=1
        <if test="number != null">
            and dh.Number='${number}'
        </if>
        and ifnull(dh.delete_Flag,'0') !='1'
    </select>

    <insert id="adddepotHead" parameterType="com.jsh.erp.datasource.entities.DepotHead"
            useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into jsh_depothead (Type, SubType,
        ProjectId, DefaultNumber, Number,
        OperPersonName, CreateTime, OperTime,
        OrganId, HandsPersonId, AccountId,
        ChangeAmount, AllocationProjectId, TotalPrice,
        PayType, Remark, Salesman,
        AccountIdList, AccountMoneyList, Discount,
        DiscountMoney, DiscountLastMoney, OtherMoney,
        OtherMoneyList, OtherMoneyItem, AccountDay,
        Status, LinkNumber)
        values (#{type,jdbcType=VARCHAR}, #{subtype,jdbcType=VARCHAR},
        #{projectid,jdbcType=BIGINT}, #{defaultnumber,jdbcType=VARCHAR}, #{number,jdbcType=VARCHAR},
        #{operpersonname,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{opertime,jdbcType=TIMESTAMP},
        #{organid,jdbcType=BIGINT}, #{handspersonid,jdbcType=BIGINT}, #{accountid,jdbcType=BIGINT},
        #{changeamount,jdbcType=DECIMAL}, #{allocationprojectid,jdbcType=BIGINT}, #{totalprice,jdbcType=DECIMAL},
        #{paytype,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}, #{salesman,jdbcType=VARCHAR},
        #{accountidlist,jdbcType=VARCHAR}, #{accountmoneylist,jdbcType=VARCHAR}, #{discount,jdbcType=DECIMAL},
        #{discountmoney,jdbcType=DECIMAL}, #{discountlastmoney,jdbcType=DECIMAL}, #{othermoney,jdbcType=DECIMAL},
        #{othermoneylist,jdbcType=VARCHAR}, #{othermoneyitem,jdbcType=VARCHAR}, #{accountday,jdbcType=INTEGER},
        #{status,jdbcType=VARCHAR}, #{linknumber,jdbcType=VARCHAR})
    </insert>
    <update id="updatedepotHead" parameterType="com.jsh.erp.datasource.entities.DepotHead">
        update jsh_depothead
        <set>
            <if test="type != null">
                Type = #{type,jdbcType=VARCHAR},
            </if>
            <if test="subtype != null">
                SubType = #{subtype,jdbcType=VARCHAR},
            </if>
            <if test="projectid != null">
                ProjectId = #{projectid,jdbcType=BIGINT},
            </if>
            <if test="defaultnumber != null">
                DefaultNumber = #{defaultnumber,jdbcType=VARCHAR},
            </if>
            <if test="number != null">
                Number = #{number,jdbcType=VARCHAR},
            </if>
            <if test="operpersonname != null">
                OperPersonName = #{operpersonname,jdbcType=VARCHAR},
            </if>
            <if test="opertime != null">
                OperTime = #{opertime,jdbcType=TIMESTAMP},
            </if>
            <if test="organid != null">
                OrganId = #{organid,jdbcType=BIGINT},
            </if>
            <if test="handspersonid != null">
                HandsPersonId = #{handspersonid,jdbcType=BIGINT},
            </if>
            <if test="accountid != null">
                AccountId = #{accountid,jdbcType=BIGINT},
            </if>
            <if test="changeamount != null">
                ChangeAmount = #{changeamount,jdbcType=DECIMAL},
            </if>
            <if test="allocationprojectid != null">
                AllocationProjectId = #{allocationprojectid,jdbcType=BIGINT},
            </if>
            <if test="totalprice != null">
                TotalPrice = #{totalprice,jdbcType=DECIMAL},
            </if>
            <if test="paytype != null">
                PayType = #{paytype,jdbcType=VARCHAR},
            </if>
            <if test="remark != null">
                Remark = #{remark,jdbcType=VARCHAR},
            </if>
            <if test="salesman != null">
                Salesman = #{salesman,jdbcType=VARCHAR},
            </if>
            <if test="accountidlist != null">
                AccountIdList = #{accountidlist,jdbcType=VARCHAR},
            </if>
            <if test="accountmoneylist != null">
                AccountMoneyList = #{accountmoneylist,jdbcType=VARCHAR},
            </if>
            <if test="discount != null">
                Discount = #{discount,jdbcType=DECIMAL},
            </if>
            <if test="discountmoney != null">
                DiscountMoney = #{discountmoney,jdbcType=DECIMAL},
            </if>
            <if test="discountlastmoney != null">
                DiscountLastMoney = #{discountlastmoney,jdbcType=DECIMAL},
            </if>
            <if test="othermoney != null">
                OtherMoney = #{othermoney,jdbcType=DECIMAL},
            </if>
            <if test="othermoneylist != null">
                OtherMoneyList = #{othermoneylist,jdbcType=VARCHAR},
            </if>
            <if test="othermoneyitem != null">
                OtherMoneyItem = #{othermoneyitem,jdbcType=VARCHAR},
            </if>
            <if test="accountday != null">
                AccountDay = #{accountday,jdbcType=INTEGER},
            </if>
            <if test="status != null">
                Status = #{status,jdbcType=VARCHAR},
            </if>
            <if test="linknumber != null">
                LinkNumber = #{linknumber,jdbcType=VARCHAR},
            </if>
        </set>
        where Id = #{id,jdbcType=BIGINT}
    </update>
    <select id="getBuildOnlyNumber" resultType="java.lang.Long">
      select _nextval(#{seq_name}) from dual;
    </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>

</mapper>