package com.hfkj.service.sec.impl;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.dao.SecRoleMapper;
import com.hfkj.entity.SecRole;
import com.hfkj.entity.SecRoleExample;
import com.hfkj.service.sec.SecRoleService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

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

/**
 * @className: SecRoleServiceImpl
 * @author: HuRui
 * @date: 2024/3/26
 **/
@Service("secRoleService")
public class SecRoleServiceImpl implements SecRoleService {

    @Resource
    private SecRoleMapper secRoleMapper;

    @Override
    public void editData(SecRole role) {
        role.setUpdateTime(new Date());
        if (role.getId() == null) {
            role.setCreateTime(new Date());
            secRoleMapper.insert(role);
        } else {
            secRoleMapper.updateByPrimaryKey(role);
        }
    }

    @Override
    public void delete(Long roleId) {
        // 查询角色
        SecRole secRole = getDetail(roleId);
        if (secRole == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到数据");
        }
        secRole.setStatus(0);
        editData(secRole);
    }

    @Override
    public SecRole getDetail(Long roleId) {
        return secRoleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public List<SecRole> getList(Map<String, Object> param) {
        SecRoleExample example = new SecRoleExample();
        SecRoleExample.Criteria criteria = example.createCriteria().andStatusNotEqualTo(0);

        if (StringUtils.isNotBlank(MapUtils.getString(param, "roleName"))) {
            criteria.andRoleNameLike("%"+MapUtils.getString(param, "roleName")+"%");
        }

        example.setOrderByClause("create_time desc");
        return secRoleMapper.selectByExample(example);
    }


}
