package com.codvision.monitor.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codvision.monitor.entity.Website;
import com.codvision.monitor.entity.vo.WebsiteVO;
import com.codvision.monitor.mapper.WebsiteMapper;
import com.codvision.monitor.service.IWebsiteService;
import com.codvision.webcore.bean.QueryPage;
import com.codvision.webcore.service.impl.BaseServiceImpl;
import com.codvision.webcore.util.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 站点信息 服务实现类
 * </p>
 *
 * @author lingee
 * @since 2023-11-20
 */
@Service
public class WebsiteServiceImpl extends BaseServiceImpl<WebsiteMapper, Website, WebsiteVO> implements IWebsiteService {

    @Override
    public WebsiteVO getDetail(Long id) {
        WebsiteVO vo = new WebsiteVO();
        Website po = this.getById(id);
        if (null != po) {
            BeanUtil.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public IPage<WebsiteVO> getPageResult(QueryPage<Website> queryPage) {
        IPage<Website> page = queryPage.getPage();
        Website queryBean = queryPage.getCondition();
        List<WebsiteVO> pageResult = this.getBaseMapper().findAll(page, queryBean.getWebsiteName(), queryBean.getKey());
        return PageUtil.convertPage(page, pageResult);
    }

    @Override
    public boolean save(Website website) {
        Assert.notNull(website, "站点信息不能为空");
        getBaseMapper().insert(website);
        return true;
    }

    @Override
    public boolean updateById(Website website) {
        Assert.notNull(website, "站点信息不能为空");
        Assert.notNull(website.getId(), "站点信息id不能为空");
        getBaseMapper().updateById(website);
        return true;
    }

    @Override
    public boolean removeById(Serializable id) {
        Website website = getById(id);
        Assert.notNull(website, "站点信息不存在");
        getBaseMapper().deleteById(id);
        return true;
    }
}
