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

import com.njcn.common.pojo.annotation.OperateInfo;
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.pms.api.PmsTerminalClient;
import com.njcn.device.pms.pojo.param.PmsBaseParam;
import com.njcn.device.pms.pojo.po.PmsTerminal;
import com.njcn.prepare.harmonic.pojo.param.PrimaryGridDataIntegrityParam;
import com.njcn.prepare.harmonic.pojo.param.PrimaryGridParam;
import com.njcn.prepare.harmonic.service.mysql.area.PrimaryGridDataService;
import com.njcn.user.api.DeptFeignClient;
import com.njcn.user.pojo.vo.PvTerminalTreeVO;
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.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
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.List;
import java.util.stream.Collectors;

/**
 * Description:r_operating_index_d
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/11/24  14:31【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Api(tags = "主网单位数据统计(r_operating_index_d表数据需要先生成r_stat_abnormal_d，r_mp_integrity_d表)")
@RestController
@RequestMapping("primaryGridData")
@RequiredArgsConstructor
public class PrimaryGridDataController extends BaseController {

    private  @Autowired
    PrimaryGridDataService primaryGridDataService;

    private @Autowired
    DeptFeignClient deptFeignClient;

    private @Autowired
    PmsTerminalClient pmsTerminalClient;


    @ApiOperation("主网单位数据同步(r_operating_index_d表数据)")
    @ApiImplicitParam(value = "primaryGridParam",name = "primaryGridParam",required = true)
    @PostMapping("primaryGridDataHanlder")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> primaryGridDataHanlder(@RequestBody PrimaryGridParam primaryGridParam ){
        log.info(LocalDateTime.now()+"primaryGridDataHanlder开始执行");
        String methodDescribe = getMethodDescribe("primaryGridDataHanlder");
        Boolean result = true;
        List<String> orgIdList = new ArrayList<> ();
        if(CollectionUtils.isEmpty (primaryGridParam.getOrgIdList ())){
            /*todo 不知道是否需要补招*/
            List<PvTerminalTreeVO> dept = deptFeignClient.allDeptList().getData();
            orgIdList = dept.stream ( ).map (PvTerminalTreeVO::getId).collect (Collectors.toList ( ));
        }else{
            orgIdList = primaryGridParam.getOrgIdList ();
        }
        primaryGridParam.setOrgIdList (orgIdList);

        primaryGridDataService.primaryGridDataHanlder (primaryGridParam);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }

    @ApiOperation("主网终端数据完整率月统计")
    @ApiImplicitParam(value = "primaryGridDataIntegrityParam",name = "primaryGridDataIntegrityParam",required = true)
    @PostMapping("primaryGridDataIntegrityHanlder")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> primaryGridDataIntegrityHanlder(@RequestBody PrimaryGridDataIntegrityParam primaryGridDataIntegrityParam ){
        log.info(LocalDateTime.now()+"primaryGridDataIntegrityHanlder开始执行");
        String methodDescribe = getMethodDescribe("primaryGridDataIntegrityHanlder");
        Boolean result = true;
        List<String> orgIdList = new ArrayList<> ();
        if(CollectionUtils.isEmpty (primaryGridDataIntegrityParam.getDeviceId ())){
            /*todo 不知道是否需要补招*/
            List<PmsTerminal> data = pmsTerminalClient.getTerminalSelectList (new PmsBaseParam()).getData ( );
            orgIdList = data.stream ().map (PmsTerminal::getId).collect(Collectors.toList());
        }else{
            orgIdList = primaryGridDataIntegrityParam.getDeviceId ();
        }
        primaryGridDataIntegrityParam.setDeviceId (orgIdList);

        primaryGridDataService.primaryGridDataIntegrityHanlder (primaryGridDataIntegrityParam);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }





}
