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.prepare.harmonic.pojo.param.PrimaryGridParam;
import com.njcn.prepare.harmonic.service.mysql.area.AreaCountService;
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:
 * 接口文档访问地址：http://serverIP:port/swagger-ui.html
 * Date: 2022/12/13  11:14【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Api(tags = "区域统计(r_stat_org_d表数据需要先生成r_stat_abnormal_d，r_mp_integrity_d表)")
@RestController
@RequestMapping("AreaCount")
@RequiredArgsConstructor
@Deprecated
public class AreaCountController extends BaseController {

    private final DeptFeignClient deptFeignClient;

    private final AreaCountService areaCountService;

    @ApiOperation("区域统计有效接入监测点个数计算(r_stat_org_d表数据)")
    @ApiImplicitParam(value = "primaryGridParam", name = "primaryGridParam", required = true)
    @PostMapping("areaCountHanlder")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> areaCountHanlder(@RequestBody PrimaryGridParam primaryGridParam) {
        log.info(LocalDateTime.now() + "areaCountHanlder开始执行");
        String methodDescribe = getMethodDescribe("areaCountHanlder");
        Boolean result = true;
        List<String> orgIdList = new ArrayList<>();
        if (CollectionUtils.isEmpty(primaryGridParam.getOrgIdList())) {
            List<PvTerminalTreeVO> dept = deptFeignClient.allDeptList().getData();
            orgIdList = dept.stream().map(PvTerminalTreeVO::getId).collect(Collectors.toList());
        } else {
            orgIdList = primaryGridParam.getOrgIdList();
        }
        primaryGridParam.setOrgIdList(orgIdList);

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