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

import cn.hutool.core.util.StrUtil;
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.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxResult;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxResultParam;
import cn.superdesk.uniorder.dto.param.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxResultQueryCriteria;
import cn.superdesk.uniorder.dto.vo.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxListItemVo;
import cn.superdesk.uniorder.dubbo.facade.thirdpart.szhtxx.electronicInvoice.DubboSzhtxxElectronicInvoiceFptxxResultService;
import cn.superdesk.uniorder.service.thirdpart.szhtxx.electronicInvoice.SzhtxxElectronicInvoiceFptxxResultService;
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 DubboSzhtxxElectronicInvoiceFptxxResultServiceImpl implements DubboSzhtxxElectronicInvoiceFptxxResultService {

    @Autowired
    private SzhtxxElectronicInvoiceFptxxResultService szhtxxElectronicInvoiceFptxxResultService;


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

    @Override
    public int insertSelective(SzhtxxElectronicInvoiceFptxxResult entity) {

        return szhtxxElectronicInvoiceFptxxResultService.insertSelective(entity);

    }

    @Override
    public int updateByPrimaryKeySelective(SzhtxxElectronicInvoiceFptxxResult entity) {

        return szhtxxElectronicInvoiceFptxxResultService.updateByPrimaryKeySelective(entity);
    }

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

    @Override
    public SzhtxxElectronicInvoiceFptxxResult findById(Long id) {
        return szhtxxElectronicInvoiceFptxxResultService.findById(id);
    }

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

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

        return szhtxxElectronicInvoiceFptxxResultService.findAll(queryMap);
    }

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


    @Override
    public void addSzhtxxElectronicInvoiceFptxxResult(SzhtxxElectronicInvoiceFptxxResultParam param) {

        SzhtxxElectronicInvoiceFptxxResult szhtxxElectronicInvoiceFptxxResult = AnBeanUtils.copy(param, SzhtxxElectronicInvoiceFptxxResult.class);
        szhtxxElectronicInvoiceFptxxResultService.addSzhtxxElectronicInvoiceFptxxResult(szhtxxElectronicInvoiceFptxxResult);
    }

    @Override
    public void updateSzhtxxElectronicInvoiceFptxxResult(SzhtxxElectronicInvoiceFptxxResultParam param) {

        SzhtxxElectronicInvoiceFptxxResult szhtxxElectronicInvoiceFptxxResult = AnBeanUtils.copy(param, SzhtxxElectronicInvoiceFptxxResult.class);
        szhtxxElectronicInvoiceFptxxResultService.updateSzhtxxElectronicInvoiceFptxxResult(szhtxxElectronicInvoiceFptxxResult);
    }

    @Override
    public void deleteSzhtxxElectronicInvoiceFptxxResult(Long id) {
        szhtxxElectronicInvoiceFptxxResultService.deleteSzhtxxElectronicInvoiceFptxxResult(id);
    }

    @Override
    public SzhtxxElectronicInvoiceFptxxResult findSzhtxxElectronicInvoiceFptxxResultById(Long id) {
        return szhtxxElectronicInvoiceFptxxResultService.findSzhtxxElectronicInvoiceFptxxResultById(id);
    }

    @Override
    public SzhtxxElectronicInvoiceFptxxResult findByFpqqlsh(String fpqqlsh) {
        return szhtxxElectronicInvoiceFptxxResultService.findByFpqqlsh(fpqqlsh);
    }

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

    @Override
    public R<List<SzhtxxElectronicInvoiceFptxxResult>> pageQueryGenericParam(SzhtxxElectronicInvoiceFptxxResultQueryCriteria criteria) {
        return szhtxxElectronicInvoiceFptxxResultService.pageQueryGenericParam(criteria);
    }


    /****************************************** biz start ******************************************/

    @Override
    public int savedPdfUrl(String fpqqlsh, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxResultService.savedPdfUrl(fpqqlsh, loginSysUser);
    }

    @Override
    public int savedPdfUrl(SzhtxxElectronicInvoiceFptxxListItemVo fptxxResult, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxResultService.savedPdfUrl(fptxxResult, loginSysUser);
    }

    @Override
    public int callServiceSzhtxxElectronicInvoiceApiBatchQuery(List<SzhtxxElectronicInvoiceFptxxListItemVo> fptxxListItemVoList, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxResultService.callServiceSzhtxxElectronicInvoiceApiBatchQuery(fptxxListItemVoList, loginSysUser);
    }

    @Override
    public int entryFileNotFoundPdfSingleTime(SzhtxxElectronicInvoiceFptxxListItemVo listItemVo, LoginSysUser loginSysUser) {
        return szhtxxElectronicInvoiceFptxxResultService.entryFileNotFoundPdfSingleTime(listItemVo, loginSysUser);
    }

    @Override
    public int entryFileNotFoundPdf(List<SzhtxxElectronicInvoiceFptxxListItemVo> fptxxListItemVoList, LoginSysUser loginSysUser) {

        log.info("\nentryFileNotFoundPdf main start ... ");

        CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {

            StringBuffer report = new StringBuffer();

            report.append(StrUtil.format("\nasync thread start \nsize = {}", fptxxListItemVoList.size()));

            try {
                int ret = szhtxxElectronicInvoiceFptxxResultService.callServiceSzhtxxElectronicInvoiceApiBatchQuery(fptxxListItemVoList, loginSysUser);

                if (ret == 1) {
                    fptxxListItemVoList.forEach(item -> {

                        report.append(StrUtil.format("\n{}", JSONUtil.toJsonStr(item)));

                        szhtxxElectronicInvoiceFptxxResultService.savedPdfUrl(item, loginSysUser);
                    });
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            log.info("async thread end\n#report# entryFileNotFoundPdf 金税盘 尝试下载发票pdf {}", report);
        });


        log.info("\nentryFileNotFoundPdf main  end ... ");

        return 1;
    }
}
