const system = require("../../../system");
const ServiceBase = require("../../sve.base")
const settings = require("../../../../config/settings")
class TcompanytaxitemService extends ServiceBase {
  constructor() {
    super("tax", ServiceBase.getDaoName(TcompanytaxitemService));

    this.tcompanytaxocrDao = system.getObject("db.tax.tcompanytaxocrDao");
  }

  async pageByCondition(condition) {
    condition.currentPage = Number(condition.currentPage || 1);
    condition.pageSize = Number(condition.pageSize || 10);
    condition.startRow = Number((condition.currentPage - 1) * condition.pageSize);

    let total = await this.dao.countByCondition(condition);
    if (total == 0) {
      return { count: 0, rows: [] };
    }

    let list = await this.dao.pageByCondition(condition);
    if (list) {
      for (var item of list) {
        this.handleDate(item, ['updated_at', 'created_at'], 'YYYY-MM-DD HH:mm:ss');
        this.handleDate(item, ['month_date'], 'YYYY-MM');
      }
    }
    return { count: total, rows: list };
  }


  async saveOcrItems(data) {
    let taxOcrId = Number(data.taxOcrId || 0);
    let itemList = data.itemList || [];
    if(!taxOcrId) {
      return system.getResult(null, "taxOcrId为空");
    }
    if(itemList.length == 0) {
      return system.getResult(null, "itemList为空");
    }

    let ocr = await this.tcompanytaxocrDao.findById(taxOcrId);
    if(!ocr) {
      return system.getResult(null, "taxOcrId错误");
    }

    let items = [];

    for(let obj of itemList) {
      if(!obj.idNo) {
        continue;
      }

      items.push({
        companyId: ocr.companyId,
        taxId: ocr.taxId,
        taxOcrId: taxOcrId,
        month: ocr.month,
        month_date: ocr.month_date,
        idName: obj.idName,
        idNo: obj.idNo,
        incomeTax: obj.incomeTax,
        actualAmt: obj.actualAmt,
        grant_time: obj.grant_time,
        param: obj.param,
      });
    }

    await this.dao.bulkCreate(items);

    ocr.isOCR = true;
    ocr.ocr_time = new Date();
    await ocr.save();
    return system.getResultSuccess();
  }

  async byImagepath(imagepath) {
    return this.dao.byImagepath(imagepath);
  }
}
module.exports = TcompanytaxitemService;
