package com.yolo.keyboard.service.appversions;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.yolo.keyboard.controller.admin.appversions.vo.*;
import com.yolo.keyboard.dal.dataobject.appversions.AppVersionsDO;
import com.yolo.keyboard.framework.common.pojo.PageResult;
import com.yolo.keyboard.framework.common.pojo.PageParam;
import com.yolo.keyboard.framework.common.util.object.BeanUtils;

import com.yolo.keyboard.dal.mysql.appversions.AppVersionsMapper;

import static com.yolo.keyboard.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.convertList;
import static com.yolo.keyboard.framework.common.util.collection.CollectionUtils.diffList;import static com.yolo.keyboard.module.infra.enums.ErrorCodeConstants.APP_VERSIONS_NOT_EXISTS;

/**
 * App 版本发布与更新检查表：区分 Android/iOS、渠道，支持最低支持版本与强更策略。 Service 实现类
 *
 * @author ziin
 */
@Service
@Validated
public class AppVersionsServiceImpl implements AppVersionsService {

    @Resource
    private AppVersionsMapper appVersionsMapper;

    @Override
    public Long createAppVersions(AppVersionsSaveReqVO createReqVO) {
        // 插入
        AppVersionsDO appVersions = BeanUtils.toBean(createReqVO, AppVersionsDO.class);
        appVersionsMapper.insert(appVersions);

        // 返回
        return appVersions.getId();
    }

    @Override
    public void updateAppVersions(AppVersionsSaveReqVO updateReqVO) {
        // 校验存在
        validateAppVersionsExists(updateReqVO.getId());
        // 更新
        AppVersionsDO updateObj = BeanUtils.toBean(updateReqVO, AppVersionsDO.class);
        appVersionsMapper.updateById(updateObj);
    }

    @Override
    public void deleteAppVersions(Long id) {
        // 校验存在
        validateAppVersionsExists(id);
        // 删除
        appVersionsMapper.deleteById(id);
    }

    @Override
        public void deleteAppVersionsListByIds(List<Long> ids) {
        // 删除
        appVersionsMapper.deleteByIds(ids);
        }


    private void validateAppVersionsExists(Long id) {
        if (appVersionsMapper.selectById(id) == null) {
            throw exception(APP_VERSIONS_NOT_EXISTS);
        }
    }

    @Override
    public AppVersionsDO getAppVersions(Long id) {
        return appVersionsMapper.selectById(id);
    }

    @Override
    public PageResult<AppVersionsDO> getAppVersionsPage(AppVersionsPageReqVO pageReqVO) {
        return appVersionsMapper.selectPage(pageReqVO);
    }

}