package com.huimv.admin.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.admin.system.entity.Account;
import com.huimv.admin.system.entity.User;
import com.huimv.admin.system.mapper.AccountMapper;
import com.huimv.admin.system.mapper.UserMapper;
import com.huimv.admin.system.service.IAccountService;
import com.huimv.common.utils.GetMD5Str;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-09
 */
@Service
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> implements IAccountService {
    @Autowired
    private AccountMapper accountMapper;

    @Autowired
    private UserMapper userMapper;

    @Transactional
    @Override
    public Result setAccountStatus(List<Integer> ids, Integer status) {
        try {
            for (Integer id : ids) {
                Account account= new Account();
                account.setId(id);
                account.setAccountStatus(status);
                accountMapper.updateById(account);
            }
        }catch (Exception e){
            return new Result(10001,"插入失败",false);
        }
        String message = "";
        if(status == 1){
            message = "你选择的账号已经启用。";
        }else{
            message = "你选择的账号已经暂停启用。";
        }
        return new Result(10000,message,true);
    }

    @Override
    public Result newAccount(Integer userId) {
        try {
            Account Account = new Account();
            User User = userMapper.selectById(userId);
            String mobile = User.getMobile();
            String userName = User.getUserName();
            if (mobile != null && !"".equals(mobile) ){
                Account.setAccountName(mobile);
            }else if (userName != null && !"".equals(userName)){
                Account.setAccountName(userName);
            }else {
                return new Result(10000,"新增失败",false);
            }
            Account.setAccountStatus(1);
            Account.setRemark("无");
            Account.setPassword(GetMD5Str.getMD5Str("123456"));
            accountMapper.insert(Account);
            return new Result(10000,"插入成功",true);
        }catch (Exception e){
            return new Result(10001,"插入失败",false);
        }
    }

    @Override
    public Result cancelAccount(Integer id, Integer accountStatus) {
        try {
            Account Account = accountMapper.selectById(id);
            Account.setAccountStatus(accountStatus);
            accountMapper.updateById(Account);
            return new Result(10000,"修改成功",true);
        }catch (Exception e){
            return new Result(10001,"修改失败",false);
        }
    }

    @Override
    public Result removeAccount(List<Integer> ids) {
        try {
            for (Integer id : ids) {
                accountMapper.deleteById(id);
            }
        }catch (Exception e){
            return new Result(10001,"删除失败",false);
        }
        return new Result(10000,"删除成功",true);
    }

    @Override
    public Result findAll() {
        List<Account> all = accountMapper.selectList(null);
        return new Result(ResultCode.SUCCESS,all);
    }
}
