package com.bonus.gs.sub.evaluate.manager.dao;

import java.util.List;
import java.util.Map;

import com.bonus.gs.sub.evaluate.manager.model.SysUser;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

@Mapper
public interface UserDao {

	@Options(useGeneratedKeys = true, keyProperty = "id")
	@Insert("insert into sys_user(username, password, nickname, headImgUrl, phone, email, birthday, sex, status, createTime, updateTime) values(#{username}, #{password}, #{nickname}, #{headImgUrl}, #{phone}, #{email}, #{birthday}, #{sex}, #{status}, now(), now())")
	int save(SysUser user);

	@Select("select *,org_id as deptId from sys_user t where t.id = #{id}")
	SysUser getById(Long id);

	//@Select("select *,org_id as deptId from sys_user t where t.username = #{username}")
	@Select("select t.*,t.org_id as deptId,sr.`name` as roleName,ifnull(poi.org_type,'0') as orgType from sys_user t  LEFT JOIN pm_org_info poi on poi.id = t.org_id LEFT JOIN sys_role_user sru on sru.userId = t.id LEFT JOIN sys_role sr on sr.id = sru.roleId  where t.username = #{username}")
	SysUser getUser(String username);

	@Update("update sys_user t set t.password = #{password} where t.id = #{id}")
	int changePassword(@Param("id") Long id, @Param("password") String password);

	Integer count(@Param("params") Map<String, Object> params);

	List<SysUser> list(@Param("params") Map<String, Object> params, @Param("offset") Integer offset,
			@Param("limit") Integer limit);

	@Delete("delete from sys_role_user where userId = #{userId}")
	int deleteUserRole(Long userId);

	int saveUserRoles(@Param("userId") Long userId, @Param("roleIds") List<Long> roleIds);

	int update(SysUser user);

	@Select("select count(id) from sys_user t where t.username = #{username}")
    int getUserCount(String username);

	String getUserRoles(Long id);

	String getIsBusinessDivision(Long id);

	void changOldTablePassword(String phone, String password);
}
