package com.glxp.mipsdl.client;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.glxp.mipsdl.config.ThirdSysConfig;
import com.glxp.mipsdl.constant.Constants;
import com.glxp.mipsdl.dao.auth.AuthWarehouseDao;
import com.glxp.mipsdl.dao.basic.BasicBussinessTypeDao;
import com.glxp.mipsdl.dao.basic.BasicCorpDao;
import com.glxp.mipsdl.dao.inout.IoCodeDao;
import com.glxp.mipsdl.dao.inout.IoOrderDao;
import com.glxp.mipsdl.dao.inout.IoOrderDetailBizDao;
import com.glxp.mipsdl.dao.thrsys.ThrBustypeOriginDao;
import com.glxp.mipsdl.dao.thrsys.ThrInvWarehouseDao;
import com.glxp.mipsdl.dao.thrsys.ThrSystemBusApiDao;
import com.glxp.mipsdl.entity.inout.IoCodeEntity;
import com.glxp.mipsdl.entity.inout.IoOrderDetailBizEntity;
import com.glxp.mipsdl.http.HttpClient;
import com.glxp.mipsdl.req.base.*;
import com.glxp.mipsdl.req.base.forthird.ForBasicProductsSearchFilter;
import com.glxp.mipsdl.req.base.forthird.ForInvOutScanCodeFilter;
import com.glxp.mipsdl.req.base.forthird.ForInvProductsFilter;
import com.glxp.mipsdl.req.ctqyy.DlThirdProjectRequest;
import com.glxp.mipsdl.req.ctqyy.FilterThrHslbRequest;
import com.glxp.mipsdl.req.ctqyy.GetSickPrescribeRequest;
import com.glxp.mipsdl.req.ctqyy.GetSickRequest;
import com.glxp.mipsdl.req.post.*;
import com.glxp.mipsdl.res.BaseResponse;
import com.glxp.mipsdl.service.auth.AuthWarehouseService;
import com.glxp.mipsdl.service.thrsys.ThrCorpService;
import com.glxp.mipsdl.service.thrsys.ThrSystemDetailService;
import com.glxp.mipsdl.util.DictUtl;
import com.glxp.mipsdl.util.ResultVOUtils;
import com.glxp.mipsdl.util.SubmitOrderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.List;

@Slf4j
@Service
public abstract class CommonHttpClient implements BaseHttpClient {

    @Autowired
    private HttpClient httpClient;
    @Autowired
    private ThirdSysConfig thirdSysConfig;
    @Autowired
    private ThrSystemDetailService thrSystemDetailService;
    @Autowired
    private ThrBustypeOriginDao thrBustypeOriginDao;
    @Autowired
    private IoOrderDao orderDao;
    @Autowired
    private ThrSystemBusApiDao thrSystemBusApiDao;
    @Autowired
    private BasicBussinessTypeDao basicBussinessTypeDao;
    @Autowired
    private BasicCorpDao basicCorpDao;
    @Autowired
    private AuthWarehouseDao authWarehouseDao;
    @Autowired
    private ThrInvWarehouseDao thrInvWarehouseDao;
    @Autowired
    private AuthWarehouseService authWarehouseService;
    @Autowired
    private ThrCorpService thrCorpService;
    @Autowired
    private IoCodeDao codeDao;
    @Autowired
    private IoOrderDetailBizDao orderDetailBizDao;
    @Autowired
    private SubmitOrderUtil submitOrderUtil;

