package vip.xiaonuo.write.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import vip.xiaonuo.write.entity.WriteWarning;
import vip.xiaonuo.write.mapper.WriteWarningMapper;
import vip.xiaonuo.write.param.WriteWarningPageParam;
import vip.xiaonuo.write.service.IWriteWarningService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Calendar;

/**
 * <p>
 * 通用预警列表 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-01
 */
@Service
public class WriteWarningServiceImpl extends ServiceImpl<WriteWarningMapper, WriteWarning> implements IWriteWarningService {

    @Autowired
    private WriteWarningMapper warningMapper;

    @Override
    public Page<WriteWarning> page(WriteWarningPageParam pageParam) {
        String orgId = pageParam.getOrgId();
        Integer timeType = pageParam.getTimeType();
        Integer type = pageParam.getType();
        Integer pageNum = pageParam.getPageNum();
        Integer pageSize = pageParam.getPageSize();

        Page<WriteWarning> page = new Page<>(pageNum,pageSize);
        QueryWrapper<WriteWarning> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(orgId), "org_id", orgId);
        queryWrapper.eq( "type", type);
        if (0 == timeType) {
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DAY_OF_MONTH, -7);
            queryWrapper.ge("warning_date", calendar.getTime());
        }
        return warningMapper.selectPage(page, queryWrapper);
    }
}
