package com.huimv.env.admin.controller;


import cn.hutool.core.util.ObjectUtil;
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.TerminalPort;
import com.huimv.env.admin.entity.vo.PortListByTerminalIdParam;
import com.huimv.env.admin.entity.vo.PortPageByLocationIdParam;
import com.huimv.env.admin.entity.vo.TerminalPortConfigParam;
import com.huimv.env.admin.service.IBasePigpenService;
import com.huimv.env.admin.service.ITerminalPortService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 端口 前端控制器
 * </p>
 *
 * @author newspaper
 * @since 2024-05-07
 */
@RestController
@CrossOrigin
@RequestMapping("/port")
public class TerminalPortController {
    @Autowired
    private ITerminalPortService portService;
    @Autowired
    private IBasePigpenService basePigpenService;

    @PostMapping("/listByTerminalId")
    public Result list(@RequestBody PortListByTerminalIdParam portListByTerminalIdParam){
        Integer terminalId = portListByTerminalIdParam.getTerminalId();
        List<TerminalPort> terminalPorts = portService.list(new QueryWrapper<TerminalPort>().lambda()
                .eq(TerminalPort::getTerminalId, terminalId));
        return new Result(ResultCode.SUCCESS,terminalPorts);
    }

    @PostMapping("/config")
    public Result config(@RequestBody TerminalPortConfigParam terminalPortConfigParam){
        List<TerminalPort> list = terminalPortConfigParam.getList();
        for (TerminalPort terminalPort : list) {
            TerminalPort port = portService.getById(terminalPort.getId());
            if (ObjectUtil.isNotEmpty(terminalPort.getLocationId())){
                port.setLocationId(terminalPort.getLocationId());
                portService.updateById(port);
            }
        }
        return Result.SUCCESS();
    }

    @PostMapping("/pageByLocationId")
    public Result pageByLocationId(@RequestBody PortPageByLocationIdParam portPageByLocationIdParam){
        Page<TerminalPort> page = new Page<>(portPageByLocationIdParam.getPageNum(),portPageByLocationIdParam.getPageSize());
        Integer locationId = portPageByLocationIdParam.getLocationId();
        Page<TerminalPort> terminalPortPage = new Page<>();
        if (ObjectUtil.isEmpty(locationId)){
            terminalPortPage = portService.page(page, new QueryWrapper<TerminalPort>().lambda()
                    .eq(TerminalPort::getFarmId,portPageByLocationIdParam.getFarmId())
                    .eq(ObjectUtil.isNotEmpty(portPageByLocationIdParam.getStatus()), TerminalPort::getStatus, portPageByLocationIdParam.getStatus())
                    .eq(ObjectUtil.isNotEmpty(portPageByLocationIdParam.getTerminalId()),TerminalPort::getTerminalId,portPageByLocationIdParam.getTerminalId())
                    .isNotNull(TerminalPort::getLocationId));
        }else {
            BasePigpen basePigpen = basePigpenService.getById(locationId);
            List<BasePigpen> list = basePigpenService.list(new QueryWrapper<BasePigpen>().lambda().eq(BasePigpen::getFType,4).like(BasePigpen::getOther2, basePigpen.getId()));
            List<Integer> idList = list.stream().map(BasePigpen::getId).collect(Collectors.toList());
            terminalPortPage = portService.page(page, new QueryWrapper<TerminalPort>().lambda()
                    .in(TerminalPort::getLocationId,idList)
                    .eq(TerminalPort::getFarmId,portPageByLocationIdParam.getFarmId())
                    .eq(ObjectUtil.isNotEmpty(portPageByLocationIdParam.getStatus()), TerminalPort::getStatus, portPageByLocationIdParam.getStatus())
                    .eq(ObjectUtil.isNotEmpty(portPageByLocationIdParam.getTerminalId()),TerminalPort::getTerminalId,portPageByLocationIdParam.getTerminalId()));
        }
        return new Result(ResultCode.SUCCESS,terminalPortPage);
    }

}
