<?php

namespace app\common\library;

use app\common\model\school\Area;
use app\common\model\school\classes\Cate;
use app\common\model\school\classes\ClassesLib;
use app\common\model\school\classes\ClassesSpec;
use app\common\model\school\classes\Evaluate;
use app\common\model\school\classes\Order;
use app\common\model\school\classes\order\OrderDetail;
use app\common\model\school\classes\order\ServiceOrder;
use app\common\model\school\classes\Type;
use app\common\model\school\classes\VirtualHead;
use app\common\model\school\classes\VirtualUser;
use think\Db;

/**
 *  Sql脚本类
 */
class Sqlscript
{

    public static function importClasses($page=1,$limit=1000){
       $kechengs  =  Db::table("kecheng")
      ->page($page,$limit)->select();

       //默认机构id
            $manystore_id = 1;
            $shop_id = 1;
            $user_id = 72;
            $teacher_id = 15;

       foreach ($kechengs as $k=>$v){
               $classes_cate_ids = Cate::where( "name",$v["gongyi"])->value("id");
               if(!$classes_cate_ids)$classes_cate_ids="";
$classes_label_ids = "";
$classes_type = Type::where( "name",$v["kecheng_type"])->value("id");
$add_type = "2";
$add_id = 0;
$title = $v["name"];
$headimage = $v["img"];
if(!$headimage)continue;
$images = Db::table("kecheng_img")->where("ke_id",$v["id"])->column("path");
$images = implode(",",$images);
$type = "in";
$classes_num = 1;
$address_type = "2";
// "address_city": "河南省/洛阳市",
//        "province": "河南省",
//        "city": "洛阳市",
//        "area": null,
//        "province_code": 410000,
//        "city_code": 410300,
//        "area_code": 0
   $address_city = "河南省/洛阳市"."/".$v["district"];
   $address = $v["district"];
   $address_detail = $v["shangkedidian"];
   $province  = 410000;
   $city = 410300;
   $district = Area::where( "name","like","%".$v["district"]."%")
       ->where("level",3)
       ->where("pid",$city)
       ->value("id");
   $longitude = $v["longitude"];
   $latitude = $v["latitude"];
   //html解码
   $content =  html_entity_decode($v["blurb"]);
   $notice = html_entity_decode($v["experience"]);
   $price = 0;
   $status = "1";
   $weigh = 0;
   $recommend = ''.($v["por"] ?:0);
   $hot = ''.($v["hot"] ?:0);
   $new = ''.($v["top"] ?:0);
   $auth_status = 1;
   $feel = "1";
   //如果不存在开始和结束时间
    //随机生成个至少过去一个月的开始和结束时间, 结束时间至少过去一个月
       if(!$v["start_time"]||!$v["end_time"]){
           $v["start_time"] = time()-rand(2592000,2592000*10);
           $v["end_time"] = rand($v["start_time"]+24536,$v["start_time"] + time()-$v["start_time"]);
       }




   //开始时间和结束时间如果是毫秒级时间戳，截取成秒级
     $v["start_time"] = substr($v["start_time"],0,10);
     $v["end_time"] = substr($v["end_time"],0,10);


     //如果是非过期课程则不添加
      if($v["end_time"] >= time()){
          continue;
      }

    $spec = [
        "classes_lib_id"=>0,
        "name"=>"单课时默认课时",
        "start_time"=>$v["start_time"],
        "end_time"=>$v["end_time"],
        "limit_num"=>100,
        "status"=>"1",
        "weigh"=>0
    ];
    $classes_lib = ClassesLib::where("title",$title)->find();
        if(!$classes_lib){
              $classes_lib = ClassesLib::create(compact("feel","auth_status","manystore_id", "shop_id", "user_id", "teacher_id",  "classes_cate_ids","classes_label_ids","classes_type","add_type","add_id","title","headimage","images","type","classes_num","address_type","address_city","address","address_detail","province","city","district","longitude","latitude","content","notice","price","status","weigh","recommend","hot","new"));
        $spec["classes_lib_id"] = $classes_lib->id;
        $spec = ClassesSpec::create($spec);
//        \app\common\model\school\classes\ClassesLib::add_virtual_init($classes_lib->id);
         \app\common\model\school\classes\ClassesLib::update_classes($classes_lib->id);
        }else{
            $classes_lib->save(compact("feel","auth_status","manystore_id", "shop_id", "user_id", "teacher_id",  "classes_cate_ids","classes_label_ids","classes_type","add_type","add_id","title","headimage","images","type","classes_num","address_type","address_city","address","address_detail","province","city","district","longitude","latitude","content","notice","price","status","weigh","recommend","hot","new"));
            $spec["classes_lib_id"] = $classes_lib->id;
            $ClassesSpec = ClassesSpec::where("classes_lib_id",$classes_lib->id)->find();
            if($ClassesSpec){
                 ClassesSpec::where("classes_lib_id",$classes_lib->id)->update($spec);
            }else{
                  ClassesSpec::create($spec);
            }


//             \app\common\model\school\classes\ClassesLib::add_virtual_init($classes_lib->id);
         \app\common\model\school\classes\ClassesLib::update_classes($classes_lib->id);
        }

       }
    }



