const system = require("../../../system");
const ServiceBase = require("../../sve.base");
const settings = require('../../../../config/settings')
class TrademarkcaseService extends ServiceBase {
  constructor() {
    super("rejreex", ServiceBase.getDaoName(TrademarkcaseService))
    this.agentDao = system.getObject("db.rejreex.agentDao")
    this.applicantDao = system.getObject('db.rejreex.applicantDao')
    this.restClient = system.getObject("util.restClient")
    this.tkurl = settings.qftkurl()
    this.qfurl = settings.qifuurl()
  }

  async createOrUpdate (params) {
    console.log(params)
    let isOrder = undefined
    if (params.orderId) {
      isOrder = await this.dao.findOne({ orderId: params.orderId })
    }
    if (isOrder) {
      params.id = isOrder.id
      // this.pushQf(params.id)
      return await this.dao.update(params)
    } else {
      params.recDate = new Date()
      if (params.id) {
        let statusArr = ["待完善材料", "待审核", "申请递交中"]
        let dt = await this.dao.findOne({ id: params.id })
        if (statusArr.indexOf(dt.tradeStatus) != -1) {
          params.rejStatus = '待提报'
        }
        if (params.askForData && params.askForData.rmLicense) {
          if (statusArr.indexOf(dt.tradeStatus) != -1) {
            params.caStatus = "待提报"
          }
        }
        if (params.usccode && params.applicant && params.applicant.applicantName) {
          let sql = ""
          sql = `UPDATE trademarkcase SET applicant=json_set(applicant, '$.applicantName', "${params.applicant.applicantName}"), ${params.tradeType ? `tradeType=${params.tradeType}` : ""}, ${params.usccode ? `usccode="${params.usccode}"` : ""}, ${params.agentName ? `agentName="${params.agentName}"` : ""}, ${params.askforId ? `askforId="${params.askforId}"` : ""} WHERE id = ${params.id};`
          // this.pushQf(params.id)
          return await this.dao.customExecAddOrPutSql(sql);
        } else {
          // this.pushQf(params.id)
          return await this.dao.update(params)
        }
      } else {
        if (params.askForData && params.askForData.rmLicense) {
          params.caStatus = "待提报"
        }
        if (!params.caseId) {
          params.caseId = `CS${new Date().getTime()}ae${Math.floor(Math.random() * 100)}`
        }
        // this.pushQf(params.orderId, 'od')
        return await this.dao.create(params)
      }
    }
  }

  async getCase (id) {
    let query = {
      where: {
        caseId: id
      }
    }
    let result = await this.dao.model.findOne(query)
    return result
  }

  async getCaseList (params) {
    params.pageInfo = params.pageInfo || { pageNo: 1, pageSize: 10 }
    return await this.dao.findAndCountAll(params)
    // return await this.dao.model.findAndCountAll(query)
  }

  async pushQf (id, tp) {
    let tmpdata
    if (tp == 'od') {
      tmpdata = await this.dao.findOne({ orderId: id })
    } else {
      tmpdata = await this.dao.findOne({ id })
    }
    let reqdata = this.orgdata(tmpdata)

    let req = {
      actionType: "getAppTokenByHosts",
      actionBody: {}
    }
    let tkurl = this.tkurl
    let tokenData = await this.restClient.execPost(req, tkurl)
    console.log(tokenData)
    tokenData = JSON.parse(tokenData.stdout)
    if (tokenData.status == 0) {
      let token = tokenData.data.token
      console.log(token)
      let qfurl = this.qfurl
      req.actionType = "receiveDeliveryData"
      req.actionBody = reqdata
      let res = await this.restClient.execPostWithToken(req, qfurl, token)
      console.log(res)
      return res
    } else {
      return "--"
    }
  }

  orgdata (data) {
    let datat = {}
    datat.orderNo = data.orderId
    let apply = {
      askforId: data.askforId,
      tradeImg: data.tradeImg,
      tradeType: data.tradeType,
      applicantName: data.v_applicantName,
      usccode: data.usccode,
      agentname: data.agentName
    }
    datat.apply = apply
    datat.applyUser = data.applicant
    datat.applyUser.usccode = data.usccode

    datat.trademarkRejInfo = data.trademarkRejInfo

    datat.askForData = data.askForData

    datat.deliveryStatusName = data.tradeStatus

    datat.supData = data.supData
    datat.recDate = data.recDate
    datat.askforDate = data.askforDate
    return datat
  }
}
module.exports = TrademarkcaseService;
