package com.gxwebsoft.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gxwebsoft.app.mapper.AppVersionMapper;
import com.gxwebsoft.app.service.AppVersionService;
import com.gxwebsoft.app.entity.AppVersion;
import com.gxwebsoft.app.param.AppVersionParam;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 应用版本发布记录Service实现
 *
 * @author 科技小王子
 * @since 2026-03-28 21:29:44
 */
@Slf4j
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersion> implements AppVersionService {

    @Override
    public PageResult<AppVersion> pageRel(AppVersionParam param) {
        PageParam<AppVersion, AppVersionParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        List<AppVersion> list = baseMapper.selectPageRel(page, param);
        return new PageResult<>(list, page.getTotal());
    }

    @Override
    public List<AppVersion> listRel(AppVersionParam param) {
        List<AppVersion> list = baseMapper.selectListRel(param);
        PageParam<AppVersion, AppVersionParam> page = new PageParam<>();
        page.setDefaultOrder("create_time desc");
        return page.sortRecords(list);
    }

    @Override
    public AppVersion getByIdRel(Integer id) {
        AppVersionParam param = new AppVersionParam();
        param.setId(id.longValue());
        return param.getOne(baseMapper.selectListRel(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean publish(Long id, Integer publishBy) {
        AppVersion version = getById(id);
        if (version == null) {
            throw new RuntimeException("版本不存在");
        }
        Long websiteId = version.getWebsiteId();

        // 1. 将该应用下所有版本的 isCurrent 设为 false
        update(new LambdaUpdateWrapper<AppVersion>()
                .eq(AppVersion::getWebsiteId, websiteId)
                .set(AppVersion::getIsCurrent, false));

        // 2. 将当前版本设为已发布+当前版本
        version.setStatus(1);
        version.setIsCurrent(true);
        version.setPublishBy(publishBy.longValue());
        version.setPublishTime(LocalDateTime.now());

        boolean result = updateById(version);
        log.info("发布版本成功，id={}, versionNo={}", id, version.getVersionNo());
        return result;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean rollback(Long id, Integer publishBy) {
        AppVersion targetVersion = getById(id);
        if (targetVersion == null) {
            throw new RuntimeException("目标版本不存在");
        }
        Long websiteId = targetVersion.getWebsiteId();

        // 1. 将当前版本标记为已回滚
        update(new LambdaUpdateWrapper<AppVersion>()
                .eq(AppVersion::getWebsiteId, websiteId)
                .eq(AppVersion::getIsCurrent, true)
                .set(AppVersion::getStatus, 2)       // 2=已回滚
                .set(AppVersion::getIsCurrent, false));

        // 2. 将目标版本设为当前版本
        targetVersion.setStatus(1);
        targetVersion.setIsCurrent(true);
        targetVersion.setPublishBy(publishBy.longValue());
        targetVersion.setPublishTime(LocalDateTime.now());

        boolean result = updateById(targetVersion);
        log.info("回滚版本成功，id={}, versionNo={}", id, targetVersion.getVersionNo());
        return result;
    }

    @Override
    public AppVersion getCurrentVersion(Long websiteId) {
        return getOne(new LambdaQueryWrapper<AppVersion>()
                .eq(AppVersion::getWebsiteId, websiteId)
                .eq(AppVersion::getIsCurrent, true)
                .last("LIMIT 1"));
    }

}
