package com.hssx.pcbms.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hssx.pcbms.entity.Permission;
import com.hssx.pcbms.entity.UserPermission;
import com.hssx.pcbms.mapper.PermissionMapper;
import com.hssx.pcbms.mapper.UserPermissionMapper;
import com.hssx.pcbms.service.PermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hssx.pcbms.util.HttpRespMsg;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴涛涛
 * @since 2019-10-24
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;
    @Resource
    private UserPermissionMapper userPermissionMapper;
    @Override
    public HttpRespMsg add(Permission permission) {
        HttpRespMsg msg = new HttpRespMsg();
        permissionMapper.insert(permission);
        return msg;
    }

    @Override
    public HttpRespMsg del(Permission permission) {
        HttpRespMsg msg = new HttpRespMsg();
        List<UserPermission> list = userPermissionMapper.selectList(new QueryWrapper<UserPermission>().eq("permission_id", permission.getId()).last("limit 1"));
        if(list.size()>0){
            msg.setError("该权限已被使用到系统中，暂不支持删除操作。");
        }
        permissionMapper.deleteById(permission.getId());
        return msg;
    }
}
