package com.huimv.env.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.env.admin.common.utils.Result;
import com.huimv.env.admin.common.utils.ResultCode;
import com.huimv.env.admin.entity.BasePigpen;
import com.huimv.env.admin.entity.PortWarningInfo;
import com.huimv.env.admin.entity.Terminal;
import com.huimv.env.admin.entity.vo.WarningPageParam;
import com.huimv.env.admin.entity.vo.WarningTotalByLocationIdParam;
import com.huimv.env.admin.entity.vo.WarningTotalResult;
import com.huimv.env.admin.mapper.TerminalMapper;
import com.huimv.env.admin.mapper.TerminalPortMapper;
import com.huimv.env.admin.service.IBasePigpenService;
import com.huimv.env.admin.service.IPortWarningInfoService;
import com.huimv.env.admin.service.ITerminalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2024-05-09
 */
@RestController
@CrossOrigin
@RequestMapping("/port-warning-info")
public class PortWarningInfoController {

    @Autowired
    private IPortWarningInfoService portWarningInfoService;
    @Autowired
    private ITerminalService terminalService;
    @Autowired
    private TerminalMapper terminalMapper;
    @Autowired
    private IBasePigpenService basePigpenService;

    @PostMapping("/page")
    public Result page(@RequestBody WarningPageParam warningPageParam){
        String startTime = warningPageParam.getStartTime();
        String endTime = warningPageParam.getEndTime();
        if (ObjectUtil.isNotEmpty(startTime)){
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
        }
        Page<PortWarningInfo> page = new Page<>(warningPageParam.getPageNum(),warningPageParam.getPageSize());
        LambdaQueryWrapper<PortWarningInfo> queryWrapper = new QueryWrapper<PortWarningInfo>().lambda()
                .eq(PortWarningInfo::getFarmId,warningPageParam.getFarmId())
                .eq(ObjectUtil.isNotEmpty(warningPageParam.getWarningType()), PortWarningInfo::getWarningType, warningPageParam.getWarningType())
                .between(ObjectUtil.isNotEmpty(startTime), PortWarningInfo::getCreateTime, startTime, endTime)
                .orderByDesc(PortWarningInfo::getCreateTime);

        Integer locationId = warningPageParam.getLocationId();
        if (ObjectUtil.isNotEmpty(locationId)){
            List<BasePigpen> basePigpens = basePigpenService.list(new QueryWrapper<BasePigpen>().lambda().like(BasePigpen::getOther2, locationId).eq(BasePigpen::getFType, 4));
            List buildIds = basePigpens.stream().map(BasePigpen::getId).collect(Collectors.toList());
            queryWrapper.in(PortWarningInfo::getLocationId,buildIds);
        }
        Page<PortWarningInfo> warningInfoPage = portWarningInfoService.page(page, queryWrapper);
        return new Result(ResultCode.SUCCESS,warningInfoPage);
    }


}
