package com.ruoyi.platform.service.impl;


import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.platform.domain.ComputingResource;
import com.ruoyi.platform.mapper.ComputingResourceDao;
import com.ruoyi.platform.service.ComputingResourceService;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

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

/**
 * (ComputingResource)表服务实现类
 *
 * @author Xidaray
 * @since 2023-11-29 14:23:01
 */
@Service("computingResourceService")
public class ComputingResourceServiceImpl implements ComputingResourceService {
    @Resource
    private ComputingResourceDao computingResourceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ComputingResource queryById(Integer id) {
        return this.computingResourceDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param computingResource 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<ComputingResource> queryByPage(ComputingResource computingResource, PageRequest pageRequest) {
        long total = this.computingResourceDao.count(computingResource);
        return new PageImpl<>(this.computingResourceDao.queryAllByLimit(computingResource, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param computingResource 实例对象
     * @return 实例对象
     */
    @Override
    public ComputingResource insert(ComputingResource computingResource) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        computingResource.setCreateBy(loginUser.getUsername());
        computingResource.setUpdateBy(loginUser.getUsername());
        computingResource.setUpdateTime(new Date());
        computingResource.setCreateTime(new Date());
        computingResource.setState(1);
        this.computingResourceDao.insert(computingResource);
        return computingResource;
    }

    /**
     * 修改数据
     *
     * @param computingResource 实例对象
     * @return 实例对象
     */
    @Override
    public ComputingResource update(ComputingResource computingResource) {
        this.computingResourceDao.update(computingResource);
        return this.queryById(computingResource.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.computingResourceDao.deleteById(id) > 0;
    }

    @Override
    public String removeById(Integer id) throws Exception {
        ComputingResource computingResource = this.computingResourceDao.queryById(id);
        if (computingResource == null){
            throw new Exception("计算资源不存在");
        }

        //判断权限，只有admin和创建者本身可以删除该数据集
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();
        String createdBy = computingResource.getCreateBy();
        if (!(StringUtils.equals(username,"admin") || StringUtils.equals(username,createdBy))){
            throw new Exception("无权限删除该计算资源");
        }

        computingResource.setState(0);
        return this.computingResourceDao.update(computingResource)>0?"删除成功":"删除失败";

    }
}
