package com.tuoheng.admin.service.accident.index;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.query.AccidentQuery;
import com.tuoheng.admin.utils.CurrentUserUtil;
import com.tuoheng.common.core.common.BaseEntity;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author xiaoying
 * @Date 2023/4/14 9:59
 */
@Slf4j
@Service
public class QueryAccidentIndexService {

    @Autowired
    private AccidentMapper accidentMapper;

    /**
     * 查询应急事故记录
     *
     * @param query
     * @return
     */
    public JsonResult index(AccidentQuery query) {
        //分页校验
        query.checkParam();
        //传参时间校验
        boolean flag = StringUtils.isNotNull(query.getBeginTime()) || StringUtils.isNotNull(query.getEndTime());
        //开启分页
        IPage<Accident> page = new Page<>(query.getPage(), query.getLimit());

        IPage<Accident> pageDate = accidentMapper.selectPage(page, Wrappers.<Accident>lambdaQuery()
                .like(StringUtils.isNotEmpty(query.getDeptName()), Accident::getDeptName, query.getDeptName())
                .like(StringUtils.isNotEmpty(query.getChannelName()), Accident::getChannelName, query.getChannelName())
                .between(flag, BaseEntity::getCreateTime, query.getBeginTime(), query.getEndTime())
                .eq(Accident::getTenantId, CurrentUserUtil.getTenantId())
                .eq(BaseEntity::getMark, MarkEnum.VALID.getCode())
                .orderByDesc(BaseEntity::getCreateTime));

        return JsonResult.success(pageDate);
    }
}
