package cn.superdesk.uniorder.dubbo.provider.thirdpart.szhtxx.electronicInvoice;

import cn.hutool.json.JSONUtil;
import cn.superdesk.uniorder.common.numerical.AnBeanUtils;
import cn.superdesk.uniorder.common.rest.response.R;
import cn.superdesk.uniorder.common.security.dto.LoginSysUser;
import cn.superdesk.uniorder.dao.entity.invoice.customer.InvoiceApply;
import cn.superdesk.uniorder.dao.entity.sys.notify.SysNotify;
import cn.superdesk.uniorder.dao.entity.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxx;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.common.singleBilling.SzhtxxInvoiceFptxxOpSingleBillingForm;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxOpSendEmailPdfAttachmentForm;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxParam;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxQueryCriteria;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxDetailVo;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxHeadDataVo;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxListItemVo;
import cn.superdesk.uniorder.dubbo.facade.sys.notify.DubboSysNotifyService;
import cn.superdesk.uniorder.dubbo.facade.thirdpart.szhtxx.electronicInvoice.DubboSzhtxxElectronicInvoiceFptxxService;
import cn.superdesk.uniorder.openapicall.thirdpart.szhtxx.electronicInvoice.dto.request.SingleBillingRequestBodyKpContent;
import cn.superdesk.uniorder.service.invoice.customer.InvoiceApplyCustomerService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService;
import cn.superdesk.uniorder.service.invoice.customer.state.InvoiceApplyCustomerFieldMakeInvoiceStatusService;
import cn.superdesk.uniorder.service.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxExportExcelService;
import cn.superdesk.uniorder.service.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @Description:
 * @Author: linjinyu
 * @Email linjinyu@avic-s.com
 * @CreateDate: 2022-01-24 11:50:43
 * @UpdateUser: linjinyu
 * @UpdateDate: 2022-01-24 11:50:43
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
@Slf4j
@Service(version = "1.0.0", retries = -1, timeout = 10000)
public class DubboSzhtxxElectronicInvoiceFptxxServiceImpl implements DubboSzhtxxElectronicInvoiceFptxxService {

    @Autowired
    private SzhtxxElectronicInvoiceFptxxService            szhtxxElectronicInvoiceFptxxService;
    @Autowired
    private SzhtxxElectronicInvoiceFptxxExportExcelService szhtxxElectronicInvoiceFptxxExportExcelService;

    @Autowired
    private InvoiceApplyCustomerService invoiceApplyCustomerService;

    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusService invoiceApplyCustomerFieldMakeInvoiceStatusService;

    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService                 invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingOffLineService;
    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService;
    @Autowired
    private InvoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService      invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxPaperInvoiceService;


    /****************************************** 引用自身 Dubbo provider start ******************************************/
    @Reference(version = "1.0.0", timeout = 10000, retries = -1)
    private DubboSysNotifyService dubboSysNotifyService;


    /****************************************** extends BaseService 部分 start ******************************************/

    @Override
    public int insertSelective(SzhtxxElectronicInvoiceFptxx entity) {

        return szhtxxElectronicInvoiceFptxxService.insertSelective(entity);

    }

    @Override
    public int updateByPrimaryKeySelective(SzhtxxElectronicInvoiceFptxx entity) {

        return szhtxxElectronicInvoiceFptxxService.updateByPrimaryKeySelective(entity);
    }

    @Override
    public void deleteObj(Long id) {
        szhtxxElectronicInvoiceFptxxService.deleteObj(id);
    }

    @Override
    public SzhtxxElectronicInvoiceFptxx findById(Long id) {
        return szhtxxElectronicInvoiceFptxxService.findById(id);
    }

    @Override
    public PageInfo<SzhtxxElectronicInvoiceFptxx> findByPaging(Map<String, Object> conditions, Integer pageNun, Integer pageSize) {
        return szhtxxElectronicInvoiceFptxxService.findByPaging(conditions, pageNun, pageSize);
    }

    @Override
    public List<SzhtxxElectronicInvoiceFptxx> findAll(Map<String, Object> queryMap) {

        return szhtxxElectronicInvoiceFptxxService.findAll(queryMap);
    }

