package com.ruoyi.platform.service.impl;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.platform.domain.AssetIcon;
import com.ruoyi.platform.domain.DatasetVersion;
import com.ruoyi.platform.domain.WorkflowParam;
import com.ruoyi.platform.mapper.WorkflowParamDao;
import com.ruoyi.platform.service.WorkflowParamService;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

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

/**
 * (WorkflowParam)表服务实现类
 *
 * @author Xidaray
 * @since 2024-03-18 14:12:07
 */
@Service("workflowParamService")
public class WorkflowParamServiceImpl implements WorkflowParamService {
    @Resource
    private WorkflowParamDao workflowParamDao;

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

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

    /**
     * 新增数据
     *
     * @param workflowParam 实例对象
     * @return 实例对象
     */
    @Override
    public WorkflowParam insert(WorkflowParam workflowParam) throws Exception {
        insertPrepare(workflowParam);
        this.workflowParamDao.insert(workflowParam);
        return workflowParam;
    }

    /**
     * 修改数据
     *
     * @param workflowParam 实例对象
     * @return 实例对象
     */
    @Override
    public WorkflowParam update(WorkflowParam workflowParam) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        workflowParam.setUpdateBy(loginUser.getUsername());
        workflowParam.setUpdateTime(new Date());
        this.workflowParamDao.update(workflowParam);
        return this.queryById(workflowParam.getId());
    }

    @Override
    public String removeById(Integer id) throws Exception {
        WorkflowParam workflowParam = this.workflowParamDao.queryById(id);
        if (workflowParam == null){
            throw new Exception("流水线参数不存在");
        }

        //判断权限，只有admin和创建者本身可以删除
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();


        String createdBy = workflowParam.getCreateBy();
        if (!(StringUtils.equals(username,"admin") || StringUtils.equals(username,createdBy))){
            throw new Exception("无权限删除该流水线参数");
        }

        workflowParam.setState(0);
        return this.workflowParamDao.update(workflowParam)>0?"删除成功":"删除失败";
    }

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

    private void insertPrepare(WorkflowParam workflowParam) throws Exception {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        workflowParam.setCreateBy(loginUser.getUsername());
        workflowParam.setUpdateBy(loginUser.getUsername());
        workflowParam.setUpdateTime(new Date());
        workflowParam.setCreateTime(new Date());
        workflowParam.setState(1);
    }
}
