package com.ruoyi.platform.service.impl;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.platform.constant.Constant;
import com.ruoyi.platform.domain.AssetWorkflow;
import com.ruoyi.platform.domain.ImageVersion;
import com.ruoyi.platform.mapper.AssetWorkflowDao;
import com.ruoyi.platform.mapper.ImageDao;
import com.ruoyi.platform.mapper.ImageVersionDao;
import com.ruoyi.platform.domain.ModelsVersion;
import com.ruoyi.platform.service.ImageVersionService;
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;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (ImageVersion)表服务实现类
 *
 * @author Xidaray
 * @since 2024-03-05 15:00:02
 */
@Service("imageVersionService")
public class ImageVersionServiceImpl implements ImageVersionService {
    @Resource
    private ImageVersionDao imageVersionDao;

    @Resource
    private AssetWorkflowDao assetWorkflowDao;

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

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

    @Override
    public List<ImageVersion> queryByImageId(Integer imageId){
        return imageVersionDao.queryByImageId(imageId);
    }

    @Override
    public String removeById(Integer id) throws Exception {
        ImageVersion imageVersion = this.imageVersionDao.queryById(id);
        if (imageVersion == null){
            return "该版本下模型文件信息不存在";
        }

        List<AssetWorkflow> assetWorkflow = assetWorkflowDao.getAssetWorkflow(Long.valueOf(imageVersion.getImageId()), Constant.Asset_Type_Image, imageVersion.getTagName());
        if (assetWorkflow != null && !assetWorkflow.isEmpty()) {
            String workflows = String.join(",", assetWorkflow.stream().map(AssetWorkflow::getWorkflowName).collect(Collectors.toSet()));
            throw new Exception("该镜像版本被流水线：" + workflows + "使用，不能删除，请先删除流水线。");
        }

        //判断权限，只有admin和创建者本身可以删除该数据集
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();
        String createdBy = imageVersion.getCreateBy();
        if (!(StringUtils.equals(username,"admin") || StringUtils.equals(username,createdBy))){
            return "无权限删除该版本下模型信息";
        }

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

    }

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

    /**
     * 修改数据
     *
     * @param imageVersion 实例对象
     * @return 实例对象
     */
    @Override
    public ImageVersion update(ImageVersion imageVersion) {
        int currentState = imageVersion.getState();
        if(currentState == 0){
            throw new RuntimeException("镜像版本已被删除，无法更新。");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        imageVersion.setUpdateBy(loginUser.getUsername());
        imageVersion.setUpdateTime(new Date());
        this.imageVersionDao.update(imageVersion);
        return this.queryById(imageVersion.getId());

    }

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