package com.ichangzuo.system.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ichangzuo.system.mapper.SwVersionMapper;
import com.ichangzuo.system.model.entity.Message;
import com.ichangzuo.system.model.entity.SwVersion;
import com.ichangzuo.system.model.query.SwVersionPageQuery;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class SwVersionServiceImpl extends ServiceImpl<SwVersionMapper, SwVersion> {
    public IPage<SwVersion> page(SwVersionPageQuery queryParams){
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();

        Integer softwareId = queryParams.getSoftwareId();
        Integer innerId = queryParams.getInnerId();
        Integer incompatible = queryParams.getIncompatible();

        Page<SwVersion> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SwVersion> lqw = new LambdaQueryWrapper<SwVersion>();

        if(softwareId!=null)
            lqw.eq(SwVersion::getSoftwareId, softwareId);
        if(innerId!=null)
            lqw.eq(SwVersion::getInnerId, innerId);
        if(incompatible!=null)
            lqw.eq(SwVersion::getIncompatible, incompatible);

        return baseMapper.selectPage(page, lqw.orderByDesc(SwVersion::getVersionId));
    }

    public SwVersion lastVersion(Integer swId, Integer innerId){
        return getOne(new LambdaQueryWrapper<SwVersion>()
                .eq(SwVersion::getSoftwareId, swId)
                .eq(SwVersion::getInnerId, innerId)
                .eq(SwVersion::getIsDeleted, 0)
                .orderByDesc(SwVersion::getVersionId)
                .last("limit 1")
            );
    }

    public int update(SwVersion version){
        Integer verId = version.getVersionId();
        if(verId==null){ //新增软件版本
            SwVersion lastVer = lastVersion(version.getSoftwareId(), version.getInnerId());

            if(lastVer!=null) {
                int lastV1 = lastVer.getVersionMajor();
                int lastV2 = lastVer.getVersionMinor();
                int lastV3 = lastVer.getVersionModify();
                int lastV4 = lastVer.getVersionBuild();
                int newV1 = version.getVersionMajor();
                int newV2 = version.getVersionMinor();
                int newV3 = version.getVersionModify();
                int newV4 = version.getVersionBuild();

                if(lastV1>newV1 || lastV1==newV1 && ( lastV2>newV2 || lastV2==newV2
                        && ( lastV3>newV3 || lastV3==newV3 && lastV4>=newV4 )))
                    return -1;
            }

            version.setReleaseTime((int) (System.currentTimeMillis()/1000));
        }

        if( !saveOrUpdate(version) )
            return -2;

        return 0;
    }

    public boolean setByIds(String idsStr, Integer set){
        Assert.isTrue(StrUtil.isNotBlank(idsStr), "版本列表为空");
        List<Integer> ids = Arrays.stream(idsStr.split(","))
                .map(Integer::parseInt)
                .toList();
        boolean result = true;

        for(Integer id: ids){
            result = update(new LambdaUpdateWrapper<SwVersion>()
                    .eq(SwVersion::getVersionId, id)
                    .set(SwVersion::getIsDeleted, set));

            if(!result) break;
        }
        return result;
    }
}
