package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.AlarmFire;
import com.usky.fire.domain.DataJinganAnalysis;
import com.usky.fire.mapper.DataJinganAnalysisMapper;
import com.usky.fire.service.DataJinganAnalysisService;
import com.usky.common.mybatis.core.AbstractCrudService;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 静安电子督察单数据 服务实现类
 * </p>
 *
 * @author han
 * @since 2024-03-07
 */
@Service
public class DataJinganAnalysisServiceImpl extends AbstractCrudService<DataJinganAnalysisMapper, DataJinganAnalysis> implements DataJinganAnalysisService {

    @Override
    public CommonPage<DataJinganAnalysis> analysisList(String startTime, String endTime, String superviseCode,Integer id,
                                                       Integer handleStatus, Integer pageNum, Integer pageSize) {
        IPage<DataJinganAnalysis> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DataJinganAnalysis> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(DataJinganAnalysis::getTenantId, SecurityUtils.getTenantId())
                .eq(null != handleStatus,DataJinganAnalysis::getHandleStatus, handleStatus)
                .eq(null != id,DataJinganAnalysis::getId, id)
                .like(StringUtils.isNotBlank(superviseCode),DataJinganAnalysis::getSuperviseCode,superviseCode)
                .between(StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime),
                        DataJinganAnalysis::getTimestamp, startTime, endTime)
                .orderByDesc(DataJinganAnalysis::getId);
        page = this.page(page,queryWrapper);
        return new CommonPage<>(page.getRecords(),page.getTotal(),page.getCurrent(),page.getSize());
    }

    public void update(DataJinganAnalysis dataJinganAnalysis){
        dataJinganAnalysis.setHandleStatus(1);
        dataJinganAnalysis.setHandleBy(SecurityUtils.getUsername());
        dataJinganAnalysis.setHandleTime(LocalDateTime.now());
        dataJinganAnalysis.setHandlePhone(SecurityUtils.getLoginUser().getSysUser().getPhonenumber());
        this.updateById(dataJinganAnalysis);
    }
}
