package com.ruoyi.system.support;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.system.model.*;
import com.ruoyi.system.model.voucher.VoucherAuxiliary;
import com.ruoyi.system.model.voucher.VoucherBody;
import com.ruoyi.system.model.voucher.VoucherImportModel;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author jiuli
 * @Date 2024/3/5
 */
public class VoucherQuerySupport {

    private final static String TENANT_ID = "dmrn1j0i";

    static Snowflake snowflake = new Snowflake(1, 1);

    public static List<VoucherImportModel> getVoucherImportModel(List<VoucherQueryResponseModel> voucherQueryResponseModelList) {
        List<VoucherImportModel> voucherImportModelList = new ArrayList<>();
        for (VoucherQueryResponseModel voucherQueryResponseModel : voucherQueryResponseModelList) {
            List<RecordVoucherModel> recordList = voucherQueryResponseModel.getRecordList();
            for (RecordVoucherModel recordVoucherModel : recordList) {
                String id = recordVoucherModel.getHeader().getId();
                HeaderModel header = recordVoucherModel.getHeader();
                VoucherImportModel voucherImportModel = new VoucherImportModel();
                voucherImportModel.setId(id);
                voucherImportModel.setPeriod(header.getPeriod());
                voucherImportModel.setDisplayname(header.getDisplayname());
                voucherImportModel.setSrcsystem(header.getSrcsystem());
                voucherImportModel.setVoucherstatus(header.getVoucherstatus());
                voucherImportModel.setTotaldebit_org(header.getTotaldebit_org());
                voucherImportModel.setTotalcredit_org(header.getTotalcredit_org());
                voucherImportModel.setOrganizeDebitTotalAmount(header.getOrganizeDebitTotalAmount());
                voucherImportModel.setOrganizeCreditTotalAmount(header.getOrganizeCreditTotalAmount());
                voucherImportModel.setTotaldebit_global(header.getTotaldebit_global());
                voucherImportModel.setTotalcredit_group(header.getTotalcredit_group());
                voucherImportModel.setTotaldebit_group(header.getTotaldebit_group());
                voucherImportModel.setTotalcredit_global(header.getTotalcredit_global());
                voucherImportModel.setMakerId(header.getMaker().getId());
                voucherImportModel.setMakerName(header.getMaker().getName());
                if (ObjectUtils.isNotEmpty(header.getAuditor())) {
                    voucherImportModel.setAuditorId(header.getAuditor().getId());
                    voucherImportModel.setAuditorName(header.getAuditor().getName());
                }
                if (ObjectUtils.isNotEmpty(header.getTallyman())) {
                    voucherImportModel.setTallymanId(header.getTallyman().getId());
                    voucherImportModel.setTallymanName(header.getTallyman().getName());
                }
                voucherImportModel.setAccBookCode(header.getAccbook().getCode());
                voucherImportModel.setAccBookName(header.getAccbook().getName());
                voucherImportModel.setOrgCode(header.getAccbook().getPk_org().getCode());
                voucherImportModel.setOrgName(header.getAccbook().getPk_org().getName());
                voucherImportModel.setVoucherTypeName(header.getVouchertype().getName());
                voucherImportModel.setVoucherStr(header.getVouchertype().getVoucherstr());
                voucherImportModel.setYtenantId(TENANT_ID);
                voucherImportModel.setDescription(header.getDescription());
                voucherImportModelList.add(voucherImportModel);
            }
        }
        return voucherImportModelList;
    }

