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.common.utils.NjcnDateUtils;
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.prepare.harmonic.pojo.param.DeviceParam;
import com.njcn.prepare.harmonic.service.mysql.area.RMpDevEvaluateDetailPOService;
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.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("/rMpDevEvaluateDetail")
@RequiredArgsConstructor
public class RMpDevEvaluateDetailController extends BaseController {


    private final RMpDevEvaluateDetailPOService rMpDevEvaluateDetailPOService;
    private final DeptFeignClient deptFeignClient;
    private final CommTerminalGeneralClient commTerminalGeneralClient;

    @OperateInfo(info = LogEnum.BUSINESS_COMMON)
    @PostMapping("/handler")
    @ApiOperation("终端数据评价_日统计")
    @ApiImplicitParam(name = "deviceParam", value = "参数", required = true)
    public HttpResult<String> handler(@RequestBody @Validated DeviceParam deviceParam) {
        log.info(LocalDateTime.now() + "handler始执行");
        String methodDescribe = getMethodDescribe("handler");
        /**
         * 支持补录
         */
        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.deptGetDeviceAndMonitor(deptGetLineParam).getData();

            List<LineDevGetDTO> info = new ArrayList<>();
            /*主网*/
            List<LineDevGetDTO> collect = data1.stream().map(DeptGetDeviceDTO::getDeviceList).flatMap(Collection::stream).collect(Collectors.toList());
            /*配网*/
            List<LineDevGetDTO> collect2 = data1.stream().map(DeptGetDeviceDTO::getPwDeviceList).flatMap(Collection::stream).collect(Collectors.toList());
            info.addAll(collect);
            info.addAll(collect2);
            List<LineDevGetDTO> collect1 = info.stream().distinct().collect(Collectors.toList());
            collect1.forEach(temp -> {
                DeviceParam.DeviceDetail deviceDetail = new DeviceParam.DeviceDetail();
                deviceDetail.setDevId(temp.getDevId());
                deviceDetail.setLineIds(temp.getMonitorIds());
                deviceDetail.setUpdateTime(temp.getUpdateTime() + "");
                deviceDetail.setDeviceStatus(temp.getComFlag() + "");
                list.add(deviceDetail);
            });
            deviceParam.setDetailList(list);
        }
        if (deviceParam.getRepairFlag()) {
            List<String> timeRange = NjcnDateUtils.findEveryDay(deviceParam.getBeginTime(), deviceParam.getEndTime());
            for (String item : timeRange) {
                log.info(item + "-->开始执行");
                deviceParam.setDataDate(item);
                deviceParam.setBeginTime(item + " " + "00:00:00");
                deviceParam.setEndTime(item + " " + "23:59:59");
//                rMpDevEvaluateDetailPOService.handler(deviceParam);
            }
        } else {
//            rMpDevEvaluateDetailPOService.handler(deviceParam);
        }
        return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, null, methodDescribe);
    }
}
