package pwc.taxtech.atms.dao;

import java.util.List;
import org.apache.ibatis.annotations.Mapper;
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.session.RowBounds;
import pwc.taxtech.atms.MyMapper;
import pwc.taxtech.atms.dto.user.RoleInfo;
import pwc.taxtech.atms.dto.user.UserDto;
import pwc.taxtech.atms.dto.user.UserOrgRoleDto;
import pwc.taxtech.atms.entitiy.User;
import pwc.taxtech.atms.entitiy.UserExample;

@Mapper
public interface UserMapper extends MyMapper {
    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    long countByExample(UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int deleteByExample(UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int deleteByPrimaryKey(String ID);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int insert(User record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int insertSelective(User record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    List<User> selectByExampleWithRowbounds(UserExample example, RowBounds rowBounds);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    List<User> selectByExample(UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    User selectByPrimaryKey(String ID);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int updateByExample(@Param("record") User record, @Param("example") UserExample example);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int updateByPrimaryKeySelective(User record);

    /**
     * This method was generated by MyBatis Generator.
     * This method corresponds to the database table User
     *
     * @mbg.generated
     */
    int updateByPrimaryKey(User record);

    /**
     * select one user record by user name
     */
    User selectByUserName(String userName);
    /**
     * select one user record by user name with ingore case
     */
    User selectByUserNameIgnoreCase(String userName);
    /**
     * select one user record by user name with ingore case and different user ID
     */
    User selectUserWithSameUserName(UserDto userDto);
    /**
     * select one user record by email with ingore case and different user ID
     */
    User selectUserWithSameEmail(UserDto userDto);
    /**
     * select one user record by email with ingore case
     */
    User selectByEmailIgnoreCase(String email);
    /**
     * select one user record with its related organization information by user ID
     */
    @Select("select u.UserName as UserName, o.ID as OrganizationID, o.Name as OrganizationName, "
            + "u.Email as Email, u.Status as Status, u.AttemptTimes as AttemptTimes, u.CreateTime as CreateTime, "
            + "u.ExpirationDate as ExpirationDate, u.IsAdmin as IsAdmin, u.ID as ID, u.LockedTime as LockedTime, "
            + "u.LoginType as LoginType, u.Password as Password, u.PasswordUpdateTime as PasswordUpdateTime, u.UpdateTime as UpdateTime from User as u "
            + "left join Organization as o on u.OrganizationID = o.ID "
            + "where u.ID = #{userId}")
    @Results({
        @Result(id = true, column = "ID", property = "ID"),
        @Result(column = "UserName", property = "userName"),
        @Result(column = "OrganizationID", property = "organizationID"),
        @Result(column = "OrganizationName", property = "organizationName"),
        @Result(column = "Email", property = "email"),
        @Result(column = "Status", property = "status"),
        @Result(column = "AttemptTimes", property = "attemptTimes"),
        @Result(column = "CreateTime", property = "createTime"),
        @Result(column = "ExpirationDate", property = "expirationDate"),
        @Result(column = "IsAdmin", property = "isAdmin"),
        @Result(column = "LockedTime", property = "lockedTime"),
        @Result(column = "LoginType", property = "loginType"),
        @Result(column = "Password", property = "password"),
        @Result(column = "PasswordUpdateTime", property = "passwordUpdateTime"),
        @Result(column = "UpdateTime", property = "updateTime")
    })
    List<UserDto> selectUserWithOrgInfoByID(@Param(value="userId") String userId);

    @Select("select r.ID as ID, r.Name as Name from UserRole as u "
            + "join Role as r on u.RoleID = r.ID "
            + "where u.UserID = #{userId}")
    @Results({
        @Result(id = true, column = "ID", property = "ID"),
        @Result(column = "Name", property = "name")
    })
    List<RoleInfo> selectUserRoleInfoByID(@Param(value="userId") String userId);

    @Select("select udv.RoleID as RoleID, r.Name as RoleName, udv.UserDimensionValueID as ID "
            + "from UserDimensionValueRole as udv "
            + "join Role as r on udv.RoleID = r.ID")
    @Results({
        @Result(id = true, column = "ID", property = "ID"),
        @Result(column = "RoleID", property = "roleID"),
        @Result(column = "RoleName", property = "roleName")
    })
    List<UserOrgRoleDto> selectUserDimensionValueRole();

    @Select("select q.RoleID as RoleID, r.Name as RoleName, q.UserOrganizationID as ID "
            + "from UserOrganizationRole as q "
            + "join Role as r on q.RoleID = r.ID")
    @Results({
        @Result(id = true, column = "ID", property = "ID"),
        @Result(column = "RoleID", property = "roleID"),
        @Result(column = "RoleName", property = "roleName")
    })
    List<UserOrgRoleDto> selectUserOrganizationRole();
}