package sikd.sikd5.sikd5ejb.mapper.login;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.One;

import java.util.List;

import org.apache.ibatis.annotations.Many;

import sikd.sikd5.sikd5bean.tabel.Pemda;
import sikd.sikd5.sikd5bean.tabel.mgr.TUserAccount;
import sikd.sikd5.sikd5bean.tabel.mgr.TUserContact;
import sikd.sikd5.sikd5bean.tabel.mgr.TUserGroup;
import sikd.sikd5.sikd5bean.tabel.mgr.WilayahKerja;

public interface LoginMapper {

	@Select("SELECT recordindex, username, \"password\", isactive, idgroup FROM ms5.tuseraccount where "
			+ "username=#{userName} limit 1")
	@Results({
			@Result(property = "id", column = "recordindex"),
			@Result(property = "username", column = "username"),
			@Result(property = "password", column = "password"),
			@Result(property = "actives", column = "isactive"),
			@Result(property = "group", column = "idgroup", one = @One(select = "getTUserGroup")),
			@Result(property = "pemda", column = "recordindex", one = @One(select = "getTpemda")),
			@Result(property = "wilayah", column = "recordindex", one = @One(select = "getWilayahKerja")),
			@Result(property = "userContact", column = "recordindex", one = @One(select = "getTuserContact"))
	})
	public TUserAccount getUserAccountbyName(@Param("userName") String userName);

	@Select("SELECT masterkey, groupname, description FROM ms5.tusergroup where masterkey=#{id}")
	@Results({
			@Result(property = "id", column = "masterkey"),
			@Result(property = "nama", column = "groupname"),
			@Result(property = "deskripsi", column = "description")
	})
	public TUserGroup getTUserGroup(long id);

	@Select("select idpemda, kodeprovinsi, kodepemda, kodesatker, namapemda, namapemdasingkat, tipepemda"
			+ " from ms5.tpemda where idpemda =(select idpemda from ms5.tuserpemda where iduser=(#{iduser}))")
	@Results({
			@Result(property = "id", column = "idpemda"),
			@Result(property = "kodeProvinsi", column = "kodeprovinsi"),
			@Result(property = "kodePemda", column = "kodepemda"),
			@Result(property = "kodeSatker", column = "kodesatker"),
			@Result(property = "namaPemda", column = "namapemda")

	})
	public Pemda getTpemda(long iduser);

	@Select("select idwilkerja,nama,description from ms5.twilkerja where idwilkerja =(select idwilkerja from ms5.tuserwilkerja where iduser=#{id})")
	@Results({
			@Result(property = "id", column = "idwilkerja"),
			@Result(property = "nama", column = "nama"),
			@Result(property = "deskripsi", column = "description"),
			@Result(property = "pemdas", column = "idwilkerja", javaType = List.class, many = @Many(select = "getPemdasByWilayahKerja"))

	})
	public WilayahKerja getWilayahKerja(long id);

	@Select("select idpemda,kodeprovinsi,kodepemda,kodesatker,namapemda,namapemdasingkat \n" +
			"from ms5.tpemda where idpemda in (select idpemda from ms5.twilkerja_has_tpemda where idwilkerja=#{idwilkerja})")
	@Results({
			@Result(property = "id", column = "idpemda"),
			@Result(property = "kodeProvinsi", column = "kodeprovinsi"),
			@Result(property = "kodePemda", column = "kodepemda"),
			@Result(property = "kodeSatker", column = "kodesatker"),
			@Result(property = "namaPemda", column = "namapemda")
	})

	public List<Pemda> getPemdasByWilayahKerja(long idwilkerja);

	
	@Select("select tpemda.* from ms5.tpemda "
			+ " inner join ms5.tpemdatahun on tpemda.idpemda=tpemdatahun.idpemda "
			+ " where tahun= #{tahun}"
			+ " order by kodeprovinsi, kodepemda ")
	@Results({
			@Result(property = "id", column = "idpemda"),
			@Result(property = "kodeProvinsi", column = "kodeprovinsi"),
			@Result(property = "kodePemda", column = "kodepemda"),
			@Result(property = "kodeSatker", column = "kodesatker"),
			@Result(property = "namaPemda", column = "namapemda")
	})
	public List<Pemda> getPemdaByTahuns(short tahun);
	
	@Select("SELECT recordindex, useraccount, fullname, phone, email, workplace, address, avatar, jenis\n" + 
	      "FROM ms5.tusercontact where useraccount= #{id}")
	@Results({
      @Result(property = "id", column = "recordindex"),
      @Result(property = "fullName", column = "fullname"),
      @Result(property = "phone", column = "phone"),
      @Result(property = "email", column = "email"),
      @Result(property = "workplace", column = "workplace"),
      @Result(property = "address", column = "address"),
      @Result(property = "avatar", column = "avatar")
})
	public TUserContact getTuserContact(long id);
        
        
}
