package com.yihu.hos.system.service;

import com.yihu.hos.system.dao.UserDao;
import com.yihu.hos.system.model.SystemUser;
import com.yihu.hos.system.model.SystemUserRole;
import com.yihu.hos.web.framework.model.ActionResult;
import com.yihu.hos.web.framework.model.Result;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by chenweida on 2016/1/19.
 */
@Service("UserManager")
public class UserManager {

    public static final String BEAN_ID = "UserManager";

    @Resource(name = UserDao.BEAN_ID)
    private UserDao userdao;

   
    public Result getUserList(Map<String, Object> params) throws Exception {

        return userdao.getUserList(params);
    }

   
    public SystemUser getUserByLoginCode(String loginCode) throws Exception {
        return userdao.getUserByLoginCode(loginCode);
    }

   
    public SystemUser getUserById(String userid) throws Exception {
        return userdao.getEntity(SystemUser.class, userid);
    }

    @Transactional
    public Result addUser(SystemUser obj) throws Exception {
        SystemUser user = userdao.getUserByLoginCode(obj.getLoginCode());
        if (user != null) {
            throw new Exception("用户名存在");
        }
        userdao.saveEntity(obj);

        return Result.success("保存成功");
    }

    @Transactional
    public Result updateUser(SystemUser obj) throws Exception {
        SystemUser systemUser = userdao.getEntity(SystemUser.class, obj.getId());
        systemUser.setUserBirthday(obj.getUserBirthday());
        systemUser.setUserName(obj.getUserName());
        systemUser.setUserWechat(obj.getUserWechat());
        systemUser.setUserQq(obj.getUserQq());
        systemUser.setUserEmail(obj.getUserEmail());
        systemUser.setUserSex(obj.getUserSex());
        systemUser.setUserMobile(obj.getUserMobile());
        return Result.success("更新成功");
    }

    @Transactional
    public Result activityUser(String useid, String flag) throws Exception {
        try {
            userdao.activityUser(useid, flag);
            return new ActionResult(true, "修改成功！");
        } catch (Exception ex) {
            return new ActionResult(false, "状态修改失败！");
        }
    }

    @Transactional
    public Result deleteUser(String userid) throws Exception {
        userdao.activityUser(userid, SystemUser.ACTIVATED_DELETE);
        return Result.success("删除成功");
    }

    @Transactional
    public Result resetPassword(String userid) throws Exception {
        SystemUser systemUser=userdao.getEntity(SystemUser.class,userid);
        String saltString=systemUser.getSaltValue();
        systemUser.setPassword(DigestUtils.md5Hex("111111"+saltString));
        return Result.success("重置成功");
    }

    @Transactional
    public Result changePassword(String userid, String newps) throws Exception {
        SystemUser systemUser=userdao.getEntity(SystemUser.class,userid);
        String saltString=systemUser.getSaltValue();
        systemUser.setPassword(DigestUtils.md5Hex(newps+saltString));
        return Result.success("密码修改成功");
    }

    @Transactional
    public Result userRoleDistribute(String userId,String roleId) throws Exception
    {
        userdao.deleteUserRoleByUserId(userId);
        if(!StringUtils.isEmpty(roleId))
        {
            String[] roles = roleId.split(",");

            for(String role:roles)
            {
                SystemUserRole userRole = new SystemUserRole();
                userRole.setUserId(userId);
                userRole.setRoleId(role);

                userdao.saveEntity(userRole);
            }
        }

        return Result.success("角色分配成功");
    }

    public List<SystemUserRole> getUserRoleByUserId(String userId) throws Exception
    {
         return userdao.getUserRoleByUserId(userId);
    }
}
