var system=require("../../system")
const http=require("http")
const querystring = require('querystring');
var settings=require("../../../config/settings");
const AppletBase =require("../applet.base");
const logCtl=system.getObject("web.oplogCtl");
class cailiaoApplet extends AppletBase{
   constructor(){
     super();
     this.Mail=system.getObject("util.mailClient");
     this.filetomailService=system.getObject("service.filetomailSve");
     this.partnerinfoService=system.getObject("service.partnerinfoSve");
     this.userService=system.getObject("service.userSve");
     this.connectionbh=system.getObject("db.connection").getConhb();
   }
   async findAllcailiao(obj,req){
     var result = await this.filetomailService.findAllByType(obj);
     return result;
   }

   async findMycailiao(obj,req){
     try {
       var openid = obj.openid;
       if( openid ==null||openid ==""|| openid =="undefined" )
       {
         return {code:-200,message:'参数错误',data:null};
       }
       var userInfo =await this.cacheManager["AppletUserinfoCache"].get(openid);
       if(!userInfo){
         return {code:-200,message:'用户不存在',data:null};
       }
       userInfo=JSON.parse(userInfo);
       var sources=[];
       var result={
          code: 1,
          message: "success",
          data: sources
       };
       var sql='SELECT itm.`code` code, itm.momo tmname, sa.name filename, sa.path url';
       sql +=' FROM ig_trade_mark_case itmc JOIN ig_trade_mark itm JOIN sys_attachment sa ON itmc.id = itm.trade_mark_caseid AND itm.id = sa.foreign_key';
       sql +=" WHERE (sa.`name` LIKE '%RECVCOMMIT%' OR sa.`name` LIKE '%3%') and itmc.mobile='"+ userInfo.mobile +"'"
       var myfiles=await this.connectionbh.query(sql);
        if(myfiles){
          myfiles[0,0].forEach(a=>{
            var source={};
            if(a.filename.indexOf("RECVCOMMIT")>0){
              source.name="商标回执（商标名称："+　a.tmname + "  注册号：" + a.code + ")";
              source.filepath=a.url;
            }
            if(a.filename.indexOf("3")>0){
              source.name="商标受理通知书（商标名称："+　a.tmname + "  注册号：" + a.code + ")";
              source.filepath=a.url;
            }
            sources.push(source);
          })
        }
       return result;
     } catch (e) {
       //日志记录
       logCtl.error({
          optitle:"获取我的回执信息失败,openid="+req.openid,
          op:"wxapplet/impl/cailiaoApplet/findMycailiao",
          content:e.stack,
          clientIp:req.clientIp
          });
         return {
              code: -200,
              message: "error",
              data: null
           };
     }
   }
   async mail(obj,req){
     console.log("---------获取所选择的材料---------")
     console.log(obj)
     var va=JSON.parse(obj.value);
     va = va.v;
     console.log("----------------获取onlyCode-------------------")
     var openid = obj.openid;
     var userInfo =await this.cacheManager["AppletUserinfoCache"].get(openid);
     userInfo=JSON.parse(userInfo);
     console.log(userInfo)
     var putItem={email:obj.email}
     var sqlWheres={where:{onlyCode:userInfo.onlyCode}};
     await this.userService.dao.updateByWhere(putItem,sqlWheres);
     var to=obj.to;
     var title='材料发送';
     var text=null;
     var html='感谢您对创知厚德知产在线小程序的使用，请点击下列文件名字获取相应文件下载链接:';
     var cc=null;
     var bcc=null;
     var atts=[];
     for (var i=0;i<va.length;i++){
       var vai=va[i].split(',');
       var h='<a href="'+vai[1]+'">'+vai[0]+'</a>'
       html=html+"<br>"+h;
     }
     try{
       var result=await this.Mail.sendMsg(to,title,text,html,cc,bcc,atts)//发送成功后result的值:250 Data Ok: queued as freedom
       console.log(result);
       if(result.indexOf("Ok")>=0){
         return {code:1,msg:"发送成功"}
       }else{
         return {code:0,msg:"发送失败"}
       }
     }catch(e){
       console.log(e);
       return {code:-1,msg:"发送失败"}
     }

   }

}
module.exports=cailiaoApplet;
