package com.dxhy.core.service.dataupload;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dxhy.common.service.impl.MpBaseServiceImpl;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.core.dao.TdxScanCustomsLogDao;
import com.dxhy.core.entity.TDxInvoiceImg;
import com.dxhy.core.entity.TDxScanCustomsLog;
import com.dxhy.core.model.openservice.OpenServiceOcr;
import com.dxhy.core.service.openservice.IOpenServicePlatformService;
import com.dxhy.core.service.singletable.IDxInvoiceImgService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author liudan
 * @date 2020/12/18
 */
@Service
public class TdxScanCustomsLogServiceImpl extends MpBaseServiceImpl<TdxScanCustomsLogDao, TDxScanCustomsLog>
    implements TdxScanCustomsLogService {

    @Resource
    private IOpenServicePlatformService openService;

    @Resource
    private IDxInvoiceImgService imgService;

    @Override
    public void getOcrData(String pch, String scanId, String img) {

        try {

            final String uuidCode = UUID.randomUUID().toString().replace("-", "");
            List<OpenServiceOcr> customsOcrResult = openService.getCustomsOcrResult(uuidCode, img);
            OpenServiceOcr.CustomsOcrData customsOcrData = customsOcrResult.get(0).getCustomsOcrData();

            TDxScanCustomsLog customsLog = new TDxScanCustomsLog();
            customsLog.setPch(pch);
            customsLog.setAmount(customsOcrData.getHJJE());
            customsLog.setCustomsCode(customsOcrData.getHM());
            customsLog.setFillInDate(DateUtils.strToDate(customsOcrData.getRQ()));
            customsLog.setScanId(scanId);
            customsLog.setCreateDate(new Date());
            customsLog.setImportPortCode(customsOcrData.getJKKADM());
            customsLog.setImportPortName(customsOcrData.getJKKAMC());

            this.baseMapper.insert(customsLog);

            TDxInvoiceImg invoiceImg = new TDxInvoiceImg();
            invoiceImg.setUuid(customsOcrData.getHM());
            invoiceImg.setOcrStatus("1");
            // 更新图片表识别标识
            UpdateWrapper<TDxInvoiceImg> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("scan_id", scanId);
            imgService.update(invoiceImg, updateWrapper);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
