package com.sifang.modules.customer.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.partner.service.HuaRongApiService;
import com.sifang.modules.releorder.dao.CrmInvoiceFileDao;
import com.sifang.modules.releorder.dao.CrmInvoiceInfoDao;
import com.sifang.modules.releorder.dao.CrmReleOrderDao;
import com.sifang.modules.releorder.dao.CrmReleOrderDetailDao;
import com.sifang.modules.releorder.entity.CrmInvoiceFile;
import com.sifang.modules.releorder.entity.CrmInvoiceInfo;
import com.sifang.modules.releorder.entity.CrmReleOrder;
import com.sifang.modules.releorder.entity.CrmReleOrderDetail;
import com.sifang.modules.sys.common.service.BaseService;
import com.sifang.modules.sys.entity.Operator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @title: HuaRongReleOrderService
 * @Author Tan
 * @Date: 2024/10/21 11:03
 * @Version 1.0
 */
@Service
public class HuaRongReleOrderService extends BaseService {

    public static final String clientId = "C2024071000004779074";

    Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    private HuaRongApiService huaRongApiService;

    @Autowired
    private CrmReleOrderDao crmReleOrderDao;

    @Autowired
    private CrmReleOrderDetailDao crmReleOrderDetailDao;

    @Autowired
    private CrmInvoiceInfoDao crmInvoiceInfoDao;

    @Autowired
    private CrmInvoiceFileDao crmInvoiceFileDao;

