var System=require("../../system");
var settings=require("../../../config/settings");
const CtlBase = require("../ctl.base");
const uuidv4 = require('uuid/v4');
class CopyrightinfoCtl extends CtlBase{
  constructor(){
    super(CtlBase.getServiceName(CopyrightinfoCtl));
  }
  async initNewInstance(queryobj,req){
    return system.getResult2({},null);
  }
  async createOrEdit(queryobj,qobj,req){
    try {
      console.log(req.session.user);
      var copyRight=null;
      if(qobj.id){
        copyRight = await this.service.findById(qobj.id);
      }
      console.log(copyRight);
      if(copyRight){//修改
        console.log("edit++++++++++++++++++++++++++++++++++++");
        delete qobj.applierTypeName;
        delete qobj.copyrightTypeName;
        delete qobj.compositionTypeName;
        delete qobj.statusProgressName;
        var result = await this.service.update(qobj);
        return {code:1,data:qobj};
      }else{//新增
        console.log("new++++++++++++++++++++++++++++++++++++");
        if(req && req.session && req.session.app){
          qobj.app_id=req.session.app.id;
        }
        var obj={//compositionType itemCode itemName
          app_id:qobj.app_id,user_id:req.session.user.id,onlyCode:req.session.onlyCode,userName:req.session.userName,nickName:req.session.nickName,
          copyrightType:qobj.copyrightType,status:1,statusProgress:"AWAITCHECK",applierType:qobj.applierType,applyName:qobj.applyName,
          mobile:qobj.mobile,identityCardPic:qobj.identityCardPic,businessLicensePic:qobj.businessLicensePic,specimenPic:qobj.specimenPic,
          registrationDocument:qobj.registrationDocument,codeDocument:qobj.codeDocument,instructionBookDocument:qobj.instructionBookDocument,
          orderNum:"",customerContact:qobj.customerContact,customerMobile:qobj.customerMobile
        }
        if(qobj.copyrightType=="zpzz"){
          obj["compositionType"]=qobj.compositionType;
        }
        var rd=await this.service.create(obj);
        return {code:1,data:rd};
      }
    } catch (e) {
      return {code:-1}
    }
  }
  async hookOrder(qobj,obj){
    console.log(obj);
    var result=await this.service.hookOrder(obj);
    return System.getResult2(result,null);
  }
  async copy(obj,req){
    var user=req.session.user;
    var id=obj.id;
    var form=await this.service.findById(id);
    form=form.dataValues;
    form.id="";
    form.created_at=null;
    form.updated_at=null;
    if(req && req.session && req.session.app){
      form.app_id=req.session.app.id;
      form["user_id"]=req.session.user.id;
    }
    form.orderNum="";
    form.status="1";
    form.statusName="未关联订单";
    form.statusProgress="AWAITCHECK";
    form.statusProgressName="未提交";
    form["createAuthorizeStuff"]="";

    var rd=await this.service.create(form);
    return System.getResult2(rd,null);
  }
  async changeStatus(qobj,obj){
    try {
      var r = await this.service.changeStatus(obj);
      return r;
    } catch (e) {
      return {code:-1}
    }
  }
}
module.exports=CopyrightinfoCtl;
