package com.yem.tws.aip;

import com.yem.tws.common1.ApilogUtils;
//import json.JSONObject;
import com.alibaba.fastjson.JSONObject;
import kd.bos.bill.IBillWebApiPlugin;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.api.ApiResult;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;

import java.util.*;

import static com.yem.tws.common1.JsonUtils.jsonToData;

/**
 * @author zhouc
 * @date 2023/5/31 21:46
 * @className FecGpiquery
 * @description 出口收汇\进口付汇接口
 */
public class FecGpiquery  implements IBillWebApiPlugin {
    public ApiResult doCustomService(Map<String, Object> params) {
        List<Object> resultlist = Collections.synchronizedList(new ArrayList<>());
        boolean BollISSucces = false;
        String ErrMassage = "";
        String FID = "";
        String billno = "";
        JSONObject jsonData = null;
        String billnoname ="",interfacetype="";
        String billnoNumber="";
        try {
            jsonData = JSONObject.parseObject(JSONObject.toJSONString(params));
            if (null != jsonData) {
                String entryId = JSONObject.parseObject(jsonData.getString("jsion")).getString("entryId");//单据编号
                String entityName = jsonData.getString("entityName");
                if(entityName.equals("yem_ca_gpiquery")){
                    billnoname="进口付汇";
                    interfacetype="AddGpiquery";
                    billnoNumber="gpiquery";
                }else if(entityName.equals("yem_ca_fecquery")){
                    billnoname="出口收汇";
                    interfacetype="AddFecquery";
                    billnoNumber="fecquery";
                }
                QFilter filter1 = new QFilter("billno", QCP.equals, entryId);
                DynamicObject simpleBill1 = BusinessDataServiceHelper.loadSingle(entityName, "id", new QFilter[]{filter1});
                if (simpleBill1 == null) {
                    QFilter qFilter = new QFilter("billno", QCP.equals, billnoNumber);
                    DynamicObject[] yem_ca_billlogon_s = BusinessDataServiceHelper.load("yem_ca_billlogon",
                            "id,billno,billstatus", new QFilter[]{qFilter}, null);
                    if(yem_ca_billlogon_s != null){
                        DynamicObject data = jsonToData(jsonData.getString("jsion"), entityName, yem_ca_billlogon_s, entryId,JSONObject.parseObject(jsonData.getString("jsion")));
                        if(data != null){
                            OperationResult Operate = OperationServiceHelper.executeOperate("save",
                                    data.getDynamicObjectType().getName(), new DynamicObject[]{data},
                                    OperateOption.create());
                            if (Operate.isSuccess()) {//保存成功
                                billno = data.getString("billno");
                                ErrMassage = "同步成功，单据编号为：" + billno;
                                BollISSucces = true;
                                FID = data.getString("id");
                            } else {
                                //保存失败
                                ErrMassage = billnoname+"保存失败！";
                            }
                        }else{
                            ErrMassage = "Json转DynamicObject失败，详情请联系管理员查看日志！";
                        }
                    }
                    else{
                        ErrMassage = "未获取到关单注册注册编号为：【taxpayment】注册单据，Json转换失败！";
                    }
                }
                else{
                    ErrMassage = billnoname+"已存在单据编号为：【" + entryId + "】的单据，禁止新增！";
                }
            } else {
                ErrMassage = "获取到Json为空，获取失败";

            }
        } catch (Exception e) {
            //获取到错误，记录错误日志
            ErrMassage = e.getMessage();
        }finally {
            Map<String, Object> resultMap = new HashMap<String, Object>();
            resultMap.put("description", ErrMassage);
            resultMap.put("result", BollISSucces);
            resultMap.put("billno", billno);
            resultMap.put("FID", FID);
            resultlist.add(resultMap);
            String opdesc = "同步【"+billnoname+"】ERP系统";
            ApilogUtils.Createlog(resultMap, billno, "进口税费", opdesc, interfacetype, jsonData);
        }
        return ApiResult.success(resultlist);
    }
}
