const system=require("../../system");
const ServiceBase=require("../sve.base");
class EcompanycontractService extends ServiceBase {
  constructor(){
    super(ServiceBase.getDaoName(EcompanycontractService));

    this.epartnerDao = system.getObject("db.epartnerDao");
  }

  async assignContract(id, epartnerId) {
    var contract = await this.findById(id);
    if(!contract) {
      return "合同不存在";
    }

    if(contract.epartner_id) {
      return "合同已分配, 请确定";
    }

    var epartner = await this.epartnerDao.findById(epartnerId);
    if(!epartner) {
      return "合伙人不存在";
    }

    if(!epartner.user_id) {
      return "合伙人未审核通过";
    }

    contract.epartner_id = epartnerId;
    contract.user_id = epartner.user_id;

    await contract.save();

    var sql = [];
    sql.push("UPDATE");
    sql.push("c_ecompany");
    sql.push("SET");
    sql.push("epartner_id = " + epartnerId + ", owner_id = " + epartner.user_id + ", isBenefit = 1, partnerType = '" + epartner.partnerType + "'");
    sql.push("WHERE ecompanycontract_id = " + id);
    await this.customExecAddOrPutSql(sql.join(" "));
    return "";
  }
}
module.exports=EcompanycontractService;
