package yem.wmc.ca.api;


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.openapi.common.custom.annotation.ApiController;
import kd.bos.openapi.common.custom.annotation.ApiParam;
import kd.bos.openapi.common.custom.annotation.ApiPostMapping;
import kd.bos.openapi.common.result.CustomApiResult;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.OperationServiceHelper;
import org.jetbrains.annotations.NotNull;
import yem.wmc.ca.utils.ApilogUtils;

import java.util.*;

import static yem.wmc.ca.utils.JsonUtils.jsonToData;

/**
 * @author zhouc
 * @date 2023/5/31 21:46
 * @className Cuslaration
 * @description 退税报关单
 */
@ApiController(value = "Cuslaration", desc = "创建退税报关单")
public class Cuslaration {

    @ApiPostMapping("/Cuslaration")
    public CustomApiResult<Object> doCustomService(@NotNull @ApiParam("入参") String jsion, @NotNull @ApiParam("单据标识") String entityName) {
        List<Object> resultlist = Collections.synchronizedList(new ArrayList<>());
        boolean BollISSucces = false;
        String ErrMassage = "",FID = "",billno = "";
        JSONObject jsonData = null;
        try {
            jsonData = JSONObject.parseObject(jsion);
            if (null != jsonData) {
                String entryId = JSONObject.parseObject(JSONObject.parseObject(jsonData.getString("responseData")).getString("cardHeadVo")).getString("entryId");//单据编号
                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, "Cuslaration");
                    DynamicObject[] yem_ca_billlogon_s = BusinessDataServiceHelper.load("yem_ca_billlogon",
                            "id,billno,billstatus", new QFilter[]{qFilter}, null);
                    if (yem_ca_billlogon_s != null && yem_ca_billlogon_s.length > 0) {
                        DynamicObject data = jsonToData(jsonData.getString("responseData"), entityName, yem_ca_billlogon_s, entryId, JSONObject.parseObject(jsonData.getString("responseData")));
                        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 = "退税报关单保存失败！";
                            }
                        } else {
                            ErrMassage = "Json转DynamicObject失败，详情请联系管理员查看日志！";
                        }
                    } else {
                        ErrMassage = "未获取到关单注册注册编号为：【taxpayment】注册单据，Json转换失败！";
                    }
                } else {
                    ErrMassage = "退税报关单已存在单据编号为：【" + 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 = "同步退税报关单ERP系统";
            ApilogUtils.Createlog(resultMap, billno, "退税报关单", opdesc, "AddTaxPayment", jsonData);
        }
//        return ApiResult.success(resultlist);
        return CustomApiResult.success(resultlist);
    }
}
