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.PmsPowerDistributionareaClient;
import com.njcn.device.pms.pojo.param.PowerDistributionareaParam;
import com.njcn.device.pms.pojo.po.PowerDistributionarea;
import com.njcn.prepare.harmonic.pojo.param.LineParam;
import com.njcn.prepare.harmonic.service.influxdb.RStatWorkOrderCreateService;
import com.njcn.redis.utils.RedisUtil;
import com.njcn.user.api.DeptFeignClient;
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.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: 2023/2/9  9:16【需求编号】
 *
 * @author clam
 * @version V1.0.0
 */
@Slf4j
@Api(tags = "工单生成批处理逻辑")
@RestController
@RequestMapping("RStatWorkOrderCreate")
@RequiredArgsConstructor
public class RStatWorkOrderCreateController extends BaseController {


    private final PmsPowerDistributionareaClient pmsPowerDistributionareaClient;

    private final RStatWorkOrderCreateService rStatWorkOrderCreateService;
    private final DeptFeignClient deptFeignClient;
    private final RedisUtil redisUtil;


    @ApiOperation("工单生成批处理逻辑(台区测点)")
    @ApiImplicitParam(value = "lineParam",name = "lineParam",required = true)
    @PostMapping("workOrderCreate")
    @OperateInfo(info = LogEnum.BUSINESS_MEDIUM)
    public HttpResult<Boolean> workOrderCreate(@RequestBody LineParam lineParam ){
        log.info(LocalDateTime.now()+"workOrderCreate开始执行");
        String methodDescribe = getMethodDescribe("workOrderCreate");
        Boolean result = true;
        List<String>  lineIds= new ArrayList<> ();
        if(CollectionUtils.isEmpty (lineParam.getLineIds ())){
            PowerDistributionareaParam powerDistributionareaParam = new PowerDistributionareaParam();
            List<PowerDistributionarea> data = pmsPowerDistributionareaClient.getPowerDistributionList (powerDistributionareaParam).getData ( );
            lineIds = data.stream ().map (PowerDistributionarea::getId).collect(Collectors.toList());
        }else{
            lineIds = lineParam.getLineIds ( );
        }
        lineParam.setLineIds (lineIds);
        rStatWorkOrderCreateService.workOrderCreate(lineParam);
        if (result){
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe);
        } else {
            return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.FAIL, false, methodDescribe);
        }
    }
}
