package com.jkcredit.invoice.hub.service.role;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.constant.ExceptionMessage;
import com.jkcredit.invoice.hub.mapper.RoleMapper;
import com.jkcredit.invoice.hub.model.dto.role.RoleDto;
import com.jkcredit.invoice.hub.model.po.role.RolePo;
import com.jkcredit.invoice.hub.model.vo.role.RoleVo;
import com.jkcredit.invoice.hub.service.base.BaseService;
import com.jkcredit.invoice.hub.spi.lang.exception.ServiceException;
import com.jkcredit.invoice.hub.util.AssertUtils;
import com.jkcredit.invoice.hub.util.BeanUtil;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/13 11:51
 * @version: V1.0
 **/
@Slf4j
@Service
@Transactional(readOnly = true)
public class RoleServiceImpl extends BaseService implements RoleService {
    @Resource
    RoleMapper roleMapper;

    @Override
    public IPage<RoleVo> getRoles(Page page, RoleDto roleDto) {
        IPage<RolePo> rolePoIPage = roleMapper.getRoles(page, roleDto);
        return rolePoIPage.convert(this::convert);
    }

    private RoleVo convert(RolePo po) {
        RoleVo vo = new RoleVo();
        BeanUtils.copyProperties(po, vo);
        return vo;
    }

    @Transactional(rollbackFor = ServiceException.class)
    @Override
    public void addRole(RoleDto roleDto) {
        validate(roleDto);
        RolePo rolePo = new RolePo();
        BeanUtil.copyProperties(rolePo, roleDto);
        rolePo.setCreateTime(new Date());
        try {
            roleMapper.insert(rolePo);
        } catch (Exception e) {
            log.error("新增角色失败，失败原因：{}", e.getMessage());
            throw new ServiceException("新增角色失败");
        }
    }

    @Override
    public List<RoleVo> getRoles() {
        List<RolePo> rolePoList = roleMapper.getRolesWithoutPage();
        List<RoleVo> roleVoList = new ArrayList<>();
        for (RolePo rolePo : rolePoList) {
            roleVoList.add(convert(rolePo));
        }
        return roleVoList;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void deleteRole(Long id) {
        RolePo rolePo = roleMapper.selectById(id);
        if (rolePo == null) {
            throw new ServiceException(ExceptionMessage.ROLE_NOT_EXIST);
        } else {
            rolePo.setActivated(CommonConstant.ACTIVATED_DELETED);
            try {
                roleMapper.updateById(rolePo);
            } catch (Exception e) {
                log.error("删除角色失败，失败原因：{}", e.getMessage());
                throw new ServiceException("删除角色失败");
            }
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updateRole(RoleDto roleDto) {
        RolePo rolePo = roleMapper.selectById(roleDto.getId());
        AssertUtils.assertNotNull(rolePo, CommonConstant.USER_NOT_EXIST);
        if (!StringUtil.isNullOrEmpty(roleDto.getRoleName())) {
            rolePo.setRoleName(roleDto.getRoleName());
        }
        rolePo.setUpdateTime(new Date());
        try {
            roleMapper.updateById(rolePo);
        } catch (ServiceException e) {
            log.error("更新角色失败，失败原因：{}", e.getMessage());
            throw new ServiceException("更新角色失败");
        }
    }
}
