package com.wgb.system.service.service;

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.wgb.system.service.domain.mapper.SysExhibitionMapper;
import com.wgb.system.service.domain.model.SysExhibition;
import com.wgb.system.service.domain.request.SysExhibitionList;
import com.wgb.system.service.domain.response.LayuiTableResponse;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class SysExhibitionService extends ServiceBase {

    private final SysExhibitionMapper sysExhibitionMapper;

    public SysExhibitionService(SysExhibitionMapper sysExhibitionMapper) {
        this.sysExhibitionMapper = sysExhibitionMapper;
    }

    public LayuiTableResponse getList(SysExhibitionList sysExhibitionList) {
        QueryWrapper<SysExhibition> queryWrapper = new QueryWrapper<>();
        if (sysExhibitionList.getState()!=null){
            queryWrapper.lambda().eq(SysExhibition::getState, sysExhibitionList.getState());
        }
        IPage<SysExhibition> iPage = new Page<>(sysExhibitionList.getPage(), sysExhibitionList.getLimit());
        IPage<SysExhibition> pageList = sysExhibitionMapper.selectPage(iPage, queryWrapper);
        return new LayuiTableResponse(0, "", pageList.getTotal(), pageList.getRecords());
    }

    public void insertOrUpdata(SysExhibition sysExhibition) throws Exception {
        if (sysExhibition.getId() != null) {
            sysExhibitionMapper.updateById(sysExhibition);
        } else {
            sysExhibitionMapper.insert(sysExhibition);
        }
    }

    public List<SysExhibition> getSysExhibitionList(SysExhibitionList sysExhibitionList) {
        QueryWrapper<SysExhibition> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().le(SysExhibition::getStartTime, LocalDateTime.now())
                .and(i -> i.gt(SysExhibition::getEndTime, LocalDateTime.now()).or().isNull(SysExhibition::getEndTime));
        if (sysExhibitionList.getState()!=null){
            queryWrapper.lambda().eq(SysExhibition::getState, sysExhibitionList.getState());
        }
        return sysExhibitionMapper.selectList(queryWrapper);
    }

    public void updateByState(Long id, Integer state){
        SysExhibition sysExhibition= sysExhibitionMapper.selectOne(new QueryWrapper<SysExhibition>().lambda().eq(SysExhibition::getId,id));
        sysExhibition.setState(state);
        sysExhibitionMapper.updateById(sysExhibition);
    }

}
