<?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.fjhx.stock.mapper.StockBackMapper">

    <select id="getPickingToday" resultType="java.util.Map">
        select m.ID   materialId,
               m.code materialCode,
               m.name materialName
        from stock_waterdetial swd
                 left join material m on m.Code = swd.MaterialCode
                 left join acc_account aa on aa.ID = swd.OperUserId
            ${ew.customSqlSegment}
    </select>


    <select id="getPickingTodayByUserId" resultType="java.util.Map">
        select m.ID   materialId,
               m.code materialCode,
               m.name materialName
        from stock_waterdetial swd
                 left join material m on m.Code = swd.MaterialCode
            ${ew.customSqlSegment}
    </select>

    <select id="checkList" resultType="java.util.Map">
        select sb.id               stockBackId,
               sb.operation_num    operationNum,
               sb.operation_meters operationMeters,
               sb.operation_time   operationTime,
               uu.RealName         realName
        from stock_back sb
                 left join acc_account aa on aa.AccountNo = sb.operator_id
                 left join u_user uu on aa.ID = uu.ID
        where sb.status = 0
          and sb.del_flag = 0
    </select>

    <select id="userStatistics" resultType="java.util.Map">
        select distinct uu.ID           userId,
                        uu.JobNo        jobNo,
                        uu.RealName     realName,
                        uu.DepartmentID departmentID,
                        aa.AccountNo    account
        from u_user uu
                 left join acc_account aa on uu.ID = aa.ID
            ${ew.customSqlSegment}
    </select>
    <select id="selectSchedule" resultType="java.lang.Double">
        select sum(m.Width * t.materialQuantity / 100) area
        from
        (
        (
        select
        ss.printer jobNo,
        ss.material_code materialCode,
        ss.material_quantity materialQuantity
        from stock_scheduling ss
        where ss.create_time between #{beginTime} and #{endTime}
        <if test="jobNo neq null and jobNo neq ''">
            and ss.printer = #{jobNo}
        </if>
        <if test="materialCode neq null and materialCode neq ''">
            and ss.material_code = #{materialCode}
        </if>
        )
        union all
        (
        select
        ss.paper_man jobNo,
        ss.paper_code materialCode,
        ss.paper_quantity materialQuantity
        from stock_scheduling ss
        where ss.create_time between #{beginTime} and #{endTime}
        <if test="jobNo neq null and jobNo neq ''">
            and ss.paper_man = #{jobNo}
        </if>
        <if test="materialCode neq null and materialCode neq ''">
            and ss.paper_code = #{materialCode}
        </if>
        )
        )
        t
        left join material m on t.materialCode = m.code
        where
        <choose>
            <when test="technologyType neq null and technologyType neq ''">
                m.TechnologyType = #{technologyType}
            </when>
            <otherwise>
                m.TechnologyType in (0, 1, 2)
            </otherwise>
        </choose>
    </select>
    <select id="selectPicking" resultType="com.fjhx.params.stock.StatisticsResult">
        select
        count(0) pickingNum,
        sum(swd.ChangeNum) pickingMeters,
        sum(m.Width * swd.ChangeNum / 100) pickingArea,
        sum(swd.ChangeNum * m.Price) pickingMoney
        from stock_waterdetial swd
        left join material m on swd.MaterialCode = m.Code
        where
        swd.StockChangeType in (20,23)
        and swd.CreatedTime between #{beginTime} and #{endTime}
        <if test="userId neq null and userId neq ''">
            and swd.OperUserId = #{userId}
        </if>
        and
        <choose>
            <when test="technologyType neq null and technologyType neq ''">
                m.TechnologyType = #{technologyType}
            </when>
            <otherwise>
                m.TechnologyType in (0, 1, 2)
            </otherwise>
        </choose>
    </select>

    <select id="selectBack" resultType="com.fjhx.params.stock.StatisticsResult">
        select
        count(0) backNum,
        sum(swd.ChangeNum) backMeters,
        sum(m.Width * swd.ChangeNum / 100) backArea,
        sum(swd.ChangeNum * m.Price) backMoney
        from stock_waterdetial swd
        left join material m on swd.MaterialCode = m.Code
        where
        swd.StockChangeType = 16
        and swd.CreatedTime between #{beginTime} and #{endTime}
        <if test="userId neq null and userId neq ''">
            and swd.OperUserId = #{userId}
        </if>
        <if test="materialCode neq null and materialCode neq ''">
            and m.code = #{materialCode}
        </if>
        and
        <choose>
            <when test="technologyType neq null and technologyType neq ''">
                m.TechnologyType = #{technologyType}
            </when>
            <otherwise>
                m.TechnologyType in (0, 1, 2)
            </otherwise>
        </choose>
    </select>

    <select id="selectCorrect" resultType="java.lang.Double">
        select sum(correct_area) correctArea
        from stock_back_correct sbc
        left join material m on sbc.material_id = m.ID
        where
        sbc.correct_time between #{beginTime} and #{endTime}
        <if test="userId neq null and userId neq ''">
            and sbc.user_id = #{userId}
        </if>
        <if test="materialId neq null and materialId neq ''">
            and m.ID = #{materialId}
        </if>
        and
        <choose>
            <when test="technologyType neq null and technologyType neq ''">
                m.TechnologyType = #{technologyType}
            </when>
            <otherwise>
                m.TechnologyType in (0, 1, 2)
            </otherwise>
        </choose>
    </select>

    <select id="selectPickingPage" resultType="com.fjhx.params.stock.StatisticsResult">
        select
        uu.ID userId,
        uu.JobNo jobNo,
        uu.RealName userName,
        sbpd.plan_material materialName,
        sum(sbpd.quantity) scheduleArea,
        sum(sbpd.correct_area) correctArea,
        sum(sbpd.picking_money) pickingMoney,
        sum(sbpd.picking_num) pickingNum,
        sum(sbpd.picking_meters) pickingMeters,
        sum(sbpd.picking_area) pickingArea,
        sum(sbpd.back_money) backMoney,
        sum(sbpd.back_num) backNum,
        sum(sbpd.back_meters) backMeters,
        sum(sbpd.back_area) backArea,
        sum(sbpd.transfer_in_area) transferInArea,
        sum(sbpd.transfer_out_area) transferOutArea
        from stock_back_plan_details sbpd
        left join u_user uu on uu.JobNo = sbpd.job_no
        where
        sbpd.plan_time between #{beginTime} and #{endTime}
        and
        <choose>
            <when test="jobNo neq null and jobNo neq ''">
                uu.JobNo = #{jobNo}
            </when>
            <otherwise>
                uu.departmentID in ('A007', 'A008', 'A012')
            </otherwise>
        </choose>
        group by uu.JobNo,sbpd.plan_material
    </select>

    <select id="selectScheduleArea" resultType="java.lang.Double">
        select sum(m.Width * t.materialQuantity / 100) area
        from
        (
        (
        select
        ss.printer jobNo,
        ss.material_code materialCode,
        ss.material_quantity materialQuantity
        from stock_scheduling ss
        where ss.create_time between #{beginTime} and #{endTime}
        <if test="jobNo neq null and jobNo neq ''">
            and ss.printer = #{jobNo}
        </if>
        )
        union all
        (
        select
        ss.paper_man jobNo,
        ss.paper_code materialCode,
        ss.paper_quantity materialQuantity
        from stock_scheduling ss
        where ss.create_time between #{beginTime} and #{endTime}
        <if test="jobNo neq null and jobNo neq ''">
            and ss.paper_man = #{jobNo}
        </if>
        )
        )
        t
        left join material m on t.materialCode = m.code
        where
        <choose>
            <when test="technologyType neq null and technologyType neq ''">
                m.TechnologyType = #{technologyType}
            </when>
            <otherwise>
                m.TechnologyType in (0, 1, 2)
            </otherwise>
        </choose>
    </select>

    <select id="getBackStatistics" resultType="java.util.Map">
        select count(0)                                                      count,
               ifnull(sum(sbd.material_meters), 0)                           meters,
               round(ifnull(sum(m.Width * sbd.material_meters / 100), 0), 2) area,
               round(ifnull(sum(sbd.material_meters * m.Price), 0), 2)       price
        from stock_back sb
                 inner join stock_back_details sbd on sb.id = sbd.stock_back_id
                 inner join material m on sbd.material_id = m.ID
                 left join acc_account ac on ac.AccountNo = sb.operator_id
                 left join u_user uu on ac.ID = uu.ID
            ${ew.customSqlSegment}
    </select>

    <select id="getBackPage" resultType="java.util.Map">
        select sbd.rfid,
               uu.RealName                           realName,
               sb.operation_time                     time,
               m.Name                                materialName,
               sbd.material_meters                   meters,
               (m.Width * sbd.material_meters / 100) area,
               sb.status,
               m.id                                  materialId,
               sbd.flow_status                       flowStatus,
               sbd.id
        from stock_back sb
                 inner join stock_back_details sbd on sb.id = sbd.stock_back_id
                 inner join material m on sbd.material_id = m.ID
                 left join acc_account ac on ac.AccountNo = sb.operator_id
                 left join u_user uu on ac.ID = uu.ID
            ${ew.customSqlSegment}
    </select>


</mapper>
