package easydo.technology.service;

import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.sql.DataSource;

import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import easydo.technology.components.JdbcClient;
import easydo.technology.model.Supplier;
import easydo.technology.model.SupplierBank;
import easydo.technology.util.HttpClientUtil;
import easydo.technology.utils.StringUtil;

@Component
@Log
public class MDMService {
    @Value("${MDM.host}")
    String mdmHost;
    @Value("${MDM.supplierPush}")
    String supplierPath;
    @Resource
    JdbcClient jdbcClient;
    @Resource
    DataSource dataSource;

    public Boolean sendSupplier(Supplier supplier, boolean isDeleted) throws Exception {
        String url = mdmHost + supplierPath;
        JSONObject object = new JSONObject();

        JSONArray parameters = new JSONArray();
        JSONObject supplierObj = new JSONObject();
        if (isDeleted) {
            supplierObj.put("STATUS", 0);
        } else {
            supplierObj.put("STATUS", 1);
        }
        supplierObj.put("C_SORCE_SYSTEM", "04");
        supplierObj.put("C_FULLNAME", supplier.getSupplierName());
        supplierObj.put("C_NATURE", supplier.getNature());
        supplierObj.put("C_WHETHER_CUSTOMER", supplier.getIsCustomer() ? 1 : 0);
        supplierObj.put("C_WHETHER_SUPPLIER", 1);
        supplierObj.put("C_UNISOCIALCREDIT_CODE", supplier.getCreditCode());
        supplierObj.put("C_COUNTRY", "CHN");
        supplierObj.put("C_PROVINCE", supplier.getProvinceCode());
        supplierObj.put("C_CITY", supplier.getCityCode());
        if (StringUtil.isEmpty(supplier.getRemark())) {
            supplierObj.put("C_REMARKS", "无");
        } else {
            supplierObj.put("C_REMARKS", supplier.getRemark());
        }

        JSONArray bankArray = new JSONArray();
        List<SupplierBank> bankList = supplier.getBankList();
        for (SupplierBank bank : bankList) {
            JSONObject bankObj = new JSONObject();
            bankObj.put("C_ACCOUNTNAME", bank.getAccountName());
            bankObj.put("C_BANKNAME", bank.getBankName());
            bankObj.put("C_BANKCODE", bank.getSysBankCode());
            bankObj.put("C_ACCOUNTCODE", bank.getAccountCode());
            bankArray.add(bankObj);
        }
        supplierObj.put("C_YHST", bankArray);
        parameters.add(supplierObj);
        object.put("Parameters", parameters);

        String uuid = UUID.randomUUID().toString();
        log.info(uuid + "推送MSM供应商数据req========>" + object.toJSONString() + ",url========>" + url);
        JSONObject resObj = HttpClientUtil.sendPOST(url, object.toJSONString());
        log.info(uuid + "推送MSM供应商数据res========>" + resObj.toJSONString());
        return null;
    }
}