    @Override
    public BaseResponse getUnit(UdiwmsUnitRequest udiwmsUnitRequest) {
        String response = httpClient.postJson(thrSystemDetailService.getUrl(Constants.URL_NAME_CORP_URL), udiwmsUnitRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse getManu(UdiwmsManuRequest udiwmsManuRequest) {
        return null;
    }

    @Override
    public BaseResponse getProducts(UdiwmsProductRequest udiwmsProductRequest) {
        String response = httpClient.postJson(thrSystemDetailService.getUrl(Constants.URL_NAME_PI_QUERY_URL), udiwmsProductRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse getOrderTypes(UdiwmsBusTypeRequest udiwmsBusTypeRequest) {
        String response = httpClient.postJson(thrSystemDetailService.getUrl(Constants.URL_NAME_BUS_TYPE_QUERY_URL), udiwmsBusTypeRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse getOrders(UdiwmsOrderQueryRequest udiwmsOrderQueryRequest) {
        if (StrUtil.isBlank(udiwmsOrderQueryRequest.getBillType())) {
            return ResultVOUtils.error(500, "单据类型不能为空");
        }
        //查询绑定的第三方单据类型
        String thirdAction = thrBustypeOriginDao.findThirdActionByBillType(udiwmsOrderQueryRequest.getBillType(), DictUtl.CURRENT_HOSP_CODE);
        udiwmsOrderQueryRequest.setBillType(thirdAction);
        String response = httpClient.postJson(thrSystemDetailService.getUrl(Constants.URL_NAME_ORDER_QUERY_URL), udiwmsOrderQueryRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public abstract BaseResponse submitOrders(UdiwmsOrderRequest udiwmsOrderRequest);

    /**
     * 封装单据详情参数
     *
     * @param ioCodeEntities      单据码明细
     * @param orderDetailRequests 单据详情参数
     * @param detailBizEntities   单据业务详情
     */
    private void setOrderDetailList(List<IoCodeEntity> ioCodeEntities, List<UdiwmsOrderDetailRequest> orderDetailRequests, List<IoOrderDetailBizEntity> detailBizEntities) {
        for (IoOrderDetailBizEntity detailBizEntity : detailBizEntities) {
            UdiwmsOrderDetailRequest item = submitOrderUtil.buildProductInfo(detailBizEntity);
            //封装条码信息
            submitOrderUtil.setCodeList(ioCodeEntities, detailBizEntity, item);
            orderDetailRequests.add(item);
        }
    }

    @Override
    public BaseResponse getInvProducts(UdiwmsInvProductsRequest udiwmsInvProductsRequest) {
        String response = httpClient.postJson(thrSystemDetailService.getUrl(Constants.URL_NAME_INV_PI_URL), udiwmsInvProductsRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse getWarehouse(UdiwmsWarehouseRequest udiwmsWarehouseRequest) {
        String response = httpClient.postJson(thrSystemDetailService.getUrl(Constants.URL_NAME_WAREHOUSE_QUERY_URL), udiwmsWarehouseRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse testConnect() {
        UdiwmsUnitRequest udiwmsUnitRequest = new UdiwmsUnitRequest();
        udiwmsUnitRequest.setPage(1);
        udiwmsUnitRequest.setLimit(1);
        BaseResponse baseResponse = getUnit(udiwmsUnitRequest);
        if (baseResponse.getCode() == 20000) {
            return ResultVOUtils.success("连接成功");
        }
        return ResultVOUtils.error(500, "连接失败！");
    }

    @Override
    public BaseResponse queryUdiInvProducts(UdiwmsQueryUdiInvProductsRequest queryUdiInvProductsRequest) {
        String response = httpClient.postUDIWMSJson(thirdSysConfig.getQueryInvUrl(), queryUdiInvProductsRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse submitProducts(UdiwmsAddProductsRequest udiwmsAddProductsRequest) {
        return null;
    }

    @Override
    public BaseResponse submitUnit(UdiwmsAddUnitRequest udiwmsAddUnitRequest) {
        return null;
    }


    @Override
    public BaseResponse submitManu(UdiwmsAddManuRequest udiwmsAddManuRequest) {
        return null;
    }


    @Override
    public BaseResponse submitPreInOrders(UdiwmsPreInOrderRequest udiwmsPreInOrderRequest) {
        return null;
    }

    @Override
    public BaseResponse postProducts(PostThrProductsRequest postThrProductsRequest) {
        postThrProductsRequest.setUploadType("文件导入");
        String response = httpClient.postUDIWMSJson(thirdSysConfig.getPostPiUrl(), postThrProductsRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse postCorps(PostThrCorpRequest postThrCorpRequest) {
        postThrCorpRequest.setUploadType("文件导入");
        String response = httpClient.postUDIWMSJson(thirdSysConfig.getPostCorpUrl(), postThrCorpRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse postOrderDetail(PostFileThrOrderRequest postFileThrOrderRequest) {
        postFileThrOrderRequest.setUploadType("文件导入");
        String response = httpClient.postUDIWMSJson(thirdSysConfig.getPostOrderUrl(), postFileThrOrderRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }

    @Override
    public BaseResponse postInvProducts(PostThrInvProductsRequest postThrInvProductsRequest) {
        String response = httpClient.postUDIWMSJson(thirdSysConfig.getPostInvPiUrl(), postThrInvProductsRequest);
        BaseResponse baseResponse = JSONObject.parseObject(response, BaseResponse.class);
        return baseResponse;
    }


    @Override
    public BaseResponse postRelProduct(PostRelProductRequest postThrInvProductsRequest) {
        return null;
    }

    @Override
    public BaseResponse queryUdiHighInvProductsFilter(ForInvProductsFilter forInvProductsFilter) {
        return null;
    }

    @Override
    public BaseResponse queryUdiInvProductsFilter(ForInvProductsFilter forInvProductsFilter) {
        return null;
    }

    @Override
    public BaseResponse queryUdiBasicProductsSearch(ForBasicProductsSearchFilter forBasicProductsSearchFilter) {
        return null;
    }

    @Override
    public BaseResponse queryUdiInvOutScanCode(ForInvOutScanCodeFilter forInvOutScanCodeFilter) {
        return null;
    }

    /**
     * 获取患者信息
     *
     * @param getSickRequest
     * @return
     */
    @Override
    public BaseResponse getSickInfo(GetSickRequest getSickRequest) {
        return null;
    }

    /**
     * 获取患者处方信息
     *
     * @param getSickPrescribeRequest
     * @return
     */
    @Override
    public BaseResponse getSickPescribe(GetSickPrescribeRequest getSickPrescribeRequest) {
        return null;
    }


    /**
     * 获取项目组套字典
     */
    @Override
    public BaseResponse getBasicProject(DlThirdProjectRequest dlThirdProjectRequest) {
        return null;
    }

    @Override
    public BaseResponse getHsfl(FilterThrHslbRequest hslbRequest) {
        return null;
    }

    @Override
    public BaseResponse getInvResult(UdiwmsInvProductsRequest udiwmsInvProductsRequest) {
        return null;
    }


    @Override
    public BaseResponse getInvCodeResult(UdiwmsInvProductsRequest udiwmsInvProductsRequest) {
        return null;
    }


    @Override
    public BaseResponse getInvFeeResult(UdiwmsInvProductsRequest udiwmsInvProductsRequest) {
        return null;
    }

    @Override
    public BaseResponse getPrescribes(UdiwmsBasicSkPrescribeRequest udiwmsBasicSkPrescribeRequest) {
        return null;
    }

    @Override
    public BaseResponse getThrOrders(UdiwmsThrOrderRequest udiwmsThrOrderRequest) {
        return null;
    }
    @Override
    public BaseResponse uploadCollectOrder(PostCollectOrderRequest postCollectOrderRequest) {
        return null;
    }

    @Override
    public BaseResponse getPrescribesQx(UdiwmsBasicSkPrescribeRequest udiwmsBasicSkPrescribeRequest) {
        return null;
    }

    @Resource
    private DataSource dataSource;

    public String getDataSourceType(){
        Connection conn = null;
        DatabaseMetaData metaData = null;
        String databaseProductName = "";
        try {
            conn = dataSource.getConnection();
            metaData = conn.getMetaData();
            databaseProductName = metaData.getDatabaseProductName();//MySQL //Oracle
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            // 释放连接资源
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

        return databaseProductName;

    }
}
