<?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.applet.mapper.FabricStatisticsMapper">

    <select id="selectFabricDetails" resultType="java.util.Map">
        SELECT sh.`Name`                               houseName,
               count(1)                             AS count,
               sum(sd.Quantity)                     AS quantity,
               round(sum(sd.Quantity * m.Price), 2) AS amount
        FROM stock_detail sd
                 LEFT JOIN stock_house sh ON sd.StockHouseId = sh.ID
                 LEFT JOIN material m ON sd.MaterialCode = m.`Code`
        WHERE m.TechnologyType != '3'
        GROUP BY sh.`Name`
    </select>

    <select id="selectFabricTypeDetails" resultType="java.util.Map">
        SELECT IF(m.Purpose is null, "其他", m.Purpose)  rurpose,
               count(1)                             AS count,
               sum(sd.Quantity)                     AS quantity,
               round(sum(sd.Quantity * m.Price), 2) AS amount
        FROM stock_detail sd
                 LEFT JOIN material m ON sd.MaterialCode = m.`Code`
                 LEFT JOIN stock_house sh ON sd.StockHouseId = sh.ID
            ${ew.customSqlSegment}
    </select>

    <select id="selectFabricWDetails" resultType="java.util.Map">
        SELECT sh.`Name`                                 houseName,
               Count(1)                               AS count,
               sum(swd.ChangeNum)                     AS changeNum,
               round(sum(swd.ChangeNum * m.Price), 2) AS amount
        FROM stock_waterdetial swd
                 LEFT JOIN stock_water sw ON swd.WaterId = sw.ID
                 LEFT JOIN stock_house sh ON sw.StockHouseId = sh.ID
                 LEFT JOIN material m ON swd.MaterialCode = m.`Code`
            ${ew.customSqlSegment}
    </select>

    <select id="selectFabricWTypeDetails" resultType="java.util.Map">
        SELECT sh.`Name`                                                 houseName,
               IF(m.Purpose is null and m.Purpose = '', "其他", m.Purpose) rurpose,
               Count(1)                     AS                           count,
               sum(swd.ChangeNum)           AS                           changeNum,
               sum(swd.ChangeNum * m.Price) AS                           amount
        FROM stock_waterdetial swd
                 LEFT JOIN material m ON swd.MaterialCode = m.`Code`
                 LEFT JOIN stock_water sw ON swd.waterId = sw.ID
                 LEFT JOIN stock_house sh ON sw.StockHouseId = sh.ID
            ${ew.customSqlSegment}
    </select>

    <select id="getInOutBill" resultType="java.util.Map">
        SELECT sum(tsi.quantity)           planArea,
               tsi.job_no                  jobNo,
               uu.RealName                 realName,
               CASE
                   m.technologyType
                   WHEN 0 THEN
                       '直喷'
                   WHEN 1 THEN
                       '热转'
                   WHEN 2 THEN
                       '打纸'
                   WHEN 3 THEN
                       '墨水'
                   ELSE '其他'
                   END                     'techType',
               ifnull(m.technologyType, 4) technologyType
        FROM t_stock_inoutbill tsi
                 LEFT JOIN u_user uu ON tsi.job_no = uu.JobNo
                 LEFT JOIN (
            SELECT SUBSTRING_INDEX(`name`, '-', 1) mat_name,
                   m.technologyType
            FROM material m
            WHERE m.ID IN (SELECT min(id)
                           FROM `storage`.material
                           WHERE IsDelete = 0
                           GROUP BY SUBSTRING_INDEX(`name`, '-', 1))) m ON tsi.scheduling_material = m.mat_name
            ${ew.customSqlSegment}
    </select>

    <select id="getWaterStatistics" resultType="java.util.Map">
        SELECT uu.JobNo                                     jobNo,
               round(sum(swd.ChangeNum), 2)                 quantity,
               round(sum(swd.ChangeNum * swd.Price), 2)     amount,
               round(sum(swd.ChangeNum * m.Width / 100), 2) area,
               ifnull(count(1), 0)                          pcs,
               ifnull(m.technologyType, 4)                  technologyType
        FROM `storage`.stock_waterdetial swd
                 LEFT JOIN `storage`.material m ON swd.MaterialCode = m.`Code`
                 LEFT JOIN `storage`.u_user uu ON swd.OperUserId = uu.ID
            ${ew.customSqlSegment}
    </select>

    <select id="getReturnMaterial" resultType="java.util.Map">
        SELECT uu.JobNo                               jobNo,
               round(sum(swd.ChangeNum * m.Width), 2) backQty,
               ifnull(m.technologyType, 4)            technologyType
        FROM `storage`.stock_waterdetial swd
                 LEFT JOIN `storage`.material m ON swd.MaterialCode = m.`Code`
                 LEFT JOIN `storage`.u_user uu ON swd.OperUserId = uu.ID
            ${ew.customSqlSegment}
    </select>

    <select id="selectFabricStatistics" resultType="java.util.Map">
        SELECT m.TechnologyType                     technologyType,
               count(1)                             count,
               round(sum(sd.Quantity), 2)           quantity,
               round(sum(sd.Quantity * m.Price), 2) amount
        FROM stock_detail sd
                 LEFT JOIN material m ON sd.MaterialCode = m.`Code`
                 LEFT JOIN stock_house sh ON sd.StockHouseId = sh.ID
            ${ew.customSqlSegment}
    </select>

    <select id="selectPickingQuantitySum" resultType="java.util.Map">
        SELECT sum(quantity)   quantity,
               technology_type technologyType,
               nick_name
        FROM (
                 SELECT si.scheduling_material AS scheduling_material,
                        si.job_no              AS job_no,
                        si.quantity            AS quantity,
                        m.TechnologyType       AS technology_type,
                        u.RealName             AS nick_name
                 FROM t_stock_inoutbill si
                          LEFT JOIN storage.material m ON m.`name` LIKE CONCAT(si.scheduling_material, '%')
                          LEFT JOIN u_user u ON si.job_no = u.JobNo
                     ${ew.customSqlSegment}
             ) tb
        GROUP BY technology_type
    </select>

    <select id="selectFabricEW" resultType="java.util.Map">
        SELECT m.TechnologyType                             technologyType,
               Count(1)                                     count,
               round(sum(swd.ChangeNum), 2)                 changeNum,
               round(sum(swd.ChangeNum * m.Price), 2)       amount,
               round(sum(swd.ChangeNum * m.Width / 100), 2) square
        FROM stock_waterdetial swd
                 LEFT JOIN material m ON swd.MaterialCode = m.`Code`
            ${ew.customSqlSegment}
    </select>

    <select id="selectFabricW" resultType="java.util.Map">
        SELECT m.TechnologyType                       technologyType,
               Count(1)                               count,
               round(sum(swd.ChangeNum), 2)           totalQty,
               round(sum(swd.ChangeNum * m.Price), 2) amount
        FROM stock_waterdetial swd
                 left join stock_water sw on swd.WaterId = sw.id
                 LEFT JOIN material m ON swd.MaterialCode = m.`Code`
            ${ew.customSqlSegment}
    </select>

    <select id="flowNum" resultType="java.util.Map">
        select f.ID id, f.FlowName flowName, count(0) count
        from flow f
                 left join
             (
                 SELECT t.FlowId, t.LinkId, t.CreatedTime, t.CheckState
                 FROM (SELECT FlowId, LinkId, CreatedTime, CheckState
                       FROM flow_applycheck
                       ORDER BY CreatedTime DESC) t
                 GROUP BY t.LinkId
             ) fa on f.ID = fa.FlowId
            ${ew.customSqlSegment}
    </select>

    <select id="flowPage" resultType="java.util.Map">
        select fa.ID           flowChekId,
               fa.ApprovalItem dealWithItem,
               fa.CreatedTime  createTime,
               fa.CheckState   checkState,
               CASE
                   fa.CheckState
                   WHEN 0 THEN
                       '已发起'
                   WHEN 1 THEN
                       '待审批'
                   WHEN 2 THEN
                       '审批通过'
                   WHEN 3 THEN
                       '审批驳回'
                   END         checkStateStr,
               fa.LinkId       linkId,
               f.ModelName     modelName,
               f.DealWithType  dealWithType,
               f.FlowName      dealWithTypeStr
        from flow f
                 left join
             (
                 SELECT t.FlowId, t.LinkId, t.CreatedTime, t.CheckState, t.ApprovalItem, t.ID
                 FROM (SELECT FlowId, LinkId, CreatedTime, CheckState, ApprovalItem, ID
                       FROM flow_applycheck
                       ORDER BY CreatedTime DESC) t
                 GROUP BY t.LinkId
             ) fa on f.ID = fa.FlowId
            ${ew.customSqlSegment}
    </select>

    <select id="getApproveType" resultType="java.lang.Integer">
        ${sql}
    </select>

</mapper>
