package com.fkzy.warn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fkzy.warn.mapper.VersionManageMapper;
import com.fkzy.warn.model.VersionManage;
import com.fkzy.warn.service.VersionManageService;
import com.fkzy.warn.common.constants.EntityConstants;
import com.fkzy.warn.common.request.RequestParameter;
import com.fkzy.warn.common.response.ApiResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangjing
 * @date 2023/10/16 18:17
 * @description
 */
@Service
public class VersionManageServiceImpl extends ServiceImpl<VersionManageMapper, VersionManage> implements
    VersionManageService {


    @Override
    public ApiResponse saveModel(VersionManage entity, String ticket) {
        save(entity);
        return new ApiResponse();
    }

    @Override
    public ApiResponse updateModel(VersionManage entity, String ticket) {
        this.updateById(entity);
        return new ApiResponse();
    }

    @Override
    public ApiResponse deleteModel(List<String> ids) {
        VersionManage entity = new VersionManage();
        entity.setIsDel(EntityConstants.DEL);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("id", ids);
        update(entity, wrapper);
        return new ApiResponse();
    }

    @Override
    public Page<VersionManage> queryPage(RequestParameter parameter) {
        VersionManage entity = parameter.getParameter().toJavaObject(VersionManage.class);
        Page<VersionManage> page = new Page<VersionManage>(parameter.getCurrent(), parameter.getSize());
        page.setSearchCount(true);
        page.setOptimizeCountSql(true);
        QueryWrapper<VersionManage> eWrapper = new QueryWrapper<VersionManage>(entity);
        eWrapper.eq("is_del", EntityConstants.NORMAL)
            .orderByDesc("is_shelf")
            .orderByDesc("create_time");
        Page<VersionManage> result = this.page(page, eWrapper);
        return result;
    }

    @Override
    public ApiResponse shelf(VersionManage entity, String ticket) {
        ApiResponse response = new ApiResponse();
        QueryWrapper<VersionManage> wrapper = new QueryWrapper();
        wrapper.eq("is_del", EntityConstants.NORMAL);
        wrapper.eq("is_shelf", EntityConstants.IS_ONLINE);
        if (entity.getIsShelf().equals(EntityConstants.IS_ONLINE) && getOne(wrapper) != null) {
            response.recordMsgError(EntityConstants.SHELF_MSG);
            return response;
        }
        updateById(entity);
        return response;
    }
}
