/*
 * Copyright © 2013 VillageReach.  All Rights Reserved.  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 *
 * If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package org.openlmis.core.repository.mapper;

import org.apache.ibatis.annotations.*;
import org.openlmis.core.domain.Program;
import org.openlmis.core.domain.SupervisoryNode;
import org.openlmis.core.domain.SupplyLine;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface SupplyLineMapperExtension {

    @Select("SELECT " +
            "sl.id, sl.description, p.name AS programName,f.name AS facilityName, n.name As supervisorynodeName " +
            "FROM supply_lines AS sl " +
            "JOIN programs AS p ON p.id = sl.programid " +
            "JOIN facilities AS f ON f.id = sl.supplyingfacilityid " +
            "INNER JOIN supervisory_nodes AS n ON n.id = sl.supervisorynodeid")
    @Results(value={
            @Result(property = "supervisoryNode.name", column = "supervisorynodeName"),
            @Result(property = "supplyingFacility.name", column = "facilityName"),
            @Result(property = "program.name", column = "programName"),
    })
    List<SupplyLine> getAllSupplyLine();

    @Select("SELECT * FROM supply_lines WHERE id = #{id}")
    @Results(value={
            @Result(property = "supervisoryNode.id", column = "supervisorynodeid"),
            @Result(property = "supplyingFacility.id", column = "supplyingfacilityid"),
            @Result(property = "program.id", column = "programid"),
    })
    SupplyLine getSupplylineById(Long id);

    @Select("SELECT " +
            "sl.id, sl.description, p.name AS programName,f.name AS facilityName, n.name As supervisorynodeName " +
            "FROM supply_lines AS sl " +
            "JOIN programs AS p ON p.id = sl.programid " +
            "JOIN facilities AS f ON f.id = sl.supplyingfacilityid " +
            "INNER JOIN supervisory_nodes AS n ON n.id = sl.supervisorynodeid " +
            "WHERE sl.id = #{id}")
    @Results(value={
            @Result(property = "supervisoryNode.name", column = "supervisorynodeName"),
            @Result(property = "supplyingFacility.name", column = "facilityName"),
            @Result(property = "program.name", column = "programName"),

    })
    SupplyLine getSupplylineDetailById(Long id);


    @Delete("DELETE FROM supply_lines where id = #{supplylineId}")
    int deleteById(Long supplylineId);

    @Select("SELECT Count(Id) FROM orders where supplyLineId = #{id}")
    int getOrderCountById(Long id);

}

@@ -645,32 +645,92 @@
          %22sl.id,
+ sl.programId, sl.supervisoryNodeId, sl.supplyingFacilityId,
  sl.description,
@@ -789,33 +789,33 @@
 e As supervisory
-n
+N
 odeName %22 +%0A    
@@ -895,33 +895,33 @@
 .id = sl.program
-i
+I
 d %22 +%0A          
@@ -962,33 +962,33 @@
 sl.supplying
-f
+F
 acility
-i
+I
 d %22 +%0A      
@@ -1052,22 +1052,71 @@
 ervisory
-nodeid
+NodeId %22 +%0A            %22ORDER BY p.name, n.name, f.name
 %22)%0A    @
@@ -1199,33 +1199,33 @@
 n = %22supervisory
-n
+N
 odeName%22),%0A     
@@ -1370,24 +1370,263 @@
 gramName%22),%0A
+            @Result(property = %22supervisoryNode.id%22, column = %22supervisoryNodeId%22),%0A            @Result(property = %22program.id%22, column = %22programId%22),%0A            @Result(property = %22supplyingFacility.id%22, column = %22supplyingFacilityId%22)%0A
     %7D)%0A    L

Fix query that brings supply lines.