    public static function deleteClasses($page=1,$limit=1000){
        $kechengs  =  Db::table("kecheng")
            ->page($page,$limit)->select();

        //默认机构id
        $manystore_id = 1;
        $shop_id = 1;
        $user_id = 72;
        $teacher_id = 15;
        foreach ($kechengs as $k=>$v){
            $classes_cate_ids = Cate::where( "name",$v["gongyi"])->value("id");
            if(!$classes_cate_ids)$classes_cate_ids="";
            $classes_label_ids = "";
            $classes_type = Type::where( "name",$v["kecheng_type"])->value("id");
            $add_type = "2";
            $add_id = 0;
            $title = $v["name"];
            $headimage = $v["img"];
            if(!$headimage)continue;
            $images = Db::table("kecheng_img")->where("ke_id",$v["id"])->column("path");
            $images = implode(",",$images);
            $type = "in";
            $classes_num = 1;
            $address_type = "2";
// "address_city": "河南省/洛阳市",
//        "province": "河南省",
//        "city": "洛阳市",
//        "area": null,
//        "province_code": 410000,
//        "city_code": 410300,
//        "area_code": 0
            $address_city = "河南省/洛阳市"."/".$v["district"];
            $address = $v["district"];
            $address_detail = $v["shangkedidian"];
            $province  = 410000;
            $city = 410300;
            $district = Area::where( "name","like","%".$v["district"]."%")
                ->where("level",3)
                ->where("pid",$city)
                ->value("id");
            $longitude = $v["longitude"];
            $latitude = $v["latitude"];
            //html解码
            $content =  html_entity_decode($v["blurb"]);
            $notice = html_entity_decode($v["experience"]);
            $price = 0;
            $status = "1";
            $weigh = 0;
            $recommend = ''.($v["por"] ?:0);
            $hot = ''.($v["hot"] ?:0);
            $new = ''.($v["top"] ?:0);
            $auth_status = 1;
            $feel = "1";
            //如果不存在开始和结束时间
            //随机生成个至少过去一个月的开始和结束时间, 结束时间至少过去一个月
            if(!$v["start_time"]||!$v["end_time"]){
                $v["start_time"] = time()-rand(2592000,2592000*10);
                $v["end_time"] = rand($v["start_time"]+24536,$v["start_time"] + time()-$v["start_time"]);
            }




            //开始时间和结束时间如果是毫秒级时间戳，截取成秒级
            $v["start_time"] = substr($v["start_time"],0,10);
            $v["end_time"] = substr($v["end_time"],0,10);


            $classes_lib = ClassesLib::where("title",$title)->find();
            if($classes_lib){
                ClassesLib::deleteAllByClassesId($classes_lib["id"]);
            }
        }
    }

    public static function importUser($page=1,$limit=1000){
        $userwxs  =  Db::table("userwx")
            ->page($page,$limit)->select();

        //默认机构id
        $manystore_id = 1;
        $shop_id = 1;
        $user_id = 72;
        $teacher_id = 15;
        foreach ($userwxs as $k=>$userwx){
            $nickname = $userwx["user"] ?: $userwx["name"];
            $mobile = $userwx["phone"];
              //创建用户
            $user = \app\common\model\User::where("mobile",$mobile)->find();
            //检测更新教练下单学员账号创建状态 2022/8/27 new
            if(!$user)$user = (new \app\common\model\User)->addUserByMobile($mobile,$nickname,false);
            //更新附加信息
            $user['nickname'] = $userwx["name"];
            $user['realname'] = $userwx["user"];
            if(!$userwx['sex'] || $userwx['sex'] == "男" || $userwx['sex'] == "1"){
                $user['gender'] =  1;
            }else{
                $user['gender'] =  0;
            }

            $user['avatar'] = $userwx['head_img'] ?: "/old/static/common/images/tou2.png";
            $user['birthday'] = $userwx['birthday'];
            $user['work'] = $userwx['career'];
            $user['createtime'] =  strtotime($userwx["uptime"]);
            $user['updatetime'] =  strtotime($userwx["uptime"]);
            $user->save();


        }


    }


}