chenlu
2024-09-25 430ab98c6225a276bab44c5eebb304fc6206cb73
north-glass-erp/src/main/resources/mapper/mm/MaterialInventory.xml
New file
@@ -0,0 +1,917 @@
<?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.example.erp.mapper.mm.MaterialInventoryMapper">
    <resultMap id="selectMaterialOutboundDetail" type="com.example.erp.entity.mm.MaterialOutboundDetail" >
        <id column="id" property="id"/>
        <result column="material_outbound_id" property="materialOutboundId"/>
        <result column="material_outbound_number" property="materialOutboundNumber"/>
        <result column="inventory_id" property="inventoryId"/>
        <result column="inventory_organization" property="inventoryOrganization"/>
        <result column="material_code" property="materialCode"/>
        <result column="material_name" property="materialName"/>
        <result column="use_id" property="useId"/>
        <result column="producer" property="producer"/>
        <result column="unit" property="unit"/>
        <result column="width" property="width"/>
        <result column="height" property="height"/>
        <result column="thickness" property="thickness"/>
        <result column="outbound_quantity" property="outboundQuantity"/>
        <result column="single_piece_area" property="singlePieceArea"/>
        <result column="inventory_area" property="inventoryArea"/>
        <result column="date_of_manufacture" property="dateOfManufacture"/>
        <result column="remarks" property="remarks"/>
        <result column="material_requisition_personnel" property="materialOutbound.materialRequisitionPersonnel"/>
        <result column="material_requisition_team" property="materialOutbound.materialRequisitionTeam"/>
        <result column="order_id" property="materialOutbound.orderId"/>
        <result column="outbound_type" property="materialOutbound.outboundType"/>
        <result column="warehouse_manager" property="materialOutbound.warehouseManager"/>
        <result column="material_requisition_date" property="materialOutbound.materialRequisitionDate"/>
        <result column="reviewed" property="materialOutbound.reviewed"/>
        <result column="reviewed_time" property="materialOutbound.reviewedTime"/>
    </resultMap>
    <select id="getSelectMaterialInventoryDate">
        select
        mi.id,
        mi.inventory_organization as inventoryOrganization,
        mi.material_code AS materialCode,
        mi.producer,
        mi.inventory_quantity AS inventoryQuantity,
        mi.available_quantity AS availableQuantity,
        mi.plan_quantity AS planQuantity,
        mi.safety_stock AS safetyStock,
        mi.total_area AS totalArea,
        mi.single_piece_area singlePieceArea,
        date(mi.date_of_manufacture) AS dateOfManufacture,
        mi.quality_guarantee_period qualityGuaranteePeriod,
        mi.inventory_area AS inventoryArea,
        mi.dead_stock AS deadStock,
        mi.remarks,
        date(mi.create_time) AS createTime,
        ms.json
        from mm.material_inventory mi left join mm.material_store ms on mi.material_code=ms.id
        <where>
                date(mi.create_time)>=#{startDate} and date(mi.create_time) &lt;= #{endDate} and mi.available_quantity>0
        </where>
        order by mi.id desc
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialInventoryDatePageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        mm.material_inventory mi
        <where>
                date(mi.create_time)>=#{startDate} and date(mi.create_time) &lt;= #{endDate} and mi.available_quantity>0
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialInventoryEngineeringDate">
        select
        ou.id as useId,
        ou.project_no as projectNo,
        mi.inventory_quantity AS inventoryQuantity,
        mi.plan_quantity AS availableQuantity,
        mi.plan_quantity AS planQuantity,
        mi.id,
        mi.inventory_organization as inventoryOrganization,
        mi.material_code AS materialCode,
        mi.producer,
        mi.safety_stock AS safetyStock,
        mi.total_area AS totalArea,
        mi.single_piece_area singlePieceArea,
        date(mi.date_of_manufacture) AS dateOfManufacture,
        mi.quality_guarantee_period qualityGuaranteePeriod,
        mi.inventory_area AS inventoryArea,
        mi.dead_stock AS deadStock,
        mi.remarks,
        date(mi.create_time) AS createTime,
        ms.json from pp.optimize_use ou left join mm.material_inventory mi on ou.raw_stock_code=mi.id left join mm.material_store ms on mi.material_code=ms.id
        left join pp.optimize_project op on op.project_no=ou.project_no
        <where>
            mi.plan_quantity>0 and op.state>=100 and
            date(ou.create_time)>=#{startDate} and date(ou.create_time) &lt;= #{endDate}
        </where>
        order by ou.id desc
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialInventoryEngineeringDatePageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        pp.optimize_use ou
        <where>
            ou.not_use_count>0 and ou.state=1 and
            date(ou.create_time)>=#{startDate} and date(ou.create_time) &lt;= #{endDate}
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialInventory">
        select
        mi.id,
        mi.inventory_organization as inventoryOrganization,
        mi.material_code AS materialCode,
        mi.producer,
        mi.inventory_quantity AS inventoryQuantity,
        mi.available_quantity AS availableQuantity,
        mi.plan_quantity AS planQuantity,
        mi.safety_stock AS safetyStock,
        mi.total_area AS totalArea,
        mi.single_piece_area singlePieceArea,
        date(mi.date_of_manufacture) AS dateOfManufacture,
        mi.quality_guarantee_period qualityGuaranteePeriod,
        mi.inventory_area AS inventoryArea,
        mi.dead_stock AS deadStock,
        mi.remarks,
        date(mi.create_time) AS createTime,
        ms.json
        from mm.material_inventory mi left join mm.material_store ms on mi.material_code=ms.id
        <where>
            <if test="materialInventory.stockId != null and materialInventory.stockId != ''">
                and mi.id in
                <foreach item="id" collection="ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialInventoryPageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        mm.material_inventory mi
        <where>
            <if test="materialInventory.id != null and materialInventory.id != ''">
                and mi.id regexp #{materialInventory.id}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialInventoryEngineering">
        select
        ou.id as useId,
        ou.project_no as projectNo,
        mi.inventory_quantity AS inventoryQuantity,
        mi.plan_quantity AS availableQuantity,
        mi.plan_quantity AS planQuantity,
        mi.id,
        mi.inventory_organization as inventoryOrganization,
        mi.material_code AS materialCode,
        mi.producer,
        mi.safety_stock AS safetyStock,
        mi.total_area AS totalArea,
        mi.single_piece_area singlePieceArea,
        date(mi.date_of_manufacture) AS dateOfManufacture,
        mi.quality_guarantee_period qualityGuaranteePeriod,
        mi.inventory_area AS inventoryArea,
        mi.dead_stock AS deadStock,
        mi.remarks,
        date(mi.create_time) AS createTime,
        ms.json from pp.optimize_use ou left join mm.material_inventory mi on ou.raw_stock_code=mi.id left join mm.material_store ms on mi.material_code=ms.id
        <where>
            <if test="optimizeUse.rawStockCode != null and optimizeUse.rawStockCode != ''">
                and ou.id in
                <foreach item="id" collection="ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialInventoryEngineeringPageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        pp.optimize_use ou
        <where>
            <if test="optimizeUse.rawStockCode != null and optimizeUse.rawStockCode != ''">
                and ou.id in
                <foreach item="id" collection="ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialOutboundDate">
        select
        *
        from
        mm.material_outbound mo
        <where>
            date(mo.create_time)>=#{startDate} and date(mo.create_time) &lt;= #{endDate}
            <if test="materialOutbound.materialOutboundId != null and materialOutbound.materialOutboundId != ''">
                and mo.material_outbound_id regexp #{materialOutbound.materialOutboundId}
            </if>
            <if test="materialOutbound.materialRequisitionPersonnel != null and materialOutbound.materialRequisitionPersonnel != ''">
                and mo.material_requisition_personnel regexp #{materialOutbound.materialRequisitionPersonnel}
            </if>
            <if test="materialOutbound.materialRequisitionTeam != null and materialOutbound.materialRequisitionTeam != ''">
                and mo.material_requisition_team regexp #{materialOutbound.materialRequisitionTeam}
            </if>
            <if test="materialOutbound.orderId != null and materialOutbound.orderId != ''">
                and mo.order_id regexp #{materialOutbound.orderId}
            </if>
            <if test="materialOutbound.outboundType != null and materialOutbound.outboundType != ''">
                and mo.outbound_type regexp #{materialOutbound.outboundType}
            </if>
            <if test="materialOutbound.warehouseManager != null and materialOutbound.warehouseManager != ''">
                and mo.warehouse_manager regexp #{materialOutbound.warehouseManager}
            </if>
            <if test="materialOutbound.reviewed != null and materialOutbound.reviewed != ''">
                and mo.reviewed regexp #{materialOutbound.reviewed}
            </if>
        </where>
        order by mo.reviewed_state desc,mo.create_time desc
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialOutboundDatePageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        mm.material_outbound mo
        <where>
            date(mo.create_time)>=#{startDate} and date(mo.create_time) &lt;= #{endDate}
            <if test="materialOutbound.materialOutboundId != null and materialOutbound.materialOutboundId != ''">
                and mo.material_outbound_id regexp #{materialOutbound.materialOutboundId}
            </if>
            <if test="materialOutbound.materialRequisitionPersonnel != null and materialOutbound.materialRequisitionPersonnel != ''">
                and mo.material_requisition_personnel regexp #{materialOutbound.materialRequisitionPersonnel}
            </if>
            <if test="materialOutbound.materialRequisitionTeam != null and materialOutbound.materialRequisitionTeam != ''">
                and mo.material_requisition_team regexp #{materialOutbound.materialRequisitionTeam}
            </if>
            <if test="materialOutbound.orderId != null and materialOutbound.orderId != ''">
                and mo.order_id regexp #{materialOutbound.orderId}
            </if>
            <if test="materialOutbound.outboundType != null and materialOutbound.outboundType != ''">
                and mo.outbound_type regexp #{materialOutbound.outboundType}
            </if>
            <if test="materialOutbound.warehouseManager != null and materialOutbound.warehouseManager != ''">
                and mo.warehouse_manager regexp #{materialOutbound.warehouseManager}
            </if>
            <if test="materialOutbound.reviewed != null and materialOutbound.reviewed != ''">
                and mo.reviewed regexp #{materialOutbound.reviewed}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectReturningWarehouseDate">
        select
        *
        from
        mm.returning_warehouse rw
        <where>
            date(rw.create_time)>=#{startDate} and date(rw.create_time) &lt;= #{endDate}
            <if test="returningWarehouse.returningId != null and returningWarehouse.returningId != ''">
                and rw.returning_id regexp #{returningWarehouse.returningId}
            </if>
            <if test="returningWarehouse.materialRequisitionPersonnel != null and returningWarehouse.materialRequisitionPersonnel != ''">
                and rw.material_requisition_personnel regexp #{returningWarehouse.materialRequisitionPersonnel}
            </if>
            <if test="returningWarehouse.materialRequisitionTeam != null and returningWarehouse.materialRequisitionTeam != ''">
                and rw.material_requisition_team regexp #{returningWarehouse.materialRequisitionTeam}
            </if>
            <if test="returningWarehouse.orderId != null and returningWarehouse.orderId != ''">
                and rw.order_id regexp #{returningWarehouse.orderId}
            </if>
            <if test="returningWarehouse.returningType != null and returningWarehouse.returningType != ''">
                and rw.returning_type regexp #{returningWarehouse.returningType}
            </if>
            <if test="returningWarehouse.warehouseManager != null and returningWarehouse.warehouseManager != ''">
                and rw.warehouse_manager regexp #{returningWarehouse.warehouseManager}
            </if>
            <if test="returningWarehouse.reviewed != null and returningWarehouse.reviewed != ''">
                and rw.reviewed regexp #{returningWarehouse.reviewed}
            </if>
            <if test="returningWarehouse.project != null and returningWarehouse.project != ''">
                and rw.project regexp #{returningWarehouse.project}
            </if>
            <if test="returningWarehouse.batch != null and returningWarehouse.batch != ''">
                and rw.batch regexp #{returningWarehouse.batch}
            </if>
        </where>
        order by rw.reviewed_state desc,rw.create_time desc
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectReturningWarehouseDatePageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        mm.returning_warehouse rw
        <where>
            date(rw.create_time)>=#{startDate} and date(rw.create_time) &lt;= #{endDate}
            <if test="returningWarehouse.returningId != null and returningWarehouse.returningId != ''">
                and rw.returning_id regexp #{returningWarehouse.returningId}
            </if>
            <if test="returningWarehouse.materialRequisitionPersonnel != null and returningWarehouse.materialRequisitionPersonnel != ''">
                and rw.material_requisition_personnel regexp #{returningWarehouse.materialRequisitionPersonnel}
            </if>
            <if test="returningWarehouse.materialRequisitionTeam != null and returningWarehouse.materialRequisitionTeam != ''">
                and rw.material_requisition_team regexp #{returningWarehouse.materialRequisitionTeam}
            </if>
            <if test="returningWarehouse.orderId != null and returningWarehouse.orderId != ''">
                and rw.order_id regexp #{returningWarehouse.orderId}
            </if>
            <if test="returningWarehouse.returningType != null and returningWarehouse.returningType != ''">
                and rw.returning_type regexp #{returningWarehouse.returningType}
            </if>
            <if test="returningWarehouse.warehouseManager != null and returningWarehouse.warehouseManager != ''">
                and rw.warehouse_manager regexp #{returningWarehouse.warehouseManager}
            </if>
            <if test="returningWarehouse.reviewed != null and returningWarehouse.reviewed != ''">
                and rw.reviewed regexp #{returningWarehouse.reviewed}
            </if>
            <if test="returningWarehouse.project != null and returningWarehouse.project != ''">
                and rw.project regexp #{returningWarehouse.project}
            </if>
            <if test="returningWarehouse.batch != null and returningWarehouse.batch != ''">
                and rw.batch regexp #{returningWarehouse.batch}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialOutbound">
        select
        *
        from
        mm.material_outbound mo
        <where>
            <if test="materialOutboundDetail.materialOutboundId != null and materialOutboundDetail.materialOutboundId != ''">
                and mo.material_outbound_id regexp #{materialOutboundDetail.materialOutboundId}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="SelectMaterialOutboundDetail" >
        select
        *
        from mm.material_outbound_detail modd
        <where>
            <if test="materialOutboundDetail.materialOutboundId != null and materialOutboundDetail.materialOutboundId != ''">
                and modd.material_outbound_id regexp #{materialOutboundDetail.materialOutboundId}
            </if>
        </where>
    </select>
    <select id="getSelectMaterialOutboundDetail" >
        select
        modd.use_id as useId,
        modd.inventory_id as id,
        modd.inventory_organization as inventoryOrganization,
        modd.material_code as materialCode,
        modd.material_name as materialName,
        modd.producer,
        modd.width,
        modd.height,
        modd.thickness,
        modd.unit,
        modd.outbound_quantity as outboundQuantity,
        date(modd.date_of_manufacture) as dateOfManufacture,
        modd.single_piece_area as singlePieceArea,
        modd.inventory_area as inventoryArea,
        modd.remarks,
        mi.inventory_quantity as inventoryQuantity,
        if(mo.reviewed_state!=1,modd.outbound_quantity+mi.available_quantity,mi.available_quantity) as availableQuantity,
        ms.json
        from mm.material_outbound_detail modd left join mm.material_inventory mi on modd.inventory_id=mi.id
            left join mm.material_outbound mo on modd.material_outbound_id=mo.material_outbound_id
            left join mm.material_store ms on modd.material_code=ms.id
        <where>
            <if test="materialOutboundDetail.materialOutboundId != null and materialOutboundDetail.materialOutboundId != ''">
                and modd.material_outbound_id regexp #{materialOutboundDetail.materialOutboundId}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialOutboundEngineering" >
        select
        ou.project_no as  projectNo,
        modd.inventory_id as id,
        modd.inventory_organization as inventoryOrganization,
        modd.material_code as materialCode,
        modd.material_name as materialName,
        modd.producer,
        modd.width,
        modd.height,
        modd.thickness,
        modd.unit,
        modd.outbound_quantity as outboundQuantity,
        date(modd.date_of_manufacture) as dateOfManufacture,
        modd.single_piece_area as singlePieceArea,
        modd.inventory_area as inventoryArea,
        modd.remarks,
        ou.use_count as inventoryQuantity,
        if(mo.reviewed_state!=1,modd.outbound_quantity+ou.not_use_count,ou.not_use_count) as availableQuantity,
        ms.json
        from mm.material_outbound_detail modd left join pp.optimize_use ou on modd.use_id=ou.id
        left join mm.material_outbound mo on modd.material_outbound_id=mo.material_outbound_id
        left join mm.material_store ms on modd.material_code=ms.id
        <where>
            <if test="materialOutboundDetail.materialOutboundId != null and materialOutboundDetail.materialOutboundId != ''">
                and modd.material_outbound_id regexp #{materialOutboundDetail.materialOutboundId}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectMaterialOutboundDetailPageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        mm.material_outbound_detail modd
        <where>
            <if test="materialOutboundDetail.materialOutboundId != null and materialOutboundDetail.materialOutboundId != ''">
                and modd.material_outbound_id regexp #{materialOutboundDetail.materialOutboundId}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectReturningWarehouse">
        select
        *
        from
        mm.returning_warehouse rw
        <where>
            <if test="returningWarehouseDetail.returningId != null and returningWarehouseDetail.returningId != ''">
                and returning_id regexp #{returningWarehouseDetail.returningId}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectReturningWarehouseDetail" >
        select
        rwd.inventory_id as inventoryId,
        mi.inventory_organization as inventoryOrganization,
        rwd.material_code as id,
        mi.producer,
        rwd.return_quantity as returnQuantity,
        date(mi.date_of_manufacture) as dateOfManufacture,
        mi.single_piece_area as singlePieceArea,
        mi.inventory_area as inventoryArea,
        rwd.remarks,
        mi.inventory_quantity as inventoryQuantity,
        ms.json
        from mm.returning_warehouse_detail rwd left join mm.material_inventory mi on rwd.inventory_id=mi.id
        left join mm.returning_warehouse rw on rwd.returning_id=rw.returning_id
        left join mm.material_store ms on rwd.material_code=ms.id
        <where>
            <if test="returningWarehouseDetail.returningId != null and returningWarehouseDetail.returningId != ''">
                and rwd.returning_id regexp #{returningWarehouseDetail.returningId}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectReturningWarehouseDetailPageTotal">
        select
        CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from
        mm.returning_warehouse_detail rwd
        <where>
            <if test="returningWarehouseDetail.returningId != null and returningWarehouseDetail.returningId != ''">
                and rwd.returning_id regexp #{returningWarehouseDetail.returningId}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <insert id="insertMaterialStore"  useGeneratedKeys="true"  >
        insert into mm.material_store (type,json,create_time
        )
        values (
                   #{type},#{json},now()
               )
    </insert>
    <update id="updateMaterialStore" >
        update mm.material_store set json=#{json} where id=#{id}
    </update>
    <delete id="deleteMaterialOutbound" >
        delete from mm.material_outbound where material_outbound_id=#{materialOutboundId}
    </delete>
    <delete id="deleteMaterialOutboundDetail" >
        delete from mm.material_outbound_detail where material_outbound_id=#{materialOutboundId}
    </delete>
    <delete id="deleteReturningWarehouseDetail" >
        delete from mm.returning_warehouse_detail where returning_id=#{returningId}
    </delete>
    <select id="getMaximum" >
        select count(*) from mm.material_outbound where  date(create_time)=CURDATE()
    </select>
    <select id="getMaximums" >
        select count(*) from mm.returning_warehouse where  date(create_time)=CURDATE()
    </select>
    <select id="getMaterialOutboundCount" >
        select count(*) from mm.material_outbound where  material_outbound_id=#{materialOutboundId}
    </select>
    <select id="getReturningWarehouseCount" >
        select count(*) from mm.returning_warehouse where  returning_id=#{returningId}
    </select>
    <select id="getMaterialOutboundDetailMaximum" >
        select count(*) from mm.material_outbound_detail where  material_outbound_id=#{materialOutboundId}
    </select>
    <select id="getReturningWarehouseDetailMaximum" >
        select count(*) from mm.returning_warehouse where  returning_id=#{returningId}
    </select>
    <select id="getMaterialInventoryCount" >
        select count(*) from mm.material_inventory where  material_code=#{materialCode} and date_of_manufacture=#{dateOfManufacture}
    </select>
    <select id="getMaterialInventoryCountNull" >
        select count(*) from mm.material_inventory where  material_code=#{materialCode} and date_of_manufacture is null
    </select>
    <select id="getMaterialInventoryCountId" >
        select id from mm.material_inventory where  material_code=#{materialCode} and date_of_manufacture=#{dateOfManufacture}
    </select>
    <select id="getMaterialInventoryCountNullId" >
        select id from mm.material_inventory where  material_code=#{materialCode} and date_of_manufacture is null
    </select>
    <select id="getIsNotMaterialOutboundDetail" >
        select * from mm.material_outbound_detail where  material_outbound_id=#{materialOutboundId}
    </select>
    <select id="getIsNotReturningWarehouseDetail" >
        select * from mm.returning_warehouse_detail where  returning_id=#{returningId}
    </select>
    <insert id="insertMaterialOutbound"  useGeneratedKeys="true" >
        insert into mm.material_outbound(material_outbound_id,material_requisition_personnel,material_requisition_team,order_id,outbound_type,
                                         warehouse_manager,material_requisition_date,reviewed_state,create_time)
        values (
                  #{number} ,#{materialOutbound.materialRequisitionPersonnel},#{materialOutbound.materialRequisitionTeam},
                #{materialOutbound.orderId},#{materialOutbound.outboundType},#{materialOutbound.warehouseManager},#{materialOutbound.materialRequisitionDate},
                   0,now()
               )
    </insert>
    <insert id="updateMaterialOutbound"  >
        update mm.material_outbound
        set material_requisition_personnel=#{materialOutbound.materialRequisitionPersonnel},
            material_requisition_team=#{materialOutbound.materialRequisitionTeam},
            order_id=#{materialOutbound.orderId},
            outbound_type=#{materialOutbound.outboundType},
            warehouse_manager=#{materialOutbound.warehouseManager},
            material_requisition_date=#{materialOutbound.materialRequisitionDate}
        where material_outbound_id = #{number}
    </insert>
    <insert id="insertMaterialOutboundDetail"  useGeneratedKeys="true" >
        insert into mm.material_outbound_detail (material_outbound_id,material_outbound_number,material_code,material_name,use_id,inventory_id,inventory_organization,producer,unit,width,
                                                 height,thickness,outbound_quantity,single_piece_area,inventory_area,date_of_manufacture,remarks)
        values (
                   #{number} ,#{materialOutboundNumber},#{materialOutboundDetail.materialCode},#{materialOutboundDetail.materialName},#{materialOutboundDetail.useId},#{materialOutboundDetail.id},
                   #{materialOutboundDetail.inventoryOrganization},#{materialOutboundDetail.producer},#{materialOutboundDetail.unit},
                   #{materialOutboundDetail.width},#{materialOutboundDetail.height},#{materialOutboundDetail.thickness},#{materialOutboundDetail.outboundQuantity},
                #{materialOutboundDetail.singlePieceArea},#{materialOutboundDetail.inventoryArea},#{materialOutboundDetail.dateOfManufacture},#{materialOutboundDetail.remarks}
               )
    </insert>
    <insert id="insertMaterialInventory"  useGeneratedKeys="true" >
        insert into mm.material_inventory (inventory_organization, material_code, producer, stock_id,
                                            inventory_quantity, available_quantity, safety_stock, total_area, single_piece_area,
                                           date_of_manufacture, quality_guarantee_period, inventory_area, dead_stock, remarks, create_time)
        values (
                   #{materialInventory.inventoryOrganization} ,#{materialCode},
                #{materialInventory.producer},0,#{materialInventory.inventoryQuantity},#{materialInventory.inventoryQuantity},0,
                   #{totalArea},#{singlePieceArea},#{materialInventory.dateOfManufacture},#{materialInventory.qualityGuaranteePeriod},
                   #{materialInventory.inventoryArea},0,#{materialInventory.remarks},now()
               )
    </insert>
    <insert id="insertMaterialInventoryReturning"  useGeneratedKeys="true" >
        insert into mm.material_inventory (inventory_organization, material_code, producer, stock_id,
                                           inventory_quantity, available_quantity, safety_stock, total_area, single_piece_area,
                                           date_of_manufacture, quality_guarantee_period, inventory_area, dead_stock, remarks, create_time)
        values (
                   #{returningWarehouseDetail.inventoryOrganization} ,#{materialCode},
                   #{returningWarehouseDetail.producer},0,#{returningWarehouseDetail.returnQuantity},0,0,
                   #{totalArea},#{singlePieceArea},#{returningWarehouseDetail.dateOfManufacture},#{returningWarehouseDetail.qualityGuaranteePeriod},
                   #{returningWarehouseDetail.inventoryArea},0,#{returningWarehouseDetail.remarks},now()
               )
    </insert>
    <insert id="insertReturningWarehouse"  useGeneratedKeys="true" >
        insert into mm.returning_warehouse(returning_id, returning_type, material_requisition_personnel, material_requisition_team,
                                           warehouse_manager, reviewed_state, order_id, batch, project, create_time)
        values (
                   #{number} ,#{returningWarehouse.returningType},#{returningWarehouse.materialRequisitionPersonnel},#{returningWarehouse.materialRequisitionTeam},
                   #{returningWarehouse.warehouseManager},0,#{returningWarehouse.orderId},#{returningWarehouse.batch},#{returningWarehouse.project},now()
               )
    </insert>
    <insert id="insertReturningWarehouseDetail"  useGeneratedKeys="true" >
        insert into mm.returning_warehouse_detail (returning_id, returning_number, inventory_id, material_code, return_quantity,date_of_manufacture, remarks)
        values (
                   #{number} ,#{returningWarehouseNumber},#{returningWarehouseDetail.inventoryId},#{returningWarehouseDetail.id},
                #{returningWarehouseDetail.returnQuantity},#{returningWarehouseDetail.dateOfManufacture},#{returningWarehouseDetail.remarks}
               )
    </insert>
    <update id="updateMaterialInventoryAvailableOptOut">
        update mm.material_inventory
        set available_quantity=available_quantity-#{quantity}
        where id=#{useId}
    </update>
    <update id="updateMaterialInventoryAvailableOptInt">
        update mm.material_inventory
        set available_quantity=available_quantity+#{quantity}
        where id=#{useId}
    </update>
    <update id="updateMaterialInventoryAvailableOut">
        update mm.material_inventory
        set available_quantity=available_quantity-#{quantity}
        where id=#{inventoryId}
    </update>
    <update id="updateMaterialInventoryAvailableInt">
        update mm.material_inventory
        set available_quantity=available_quantity+#{quantity}
        where id=#{inventoryId}
    </update>
    <update id="updateMaterialInventoryInventoryOut">
        update mm.material_inventory
        set inventory_quantity=inventory_quantity-#{quantity},plan_quantity=if(plan_quantity-#{quantity}>=0,plan_quantity-#{quantity},0),total_area=inventory_quantity*single_piece_area
        where id=#{inventoryId}
    </update>
    <update id="updateMaterialInventoryInventoryInt">
        update mm.material_inventory
        set inventory_quantity=inventory_quantity+#{quantity},plan_quantity=plan_quantity+#{quantity},total_area=inventory_quantity*single_piece_area
        where id=#{inventoryId}
    </update>
    <update id="updateMaterialInventoryAvailableInventoryOut">
        update mm.material_inventory
        set available_quantity=available_quantity-#{quantity},inventory_quantity=inventory_quantity-#{quantity},total_area=inventory_quantity*single_piece_area
        where id=#{inventoryId}
    </update>
    <update id="updateMaterialInventoryAvailableInventoryInt">
        update mm.material_inventory
        set available_quantity=available_quantity+#{quantity},inventory_quantity=inventory_quantity+#{quantity},total_area=inventory_quantity*single_piece_area
        where id=#{inventoryId}
    </update>
    <update id="updateMaterialOutboundToExamine">
        update mm.material_outbound set reviewed_state=#{reviewedState},reviewed=#{reviewed},reviewed_time=now()
        where material_outbound_id=#{materialOutboundId}
    </update>
    <update id="updateMaterialOutboundCounterExamination">
        update mm.material_outbound set reviewed_state=#{reviewedState}
        where material_outbound_id=#{materialOutboundId}
    </update>
    <update id="updateReturningWarehouseToExamine">
        update mm.returning_warehouse set reviewed_state=#{reviewedState},reviewed=#{reviewed},reviewed_time=now()
        where returning_id=#{returningId}
    </update>
    <update id="updateReturningWarehouseCounterExamination">
        update mm.returning_warehouse set reviewed_state=#{reviewedState}
        where returning_id=#{returningId}
    </update>
    <update id="updateMaterialInventory" >
        update mm.material_inventory set inventory_quantity=inventory_quantity+#{materialInventory.inventoryQuantity},
                                         available_quantity=available_quantity+#{materialInventory.inventoryQuantity},
                                         total_area=total_area+#{totalArea}
                                     where  material_code=#{materialCode} and date_of_manufacture=#{materialInventory.dateOfManufacture}
    </update>
    <update id="updateMaterialInventoryReturning" >
        update mm.material_inventory set available_quantity=available_quantity+#{returningWarehouseDetail.returnQuantity},
                                         total_area=total_area+#{totalArea}
       where  material_code=#{materialCode} and
                date_of_manufacture=#{returningWarehouseDetail.dateOfManufacture}
    </update>
    <update id="updateMaterialInventoryReturningNull" >
        update mm.material_inventory set available_quantity=available_quantity+#{returningWarehouseDetail.returnQuantity},
                                         total_area=total_area+#{totalArea}
        where  material_code=#{materialCode} and
            date_of_manufacture is null
    </update>
    <update id="updateReturningWarehouseDetail" >
        update mm.returning_warehouse_detail set inventory_id=#{materialInventoryId}
        where  returning_id=#{number} and returning_number=#{returningWarehouseNumber}
    </update>
    <update id="updateMaterialInventoryArea" >
        update mm.material_inventory set single_piece_area=#{singlePieceArea},
                                         total_area=inventory_quantity*#{singlePieceArea}
        where  material_code=#{materialCode}
    </update>
    <select id="getSelectSurplusMaterialsDate" >
        select *
        from mm.surplus_materials
        <where>
                and date(create_time)>=#{startDate} and date(create_time) &lt;= #{endDate} and quantity>0
            <if test="surplusMaterials.width != null and surplusMaterials.width != ''">
                and width regexp #{surplusMaterials.width}
            </if>
            <if test="surplusMaterials.height != null and surplusMaterials.height != ''">
                and height regexp #{surplusMaterials.height}
            </if>
            <if test="surplusMaterials.thickness != null and surplusMaterials.thickness != ''">
                and thickness regexp #{surplusMaterials.thickness}
            </if>
            <if test="surplusMaterials.quantity != null and surplusMaterials.quantity != ''">
                and quantity regexp #{surplusMaterials.quantity}
            </if>
            <if test="surplusMaterials.colour != null and surplusMaterials.colour != ''">
                and colour regexp #{surplusMaterials.colour}
            </if>
            <if test="surplusMaterials.remarks != null and surplusMaterials.remarks != ''">
                and remarks regexp #{surplusMaterials.remarks}
            </if>
        </where>
        limit #{offset},#{pageSize};
    </select>
    <select id="getSelectSurplusMaterialsDatePageTotal" >
        select CEILING(count(id)/#{pageSize}) as 'pageTotal',
        count(id) as 'total'
        from mm.surplus_materials
        <where>
            and date(create_time)>=#{startDate} and date(create_time) &lt;= #{endDate} and quantity>0
            <if test="surplusMaterials.width != null and surplusMaterials.width != ''">
                and width regexp #{surplusMaterials.width}
            </if>
            <if test="surplusMaterials.height != null and surplusMaterials.height != ''">
                and height regexp #{surplusMaterials.height}
            </if>
            <if test="surplusMaterials.thickness != null and surplusMaterials.thickness != ''">
                and thickness regexp #{surplusMaterials.thickness}
            </if>
            <if test="surplusMaterials.quantity != null and surplusMaterials.quantity != ''">
                and quantity regexp #{surplusMaterials.quantity}
            </if>
            <if test="surplusMaterials.colour != null and surplusMaterials.colour != ''">
                and colour regexp #{surplusMaterials.colour}
            </if>
            <if test="surplusMaterials.remarks != null and surplusMaterials.remarks != ''">
                and remarks regexp #{surplusMaterials.remarks}
            </if>
        </where>
    </select>
    <select id="selectSurplusMaterialsCount" >
        select *
        from mm.surplus_materials
        where width=#{surplusMaterials.width} and height=#{surplusMaterials.height}
          and thickness=#{surplusMaterials.thickness} and colour=#{surplusMaterials.colour} limit 0,1
    </select>
    <insert id="insertSurplusMaterials">
        insert into mm.surplus_materials (width,height,thickness,quantity,colour,create_time)
        values (#{surplusMaterials.width},#{surplusMaterials.height},#{surplusMaterials.thickness},#{surplusMaterials.quantity},#{surplusMaterials.colour},now())
    </insert>
    <insert id="insertSurplusMaterialsLog">
        insert into mm.surplus_materials_log (width,height,thickness,quantity,colour,type,creator,create_time)
        values (#{surplusMaterials.width},#{surplusMaterials.height},#{surplusMaterials.thickness},
                #{surplusMaterials.quantity},#{surplusMaterials.colour},#{type},#{creator},now())
    </insert>
    <update id="updateSurplusMaterialsOutQuantity">
        update mm.surplus_materials set quantity=quantity-#{surplusMaterials.quantity} where id=#{surplusMaterials.id}
    </update>
    <update id="updateSurplusMaterialsIntQuantity">
        update mm.surplus_materials set quantity=quantity+#{surplusMaterials.quantity} where id=#{surplusMaterials.id}
    </update>
    <select id="getPrintCreateOutbound" >
        select id,
               material_outbound_id,
               material_requisition_personnel,
               material_requisition_team,
               order_id,
               outbound_type,
               warehouse_manager,
               date(material_requisition_date) as material_requisition_date,
               reviewed,
               date(reviewed_time) as reviewed_time,
               reviewed_state,
               date(create_time) as create_time
        from mm.material_outbound
        where material_outbound_id = #{materialOutboundId}
    </select>
    <select id="getPrintCreateOutboundDetail" >
        select
            ou.project_no as  projectNo,
            modd.inventory_id as id,
            modd.inventory_organization as inventoryOrganization,
            modd.material_code as materialCode,
            modd.material_name as materialName,
            modd.producer,
            modd.width,
            modd.height,
            modd.thickness,
            modd.unit,
            modd.outbound_quantity as outboundQuantity,
            date(modd.date_of_manufacture) as dateOfManufacture,
            modd.single_piece_area as singlePieceArea,
            modd.inventory_area as inventoryArea,
            modd.remarks,
            ou.use_count as inventoryQuantity,
            if(mo.reviewed_state!=1,modd.outbound_quantity+ou.not_use_count,ou.not_use_count) as availableQuantity,
            ms.json,
            modd.producer
        from mm.material_outbound_detail modd left join pp.optimize_use ou on modd.use_id=ou.id
                                              left join mm.material_outbound mo on modd.material_outbound_id=mo.material_outbound_id
                                              left join mm.material_store ms on modd.material_code=ms.id
        where modd.material_outbound_id = #{materialOutboundId}
    </select>
    <select id="getMaterialInventoryById" >
        select
            *
        from mm.material_inventory mi
        where mi.id = #{id}
    </select>
</mapper>