package org.dromara.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.app.domain.AppVersion;
import org.dromara.app.domain.bo.AppVersionBo;
import org.dromara.app.domain.vo.AppVersionVo;
import org.dromara.app.mapper.AppVersionMapper;
import org.dromara.app.service.IAppVersionService;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * APP版本Service业务层处理
 *
 * @author Lion Li
 * @date 2026-07-01
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppVersionServiceImpl implements IAppVersionService {

    private static final String STATUS_ENABLED = "1";

    private final AppVersionMapper baseMapper;

    /**
     * 查询APP版本
     *
     * @param id 主键
     * @return APP版本
     */
    @Override
    public AppVersionVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询APP版本列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return APP版本分页列表
     */
    @Override
    public TableDataInfo<AppVersionVo> queryPageList(AppVersionBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppVersion> lqw = buildQueryWrapper(bo);
        Page<AppVersionVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的APP版本列表
     *
     * @param bo 查询条件
     * @return APP版本列表
     */
    @Override
    public List<AppVersionVo> queryList(AppVersionBo bo) {
        LambdaQueryWrapper<AppVersion> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppVersion> buildQueryWrapper(AppVersionBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppVersion> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppVersion::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getPlatform()), AppVersion::getPlatform, bo.getPlatform());
        lqw.eq(StringUtils.isNotBlank(bo.getLatestVersion()), AppVersion::getLatestVersion, bo.getLatestVersion());
        lqw.eq(bo.getVersionCode() != null, AppVersion::getVersionCode, bo.getVersionCode());
        lqw.eq(StringUtils.isNotBlank(bo.getForceUpdate()), AppVersion::getForceUpdate, bo.getForceUpdate());
        lqw.eq(StringUtils.isNotBlank(bo.getDownloadUrl()), AppVersion::getDownloadUrl, bo.getDownloadUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getReleaseNotes()), AppVersion::getReleaseNotes, bo.getReleaseNotes());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppVersion::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增APP版本
     *
     * @param bo APP版本
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppVersionBo bo) {
        AppVersion add = MapstructUtils.convert(bo, AppVersion.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP版本
     *
     * @param bo APP版本
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppVersionBo bo) {
        AppVersion update = MapstructUtils.convert(bo, AppVersion.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppVersion entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除APP版本信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
    @Override
    public AppVersionVo queryLatestByPlatform(String platform) {
        if (StringUtils.isBlank(platform)) {
            return null;
        }
        AppVersion version = baseMapper.selectOne(
            Wrappers.<AppVersion>lambdaQuery()
                .eq(AppVersion::getPlatform, platform)
                .eq(AppVersion::getStatus, STATUS_ENABLED)
                .orderByDesc(AppVersion::getVersionCode)
                .orderByDesc(AppVersion::getId)
                .last("limit 1")
        );
        if (version == null) {
            return null;
        }
        return MapstructUtils.convert(version, AppVersionVo.class);
    }
}
