package com.fdkankan.platform.agent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.constant.TbStatus;
import com.fdkankan.platform.agent.entity.AgentNotice;
import com.fdkankan.platform.agent.mapper.IAgentNoticeMapper;
import com.fdkankan.platform.agent.service.IAgentNoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fdkankan.platform.agent.vo.AgentNoticeVo;
import com.fdkankan.common.constant.ErrorCode;
import com.fdkankan.common.constant.ServerCode;
import com.fdkankan.common.exception.BusinessException;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 代理商公告表 服务实现类
 * </p>
 *
 * @author
 * @since 2021-12-23
 */
@Service
public class AgentNoticeServiceImpl extends ServiceImpl<IAgentNoticeMapper, AgentNotice> implements IAgentNoticeService {



    @Override
    public AgentNoticeVo getAgentNotice() {
        QueryWrapper<AgentNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(AgentNotice::getTbStatus, TbStatus.VALID.code());
        queryWrapper.lambda().orderByDesc(AgentNotice::getCreateTime);

        Page<AgentNotice> agentNoticePage = new Page<>();
        agentNoticePage.setCurrent(1);
        agentNoticePage.setSize(1);

        Page<AgentNotice> page = this.page(agentNoticePage, queryWrapper);
        List<AgentNotice> list = page.getRecords();
        if(list == null || list.size() <=0){
            throw new BusinessException(ErrorCode.NOT_RECORD);
        }

        AgentNoticeVo agentNoticeVo = new AgentNoticeVo();
        BeanUtils.copyProperties(list.get(0), agentNoticeVo);
        return agentNoticeVo;
    }

    @Override
    public String saveNotice(String title, String content) {
        AgentNotice agentNotice = new AgentNotice();
        agentNotice.setTitle(title);
        agentNotice.setContent(content);
        if(!this.save(agentNotice)){
            throw new BusinessException(ErrorCode.ERROR_MSG);
        }
        return ServerCode.SUCCESS.message();
    }
}
