const system=require("../../system");
const ServiceBase=require("../sve.base");
class IndividualincometaxService extends ServiceBase{
  constructor(){
     super(ServiceBase.getDaoName(IndividualincometaxService));
     //this.appDao=system.getObject("db.appDao");
     this.entiitinfoSve = system.getObject("service.entiitinfoSve");

     this.redisClient=system.getObject("util.redisClient");
     this.taskS=system.getObject("service.taskSve");
  }
  //下载数据
  async downloadData(obj){
     var sharecode=obj.sharecode;
     var email=obj.email;
     var taskdesc=sharecode+"_"+email;
     console.log("sharecode============"+sharecode);
     await this.redisClient.rpush("downloadtaxtasks",taskdesc);
     //this.taskS.makerpt(obj);
     return {code:1};
  }
  async getUserIitInfo(obj){
    var openid = obj.openid;
    var year =obj.year;
    var month=obj.month;
    var result={
       code: 1,
       message: "success",
       data: {}
    };
    if(openid==null||openid==""||openid=="undefined"){
      result.code=-101;
      result.message="openid参数有误";
      return result;
    }
    console.log(openid);
    var userInfo =await this.cacheManager["AppletUserinfoCache"].get(openid);
    console.log(userInfo);
    if(userInfo=="null"||userInfo==null||userInfo=="undefined"){
      result.code=-105;
      result.message="用户信息有误";
      return result;
    }
    userInfo=JSON.parse(userInfo);
    // if(year==null||year==""||year=="undefined"){
    //   result.code=-102;
    //   result.message="year参数有误";
    //   return result;
    // }
    // if(month==null||month==""||month=="undefined"){
    //   result.code=-103;
    //   result.message="month参数有误";
    //   return result;
    // }
    var whereobj={user_id:userInfo.id};
    if(year && month){
      var whereobj={user_id:userInfo.id,statisticalYear:year,statisticalMonth:month};
    }
    console.log(whereobj);
    var userIitInfo= await this.dao.model.findOne({where:whereobj});
    if(userIitInfo && userIitInfo.id){
      result.data=userIitInfo;
      return result;
    }else{
      result.code=-104;
      result.message="数据为空";
      return result;
    }
  }

  async saveOrUpdateIIT(obj){
    console.log("--------------start--------------------");
    var openid = obj.openid;
    var shareOnlyCode =obj.shareOnlyCode;
    var result={
       code: 1,
       message: "success",
       data: {}
    };
    if(openid==null||openid==""||openid=="undefined"){
      result.code=-101;
      result.message="openid参数有误";
      return result;
    }
    if(shareOnlyCode==null||shareOnlyCode==""||shareOnlyCode=="undefined"){
      result.code=-102;
      result.message="shareOnlyCode参数有误";
      return result;
    }
    var entIit = await this.entiitinfoSve.dao.model.findOne({where:{shareOnlyCode:shareOnlyCode}});
    console.log("################################3");
    console.log(entIit);
    if(entIit=="null"||entIit==null||entIit=="undefined"){
      result.code=-103;
      result.message="entIit有误";
      return result;
    }
    console.log(openid);
    var userInfo =await this.cacheManager["AppletUserinfoCache"].get(openid);
    console.log(userInfo);
    if(userInfo=="null"||userInfo==null||userInfo=="undefined"){
      result.code=-105;
      result.message="用户信息有误";
      return result;
    }
    userInfo=JSON.parse(userInfo);
    obj["app_id"]=userInfo.app_id;
    obj["user_id"]=userInfo.id;
    obj["onlyCode"]=userInfo.onlyCode;
    obj["userName"]=userInfo.userName;
    obj["nickName"]=userInfo.nickName;//entIit
    obj["statisticalTime"]=entIit.statisticalTime;
    obj["statisticalYear"]=entIit.statisticalYear;
    obj["statisticalMonth"]=entIit.statisticalMonth;
    obj["statisticalCompanyName"]=entIit.statisticalCompanyName;
    console.log(obj);
    // statisticalTime:DataTypes.STRING,//统计/测试日期
    // statisticalYear:DataTypes.STRING,//统计/测试年
    // statisticalMonth:DataTypes.STRING,//统计/测试月
    // statisticalCompanyName:DataTypes.STRING,//统计公司名称
    var whereObj={
      openid:openid,
      year:obj.statisticalYear,month:obj.statisticalMonth
    };
    console.log(whereObj);
    var res = await this.getUserIitInfo(whereObj);
    if(res.code==1){//update
      console.log(1);
      var iit = res.data;
      obj["id"]=iit.id;
      var r = await this.dao.update(obj);
      result.data = obj;
      return result
    }else{//create
      console.log(2);
      var d=await this.dao.create(obj);
      result.data = d;
      return result
    }
  }

