package com.securitycontrol.system.base.service.impl;

import com.securitycontrol.common.core.constant.Constant;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.security.utils.SecurityUtils;
import com.securitycontrol.entity.system.dto.UserDto;
import com.securitycontrol.entity.system.vo.UserVo;
import com.securitycontrol.system.base.mapper.IUserMapper;
import com.securitycontrol.system.base.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

/**
 * @author：cwchen
 * @date：2024-02-20-15:49
 * @version：1.0
 * @description：用户管理-业务层
 */
@Service(value = "IUserService")
@Slf4j
public class UserServiceImpl implements IUserService {

    @Resource(name = "IUserMapper")
    private IUserMapper mapper;

    @Override
    public List<UserVo> getUserLists(UserDto dto) {
        List<UserVo> list = new ArrayList<>();
        try {
            list = mapper.getUserLists(dto);
        } catch (Exception e) {
            log.error("获取用户列表",e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateUser(UserVo vo) {
        try {
            if (StringUtils.isNotEmpty(vo.getRoleName())) {
                if (vo.getRoleName().contains(Constant.MANGER)) {
                    vo.setIsAdmin(1);
                } else {
                    vo.setIsAdmin(0);
                }
            }
            if (vo.getUserId() != null) {
                vo.setType(2);
            } else {
                vo.setType(1);
                vo.setPassword(SecurityUtils.encryptPassword(vo.getPassword()));
                vo.setCreateTime(DateTimeHelper.getNowTime());
            }
            mapper.addOrUpdateUser(vo);
        } catch (Exception e) {
            log.error("用户", e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delUser(UserDto dto) {
        try {
            mapper.delUser(dto);
        } catch (Exception e) {
            log.error("删除用户",e);
            //手动回滚异常
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getUserById(UserDto dto) {
        UserVo vo = new UserVo();
        try {
            vo = mapper.getUserById(dto);
        } catch (Exception e) {
            log.error("用户详情",e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult editPwd(UserVo vo) {
        try {
            if (vo.getUserId() == null || StringUtils.isBlank(vo.getPassword())) {
                return AjaxResult.error("参数不完整");
            }
            vo.setPassword(SecurityUtils.encryptPassword(vo.getPassword()));
            mapper.editPwd(vo);
        } catch (Exception e) {
            log.error("重置密码", e);
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
