package com.huiju.estateagents.sample.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.sample.entity.TaNotice;
import com.huiju.estateagents.sample.mapper.TaNoticeMapper;
import com.huiju.estateagents.sample.service.ITaNoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 开屏通知  服务实现类
 * </p>
 *
 * @author fxf
 * @since 2020-03-18
 */
@Slf4j
@Service
public class TaNoticeServiceImpl extends ServiceImpl<TaNoticeMapper, TaNotice> implements ITaNoticeService {

    @Autowired
    private TaNoticeMapper taNoticeMapper;

    @Override
    public ResponseBean listNoticeByCondition(Integer pageNum, Integer pageSize, String title, String targetType, String targetName, Integer status, String type) {
        log.info("TaNoticeServiceImpl.listNoticeByCondition 接收参数：pageNum:{},pageSize:{},title:{},targetType:{},targetName:{},status:{},type:{}",
                pageNum, pageSize, title, targetType, targetName, status, type);

        ResponseBean responseBean = new ResponseBean();

        //使用分页插件
        IPage<TaNotice> pg = new Page<>(pageNum, pageSize);
        pg = taNoticeMapper.listNoticeByCondition(pg, title, targetType, targetName, status, type);
        responseBean.addSuccess(pg);
        return responseBean;
    }

    @Override
    public ResponseBean listNoticeByConditionForAdmin(Integer pageNum, Integer pageSize, String type) {
        log.info("TaNoticeServiceImpl.listNoticeByConditionForAdmin 接收参数：pageNum:{},pageSize:{},type:{}",
                pageNum, pageSize, type);

        ResponseBean responseBean = new ResponseBean();

        //使用分页插件
        IPage<TaNotice> pg = new Page<>(pageNum, pageSize);
        pg = taNoticeMapper.listNoticeByConditionForAdmin(pg, type);
        responseBean.addSuccess(pg);
        return responseBean;
    }

    @Override
    public ResponseBean batchDelete(List<TaNotice> taNoticeList) {
        ResponseBean responseBean = new ResponseBean();

        List<TaNotice> updateList = new ArrayList<>();
        TaNotice newNotice;
        for (TaNotice taNotice : taNoticeList) {
            newNotice = new TaNotice();
            newNotice.setNoticeId(taNotice.getNoticeId());
            newNotice.setStatus(CommConstant.STATUS_DELETE);
            updateList.add(newNotice);
        }

        responseBean.addSuccess(updateBatchById(updateList));
        return responseBean;
    }

    @Override
    public Integer updateStatusByInvalidTime() {
        return taNoticeMapper.updateStatusByInvalidTime();
    }
}