  async getEntStatisticInfo(obj){
    var openid = obj.openid;
    var shareOnlyCode = obj.shareOnlyCode;
    var result={
       code: 1,
       message: "success",
       data: {},
       list:[]
    };
    if(openid==null||openid==""||openid=="undefined"){
      result.code=-101;
      result.message="openid参数有误";
      return result;
    }
    console.log(openid);
    if(shareOnlyCode==null||shareOnlyCode==""||shareOnlyCode=="undefined"){
      result.code=-102;
      result.message="shareOnlyCode参数有误";
      return result;
    }
    console.log(shareOnlyCode);
    var userInfo =await this.cacheManager["AppletUserinfoCache"].get(openid);
    console.log(userInfo);
    if(userInfo=="null"||userInfo==null||userInfo=="undefined"){
      result.code=-105;
      result.message="用户信息有误";
      return result;
    }
    userInfo=JSON.parse(userInfo);
    //attributes: { include: [[this.db.Sequelize.fn('COUNT', this.db.Sequelize.col('hats')), 'no_hats']] }
    //findAll({where: {user_id: userInfo.id},order: [['created_at', 'DESC']]});
    // var entIit = await this.dao.model.findOne({where:{shareOnlyCode:shareOnlyCode},attributes:[
    //   [this.db.Sequelize.fn('SUM', this.db.Sequelize.col('postTaxSalary')), 'sumPostTaxSalary'],
    //   [this.db.Sequelize.fn('SUM', this.db.Sequelize.col('preTaxSalary')), 'sumPreTaxSalary'],
    //   [this.db.Sequelize.fn('SUM', this.db.Sequelize.col('insuranceAndFund')), 'sumInsuranceAndFund'],
    //   [this.db.Sequelize.fn('SUM', this.db.Sequelize.col('specialDeduction')), 'sumSpecialDeduction'],
    //   [this.db.Sequelize.fn('SUM', this.db.Sequelize.col('personalIncomeTax')), 'sumPersonalIncomeTax'],
    //   [this.db.Sequelize.fn('count',this.db.Sequelize.col('id')), 'num']
    // ]});
    var list = await this.dao.model.findAll({where:{shareOnlyCode:shareOnlyCode}});
    console.log(list);
    var d={
      sumPostTaxSalary:0,sumPreTaxSalary:0,sumInsuranceAndFund:0,sumSpecialDeduction:0,sumPersonalIncomeTax:0,num:0
    };
    d.num=list.length;
    for(var i=0;i<list.length;i++){
      var dd = list[i];
      d.sumPostTaxSalary=d.sumPostTaxSalary+Number(dd.postTaxSalary);
      d.sumPreTaxSalary = d.sumPreTaxSalary+Number(dd.preTaxSalary);
      d.sumInsuranceAndFund = d.sumInsuranceAndFund+Number(dd.insuranceAndFund);
      d.sumSpecialDeduction = d.sumSpecialDeduction+Number(dd.specialDeduction);
      d.sumPersonalIncomeTax = d.sumPersonalIncomeTax+Number(dd.personalIncomeTax);
    }
    result.data=d;
    result.list=list;
    return result;
    // attributes: [
    //   [this.db.Sequelize.fn('SUM', this.db.Sequelize.col('column')), 'sumOfColumn']
    // ]
  }

}
module.exports=IndividualincometaxService;
//var x=new IndividualincometaxService();
// (async ()=>{
//   var m =await x.create({preTaxSalary:20000});
//   console.log(m.preTaxSalary);
//   var ms=await x.findAll();
//   console.log(ms[0].preTaxSalary);
// })()
// var x=new IndividualincometaxService();
// (async ()=>{
//   var m =await x.downloadData({"sharecode":"mmmm","email":"jiangyong@gongsibao.com"});
//   console.log(m);
// })()
