package cn.sh.stc.sict.cloud.upms.service.impl;

import cn.sh.stc.sict.cloud.common.core.constant.Constant;
import cn.sh.stc.sict.cloud.common.core.util.NumberUtil;
import cn.sh.stc.sict.cloud.upms.dto.RoleDTO;
import cn.sh.stc.sict.cloud.upms.model.HpRoleMenu;
import cn.sh.stc.sict.cloud.upms.service.HpRoleMenuService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;

import cn.sh.stc.sict.cloud.upms.dao.HpRoleMapper;
import cn.sh.stc.sict.cloud.upms.model.HpRole;
import cn.sh.stc.sict.cloud.upms.service.HpRoleService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service("hpRoleService")
@AllArgsConstructor
public class HpRoleServiceImpl extends ServiceImpl<HpRoleMapper, HpRole> implements HpRoleService {

    private final HpRoleMapper hpRoleMapper;
    private final HpRoleMenuService hpRoleMenuService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveDto(RoleDTO dto) {
        if (NumberUtil.isNullOrZero(dto.getRole().getId())) {
            dto.getRole().setStatus(Constant.BYTE_YES);
        }
        this.saveOrUpdate(dto.getRole());
        if (NumberUtil.isNotNullOrZero(dto.getRole().getId())) {
            hpRoleMenuService.saveBatch(dto.getRole().getId(), dto.getMenuList());
        }
        return true;
    }

    @Override
    public List<HpRole> listByUserId(Long userId) {
        return hpRoleMapper.listByUserId(userId);
    }
}
