package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreAppVersion;
import shop.alien.mapper.StoreAppVersionMapper;
import shop.alien.store.service.StoreAppVersionService;

import java.util.List;

/**
 * App版本表 服务实现类
 *
 * @author ssk
 * @since 2025-01-07
 */
@Service
public class StoreAppVersionServiceImpl extends ServiceImpl<StoreAppVersionMapper, StoreAppVersion> implements StoreAppVersionService {

    /**
     * 获取最新版本号
     *
     * @param appType app类型: 1:用户端, 2:商家端
     * @return StoreAppVersion
     */
    @Override
    public StoreAppVersion getLatestVersion(Integer appType) {
        LambdaQueryWrapper<StoreAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreAppVersion::getAppType, appType).orderByDesc(StoreAppVersion::getVersionNum).last("limit 1");
        return this.getOne(lambdaQueryWrapper);
    }

    /**
     * 保存或修改版本信息
     *
     * @param storeAppVersion App版本类
     * @return int
     */
    @Transactional
    @Override
    public R<String> saveVersionInfo(StoreAppVersion storeAppVersion) {
        LambdaQueryWrapper<StoreAppVersion> versionQuery = new LambdaQueryWrapper<>();
        versionQuery.eq(StoreAppVersion::getVersion, storeAppVersion.getVersion());
        List<StoreAppVersion> versionList = this.list(versionQuery);
        if (!versionList.isEmpty()) {
            return R.fail("该版本号已存在");
        }
        LambdaQueryWrapper<StoreAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StoreAppVersion::getAppType, storeAppVersion.getAppType()).orderByDesc(StoreAppVersion::getVersionNum).last("limit 1");
        StoreAppVersion one = this.getOne(lambdaQueryWrapper);
        // 如果该 appType 下还没有版本记录，则从1开始；否则使用最新版本号+1
        if (one == null) {
            storeAppVersion.setVersionNum(1);
        } else {
            storeAppVersion.setVersionNum(one.getVersionNum() + 1);
        }
        return this.saveOrUpdate(storeAppVersion) ? R.success("新增成功") : R.fail("新增失败");
    }

    /**
     * 列表分页查询
     *
     * @param appType  app类型: 1:用户端, 2:商家端
     * @param pageSize 条数
     * @param pageNum  页数
     * @return list
     */
    @Override
    public IPage<StoreAppVersion> getList(Integer appType, Integer pageSize, Integer pageNum) {
        IPage<StoreAppVersion> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<StoreAppVersion> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(null != appType, StoreAppVersion::getAppType, appType);
        lambdaQueryWrapper.orderByDesc(StoreAppVersion::getCreatedTime);
        return this.page(page, lambdaQueryWrapper);
    }

}
