package cz.wicketstuff.examples.spring.persistence.mybatis.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.StatementType;
import org.springframework.stereotype.Repository;

import cz.wicketstuff.examples.spring.core.domain.Task;
import cz.wicketstuff.examples.spring.core.domain.TaskExt;

/**
 * @author Martin Strejc (strma17)
 *
 */
@Repository
public interface TaskDao {
	
	@Select("SELECT t.* FROM task t WHERE t.task_group_id = #{taskGroupId} ORDER BY t.created")
	@ResultMap("task")
	List<Task> selectAll(long taskGroupId);

	@Select("SELECT tg.*, t.id as t_id, t.name as t_name, t.created as t_created, t.uuid as t_uuid FROM task_group tg LEFT JOIN task t ON (t.task_group_id = tg.id) ORDER BY tg.created")
	@ResultMap("taskExt")
	List<TaskExt> selectAllExt();

	@Insert("INSERT INTO task (name, created, uuid, status) VALUES (#{name}, #{created}, #{uuidString}, #{status})")
	@SelectKey(
			before = false, 
			keyColumn = "id", 
			keyProperty = "id", 
			resultType = Long.class, 
			statement = "SELECT LAST_INSERT_ID()", 
			statementType = StatementType.STATEMENT)
	long insert(Task task);
	
	@Update("UPDATE task_group SET name = #{name}, status = #{status} WHERE id = #{id}")
	long update(Task task);
	
	@Delete("DELETE FROM task WHERE id = #{taskId}")
	long delete(long id);

	@Select("SELECT * FROM task WHERE id = #{id}")
	Task selectById(long id);

	@Select("SELECT t.*, tg.id as tg_id, tg.name as tg_name, tg.created as tg_created, tg.uuid as tg_uuid FROM task t LEFT JOIN task_group tg ON (tg.id = t.task_group_id) WHERE t.id = #{id}")
	@ResultMap("taskExt")
	TaskExt selectByIdExt(long id);

	@Select("SELECT * FROM task_group WHERE uuid = #{uuidString}")
	TaskExt selectByUuid(String uuidString);

	@Select("SELECT t.*, tg.id as tg_id, tg.name as tg_name, tg.created as tg_created, tg.uuid as tg_uuid FROM task t LEFT JOIN task_group tg ON (tg.id = t.task_group_id) WHERE t.id = #{uuidString}")
	@ResultMap("taskExt")
	TaskExt selectByUuidExt(String uuidString);

}

@@ -1139,16 +1139,31 @@
 , status
+, task_group_id
 ) VALUES
@@ -1209,16 +1209,33 @@
 %7Bstatus%7D
+, #%7BtaskGroup.id%7D
 )%22)%0D%0A%09@S

Fix task dao bug