    /****************************************** extends BaseService 部分   end ******************************************/


    @Override
    public int addSzhtxxElectronicInvoiceFptxx(SzhtxxElectronicInvoiceFptxxParam param) {

        SzhtxxElectronicInvoiceFptxx szhtxxElectronicInvoiceFptxx = AnBeanUtils.copy(param, SzhtxxElectronicInvoiceFptxx.class);
        return szhtxxElectronicInvoiceFptxxService.addSzhtxxElectronicInvoiceFptxx(szhtxxElectronicInvoiceFptxx);
    }

    @Override
    public int updateSzhtxxElectronicInvoiceFptxx(SzhtxxElectronicInvoiceFptxxParam param) {

        SzhtxxElectronicInvoiceFptxx szhtxxElectronicInvoiceFptxx = AnBeanUtils.copy(param, SzhtxxElectronicInvoiceFptxx.class);
        return szhtxxElectronicInvoiceFptxxService.updateSzhtxxElectronicInvoiceFptxx(szhtxxElectronicInvoiceFptxx);
    }

    @Override
    public int deleteSzhtxxElectronicInvoiceFptxx(Long id) {
        return szhtxxElectronicInvoiceFptxxService.deleteSzhtxxElectronicInvoiceFptxx(id);
    }

    @Override
    public SzhtxxElectronicInvoiceFptxx findSzhtxxElectronicInvoiceFptxxById(Long id) {
        return szhtxxElectronicInvoiceFptxxService.findSzhtxxElectronicInvoiceFptxxById(id);
    }

    @Override
    public R<List<SzhtxxElectronicInvoiceFptxx>> pageQuery(Map<String, Object> conditions, int pageNun, int pageSize) {
        return szhtxxElectronicInvoiceFptxxService.pageQuery(conditions, pageNun, pageSize);
    }

    @Override
    public R<List<SzhtxxElectronicInvoiceFptxxListItemVo>> pageQueryGenericParam(
            SzhtxxElectronicInvoiceFptxxQueryCriteria queryCriteria,
            LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxService.pageQueryGenericParam(queryCriteria, loginSysUser);
    }

    @Override
    public List<SzhtxxElectronicInvoiceFptxxListItemVo> queryGenericParam(SzhtxxElectronicInvoiceFptxxQueryCriteria queryCriteria, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxService.queryGenericParam(queryCriteria, loginSysUser);
    }

    @Override
    public SzhtxxElectronicInvoiceFptxxListItemVo queryOneByFpqqlsh(String fpqqlsh, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxService.queryOneByFpqqlsh(fpqqlsh, loginSysUser);
    }

