package com.goldenwater.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.goldenwater.common.core.domain.entity.SysUser;
import com.goldenwater.common.exception.ServiceException;
import com.goldenwater.common.utils.SecurityUtils;
import com.goldenwater.common.utils.StringUtils;
import com.goldenwater.system.domain.SysRoleApperFor;
import com.goldenwater.system.mapper.SysRoleApperForMapper;
import com.goldenwater.system.service.ISysRoleApperForService;
import com.goldenwater.system.service.ISysUserService;

/**
 * 注册审批 服务层处理
 * 
 * @author goldenwater
 */
@Service
public class SysRoleApperForServiceImpl implements ISysRoleApperForService
{
    @Autowired
    private SysRoleApperForMapper sysRoleApperForMapper;

    @Autowired
    private ISysUserService userService;

    /**
     * 查询注册审批列表
     * 
     * @param sysRoleApperFor 注册审批信息
     * @return 注册审批集合
     */
    @Override
    public List<SysRoleApperFor> selectList(SysRoleApperFor sysRoleApperFor)
    {
        return sysRoleApperForMapper.selectList(sysRoleApperFor);
    }

    /**
     * 通过申请ID查询注册审批
     * 
     * @param apperId 申请ID
     * @return 注册审批对象
     */
    @Override
    public SysRoleApperFor selectById(String apperId)
    {
        return sysRoleApperForMapper.selectById(apperId);
    }

    /**
     * 新增注册审批
     * 
     * @param sysRoleApperFor 注册审批信息
     * @return 结果
     */
    @Override
    public int insert(SysRoleApperFor sysRoleApperFor)
    {
        return sysRoleApperForMapper.insert(sysRoleApperFor);
    }

    /**
     * 修改注册审批
     * 
     * @param sysRoleApperFor 注册审批信息
     * @return 结果
     */
    @Override
    public int update(SysRoleApperFor sysRoleApperFor)
    {
        return sysRoleApperForMapper.update(sysRoleApperFor);
    }

    /**
     * 更新审批状态
     * 
     * @param apperId 申请ID
     * @param pass 审批状态
     * @return 结果
     */
    @Override
    public int updatePass(String apperId, String pass)
    {
        return sysRoleApperForMapper.updatePass(apperId, pass);
    }

    /**
     * 根据登录名查询用户类型
     * 
     * @param userName 登录名
     * @return 用户类型
     */
    @Override
    public String getUserType(String userName)
    {
        return sysRoleApperForMapper.getUserType(userName);
    }

    /**
     * 删除注册审批
     * 
     * @param apperId 申请ID
     * @return 结果
     */
    @Override
    public int deleteById(String apperId)
    {
        return sysRoleApperForMapper.deleteById(apperId);
    }

    /**
     * 创建账号
     * 
     * @param apperId 申请ID
     * @param password 密码
     * @return 结果
     */
    @Override
    @Transactional
    public int createAccount(String apperId, String password)
    {
        SysRoleApperFor apperFor = sysRoleApperForMapper.selectById(apperId);
        if (StringUtils.isNull(apperFor))
        {
            throw new ServiceException("申请记录不存在");
        }

        SysUser user = new SysUser();
        user.setUserName(apperFor.getLoginName());
        user.setNickName(apperFor.getUserName());
        user.setPassword(SecurityUtils.encryptPassword(password));
        user.setPhonenumber(apperFor.getPhone());
        user.setEmail(apperFor.getEmail());
        user.setStatus("0");

        int rows = userService.insertUser(user);

        sysRoleApperForMapper.updatePass(apperId, "7");

        return rows;
    }

    /**
     * 锁定/解锁账号
     * 
     * @param apperId 申请ID
     * @param block 是否锁定
     * @return 结果
     */
    @Override
    @Transactional
    public int blockAccount(String apperId, boolean block)
    {
        SysRoleApperFor apperFor = sysRoleApperForMapper.selectById(apperId);
        if (StringUtils.isNull(apperFor))
        {
            throw new ServiceException("申请记录不存在");
        }

        String passValue = block ? "3" : "7";
        sysRoleApperForMapper.updatePass(apperId, passValue);

        SysUser user = userService.selectUserByUserName(apperFor.getLoginName());
        if (StringUtils.isNotNull(user))
        {
            if (block)
            {
                user.setStatus("1");
            }
            else
            {
                user.setStatus("0");
            }
            return userService.updateUserStatus(user);
        }

        return 1;
    }
}
