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

import com.huimv.admin.domain.primary.SysAccountEntity;
import com.huimv.admin.domain.primary.SysUserEntity;
import com.huimv.admin.repo.primary.SysAccountEntityRepository;
import com.huimv.admin.repo.primary.SysUserEntityRepository;
import com.huimv.admin.service.primary.IAccountService;
import com.huimv.admin.utils.GetMD5Str;
import com.huimv.admin.utils.Result;
import com.huimv.admin.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountServiceImpl implements IAccountService {

    @Autowired
    private SysAccountEntityRepository sysAccountEntityRepository;

    @Autowired
    private SysUserEntityRepository sysUserEntityRepository;

    @Transactional
    @Override
    public Result setAccountStatus(List<Integer> ids, Integer status) {
        try {
            for (Integer id : ids) {
                sysAccountEntityRepository.updateAccountStatus(id,status);
            }
        }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 {
            SysAccountEntity sysAccountEntity = new SysAccountEntity();
            SysUserEntity sysUserEntity = sysUserEntityRepository.findById(userId).get();
            String mobile = sysUserEntity.getMobile();
            String userName = sysUserEntity.getUserName();
            if (mobile != null && !"".equals(mobile) ){
                sysAccountEntity.setAccountName(mobile);
            }else if (userName != null && !"".equals(userName)){
                sysAccountEntity.setAccountName(userName);
            }else {
                return new Result(10000,"新增失败",false);
            }
            sysAccountEntity.setAccountStatus(1);
            sysAccountEntity.setRemark("无");
            sysAccountEntity.setPassword(GetMD5Str.getMD5Str("123456"));
            sysAccountEntityRepository.save(sysAccountEntity);
            return new Result(10000,"插入成功",true);
        }catch (Exception e){
            return new Result(10001,"插入失败",false);
        }
    }

    @Override
    public Result cancelAccount(Integer id, Integer accountStatus) {
        try {
            SysAccountEntity sysAccountEntity = sysAccountEntityRepository.findById(id).get();
            sysAccountEntity.setAccountStatus(accountStatus);
            sysAccountEntityRepository.save(sysAccountEntity);
            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) {
                sysAccountEntityRepository.deleteById(id);
            }
        }catch (Exception e){
            return new Result(10001,"删除失败",false);
        }
        return new Result(10000,"删除成功",true);
    }

    @Override
    public Result findAll() {
        List<SysAccountEntity> all = sysAccountEntityRepository.findAll();
        return new Result(ResultCode.SUCCESS,all);
    }
}