    @Override
    public String exportExcelSzhtxxElectronicInvoiceFptxxList(SzhtxxElectronicInvoiceFptxxQueryCriteria queryCriteria, LoginSysUser loginSysUser) {


        log.info("\n异步操作 包裹 外层 exportExcelSzhtxxElectronicInvoiceFptxxList params: \n{}", JSONUtil.toJsonStr(queryCriteria));

        CompletableFuture.runAsync(() -> {

            try {

                // 三步走 mark by linjinyu at 20210305
                // 1. 数据
                // 2. 生成excel
                // 3. 通知

                List<SzhtxxElectronicInvoiceFptxxListItemVo> exportData = queryGenericParam(queryCriteria, loginSysUser);

                SysNotify sysNotify = szhtxxElectronicInvoiceFptxxExportExcelService.exportExcelSzhtxxElectronicInvoiceFptxxList(exportData);

                // 通知
                dubboSysNotifyService.notifyExportExcelAccountingSupplierNotYetDimGoods(sysNotify, loginSysUser);


            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportExcelSzhtxxElectronicInvoiceFptxxList {} -> 执行 异常:{}", JSONUtil.toJsonStr(queryCriteria), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";

    }

    @Override
    public SzhtxxElectronicInvoiceFptxxHeadDataVo headDataGenericParam(SzhtxxElectronicInvoiceFptxxQueryCriteria queryCriteria, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxService.headDataGenericParam(queryCriteria, loginSysUser);
    }

    @Override
    public SzhtxxElectronicInvoiceFptxxDetailVo detailGenericParam(SzhtxxElectronicInvoiceFptxxQueryCriteria queryCriteria, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxService.detailGenericParam(queryCriteria, loginSysUser);
    }

    /**
     * 导出发票数据
     *
     * @param queryCriteria
     * @param loginSysUser
     * @return
     */
    @Override
    public String exportExcelSzhtxxElectronicInvoiceDetail(SzhtxxElectronicInvoiceFptxxQueryCriteria queryCriteria, LoginSysUser loginSysUser) {

        log.info("\n异步操作 包裹 外层 exportExcelSzhtxxElectronicInvoiceDetail params: \n{}", JSONUtil.toJsonStr(queryCriteria));

        CompletableFuture.runAsync(() -> {

            try {

                // 三步走 mark by linjinyu at 20210305
                // 1. 数据
                // 2. 生成excel
                // 3. 通知

                SzhtxxElectronicInvoiceFptxxDetailVo exportData = detailGenericParam(queryCriteria, loginSysUser);

                SysNotify sysNotify = szhtxxElectronicInvoiceFptxxExportExcelService.exportExcelSzhtxxElectronicInvoiceFptxxDetail(exportData);

                // 通知
                dubboSysNotifyService.notifyExportExcelAccountingSupplierNotYetDimGoods(sysNotify, loginSysUser);


            } catch (Exception e2) {
                log.error("\n异步操作 包裹 内层 exportExcelSzhtxxElectronicInvoiceDetail {} -> 执行 异常:{}", JSONUtil.toJsonStr(queryCriteria), e2);
            }
        });

        return "导出任务已经接收,正在处理中...";
    }


    @Override
    public SingleBillingRequestBodyKpContent opRedWritePre(SzhtxxInvoiceFptxxOpSingleBillingForm form, SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo, LoginSysUser loginSysUser) {

        InvoiceApply invoiceApply = invoiceApplyCustomerService.findByInvoiceApplySn(form.getFpqqlsh(), loginSysUser);

        SingleBillingRequestBodyKpContent singleBillingRequestBodyKpContent = invoiceApplyCustomerFieldMakeInvoiceStatusOpOngoingSzhtxxElectronicInvoiceService.opOngoingInvoiceTypeEq1InvoiceModeEqSzhtxxElectronicInvoiceRedWrite(
                form,
                fptxxListItemVo,
                invoiceApply,
                loginSysUser
        );

        // 此处是废的
//        szhtxxElectronicInvoiceFptxxService.opRedWritePre(form, loginSysUser);

        return singleBillingRequestBodyKpContent;
    }


    @Override
    public SysNotify opRedWrite(
            SzhtxxInvoiceFptxxOpSingleBillingForm form,
            SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo,
            InvoiceApply invoiceApplyEntity,
            LoginSysUser loginSysUser) {

        SysNotify sysNotify = szhtxxElectronicInvoiceFptxxService.opRedWrite(form, fptxxListItemVo, invoiceApplyEntity, loginSysUser);

        return sysNotify;
    }

    /**************************************** 调用深圳航信电子票 start ****************************************/

    @Override
    public SysNotify callDeliverApi(SzhtxxElectronicInvoiceFptxxOpSendEmailPdfAttachmentForm form, SzhtxxElectronicInvoiceFptxxListItemVo fptxxListItemVo, LoginSysUser loginSysUser) {

        SysNotify sysNotify = szhtxxElectronicInvoiceFptxxService.callDeliverApi(form, fptxxListItemVo, loginSysUser);

        return sysNotify;
    }

    @Override
    public SysNotify callTaxInfoApi(String nsrsbh, LoginSysUser loginSysUser) {

        SysNotify sysNotify = szhtxxElectronicInvoiceFptxxService.callTaxInfoApi(nsrsbh, loginSysUser);

        return sysNotify;
    }

    /**************************************** 调用深圳航信电子票   end ****************************************/

}
