package com.yem.wm.im.giftapply.op;

import com.yem.wm.utils.GiftAmount;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @author power
 * @ClassName： ReceiveAuditOp
 * @description: 礼品领用申请 审核 服务插件
 * @date 2023/7/27
 */
public class ReceiveAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        e.getFieldKeys().add("id");
        e.getFieldKeys().add("billno");
        e.getFieldKeys().add("yem_customer");
        e.getFieldKeys().add("yem_bizdate");
        e.getFieldKeys().add("yem_currency");
        e.getFieldKeys().add("yem_amount");
        e.getFieldKeys().add("yem_exrate");
        e.getFieldKeys().add("yem_amountbase");
        e.getFieldKeys().add("yem_remark");
        e.getFieldKeys().add("yem_billtype");
        e.getFieldKeys().add("yem_bd_products");
        e.getFieldKeys().add("yem_giftapplytype");

    }
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {


        // TODO 在此添加业务逻辑
        super.endOperationTransaction(e);
        //获取按钮
        String key = e.getOperationKey();
        DynamicObject[] entities = e.getDataEntities();


        if(key.equals("audit")) {
//            需要去向类型 礼品领用 去向单号 业务日期 结算币别 金额 去向单据汇率 本位币金额 备注

            for (DynamicObject bill : entities) {
                String billType = bill.getString("yem_billtype.number");
                if (billType.equals("yem_im_giftapply_LY")){
                    String id = String.valueOf(bill.getLong("id"));
                    String billno = bill.getString("billno");
                    Date bizDate = bill.getDate("yem_bizdate");
                    DynamicObject currency = bill.getDynamicObject("yem_currency");
                    BigDecimal amount = bill.getBigDecimal("yem_amount");
                    BigDecimal exrate = bill.getBigDecimal("yem_exrate");
                    BigDecimal amountBase = bill.getBigDecimal("yem_amountbase");
                    String remark = bill.getString("yem_remark");
//                    Long customer = bill.getLong("yem_customer.id");
                    DynamicObject saleorderCustomer = bill.getDynamicObject("yem_customer");
                    Long customer = null;//
                    if (saleorderCustomer != null) {
                        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(saleorderCustomer.getPkValue(), saleorderCustomer.getDynamicObjectType().getName());
                        if (dynamicObject != null) {
                            DynamicObject groupcusfields = dynamicObject.getDynamicObject("yem_groupcustomers");
                            if (groupcusfields  != null) {
                                customer = groupcusfields.getLong("id");
                            }
                        }
                    }
                    Long org = bill.getLong("org.id");
                    Long yem_bd_products = bill.getLong("yem_bd_products.id");

                    DynamicObject data = BusinessDataServiceHelper.loadSingle("yem_im_giftamount",
                            new QFilter[]{new QFilter("yem_groupcusfields.id", QCP.equals, customer),
                                    new QFilter("org", QCP.equals,org),
                                    new QFilter("yem_bd_products", QCP.equals,yem_bd_products)});
                    if (data!=null){
                        DynamicObjectCollection entryentity = data.getDynamicObjectCollection("yem_im_giftamount_t");
                        DynamicObject newEntryEntity =  entryentity.addNew();
                        newEntryEntity.set("yem_towardtype", "A");
                        newEntryEntity.set("yem_producttypes", yem_bd_products);
                        newEntryEntity.set("yem_giftapplicationtys", bill.getDynamicObject("yem_giftapplytype"));
                        newEntryEntity.set("yem_towardno", billno);
                        newEntryEntity.set("yem_towardbizdate", bizDate);
                        newEntryEntity.set("yem_towardcurr", currency);
                        newEntryEntity.set("yem_towardamt", amount);
                        newEntryEntity.set("yem_towardexrate", exrate);
                        newEntryEntity.set("yem_amountbaseamt", amountBase);
                        newEntryEntity.set("yem_towardremark", remark);
                        newEntryEntity.set("yem_im_giftapplyid", id);
                        SaveServiceHelper.save(new DynamicObject[] {data});
                        GiftAmount.Calculate(customer,org,yem_bd_products);
                    }
                }
            }

        }
        if (key.equals("unaudit")){
            for (DynamicObject bill : entities) {
                String billType = bill.getString("yem_billtype.number");
                if (billType.equals("yem_im_giftapply_LY")){
//                    Long customer = bill.getLong("yem_customer.id");
                    DynamicObject saleorderCustomer = bill.getDynamicObject("yem_customer");
                    Long customer = null;//
                    if (saleorderCustomer != null) {
                        DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(saleorderCustomer.getPkValue(), saleorderCustomer.getDynamicObjectType().getName());
                        if (dynamicObject != null) {
                            DynamicObject groupcusfields = dynamicObject.getDynamicObject("yem_groupcustomers");
                            if (groupcusfields  != null) {
                                customer = groupcusfields.getLong("id");
                            }
                        }
                    }
                    Long org = bill.getLong("org.id");
                    Long yem_bd_products = bill.getLong("yem_bd_products.id");
                    String id = String.valueOf(bill.getLong("id"));
                    DynamicObject data = BusinessDataServiceHelper.loadSingle("yem_im_giftamount",
                            new QFilter[]{new QFilter("yem_groupcusfields.id", QCP.equals, customer),
                                    new QFilter("org", QCP.equals,org),
                                    new QFilter("yem_bd_products", QCP.equals,yem_bd_products)});

                    if (data != null) {
                        DynamicObjectCollection entryentity = data.getDynamicObjectCollection("yem_im_giftamount_t");
                        for (int j = 0; j < entryentity.size(); j++) {
                            if (entryentity.get(j).getString("yem_im_giftapplyid").equals(id)){
                                entryentity.remove(j);
                            }
                        }
                        SaveServiceHelper.save(new DynamicObject[] {data});
                        GiftAmount.Calculate(customer,org,yem_bd_products);
                    }

                }
            }
        }
    }

}
