package com.czg.service.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.service.system.mapper.VersionMapper;
import com.czg.system.dto.VersionDTO;
import com.czg.system.entity.Version;
import com.czg.system.service.VersionService;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 版本管理 服务层实现。
 *
 * @author mac
 * @since 2025-02-12
 */
@Service
public class VersionServiceImpl extends ServiceImpl<VersionMapper, Version> implements VersionService {

    @Override
    public CzgResult<Long> insertVersion(VersionDTO versionDTO) {
        Version version = getVersionBySource(versionDTO.getSource(), versionDTO.getType());
        if (version != null) {
            return CzgResult.failure("版本已存在");
        }

        version = BeanUtil.toBean(versionDTO, Version.class);
        version.setUpdateUserId(StpKit.USER.getLoginId(1L));
        save(version);
        return CzgResult.success(version.getId());
    }

    @Override
    public CzgResult<Long> updateVersion(VersionDTO versionDTO) {
        Version version = getVersionBySource(versionDTO.getSource(), versionDTO.getType());
        if (version != null && !version.getId().equals(versionDTO.getId())) {
            return CzgResult.failure("版本已存在");
        }

        version = getById(versionDTO.getId());
        if (version == null) {
            return CzgResult.failure("版本不存在");
        }

        if (StrUtil.isNotBlank(versionDTO.getSource())) {
            version.setSource(versionDTO.getSource());
        }
        if (StrUtil.isNotBlank(versionDTO.getVersion())) {
            version.setVersion(versionDTO.getVersion());
        }
        if (StrUtil.isNotBlank(versionDTO.getType())) {
            version.setType(versionDTO.getType());
        }
        if (StrUtil.isNotBlank(versionDTO.getUrl())) {
            version.setUrl(versionDTO.getUrl());
        }
        version.setIsForce(versionDTO.getIsForce());
        version.setMessage(versionDTO.getMessage());
        version.setUpdateUserId(StpKit.USER.getLoginId(1L));

        updateById(version);
        return CzgResult.success(version.getId());
    }

    @Override
    public CzgResult<Long> deleteVersion(Long id) {
        boolean exists = super.exists(new QueryWrapper().eq(Version::getId, id));
        if (!exists) {
            return CzgResult.failure("版本不存在");
        }
        removeById(id);
        return CzgResult.success(id);
    }

    @Override
    public CzgResult<VersionDTO> getVersionInfo(String source, String type) {
        Version version = getVersionBySource(source, type);
        VersionDTO versionDTO = BeanUtil.toBean(version, VersionDTO.class);
        return CzgResult.success(versionDTO);
    }

    @Override
    public CzgResult<List<VersionDTO>> getVersionList() {
        List<Version> versions = list();
        List<VersionDTO> dtoList = BeanUtil.copyToList(versions, VersionDTO.class);
        return CzgResult.success(dtoList);
    }

    private Version getVersionBySource(String source, String type) {
        return getOne(new QueryWrapper().eq(Version::getSource, source).eq(Version::getType, type));
    }
}
