var system = require("../../../system")
const http = require("http")
const querystring = require('querystring');
var settings = require("../../../../config/settings");
const CtlBase = require("../../ctl.base");
const logCtl = system.getObject("web.common.oplogCtl");
const md5 = require("MD5");
const uuidv4 = require('uuid/v4');
const crypto = require('crypto');
const axios = require("axios");

var cacheBaseComp = null;
class TcompanytaxitemCtl extends CtlBase {
  constructor() {
    super("tax", CtlBase.getServiceName(TcompanytaxitemCtl));
    this.redisClient = system.getObject("util.redisClient");
    this.tcompanytaxocrSve = system.getObject("service.tax.tcompanytaxocrSve");
  }

  async taxPage(pobj, pobj2, req, res) {
    let loginInfo = req.loginUser || {};
    let month = this.trim(pobj.month);
    if (month) {
      month = Number(month.replace("-", ""));
    }
    let condition = {
      currentPage: pobj.currentPage,
      pageSize: pobj.pageSize,
      month: month || 0,
      companyId: loginInfo.user.companyId,
      idName: pobj.idName,
      idNo: pobj.idNo,
    }

    try {
      var page = await this.service.pageByCondition(condition);
      return system.getResultSuccess(page);
    } catch (error) {
      console.log(error);
      return system.getResult(-1, `服务异常`);
    }
  }

  async taxUrl(pobj, pobj2, req, res) {
    let id = pobj.id;
    try {
      let target = await this.service.findById(id);
      if (!target) {
        return system.getResult(null, "税务信息不存在");
      }

      // 整理请求数据
      let itemList = await this.service.byImagepath(target.imagepath);

      let data = [];

      for (let item of itemList) {
        data.push({
          idName: item.idName,
          idNo: item.idNo,
          locations: JSON.parse(item.locations)
        });
      }

      let params = {
        target_id: target.idNo,
        image_path: target.imagepath,
        data: data,
      }

      // 请求ocr工具
      let res = await axios({
        method: 'post',
        url: "http://43.247.184.92:9002/gsb/ocr/mosaic",
        data: params
      });
      if(!res || !res.data || !res.data.mosaicurl) {
        return system.getResult(null, "获取完税图片失败");
      }
      return system.getResultSuccess({taxUrl: res.data.mosaicurl});
    } catch (error) {
      console.log(error);
      return system.getResult(-1, `服务异常`);
    }
  }
}
module.exports = TcompanytaxitemCtl;