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.oplogCtl");
class NeedCtl extends CtlBase{
   constructor(){
     super(CtlBase.getServiceName(NeedCtl));
     this.serviceitemSve=system.getObject("service.serviceitemSve");
   }
   async submitneed(pobj,obj,req){
     var cid = req.companyid;
     // if(obj.publisherName==""||obj.publisherName==null||obj.publisherName=="undefined"){
     //   return {code:-100,msg:"请填写您的姓名"};
     // }
     if(obj.publisherMobile==""||obj.publisherMobile==null||obj.publisherMobile=="undefined"){
       return {code:-100,msg:"请填写您的电话"};
     }
    //  if(obj.province==""||obj.province==null||obj.province=="undefined"){
    //    return {code:-100,msg:"请选择你所在的省市"};
    //  }
    //  if(obj.city==""||obj.city==null||obj.city=="undefined"){
    //    return {code:-100,msg:"请选择你所在的省市"};
    //  }
     if(obj.notes!=""&&obj.notes!=null&&obj.notes!="undefined"&&obj.notes.length>255){
       return {code:-100,msg:"备注字数超出限制，请重新填写"};
     }
     if(obj.servicecode==""||obj.servicecode==null||obj.servicecode=="undefined"){
       return {code:-110,msg:"serviceItem_code参数传递错误"};
     }
     if(obj.company_id=="" || obj.company_id==null || obj.company_id=="undefined"){
       return {code:-111,msg:"company_id参数传递错误"};
     }
     var serviceInfo=await this.serviceitemSve.findOneByCode(obj.servicecode,cid);
     console.log("---------submitneed-----------------serviceInfo-----------");
     console.log(serviceInfo);
     if(serviceInfo==""||serviceInfo==null){
       return {code:-110,msg:"servicecode参数传递错误"};
     }
     var pobj={
       publisherName:obj.publisherName,
       publisherMobile:obj.publisherMobile,
       servicecode:obj.servicecode,
       company_id:obj.company_id,
       publisherEnt:obj.entname,
       status:"1"
     };
     var result=await this.service.findchance(pobj);
     if(result!=""&&result!=null){
       return {code:"0",msg:"您已经发布需求，请不要重复提交"};
     }
     try{
       var reqParams={
         publisherName:obj.publisherName,
         publisherMobile:obj.publisherMobile,
         province:obj.provimce,
         city:obj.city,
         notes:obj.notes,
         servicecode:obj.servicecode,
         servicename:serviceInfo.name,
         itemType:serviceInfo.itemType,
         itemTypeName:serviceInfo.itemTypeName,
         status:"1",
         company_id:obj.company_id,
       };
       var need = await this.service.create(reqParams);
       return {code:"1",msg:"发布成功"};
     }catch(e){
       console.log(e);
       return {code:"-1",msg:"操作失败，请稍后重试"};
     }

   }

   //提交备注
   async submitRemarks(qobj,obj,req){
    var user = req.session.user;
    if(user){
      obj["user"]=user;
      try {
        return this.service.submitRemarks(obj);
      } catch (error) {
        return {code:-200,msg:"操作失败"};
      }
    }else{
      return {code:-100,msg:"用户数据为空"};
    }
   }

}
module.exports=NeedCtl ;
