package com.tuoheng.admin.service.warning.query;

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.common.ServiceExceptionEnum;
import com.tuoheng.admin.entity.domain.Warning;
import com.tuoheng.admin.entity.query.WarningQuery;
import com.tuoheng.admin.entity.vo.WarningVO;
import com.tuoheng.admin.enums.*;
import com.tuoheng.admin.mapper.WarningMapper;
import com.tuoheng.common.exception.ServiceException;
import com.tuoheng.common.utils.DateUtils;
import com.tuoheng.common.utils.JsonResult;
import com.tuoheng.common.utils.StringUtils;
import com.tuoheng.system.entity.User;
import com.tuoheng.system.utils.ShiroUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author ChengWang
 * @Date 2023/2/7
 */
@Slf4j
@Service
public class QueryWarningPageListService {

    @Autowired
    private WarningMapper warningMapper;

    /**
     * 告警列表查询 分页
     *
     * @param query
     * @return
     */
    public JsonResult index(WarningQuery query) {
        //校验
        if (query.getLimit() == null && query.getPage() == null) {
            throw new ServiceException(ServiceExceptionEnum.PARAMETER_IS_NULL);
        }
        User user = ShiroUtils.getUserInfo();
        Integer tenantId = user.getTenantId();

        if (null == tenantId) {
            return JsonResult.error(WarningEnum.TENANT_ID_IS_NULL.getCode(), WarningEnum.TENANT_ID_IS_NULL.getMsg());
        }
        query.setTenantId(tenantId);
        IPage<Warning> page = new Page<>(query.getPage(), query.getLimit());
        IPage<WarningVO> pageData = new Page<>(query.getPage(), query.getLimit());
        //时间
        Date startTime = null;
        Date endTime = null;
        if (StringUtils.isNotEmpty(query.getStartTime()) && StringUtils.isNotEmpty(query.getEndTime())) {
            startTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,query.getStartTime()+" 00:00:00");
            endTime = DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS,query.getEndTime()+" 23:59:59");
        }
        query.setWaringStartTime(startTime);
        query.setWaringEndTime(endTime);
        //获取分页数据

        IPage<Warning> warningPageData = warningMapper.selectPage(page, Wrappers.<Warning>lambdaQuery()
                .eq(null != query.getDiscoveryWay(), Warning::getDiscoveryWay, query.getDiscoveryWay())
                .eq(null != query.getStatus(), Warning::getStatus, query.getStatus())
                .eq(Warning::getTenantId,tenantId)
                .eq(Warning::getFlag, FlagEnum.INSPECTION_ACCIDENT.getCode())
                .between(null != query.getWaringStartTime() && null != query.getWaringEndTime(), Warning::getCreateTime, query.getWaringStartTime(), query.getWaringEndTime())
                .eq(Warning::getMark, MarkTypeEnum.VALID.getCode())
                .orderByDesc(Warning::getCreateTime));
        if (null == warningPageData) {
            return null;
        }
        //属性赋值
        List<WarningVO> collect = warningPageData.getRecords().stream().map(x -> {
            WarningVO vo = new WarningVO();
            BeanUtils.copyProperties(x, vo);
            //时间
            vo.setWaringTime(x.getCreateTime());
            //预警来源
            if(x.getDiscoveryWay() == DiscoveryWayEnum.SURVEILLANCE_CAMERA.getCode()){
                vo.setDiscoverWayName(DiscoveryWayEnum.SURVEILLANCE_CAMERA.getDescription());
            }
            if(x.getDiscoveryWay() == DiscoveryWayEnum.UAV_PATROL.getCode()){
                vo.setDiscoverWayName(DiscoveryWayEnum.UAV_PATROL.getDescription());
            }
            if(x.getDiscoveryWay() == DiscoveryWayEnum.MANUAL_INSPECTION.getCode()){
                vo.setDiscoverWayName(DiscoveryWayEnum.MANUAL_INSPECTION.getDescription());
            }
            //状态
            if(x.getStatus() == WarningStatusEnum.WAIT_CONFIRM.getCode()){
                vo.setStatusName(WarningStatusEnum.WAIT_CONFIRM.getDescription());
            }
            if(x.getStatus() == WarningStatusEnum.CONFIRM.getCode()){
                vo.setStatusName(WarningStatusEnum.CONFIRM.getDescription());
            }
            if(x.getStatus() == WarningStatusEnum.IGNORE.getCode()){
                vo.setStatusName(WarningStatusEnum.IGNORE.getDescription());
            }
            return vo;
        }).collect(Collectors.toList());
        pageData.setPages(warningPageData.getPages());
        pageData.setRecords(collect);
        pageData.setTotal(warningPageData.getTotal());
        pageData.setCurrent(warningPageData.getCurrent());
        return JsonResult.success(pageData);
    }
}
