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.common.constants.EntityConstants;
import com.fkzy.warn.common.request.RequestParameter;
import com.fkzy.warn.common.response.ApiResponse;
import com.fkzy.warn.mapper.PolicyManagementMapper;
import com.fkzy.warn.model.PolicyManagement;
import com.fkzy.warn.service.PolicyManagementService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhangjing
 * @date 2023/10/16 18:17
 * @description
 */
@Service
public class PolicyManagementServiceImpl extends ServiceImpl<PolicyManagementMapper, PolicyManagement> implements
    PolicyManagementService {


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

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

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

    @Override
    public ApiResponse getPolicy(String contentType) {
        ApiResponse response = new ApiResponse();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("is_del",EntityConstants.NORMAL);
        wrapper.eq("content_type", contentType);
        wrapper.eq("is_online", EntityConstants.IS_ONLINE);
        response.setData(getOne(wrapper));
        return response;
    }

    @Override
    public Page<PolicyManagement> queryPage(RequestParameter parameter) {
        PolicyManagement entity = parameter.getParameter().toJavaObject(PolicyManagement.class);
        Page<PolicyManagement> page = new Page<PolicyManagement>(parameter.getCurrent(), parameter.getSize());
        page.setSearchCount(true);
        page.setOptimizeCountSql(true);
        QueryWrapper<PolicyManagement> eWrapper = new QueryWrapper<PolicyManagement>(entity);
        Page<PolicyManagement> result = this.page(page, eWrapper);
        return result;
    }
}
