package com.hr.controller;

import com.alibaba.fastjson2.JSON;
import com.hr.dto.AggregateQueryDTO;
import com.hr.externelSystem.BizHandlerFactory;
import com.hr.param.AggregateRequest;
import com.hr.repository.domain.IpInterfacePO;
import com.hr.repository.domain.IpTokenInterfacePO;
import com.hr.repository.domain.IpTokenPO;
import com.hr.service.IpTokenInterfaceService;
import com.hr.service.IpAccountService;
import com.hr.service.IpInterfaceService;
import com.hr.vo.IpAccountVO;
import com.hr.vo.IpInterfaceVO;
import com.yy.basedevelop.common.data.BaseReturnDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
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 reactor.core.publisher.Mono;

/**
 * 三方调用聚合接口
 */
@RestController
@RequestMapping("/aggregate")
@Slf4j
public class AggregateController {

    @Autowired
    private BizHandlerFactory bizHandlerFactory;

    @Autowired
    private IpTokenInterfaceService ipTokenInterfaceService;

    @Autowired
    private IpAccountService ipAccountService;

    @Autowired
    private IpInterfaceService ipInterfaceService;

    @PostMapping("/openApi")
    public Mono<BaseReturnDto<Object>> aggregate(@RequestBody AggregateRequest req) {
        log.info("openApi调用{}", req);
        String accountNo = req.getAccountNo();
        String interfaceCode = req.getInterfaceCode();
        IpAccountVO ipAccount = ipAccountService.queryByAccountNo(accountNo);
        log.info("ipAccount:{},{}", accountNo, ipAccount);
        if(ipAccount == null){
            return Mono.just(BaseReturnDto.error("账户不存在"));
        }
        if(!ipAccount.getStatus().equals("normal")){
            return Mono.just(BaseReturnDto.error("账户已禁用"));
        }
        IpInterfaceVO ipInterfaceVO = ipInterfaceService.queryByInterfaceCode(interfaceCode);
        log.info("ipInterfaceVO:{},{}", interfaceCode, ipInterfaceVO);
        if(ipInterfaceVO == null){
            return Mono.just(BaseReturnDto.error("接口不存在"));
        }
        if(!ipInterfaceVO.getStatus().equals("normal")){
            return Mono.just(BaseReturnDto.error("接口已禁用"));
        }
        AggregateQueryDTO dto = ipTokenInterfaceService.queryConfigByInterfaceCodeAndAccountNo(ipAccount.getId(), ipInterfaceVO.getId());
        IpTokenInterfacePO ipTokenInterfacePO = dto.getIpTokenInterfacePO();
        if(ipTokenInterfacePO == null){
            return Mono.just(BaseReturnDto.error("接口配置已禁用"));
        }
        IpTokenPO ipTokenPO = dto.getIpTokenPO();
        if(!ipTokenPO.getToken().equals(req.getToken())){
            return Mono.just(BaseReturnDto.error("token错误"));
        }
        IpInterfacePO ipInterfacePO = new IpInterfacePO();
        BeanUtils.copyProperties(ipInterfaceVO, ipInterfacePO);
        dto.setIpInterfacePO(ipInterfacePO);
        BaseReturnDto<Object> response = bizHandlerFactory.dispatch(req, dto);
        log.info("openApi返回：{}", JSON.toJSONString(response));
        return Mono.just(response);
    }
}