    public static List<VoucherBody> getVoucherBody(List<VoucherQueryResponseModel> voucherQueryResponseModelList) {
        List<VoucherBody> voucherBodyList = new ArrayList<>();
        for (VoucherQueryResponseModel voucherQueryResponseModel : voucherQueryResponseModelList) {
            List<RecordVoucherModel> recordList = voucherQueryResponseModel.getRecordList();
            for (RecordVoucherModel recordVoucherModel : recordList) {
                String id = recordVoucherModel.getHeader().getId();
                List<BodyModel> body = recordVoucherModel.getBody();
                for (BodyModel bodyModel : body) {
                    VoucherBody voucherBody = new VoucherBody();
                    voucherBody.setVoucher_id(id);
                    voucherBody.setRecordnumber(String.valueOf(bodyModel.getRecordnumber()));
                    voucherBody.setDescription(bodyModel.getDescription());
                    voucherBody.setDebit_original(bodyModel.getDebit_original());
                    voucherBody.setCredit_original(bodyModel.getCredit_original());
                    voucherBody.setDebit_org(bodyModel.getDebit_org());
                    voucherBody.setCredit_org(bodyModel.getCredit_org());
                    voucherBody.setOrganize_debit_amount(bodyModel.getOrganize_debit_amount());
                    voucherBody.setOrganize_credit_amount(bodyModel.getOrganize_credit_amount());
                    voucherBody.setDebit_group(bodyModel.getDebit_group());
                    voucherBody.setCredit_group(bodyModel.getCredit_group());
                    voucherBody.setDebit_global(bodyModel.getDebit_global());
                    voucherBody.setCredit_global(bodyModel.getCredit_global());
                    voucherBody.setQuantity(bodyModel.getQuantity());
                    voucherBody.setDebit_quantity(bodyModel.getDebit_quantity());
                    voucherBody.setCredit_quantity(bodyModel.getCredit_quantity());
                    voucherBody.setPrice(bodyModel.getPrice());
                    voucherBody.setCheckflag(String.valueOf(bodyModel.getCheckflag()));
                    voucherBody.setAuxiliaryShow(bodyModel.getAuxiliaryShow());
                    voucherBody.setAccsubject(bodyModel.getAuxiliary());
                    voucherBody.setAccCode(bodyModel.getAccsubject().getCode());
                    voucherBody.setAccName(bodyModel.getAccsubject().getName());
                    voucherBody.setCashCategory(bodyModel.getAccsubject().getCashCategory());
                    voucherBody.setCurrencyId(bodyModel.getCurrency().getId());
                    voucherBody.setCurrencyCode(bodyModel.getCurrency().getCode());
                    voucherBody.setCurrencyName(bodyModel.getCurrency().getName());
                    voucherBody.setId(bodyModel.getId());
                    voucherBody.setYtenantId(TENANT_ID);
                    voucherBodyList.add(voucherBody);
                }
            }
        }
        return voucherBodyList;
    }

    public static List<VoucherAuxiliary> getAuxiliaryModel(List<VoucherQueryResponseModel> voucherQueryResponseModelList) {
        List<VoucherAuxiliary> auxiliaryModelList = new ArrayList<>();
        for (VoucherQueryResponseModel voucherQueryResponseModel : voucherQueryResponseModelList) {
            List<RecordVoucherModel> recordList = voucherQueryResponseModel.getRecordList();
            for (RecordVoucherModel recordVoucherModel : recordList) {
                List<BodyModel> body = recordVoucherModel.getBody();
                for (BodyModel bodyModel : body) {
                    String id = bodyModel.getId();
                    List<ClientauxiliaryModel> clientauxiliary = bodyModel.getClientauxiliary();
                    if (!CollectionUtils.isEmpty(clientauxiliary)) {
                        for (ClientauxiliaryModel clientauxiliaryModel : clientauxiliary) {
                            if (ObjectUtils.isEmpty(clientauxiliaryModel)) {
                                continue;
                            }
                            VoucherAuxiliary auxiliaryModel = new VoucherAuxiliary();
                            auxiliaryModel.setAuxiliaryCode(clientauxiliaryModel.getCode());
                            auxiliaryModel.setAuxiliaryName(clientauxiliaryModel.getName());
                            auxiliaryModel.setAuxiliarySrcCode(clientauxiliaryModel.getData().getCode());
                            auxiliaryModel.setAuxiliarySrcName(clientauxiliaryModel.getData().getName());
                            auxiliaryModel.setVoucherBody_id(id);
                            auxiliaryModel.setId(String.valueOf(snowflake.nextId()));
                            auxiliaryModel.setYtenantId(TENANT_ID);
                            auxiliaryModelList.add(auxiliaryModel);
                        }
                    }
                }
            }
        }
        return auxiliaryModelList;
    }

    public static List<String> getHeadIdList(List<VoucherQueryResponseModel> voucherQueryResponseModelList) {
        List<String> idList = new ArrayList<>();
        for (VoucherQueryResponseModel voucherQueryResponseModel : voucherQueryResponseModelList) {
            List<RecordVoucherModel> recordList = voucherQueryResponseModel.getRecordList();
            for (RecordVoucherModel voucherModel : recordList) {
                idList.add(voucherModel.getHeader().getId());
            }
        }
        return idList;
    }

    public static List<String> getBodyIdList(List<VoucherQueryResponseModel> voucherQueryResponseModelList) {
        List<String> bodyIdList = new ArrayList<>();
        for (VoucherQueryResponseModel voucherQueryResponseModel : voucherQueryResponseModelList) {
            List<RecordVoucherModel> recordList = voucherQueryResponseModel.getRecordList();
            for (RecordVoucherModel voucherModel : recordList) {
                List<BodyModel> body = voucherModel.getBody();
                for (BodyModel bodyModel : body) {
                    bodyIdList.add(bodyModel.getId());
                }
            }
        }
        return bodyIdList;
    }
}
