package org.springblade.modules.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wn.common.datamodel.ModelConvertUtil;
import com.wn.common.validator.Validator;
import org.springblade.modules.app.common.dto.AppVersionDTO;
import org.springblade.modules.app.common.query.AppVersionQuery;
import org.springblade.modules.app.common.query.LatestAppVersionQuery;
import org.springblade.modules.app.constant.DbField;
import org.springblade.modules.app.constant.ErrorCode;
import org.springblade.modules.app.entity.AppVersionDO;
import org.springblade.modules.app.mapper.AppVersionMapper;
import org.springblade.modules.app.service.IAppVersionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * app版本管理表 服务实现类
 * </p>
 *
 * @author huyinuo
 * @since 2022-02-23
 */
@Service
public class AppVersionServiceImpl extends ServiceImpl<AppVersionMapper, AppVersionDO> implements IAppVersionService {

    @Override
    public Page<AppVersionDTO> pageAppVersion(AppVersionQuery appVersionQuery) {
        Page<AppVersionDO> page = appVersionQuery.covertToPage();
        QueryWrapper<AppVersionDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc(DbField.CREATED_TIME);
        Page<AppVersionDO> appVersionDOPage = page(page, wrapper);
        return ModelConvertUtil.copyPageTo(appVersionDOPage, AppVersionDTO.class);
    }

    @Override
    public AppVersionDTO getAppVersion(Long id) {
        Validator.notNull(id, ErrorCode.INPUT_PARAMETERS_NULL);
        AppVersionDO appVersionDO = getById(id);
        return ModelConvertUtil.copyTo(appVersionDO, AppVersionDTO.class);
    }

    @Override
    public Long addAppVersion(AppVersionDTO appVersionDTO) {
        AppVersionDO appVersionDO = ModelConvertUtil.copyTo(appVersionDTO, AppVersionDO.class);
        Validator.isTrue(save(appVersionDO), ErrorCode.SAVE_DATA_INTO_DB_ERROR);
        return appVersionDO.getId();
    }

    @Override
    public Boolean updateAppVersion(AppVersionDTO appVersionDTO) {
        AppVersionDO appVersionDO = ModelConvertUtil.copyTo(appVersionDTO, AppVersionDO.class);
        Validator.isTrue(updateById(appVersionDO), ErrorCode.UPDATE_DATA_ERROR);
        return true;
    }

    @Override
    public AppVersionDTO latestAppVersion(LatestAppVersionQuery latestAppVersionQuery) {
        Validator.notNull(latestAppVersionQuery, ErrorCode.INPUT_PARAMETERS_NULL);
        Validator.notNull(latestAppVersionQuery.getChannelType(), ErrorCode.INPUT_PARAMETERS_NULL);
        QueryWrapper<AppVersionDO> wrapper = new QueryWrapper<>();
        wrapper.eq(DbField.CHANNEL_TYPE, latestAppVersionQuery.getChannelType());
        wrapper.orderByDesc(DbField.CREATED_TIME);
        wrapper.last("limit 1");
        AppVersionDO appVersionDO = getOne(wrapper);
        return ModelConvertUtil.copyTo(appVersionDO, AppVersionDTO.class);
    }
}
