package com.seeyon.apps.src_powerfeeinvoice.service;

import com.seeyon.apps.src_hxinvoice.util.FileUtil;
import com.seeyon.apps.src_hxinvoice.util.FormExportUtil;
import com.seeyon.apps.src_powerfeeinvoice.config.HttpFormRestApiConfig;
import com.seeyon.apps.src_powerfeeinvoice.config.PowerFeePluginConfigProvider;
import com.seeyon.apps.src_powerfeeinvoice.constants.PowerFeeInvoiceConstants;
import com.seeyon.apps.src_powerfeeinvoice.utils.ClauseFactor;
import com.seeyon.apps.src_powerfeeinvoice.utils.EnumMapUtils;
import com.seeyon.apps.src_powerfeeinvoice.utils.FormColumn;
import com.seeyon.apps.src_powerfeeinvoice.utils.FormDataOperator;;
import com.seeyon.apps.src_powerfeeinvoice.utils.FormWhereCondition;
import com.seeyon.apps.src_powerfeeinvoice.utils.FormUpdateField;
import com.seeyon.cap4.form.api.FormApi4Cap4;
import com.seeyon.cap4.form.bean.FormBean;
import com.seeyon.cap4.form.bean.FormTableBean;
import com.seeyon.ctp.common.AppContext;
import com.seeyon.ctp.common.exceptions.BusinessException;
import com.seeyon.ctp.common.po.filemanager.Attachment;
import com.seeyon.v3x.services.form.FormFactory;
import com.seeyon.v3x.services.form.bean.FormExport;
import com.seeyon.v3x.services.form.bean.ValueExport;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Component
public class PowerFeeService {

    public List<FormColumn> queryCondition(String formNo, List<String> queryColumnVos, List<FormWhereCondition> conditionVos) throws BusinessException {
        FormDataOperator formDataOperator = new FormDataOperator();
        return formDataOperator.queryFormDataCondition(formNo, queryColumnVos, conditionVos);
    }

    public void refreshForm(String formNo, String formId, String formLoginName, HttpFormRestApiConfig config) throws BusinessException, IOException {
        FormDataOperator formDataOperator = new FormDataOperator();
        formDataOperator.refreshForm(formId,formNo,formLoginName,config);
    }

    public FormColumn getById(String formNo,String formId) throws Exception {
        FormDataOperator formDataOperator = new FormDataOperator();
        List<FormColumn> formColumns = formDataOperator.queryFormDataCondition(formNo, null, Arrays.asList(FormWhereCondition.build().display("ID").value(formId).clauseFactor(ClauseFactor.EQ)));
        if(formColumns == null || formColumns.size() == 0 ){
            throw new RuntimeException("记录未找到");
        }
       return formColumns.get(0);
    }

    public Long count(String formNo) throws Exception{
        FormDataOperator formDataOperator = new FormDataOperator();
        return formDataOperator.countCondition(formNo,new ArrayList<>());
    }

    public void updateForm(List<FormUpdateField> updateFieldVos,List<FormWhereCondition> whereClauseData,String formNo) throws BusinessException {
        FormDataOperator formDataOperator = new FormDataOperator();
        formDataOperator.updateMasterForm(formNo,updateFieldVos,whereClauseData);
    }

