package com.hr.repository.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.github.pagehelper.PageHelper;
import com.hr.param.InterfaceCalledQueryParam;
import com.hr.repository.domain.*;
import com.hr.repository.jpa.*;
import com.hr.repository.service.IpOrderService;
import com.hr.vo.*;
import com.yy.basedevelop.common.data.BasePageResult;
import com.yy.basedevelop.common.util.ToString;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 调用记录表;(ip_order)表服务接口实现类
 * @author : http://www.yonsum.com
 * @date : 2025-10-10
 */
@Service
public class IpOrderServiceImpl implements IpOrderService {
    @Autowired
    private IpOrderRepository ipOrderRepository;
    @Autowired
    private IpLogRepository ipLogRepository;
    @Autowired
    private IpInterfaceRepository ipInterfaceRepository;
    @Autowired
    private IpTokenInterfaceRepository ipTokenInterfaceRepository;
    @Autowired
    private IpAccountRepository ipAccountRepository;
    @Autowired
    private IpTokenRepository ipTokenRepository;

    /** 
     * 通过ID查询单条数据 
     *
     * @param oid 主键
     * @return 实例对象
     */
    public IpOrderVO queryById(Long oid){
        IpOrderVO ipOrderVO = new IpOrderVO();
        IpOrderPO ipOrderPO = ipOrderRepository.queryById(oid);
        if(ipOrderPO != null){
            BeanUtils.copyProperties(ipOrderPO, ipOrderVO);
            return ipOrderVO;
        }
        return null;
    }

     @Override
     public Page<IpOrderPO> paginQuery(IpOrderPO IpOrderPO, Integer current, Integer size) {
         ExampleMatcher matcher = ExampleMatcher.matchingAll()
                 .withIgnoreNullValues()                              // 忽略 null 字段
                 .withIgnoreCase();
         Pageable pageAble = PageRequest.of(current, size);
         Example<IpOrderPO> example = Example.of(IpOrderPO, matcher);
         Page<IpOrderPO> result = ipOrderRepository.findAll(example, pageAble);
         return result;
     }


     /**
     * 新增数据
     *
     * @param IpOrderPO 实例对象
     * @return 实例对象
     */
    public void insert(IpOrderPO IpOrderPO){
        ipOrderRepository.save(IpOrderPO);
    }
    
    /** 
     * 更新数据
     *
     * @param IpOrderPO 实例对象
     * @return 实例对象
     */
    public void update(IpOrderPO IpOrderPO){
        ipOrderRepository.save(IpOrderPO);
    }

    @Override
    public List<CallbackHistoryVO> callbackHistory(long id) {
        return ipOrderRepository.queryCallbackHistory(id);
    }

    @Override
    public InterfaceCalledDetail interfaceDetail(Long id) {
        IpOrderPO ipOrderPO = ipOrderRepository.findById(id).get();
        ipOrderPO.getAccNo();
        IpLogPO ipLog = ipLogRepository.findByOid(ipOrderPO.getId());
        IpTokenInterfacePO ipTokenInterfacePO = ipTokenInterfaceRepository.findById(ipOrderPO.getTid()).get();
        IpInterfacePO ipInterfacePO = ipInterfaceRepository.findById(ipTokenInterfacePO.getIid()).get();
        Long tid = ipTokenInterfacePO.getTid();
        IpTokenPO ipTokenPO = ipTokenRepository.findById(tid).get();
        InterfaceCalledDetail vo = new InterfaceCalledDetail();
        vo.setId(ipOrderPO.getId());
        vo.setAccountNo(ipOrderPO.getAccNo());
        vo.setInterfaceName(ipOrderPO.getInterfaceName());
        vo.setRequestParam(ipLog.getRequestParam());
        vo.setResponseParam(ipLog.getResponseResult());
        JSONObject configParamJSON = new JSONObject();
        configParamJSON.put("token", ipTokenPO.getToken());
        configParamJSON.put("secret", ipTokenPO.getSecret());
        vo.setConfigParam(configParamJSON.toJSONString());
        vo.setInterfaceParam(ipInterfacePO.getInterfaceConfig());
        return vo;
    }

    @Override
    public BasePageResult<IpInterfaceCallVO> queryList(InterfaceCalledQueryParam ipInterfaceCalledQueryParam) {
        PageHelper.startPage(ipInterfaceCalledQueryParam.getPageNum(), ipInterfaceCalledQueryParam.getPageSize());
        BasePageResult<IpInterfaceCallVO> result = ipLogRepository.queryInterfaceCallHistory(ipInterfaceCalledQueryParam);
        return result;
    }

    /**
     * 通过主键删除数据
     *
     * @param oid 主键
     * @return 是否成功
     */
    public boolean deleteById(Long oid){
        ipOrderRepository.deleteById(oid);
        return false;
    }
}