package com.yem.wm.bc.helper;

import com.yem.wm.bc.common.CommonUtils;
import com.yem.wm.bc.common.enums.BillStatusEnum;
import com.yem.wm.bc.common.enums.EnableStatusEnum;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.billtype.BillTypeServiceHelper;
import kd.bos.servicehelper.parameter.SystemParamServiceHelper;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 夜子
 * @ClassName BillTypeHelper
 * @description: 单据类型帮助类
 * @date 2023年08月03日
 * @version: 1.0
 */
public class BillTypeHelper {
    public BillTypeHelper() {
    }

    public static boolean checkBillType(String formId, Long billtypeId) {
        if (CommonUtils.isNull(billtypeId)) {
            return false;
        } else {
            List<Long> billTypeList = getAllBillTypes(formId);
            return billTypeList != null && billTypeList.contains(billtypeId);
        }
    }

    public static Long getDefaultBillType(String formId) {
        if (CommonUtils.isNull(formId)) {
            return null;
        } else {
            Long billType = null;
            Map<Object, DynamicObject> map = BillTypeServiceHelper.getBillTypeObject(formId);
            if (CommonUtils.isNull(map)) {
                return null;
            } else {
                Iterator<DynamicObject> var3 = map.values().iterator();

                DynamicObject dym;
                do {
                    if (!var3.hasNext()) {
                        return (Long)billType;
                    }

                    dym = (DynamicObject)var3.next();
                } while(!dym.getBoolean("isdefault"));

                return (Long)dym.getPkValue();
            }
        }
    }

    public static List<Long> getAllBillTypes(String formId) {
        if (CommonUtils.isNull(formId)) {
            return null;
        } else {
            new ArrayList();
            Map<Object, DynamicObject> map = BillTypeServiceHelper.getBillTypeObject(formId);
            if (CommonUtils.isNull(map)) {
                return null;
            } else {
                List<Long> billTypeList = (List)map.values().stream().map((dym) -> {
                    return dym.getLong("id");
                }).collect(Collectors.toList());
                return billTypeList;
            }
        }
    }

    public static Object getBillTypeParameter(String formId, long billTypeId, String paramFormId, String propertyName) {
        if (!CommonUtils.isNull(formId) && !CommonUtils.isNull(billTypeId) && !CommonUtils.isNull(paramFormId) && !CommonUtils.isNull(propertyName)) {
            DynamicObject billTypeParam = (DynamicObject) SystemParamServiceHelper.getBillTypeParameter(formId, paramFormId, billTypeId);
            return !CommonUtils.isNull(billTypeParam) ? billTypeParam.get(propertyName) : null;
        } else {
            return null;
        }
    }

    public static Map<Object, DynamicObject> getBillTypesByEntity(String formid) {
        QFilter qFilter1 = new QFilter("status", "=", BillStatusEnum.AUDIT.getValue());
        QFilter qFilter2 = new QFilter("billformid", "=", formid);
        QFilter qFilter3 = new QFilter("enable", "=", EnableStatusEnum.ENABLE.getValue());
        Map<Object, DynamicObject> billTypes = BusinessDataServiceHelper.loadFromCache("bos_billtype", "billformid,number,name", new QFilter[]{qFilter1, qFilter2, qFilter3}, "isdefault desc");
        return CommonUtils.isNull(billTypes) ? null : billTypes;
    }

    public static DynamicObject getBillTypeByEntity(String formid) {
        Map<Object, DynamicObject> billTypes = getBillTypesByEntity(formid);
        return billTypes != null && !billTypes.isEmpty() ? (DynamicObject)billTypes.values().toArray()[0] : null;
    }
}