    public void insertIntoInvoiceForm(Map<String,Object> map,List<String> fileNames,Long memberId,Long account) throws Exception {
        FileUtil fileUtil = new FileUtil();
        PowerFeePluginConfigProvider configProvider = (PowerFeePluginConfigProvider) AppContext.getBean("powerFeePluginConfigProvider");
        List<FormWhereCondition> queryCondition = new ArrayList<>();
        String formNo = configProvider.getBizConfigByKey(PowerFeeInvoiceConstants.invoiceFormNo);
        FormApi4Cap4 formApi4Cap4 = (FormApi4Cap4) AppContext.getBean("formApi4Cap4");
        FormBean cap4FormBean = formApi4Cap4.getFormByFormCode(formNo);
        FormTableBean masterTableBean = cap4FormBean.getMasterTableBean();
        String KPLX = (String)map.get("开票类型");
        map.put("红蓝票类型", EnumMapUtils.getEnumItemValueByEnumIdAndEnumValue(KPLX, masterTableBean.getFieldBeanByDisplay("红蓝票类型").getEnumId()));
        FormExport formExport = new FormExport();
        FormExportUtil formExportUtil = new FormExportUtil();
        List<ValueExport> valueExport = formExportUtil.setFormValue(map);
        String DDQQLSH = (String) map.get("订单请求流水号");
        if(checkInvoiceRecordExist(DDQQLSH,formNo)) {
            //记录已存在则不继续添加
            return;
        }
        formExport.setValues(valueExport);
        FormFactory formFactory = (FormFactory) AppContext.getBean("formFactory");
        String loginName = configProvider.getBizConfigByKey(PowerFeeInvoiceConstants.formInsertLoginName);
        //核对枚举是否需要转换处理..
        formFactory.importBusinessFormData(loginName, formNo,
                formExport, new String[] {});
        FormDataOperator formDataOperator = new FormDataOperator();
        queryCondition.add(FormWhereCondition.build().display("订单请求流水号").value(DDQQLSH).clauseFactor(ClauseFactor.EQ));
        List<FormColumn> columns = formDataOperator.queryFormDataCondition(formNo, null, queryCondition);
        if(columns != null && columns.size() > 0) {
            String formId = columns.get(0).getId();
            List<Attachment> attachments = fileUtil.fileUpload(fileNames,formId,memberId,account);
            String refId = attachments.get(0).getSubReference()+"";
            FormUpdateField updateField = FormUpdateField.build().display("数电发票附件").value(refId);
            List<FormWhereCondition> whereConditions = new ArrayList<>();
            whereConditions.add(FormWhereCondition.build().value(formId).display("ID").clauseFactor(ClauseFactor.EQ));
            formDataOperator.updateMasterForm(formNo, Arrays.asList(updateField),whereConditions);
        }
    }

    private boolean checkInvoiceRecordExist(String DDQQLSH,String formNo) throws BusinessException {
        FormDataOperator formDataOperator = new FormDataOperator();
        List<FormWhereCondition> queryCondition = new ArrayList<>();
        queryCondition.add(FormWhereCondition.build().display("订单请求流水号").value(DDQQLSH).clauseFactor(ClauseFactor.EQ));
        List<FormColumn> columns = formDataOperator.queryFormDataCondition(formNo, null, queryCondition);
        return !(columns == null || columns.size() == 0);
    }

    public void updatePowerFeeForm(Map<String,Object> map) throws Exception {
        PowerFeePluginConfigProvider configProvider = (PowerFeePluginConfigProvider) AppContext.getBean("powerFeePluginConfigProvider");
        List<FormWhereCondition> queryCondition = new ArrayList<>();
        String qqlsh = (String) map.get("订单请求流水号");
        queryCondition.add(FormWhereCondition.build().value(qqlsh).display("订单请求流水号").clauseFactor(ClauseFactor.EQ));
        String formId = null;
        List<FormColumn> columns = queryCondition(configProvider.getBizConfigByKey(PowerFeeInvoiceConstants.formNo), null, queryCondition);
        if(columns == null || columns.size() == 0) {
            throw new RuntimeException("按照订单请求流水号查询的电费信息不存在");
        }
        FormColumn column = columns.get(0);
        formId = column.getId();
        List<FormUpdateField> updateFields = new ArrayList<>();
        updateFields.add(FormUpdateField.build().display("是否已开票").value("是"));
       List<FormWhereCondition> conditions = new ArrayList<>();
        conditions.add(FormWhereCondition.build().value(Long.parseLong(formId)).display("id").clauseFactor(ClauseFactor.EQ));
        updateForm(updateFields,conditions,configProvider.getBizConfigByKey(PowerFeeInvoiceConstants.formNo));
    }
}
