<?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.malk.diwei.mapper.ProcessMapper">
    <select id="selectProductById" resultType="java.util.Map">
        select
            g.GoodsCode,
            g.GoodsName,
            u.UnitName,
            tmp.qty,
            tmp.ProcessItemReferBillCode
        from
            (
                select
                    ProcessID,
                    GoodsID ,
                    UnitID,
                    SUM(ProcessItemQty) as qty,
                    ProcessItemReferBillCode
                from
                    ProcessItem
                where
                    ProcessID = #{processID}
                group by
                    ProcessID,
                    GoodsID ,
                    UnitID,
                    ProcessItemReferBillCode) tmp
                INNER join Goods g
                           on
                               tmp.GoodsID = g.GoodsID
                inner join Unit u
                           on
                               tmp.UnitID = u.UnitID
    </select>
    <select id="selectProcessONDetailByCode" resultType="java.util.Map">
        SELECT
            p.ProcessID ,p.ProcessCode ,p.ProcessDate ,p.ProcessStatus ,c.ClientName ,gp2.GoodsPartsName,
            wps.WPSProcStepName,g.GoodsID ,g.GoodsNeedleType,g.GoodsMachineType,wps.WPSHadRecordQty
        from
            ProcessItem pi2
        inner join
            Process p
        on
            pi2.ProcessID = p.ProcessID
        inner join
            WorkProcStep wps
        on
            pi2.ProcessID = wps.ProcessID
        inner join
            Goods g
        on
            wps.GoodsID = g.GoodsID
        inner join
            Client c
        ON
            wps.WPSFactoryID = c.ClientID
        inner JOIN
            (
            select
                pm.ProcessID,gp.GoodsPartsName
            from
                ProcessMaterial pm
            inner join
                GoodsParts gp
            on
                pm.ProcessMaterialGoodsPartsID = gp.GoodsPartsID
            group by
                pm.ProcessID,gp.GoodsPartsName
            ) gp2
        on
            pi2.ProcessID = gp2.ProcessID
        where
            pi2.ProcessItemReferBillCode = #{processCode}
    </select>
    <select id="selectOutSideDetails" resultType="java.util.Map">
        --查询外发单外发明细
        select
            gp.GoodsPartsName ,
            g.GoodsCode ,
            g.GoodsName ,
            g.GoodsNeedleType ,
            g.GoodsMachineType ,
            c.ColorName,
            s.SizeName ,
            wps.WPSProcStepName ,
            po.ProcessONDetailQty,
            wps.WPSProcStepPrice ,
            po.ProcessONDetailDes,
            pi2.ProcessItemReferBillCode
        from
            ProcessItem pi2
        inner join
            ProcessONDetail po
        on
            po.ProcessID = pi2.ProcessID
        left join
            PCONDetailParts pp
        on
            po.ProcessONDetailID = pp.ProcessONDetailID
        left join
            GoodsParts gp
        on
            pp.GoodsPartsID = gp.GoodsPartsID
        left join
            Goods g
        on
            pi2.GoodsID = g.GoodsID
        left join
            Color c
        on
            po.PCNODColorID = c.ColorID
        left join
            [Size] s
        on
            po.PCNODSizeID = s.SizeID
        left join
            WorkProcStep wps
        on
            pi2.ProcessID = wps.ProcessID
        where
            pi2.ProcessID = #{processID}
        and
            po.ProcessONDetailType = 1
    </select>

    <select id="selectOnDetails" resultType="java.util.Map">
        --查询回收单关联明细
        select
            po.ProcessID,
            po.ProcessItemID,
            g.GoodsCode,
            g.GoodsName,
            gp.GoodsPartsName ,
            c.ColorName,
            s.SizeName,
            po.ProcessONDetailQty,
            po.ProcessONDetailWeight,
            p.ProcessCode,
            po.PCONDPackageBarCode,
            pi2.ProcessItemReferBillCode
        from
            ProcessONDetail po
        left join
            Process p
        on
            po.ProcessID = p.ProcessID
        left join
            ProcessItem pi2
        on
            pi2.ProcessID = po.ProcessID and pi2.ProcessItemID = po.ProcessItemID
        left JOIN
            PCONDetailParts pp
        on
            po.ProcessONDetailID = pp.ProcessONDetailID
        left join
            GoodsParts gp
        on
            pp.GoodsPartsID = gp.GoodsPartsID
        left JOIN
            Goods g
        on
            pi2.GoodsID = g.GoodsID
        left JOIN
            Color c
        on
            po.PCNODColorID = c.ColorID
        left JOIN
            [Size] s
        on
            po.PCNODSizeID = s.SizeID
        where
            po.processONDetailCode = #{processONDetailCode}
        AND
            ProcessONDetailType = 2
        and
            ProcessONDetailReceiptWay = 2
    </select>

    <select id="selectOnCardBarDetails" resultType="java.util.Map">
        --查询回收单条码明细
        SELECT
            po.ProcessONDetailID,
            po.ProcessONDetailCode,
            pbc.ProceCardID,
            pc.ProceCardBarCode
        from
            ProcessONDetail po
        inner join
            PCONDetailBarCode pbc
        on
            po.ProcessONDetailID = pbc.ProcessONDetailID
        INNER JOIN
            ProcessCard pc
        on
            pbc.ProceCardID = pc.ProceCardID
        where
            po.ProcessONDetailCode = #{processONDetailCode}
        order BY
            pc.ProceCardBarCode
    </select>

    <select id="selectOnBaseInfo" resultType="java.util.Map">
        --查询回收单基础信息
        select top 1
	        po.ProcessONDetailDate,
            po.ProcessONDetailCode,
            wps.WPSProcStepName,
            c.ClientName
        from
            ProcessONDetail po
        inner join
            WorkProcStep wps
        on
            po.ProcessID = wps.ProcessID
        inner JOIN
            Client c
        on
            wps.WPSFactoryID = c.ClientID
        where
            ProcessONDetailCode = #{processONDetailCode}
        AND
            ProcessONDetailType = 2
        and
            ProcessONDetailReceiptWay = 2
        group by
            po.ProcessONDetailDate,
            po.ProcessONDetailCode,
            wps.WPSProcStepName,
            c.ClientName
    </select>
    <select id="selectOnCardBarDetails2" resultType="java.util.Map">
        --查询工艺回收单条码明细
        SELECT
            po.ProcessONDetailID ,po.ProcessONDetailCode,pc2.ProceCardID ,pc.ProceCardBarCode,
            pc2.PCONDDetailCardQty,pc2.PCONDCProcStepID,pc2.PCONDCFactoryName,pc2.PCONDCProcessCode
        from
            ProcessONDetail po
                inner join
            PCONDetailCard pc2
            on
                po.ProcessONDetailID = pc2.ProcessONDetailID
                INNER JOIN
            ProcessCard pc
            on
                pc2.ProceCardID = pc.ProceCardID
        where
            po.ProcessONDetailCode = #{processONDetailCode}
        order BY
            pc.ProceCardBarCode
    </select>

</mapper>
