package com.njcn.prepare.harmonic.controller.newalgorithm;

import com.njcn.common.pojo.annotation.OperateInfo;
import com.njcn.common.pojo.constant.ServerInfo;
import com.njcn.common.pojo.enums.common.LogEnum;
import com.njcn.common.pojo.enums.response.CommonResponseEnum;
import com.njcn.common.pojo.response.HttpResult;
import com.njcn.common.utils.HttpResultUtil;
import com.njcn.device.biz.commApi.CommTerminalGeneralClient;
import com.njcn.device.biz.pojo.dto.DeptGetDeviceDTO;
import com.njcn.device.biz.pojo.dto.LineDevGetDTO;
import com.njcn.device.biz.pojo.param.DeptGetLineParam;
import com.njcn.device.pq.api.DeptLineFeignClient;
import com.njcn.device.pq.api.LineFeignClient;
import com.njcn.prepare.harmonic.pojo.param.DeviceParam;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.mysql.device.RStatDevSignalDPOService;
import com.njcn.prepare.harmonic.service.mysql.line.RMpEventDetailService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.po.Dept;
import com.njcn.web.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Description:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/12/28  13:47【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Api(tags = "终端通信管理")
@RestController
@RequestMapping("/rstatdevsignal")
@RequiredArgsConstructor
public class RStatDevSignalController extends BaseController {


    private final RStatDevSignalDPOService rStatDevSignalDPOService;
    private final CommTerminalGeneralClient commTerminalGeneralClient;
    private final DeptFeignClient deptFeignClient;
    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/rstatdevsignalHandler")
    @ApiOperation("终端通信管理_日统计")
    @ApiImplicitParam(name = "deviceParam", value = "参数", required = true)
    public HttpResult<String> rstatdevsignalHandler(@RequestBody @Validated DeviceParam deviceParam ){
        log.info(LocalDateTime.now()+"rstatdevsignalHandler始执行");
        String methodDescribe = getMethodDescribe("rstatdevsignalHandler");
        /**
         * 支持补录
         */
        if(CollectionUtils.isEmpty(deviceParam.getDetailList())){
            List<DeviceParam.DeviceDetail> list = new ArrayList<>();
            Dept data = deptFeignClient.getRootDept().getData();
            DeptGetLineParam deptGetLineParam = new DeptGetLineParam();
            deptGetLineParam.setDeptId(data.getId());
            deptGetLineParam.setServerName(ServerInfo.PREPARE_BOOT);
            List<DeptGetDeviceDTO> data1 = commTerminalGeneralClient.deptGetDevice(deptGetLineParam).getData();
            /*主网*/

            List<LineDevGetDTO> collect = data1.stream().map(DeptGetDeviceDTO::getDeviceList).flatMap(Collection::stream).collect(Collectors.toList());
            collect.forEach(temp -> {
                DeviceParam.DeviceDetail deviceDetail = new DeviceParam.DeviceDetail();
                deviceDetail.setDevId(temp.getDevId());
                deviceDetail.setLineIds(temp.getMonitorIds());
                deviceDetail.setUpdateTime(temp.getUpdateTime()+"");
                list.add(deviceDetail);
            });

            /*配网*/
            List<LineDevGetDTO> collect2 = data1.stream().map(DeptGetDeviceDTO::getPwDeviceList).flatMap(Collection::stream).collect(Collectors.toList());
            collect2.forEach(temp -> {
                DeviceParam.DeviceDetail deviceDetail = new DeviceParam.DeviceDetail();
                deviceDetail.setDevId(temp.getDevId());
                deviceDetail.setLineIds(temp.getMonitorIds());
                deviceDetail.setUpdateTime(temp.getUpdateTime()+"");
                list.add(deviceDetail);
            });
            deviceParam.setDetailList(list);
        }
        rStatDevSignalDPOService.eventdetailHandler (deviceParam);
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
}
