package com.sifang.modules.releorder.service;

import com.alibaba.fastjson.JSONArray;
import com.sifang.modules.partner.service.HuaRongApiService;
import com.sifang.modules.releorder.dao.CrmInvoiceOperationDao;
import com.sifang.modules.releorder.entity.CrmInvoiceOperation;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @title: CrmInvoiceOperationService
 * @Author Tan
 * @Date: 2024/10/24 9:36
 * @Version 1.0
 */
@Service
public class CrmInvoiceOperationService extends BaseService {

    @Autowired
    private CrmInvoiceOperationDao invoiceOperationDao;

    @Autowired
    private HuaRongApiService huaRongApiService;

    public Result saveInvoiceData(CrmInvoiceOperation invoiceOperation){
        Result result=new Result();
        invoiceOperation.setCreateTime(new Date());
        invoiceOperation.setIsSubmit("0");
        invoiceOperationDao.save(invoiceOperation);
        result.setCode("200");
        return result;
    }

    public List<CrmInvoiceOperation> findInvoiceByreleCode(String releCode){
        return invoiceOperationDao.findInvoiceByreleCode(releCode);
    }

    public CrmInvoiceOperation findInvoiceById(String id){
        return invoiceOperationDao.findInvoiceById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    public Result submitInvoiceData(String releCode){
        Result result=new Result();
        //查询该投放单是否上传过发票
        int num = invoiceOperationDao.findInvoiceNoSubmitData(releCode,1).size();
        List<CrmInvoiceOperation> list = invoiceOperationDao.findInvoiceNoSubmitData(releCode,0);
        int i = 0;
        for(CrmInvoiceOperation invoice : list){
            String[] url = invoice.getInvoiceFile().split(",");
            for(String fileUrl : url){
                String[] sp = fileUrl.split("/");
                String fileName = sp[sp.length -1];
                Map<String,Object> map=new HashMap<>();
                map.put("capital","04");
                map.put("releCode",invoice.getReleCode());
                map.put("invoiceCode",invoice.getInvoiceCode());
                map.put("invoiceNumber",invoice.getInvoiceNumber());
                map.put("invoiceDate",invoice.getInvoiceDate());
                map.put("invoiceAmount",invoice.getInvoiceAmount());
                map.put("invoiceTax",invoice.getInvoiceTax()+"%");
                map.put("invoiceName",invoice.getInvoiceName());
                if(num>0){
                    map.put("invoiceUploadStatus","0");
                }else {
                    //发票上传方式 0 :追加上传 1 :覆盖上传
                    if(i>0){
                        map.put("invoiceUploadStatus","0");
                    }else{
                        map.put("invoiceUploadStatus","1");
                    }
                }
                map.put("remark",invoice.getRemark());
                map.put("fileUrl",fileUrl);
                map.put("fileName",fileName);
                JSONArray jsonArray = huaRongApiService.releOrderInfoUpload(map);
                if(jsonArray != null){
                    CrmInvoiceOperation invoiceOperation = invoiceOperationDao.get(invoice.getId());
                    invoiceOperation.setIsSubmit("1");
                    invoiceOperationDao.save(invoiceOperation);
                }
                i++;
            }
        }
        result.setCode("200");
        return result;
    }


}