    @Transactional(rollbackFor = Exception.class)
    public void queryReleOrderInfo() throws Exception {
        //Operator operator = Operator.of("1", "资方-华融", "-1");
        String partnerId = "04";
        int pageNum = 0;
        boolean flag = true;
        do {
            // 页码加1
            pageNum++;
            JSONArray jsonArray = huaRongApiService.queryReleOrderInfo(partnerId,pageNum);
            if(jsonArray != null) {
                for (int o = 0; o < jsonArray.size(); o++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(o);
                    String releCode = jsonObject.getString("releCode");
                    String releOrderStatus = jsonObject.getString("releOrderStatus");
                    String putinAmountTotal = jsonObject.getString("putinAmountTotal");
                    String actualPaymentAmount = jsonObject.getString("actualPaymentAmount");
                    String buckleAmountTotal = jsonObject.getString("buckleAmountTotal");
                    String certificateNumber = jsonObject.getString("certificateNumber");
                    String projectJobName = jsonObject.getString("projectJobName");
                    String releOrderDate = jsonObject.getString("releOrderDate");
                    String invoiceStatus = jsonObject.getString("invoiceStatus");
                    CrmReleOrder crmReleOrder = new CrmReleOrder();
                    crmReleOrder.setReleCode(releCode);
                    crmReleOrder.setReleOrderStatus(releOrderStatus);
                    crmReleOrder.setPutinAmountTotal(putinAmountTotal);
                    crmReleOrder.setActualPaymentAmount(actualPaymentAmount);
                    crmReleOrder.setBuckleAmountTotal(buckleAmountTotal);
                    crmReleOrder.setCertificateNumber(certificateNumber);
                    crmReleOrder.setProjectJobName(projectJobName);
                    crmReleOrder.setReleOrderDate(releOrderDate);
                    crmReleOrder.setInvoiceStatus(invoiceStatus);
                    crmReleOrder.setCreateTime(new Date());
                    CrmReleOrder releOrder = crmReleOrderDao.findReleOrderByCode(releCode);
                    if(releOrder == null){
                        //保存投放基本信息
                        crmReleOrderDao.save(crmReleOrder);
                        //投放明细
                        JSONArray detailArr = jsonObject.getJSONArray("loanDetailsEntitys");
                        if(detailArr != null){
                            for (int i = 0; i < detailArr.size(); i++) {
                                JSONObject detailObject = detailArr.getJSONObject(i);
                                String reqContNumber = detailObject.getString("reqContNumber").replace(clientId, "");
                                String isProjectCompanyModel = detailObject.getString("isProjectCompanyModel");
                                String customerName = detailObject.getString("customerName");
                                String idCardType = detailObject.getString("idCardType");
                                String idCardNo = detailObject.getString("idCardNo");
                                String amount = detailObject.getString("amount");
                                String incomingDate = detailObject.getString("incomingDate");
                                String ongridDate = detailObject.getString("ongridDate");
                                String powerGenerationNo = detailObject.getString("powerGenerationNo");
                                CrmReleOrderDetail crmReleOrderDetail = new CrmReleOrderDetail();
                                crmReleOrderDetail.setReleCode(releCode);
                                crmReleOrderDetail.setReqContNumber(reqContNumber);
                                crmReleOrderDetail.setIsProjectCompanyModel(isProjectCompanyModel);
                                crmReleOrderDetail.setCustomerName(customerName);
                                crmReleOrderDetail.setIdCardType(idCardType);
                                crmReleOrderDetail.setIdCardNo(idCardNo);
                                crmReleOrderDetail.setAmount(amount);
                                crmReleOrderDetail.setIncomingDate(incomingDate);
                                crmReleOrderDetail.setOngridDate(ongridDate);
                                crmReleOrderDetail.setPowerGenerationNo(powerGenerationNo);
                                //保存投放详情信息
                                crmReleOrderDetailDao.save(crmReleOrderDetail);
                            }
                        }
                        //发票明细
                        JSONArray invoiceArr = jsonObject.getJSONArray("invoiceList");
                        if(invoiceArr != null){
                            for (int j = 0; j < invoiceArr.size(); j++) {
                                JSONObject invoiceObject = invoiceArr.getJSONObject(j);
                                String invoiceCode = invoiceObject.getString("invoiceCode");
                                String invoiceNumber = invoiceObject.getString("invoiceNumber");
                                String invoiceDate = invoiceObject.getString("invoiceDate");
                                String invoiceAmount = invoiceObject.getString("invoiceAmount");
                                String invoiceTax = invoiceObject.getString("invoiceTax");
                                String invoiceName = invoiceObject.getString("invoiceName");
                                String remark = invoiceObject.getString("remark");
                                CrmInvoiceInfo invoiceInfo = new CrmInvoiceInfo();
                                invoiceInfo.setReleCode(releCode);
                                invoiceInfo.setInvoiceCode(invoiceCode);
                                invoiceInfo.setInvoiceNumber(invoiceNumber);
                                invoiceInfo.setInvoiceDate(invoiceDate);
                                invoiceInfo.setInvoiceAmount(invoiceAmount);
                                invoiceInfo.setInvoiceTax(invoiceTax);
                                invoiceInfo.setInvoiceName(invoiceName);
                                invoiceInfo.setRemark(remark);
                                crmInvoiceInfoDao.save(invoiceInfo);
                                //附件
                                JSONArray fileArr = invoiceObject.getJSONArray("fileList");
                                if(fileArr != null){
                                    for (int k = 0; k < fileArr.size(); k++) {
                                        JSONObject fileObject = fileArr.getJSONObject(k);
                                        String fileName = fileObject.getString("fileName");
                                        String invoiceFileDate = fileObject.getString("invoiceDate");
                                        CrmInvoiceFile invoiceFile = new CrmInvoiceFile();
                                        invoiceFile.setInvoiceCode(invoiceNumber);
                                        invoiceFile.setFileName(fileName);
                                        invoiceFile.setInvoiceDate(invoiceFileDate);
                                        crmInvoiceFileDao.save(invoiceFile);
                                    }
                                }
                            }
                        }
                    }else {
                        if(!invoiceStatus.equals(releOrder.getInvoiceStatus())){
                            releOrder.setInvoiceStatus(invoiceStatus);
                            //修改投放基本信息
                            crmReleOrderDao.save(releOrder);
                        }
                    }
                }
            }else {
                flag = false;
            }
        } while (flag);
        //投放基本信息
    }

}