package com.template.services.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.template.mapper.SmartWarningMapper;
import com.template.model.pojo.SmartWarning;
import com.template.model.pojo.SmartWarning;
import com.template.mapper.SmartWarningMapper;
import com.template.model.result.PageUtils;
import com.template.services.SmartWarningService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ceshi
 * @since 2023-12-04
 */
@Service
public class SmartWarningServiceImpl extends ServiceImpl<SmartWarningMapper, SmartWarning> implements SmartWarningService {
    @Autowired
    private SmartWarningMapper smartWarningMapper;

    @Override
    public int insertSmartWarning(SmartWarning sa) {
        int result = smartWarningMapper.insert(sa);
        return result;
    }

    @Override
    public int updateSmartWarning(SmartWarning sa) {
        int result = smartWarningMapper.updateById(sa);
        return result;
    }

    @Override
    public PageUtils<SmartWarning> queryPageSmartWarnings(int currentPage, int pageCount, String name,String state) {
        Page<SmartWarning> page = new Page<>(currentPage, pageCount);
        LambdaQueryWrapper<SmartWarning> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(ObjectUtils.isNotEmpty(name),SmartWarning::getLocation,name)
                .eq(ObjectUtils.isNotEmpty(state),SmartWarning::getStatu,state);
        IPage<SmartWarning> result = smartWarningMapper.selectPage(page, queryWrapper);
        return new PageUtils<>(result);
    }

    @Override
    public int deleteSmartWarningById(int id) {
        int result = smartWarningMapper.deleteById(id);
        return result;
    }

    @Override
    public SmartWarning getSmartById(int id) {
        SmartWarning result = smartWarningMapper.selectById(id);
        return result;
    }

    @Override
    public List<String> warningType() {
        return smartWarningMapper.warningType();
    }

    @Override
    public PageUtils<SmartWarning> pageWarning(int currentPage, int pageCount, String type, String dateTime) {
        DateTimeFormatter dateTimeFormatter2 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDate state = LocalDate.parse(dateTime, dateTimeFormatter2);
        LocalDate end = state.plusDays(1);
        Page<SmartWarning> page = new Page<>(currentPage, pageCount);
        LambdaQueryWrapper<SmartWarning> queryWrapper = new LambdaQueryWrapper<>();
        if (!"全部".equals(type)) {
            queryWrapper.eq(SmartWarning::getType,type);
        }
        queryWrapper.between(SmartWarning::getDateTime,state,end)
                .eq(SmartWarning::getStatu,0)
                .orderByDesc(SmartWarning::getDateTime);


        IPage<SmartWarning> result = smartWarningMapper.selectPage(page, queryWrapper);

        return new PageUtils<>(result);
    }


}
