<?php

namespace app\common\model\school\classes\activity;

use app\admin\model\manystore\Shop;
use app\common\model\school\classes\activity\order\Order;
use app\common\model\BaseModel;
use app\common\model\dyqc\ManystoreShop;
use app\common\model\school\Area;
use app\common\model\school\classes\ClassesSpec;
use app\common\model\school\classes\Collect;
use app\manystore\model\Manystore;
use think\Model;
use traits\model\SoftDelete;

class ActivityAuth extends BaseModel
{

    use SoftDelete;



    // 表名
    protected $name = 'school_classes_activity_auth';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'address_type_text',
        'start_time_text',
        'end_time_text',
        'sign_start_time_text',
        'sign_end_time_text',
        'status_text',
        'recommend_text',
        'hot_text',
        'new_text',
        'selfhot_text',
        'auth_status_text',
        'expirestatus_text',
        'add_type_text',
        'auth_time_text',
        'has_expire',
    ];

    public $no_auth_fields = ["status","weigh","recommend","hot","new","selfhot","feel","start_time","end_time","sign_start_time","sign_end_time","longitude","latitude","address_city","province","city","district","address","address_detail","address_type","title",'headimage','images','content',"price"];


    public function getHasExpireList()
    {
        return ['1' => __('Has_expire 1'), '2' => __('Has_expire 2')];
    }


    public function getHasExpireAttr($value, $data)
    {
        $end_time =  (isset($data['end_time']) ? $data['end_time'] : '');
        if(!$end_time) return '2';

        if( $end_time < time()) {
            return '1';
        }else{
            return '2';
        }

    }


    protected static function init()
    {
        self::afterInsert(function ($row) {
            if (!$row['weigh']) {
                $pk = $row->getPk();
//                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
                $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => 0]);
            }
        });
    }


    public function getAddressTypeList()
    {
        return ['1' => __('Address_type 1'), '2' => __('Address_type 2')];
    }

    public function getStatusList()
    {
        return ['1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3')];
    }

    public function getRecommendList()
    {
        return ['0' => __('Recommend 0'), '1' => __('Recommend 1')];
    }

    public function getHotList()
    {
        return ['0' => __('Hot 0'), '1' => __('Hot 1')];
    }

    public function getNewList()
    {
        return ['0' => __('New 0'), '1' => __('New 1')];
    }

    public function getSelfhotList()
    {
        return ['0' => __('Selfhot 0'), '1' => __('Selfhot 1')];
    }

    public function getAuthStatusList()
    {
        return ['0' => __('Auth_status 0'), '1' => __('Auth_status 1'), '2' => __('Auth_status 2')];
    }

    public function getExpirestatusList()
    {
        return ['1' => __('Expirestatus 1'), '2' => __('Expirestatus 2')];
    }

    public function getAddTypeList()
    {
        return ['1' => __('Add_type 1'), '2' => __('Add_type 2')];
    }


    public function getAddressTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['address_type']) ? $data['address_type'] : '');
        $list = $this->getAddressTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['start_time']) ? $data['start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['end_time']) ? $data['end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignStartTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_start_time']) ? $data['sign_start_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getSignEndTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['sign_end_time']) ? $data['sign_end_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getRecommendTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['recommend']) ? $data['recommend'] : '');
        $list = $this->getRecommendList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getHotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['hot']) ? $data['hot'] : '');
        $list = $this->getHotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getNewTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['new']) ? $data['new'] : '');
        $list = $this->getNewList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getSelfhotTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['selfhot']) ? $data['selfhot'] : '');
        $list = $this->getSelfhotList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAuthStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['auth_status']) ? $data['auth_status'] : '');
        $list = $this->getAuthStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getExpirestatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expirestatus']) ? $data['expirestatus'] : '');
        $list = $this->getExpirestatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAddTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['add_type']) ? $data['add_type'] : '');
        $list = $this->getAddTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getAuthTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['auth_time']) ? $data['auth_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSignStartTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setSignEndTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setAuthTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function activity()
    {
        return $this->belongsTo(Activity::class, 'classes_activity_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }




    public function manystore()
    {
        return $this->belongsTo(Manystore::class, 'manystore_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function shop()
    {
        return $this->belongsTo(ManystoreShop::class, 'shop_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function items()
    {
        return $this->hasMany(ActivityItem::class,'classes_activity_id', 'classes_activity_id')->order("weigh desc,id desc");
    }

    public function itemauths()
    {
        return $this->hasMany(ActivityItemAuth::class,'classes_activity_auth_id')->order("weigh desc,id desc");
    }



    public function activityCheck(&$params,$shop_id=null,$row=null,$oper_type='user',$oper_id=0)
    {

        if($row){
            //统一时间格式成时间戳
            if( is_string($row["start_time"]))$row["start_time"]=strtotime($row["start_time"]);
            if( is_string($row["end_time"]))$row["end_time"]=strtotime($row["end_time"]);
            if( is_string($row["sign_start_time"]))$row["sign_start_time"]=strtotime($row["sign_start_time"]);
            if( is_string($row["sign_end_time"]))$row["sign_end_time"]=strtotime($row["sign_end_time"]);


        }




        $params["status"] = $params["status"] ?? '3';
        if(!$shop_id)$shop_id = $params["shop_id"] ?? 0;
        if(empty($params["weigh"]))$params["weigh"] = 0;


        $manystore = Manystore::where("shop_id",$shop_id)->find();
        if(!$manystore){
            throw new \Exception("店铺不存在");
        }
        if(!(new \app\common\model\dyqc\ManystoreShop)->checkFull($shop_id))throw new \Exception("对方的认证信息未完善，请您先去帮忙完善！");


        $params["manystore_id"] = $manystore["id"];


//开始和结束时间不能为空
        $time = $params["time"];

        if(empty($time))throw new \Exception("{$params["title"]}请选择开始和结束时间".$time);
        $split_line = " - ";
        $time_arr = explode($split_line,$time);
        $params["start_time"] = $time_arr[0] ;
        $params["end_time"] = $time_arr[1];
        unset($params["time"]);

        $start_time = $params["start_time"];
        $end_time = $params["end_time"];

        if(empty($start_time) || empty($end_time)){
            throw new \Exception("{$params["title"]}请选择开始和结束时间".$time);
        }
        //转化时间戳
        $start_time = $params["start_time"] && !is_numeric($params["start_time"]) ? strtotime($params["start_time"]) : $params["start_time"];
        $end_time = $params["end_time"] && !is_numeric($params["end_time"]) ? strtotime($params["end_time"]) : $params["end_time"];
        //结束时间不能小于开始时间
        if($end_time<=$start_time){
            throw new \Exception("{$params["title"]}结束时间不能小于开始时间");
        }






        //开始和结束时间不能为空
        $time = $params["sign_time"];

        if(empty($time))throw new \Exception("{$params["title"]}请选择开始和结束时间".$time);
        $split_line = " - ";
        $time_arr = explode($split_line,$time);
        $params["sign_start_time"] = $time_arr[0] ;
        $params["sign_end_time"] = $time_arr[1];
        unset($params["sign_time"]);

        $start_time = $params["sign_start_time"];
        $end_time = $params["sign_end_time"];

        if(empty($start_time) || empty($end_time)){
            throw new \Exception("{$params["title"]}请选择开始和结束时间".$time);
        }
        //转化时间戳
        $start_time = $params["sign_start_time"] && !is_numeric($params["sign_start_time"]) ? strtotime($params["sign_start_time"]) : $params["sign_start_time"];
        $end_time = $params["sign_end_time"] && !is_numeric($params["sign_end_time"]) ? strtotime($params["sign_end_time"]) : $params["sign_end_time"];
        //结束时间不能小于开始时间
        if($end_time<=$start_time){
            throw new \Exception("{$params["title"]}结束时间不能小于开始时间");
        }





        //课时必须大于等于1
//        if($params["classes_num"] < 1) throw new \Exception("课时必须大于等于1");
        if($params["price"] < 0) throw new \Exception("售价必须大于0");


        //独立地点需传定位信息
        if($params["address_type"] == "2"){

            if(empty($params["address_city"])
                && !empty($params["province"])
                && !empty($params["city"])
                && !empty($params["district"])){

                $province_name = Area::where("id" ,$params['province'])->value("name");
                if(!$province_name) throw new \Exception("省份不存在");
                $city_name = Area::where("id" ,$params['city'])->value("name");
                if(!$city_name) throw new \Exception("市不存在");
                $district_name = Area::where("id" ,$params['district'])->value("name");
                if(!$district_name) throw new \Exception("区县不存在");
                $params['address_city'] = $province_name."/".$city_name."/".$district_name;
            }



            if(empty($params["address_city"])
                || empty($params["province"])
                || empty($params["city"])
                || empty($params["district"])
                || empty($params["longitude"])
                || empty($params["latitude"])
                || empty($params["address"])
                || empty($params["address_detail"])
            )  throw new \Exception("独立地点需传定位信息");

        }else{
            //地址取机构的
            $shop = ManystoreShop::where("id",$shop_id)->find();
            if(!$shop){
                throw new \Exception("店铺不存在");
            }
            if(empty($shop["address_city"])
                || empty($shop["province"])
                || empty($shop["city"])
                || empty($shop["district"])
                || empty($shop["longitude"])
                || empty($shop["latitude"])
                || empty($shop["address"])
                || empty($shop["address_detail"])
            )  throw new \Exception("当前机构地址并未完善，请在机构处完善！");



            $params["address_city"] = $shop["address_city"];
            $params["province"] = $shop["province"];
            $params["city"] = $shop["city"];
            $params["district"] = $shop["district"];
            $params["longitude"] = $shop["longitude"];
            $params["latitude"] = $shop["latitude"];
            $params["address"] = $shop["address"];
            $params["address_detail"] = $shop["address_detail"];
            //address
//           var_dump($params);
        }

        ;

        //收费免费判断
        if($params["price"]==0){
            $params["feel"] = "1";
        }else{

            //个人认证无法下付费课程
            $shop = ManystoreShop::where("id",$shop_id)->find();
            if($shop["type"]=="1"){
                throw new \Exception("这个机构属于个人认证，无法发布付费活动！");
            }

            $params["feel"] = "0";
        }


        $rule =  [
            'manystore_id'=>'require',
            'shop_id'=>'require',
            'headimage' => 'require',
            'title' => 'require',
            'images' => 'require',
            'address' => 'require',
            'address_detail' => 'require',
            'longitude' => 'require',
            'latitude' => 'require',
            'province' => 'require',
            'city' => 'require',
            'district' => 'require',
            'address_city' => 'require',
            'address_type' => 'require',
            'content' => 'require',
            'price' => 'require',

        ];


        $rule_msg = [
            "manystore_id.require"=>'机构id必填',
            "shop_id.require"=>'机构id必填',
            "headimage.require"=>'课程活动头图必填',
            'title.require' => '活动名称必须填写',
            'images.require' => '活动轮播图必须上传',
            'address.require' => '地址必填',
            'address_detail.require' => '详细地址必填',
            'longitude.require' => '经度必填',
            'latitude.require' => '纬度必填',
            'province.require' => '省编号必填',
            'city.require' => '市编号必填',
            'district.require' => '县区编号必填',
            'address_city.require' => '城市选择必填',
            'address_type.require'=> '地址类型必填',
            'content.require'=> '活动详情必填',
            'price.require'=> '活动售价必填',


        ];

        self::check($params,$rule,$rule_msg);


        //统一时间格式成时间戳
        if( is_string($params["start_time"]))$params["start_time"]=strtotime($params["start_time"]);
        if( is_string($params["end_time"]))$params["end_time"]=strtotime($params["end_time"]);
        if( is_string($params["sign_start_time"]))$params["sign_start_time"]=strtotime($params["sign_start_time"]);
        if( is_string($params["sign_end_time"]))$params["sign_end_time"]=strtotime($params["sign_end_time"]);



        //更新
        if($row){
//            $this->have_auth = false;
//            if($this->need_auth){
//                //判断更新的变动数据
//                $this->no_auth_fields_check($params,$row);
//                if($this->have_auth){
//                    $params['status'] = "3";
//                }
//            }

//            $this->updateCheck($row->id,$params,$row);


            //名称title不能与其他课程重复
            $check_title = self::where('id','<>',$row["id"])->where('classes_activity_id','<>',$row["classes_activity_id"])->where('title',$params["title"])->find();
            if($check_title){
                throw new \Exception("活动名称已存在或被其他机构占用，请更改！");
            }



            //如果存在课程规格，验证每个课时规格的合法性
            if(isset($params["item_json"])){
                //如果存在&quot;的HTML转义字符，先反转义再解析json
                $params["item_json"] = html_entity_decode($params["item_json"]);
                $spec = json_decode($params["item_json"],true);
                if(empty($spec))throw new \Exception("请至少添加一个课时规格");

                foreach ($spec as $k=>&$v){
                    unset($v["limit"]);
                    unset($v["status_name"]);
                    unset($v["visible"]);
                    $v["classes_activity_auth_id"] = $row->id;
                    $v["classes_activity_id"] = $row->classes_activity_id ?? ($params["classes_activity_id"] ?? 0);
                    $v["manystore_id"] = $params["manystore_id"];
                    $v["shop_id"] = $shop_id;
                    //先不进行判定，交给提交后再判定
                    $classesSpec = new ActivityItemAuth();
                    $classesSpec->specCheck($v,$shop_id,empty($v["id"])? null :  ActivityItemAuth::get($v["id"]),false,$oper_type,$oper_id);

                }
                $params["item_json"] = $spec;
            }

        }else{
            //新增

            //名称title不能重复
            $check_title = self::where('title',$params["title"])->find();
            if($check_title){
                throw new \Exception("活动名称已存在或被其他机构占用，请更改！");
            }


            //如果存在课程规格，验证每个课时规格的合法性
            if(isset($params["item_json"])){

                //如果存在&quot;的HTML转义字符，先反转义再解析json
                $params["item_json"] = html_entity_decode($params["item_json"]);
                $spec = json_decode($params["item_json"],true);
                if(empty($spec))throw new \Exception("请至少添加一个课时规格");

                foreach ($spec as $k=>&$v){
                    unset($v["limit"]);
                    unset($v["status_name"]);
                    unset($v["id"]);
                    $v["classes_activity_auth_id"] = 0;
                    $v["classes_activity_id"] = $params["classes_activity_id"] ?? 0;
                    $v["manystore_id"] = $params["manystore_id"];
                    $v["shop_id"] = $shop_id;
                    (new ActivityItemAuth)->specCheck($v,$shop_id,null,true,$oper_type,$oper_id);
                }
                $params["item_json"] = $spec;
            }

        }
        $params["delete_spec_ids"] = [];
        //按前端顺序分配显示权重
        if(isset($params["item_json"]) && $params["item_json"]){
            //数组倒过来
            $params["item_json"] = array_reverse($params["item_json"]);
            $insert_spec = [];
            foreach ($params["item_json"] as $k=>&$v){
                $v["weigh"] = $k + 1;
                if(!empty($v["id"]))$insert_spec[] = $v["id"];
            }
            if($insert_spec && $row){
                $params["delete_spec_ids"] = ActivityItemAuth::where("classes_activity_auth_id",$row->id)->where("id","not in",$insert_spec)->column("id");
            }
        }

        //存在需要删除的规格则判断规格是否能删除
        if($params["delete_spec_ids"] && $row){
            $check_spec = ActivityItemAuth::where("classes_activity_auth_id",$row->id)->where("id","in",$params["delete_spec_ids"])->select();
            foreach ($check_spec as $k=>$vv){
                (new ActivityItemAuth)->updateCheck($vv->id);
            }
            $params["delete_spec_ids"] = $check_spec;
        }


        //如果是上架，判断是否拥有课时规格，没有则无法上架
        if(empty($params["item_json"])){
            if(!$row)throw new \Exception("新添加的活动请先至少添加一个活动规格后再上架！");
            //判断是否拥有课时规格，没有则无法上架
            $check_spec = ActivityItemAuth::where("classes_activity_auth_id",$row->id)->count();
            if(!$check_spec)throw new \Exception("新添加的活动请先至少添加一个活动规格后再上架！");
        }

    }

    public function updateCheck($id,$params=[],$row=null){
        if($params && $row){
//            var_dump($this->no_auth_fields_check($params,$row));
            if(!$this->no_auth_fields_check($params,$row)){
                return true;
            }
        }

        // 课程存在售后订单则不允许操作
        $order = Order::where("classes_activity_item_id",$id)->where("status","not in","-3,6,9")->find();
        if($order)throw new \Exception("{$order['name']}存在正在使用中的订单报名学员，规格无法继续操作，如规格有误请下架！");
    }




    protected function update_check(&$params,$row=null)
    {

        //只要提交，就审核
        $params["auth_status"] = 0;
        $params["classes_activity_id"] = $params["classes_activity_id"] ?? 0;

        if($row){
            if(empty($params["shop_id"]))$params["shop_id"] = $row["shop_id"];
            //查询是否存在活动，如果存在，附上活动id
            $activity = \app\common\model\school\classes\activity\Activity::where("id",$row["classes_activity_id"])->find();
            if($activity && empty($params["classes_activity_id"])){
                $params["classes_activity_id"] = $activity["id"];
            }

        }

//        try {
            $classesLib =  new \app\common\model\school\classes\activity\ActivityAuth();
            $classesLib->no_auth_fields = $this->no_auth_fields;
            $classesLib->need_auth = $this->need_auth;
            $classesLib->have_auth = $this->have_auth;

            $classesLib->activityCheck($params,null,$row);

            $this->need_auth = $classesLib->need_auth;
            $this->have_auth = $classesLib->have_auth;

//        }catch (\Exception $e){
//            $this->error($e->getMessage());
//        }






        //修改
        if($row){

        }else{
            //新增
            //新增
            $params["add_type"] = '1';
            $params["add_id"] = $this->auth->id;
        }

    }



    /** 小程序端操作活动审核添加
     * @param bool $check
     * @param bool $trans
     * @return bool
     * @throws \Exception
     */
    public function createActivityAuthByOper(&$params,$check=false,$oper_type='user',$oper_id=0,$trans=false){


        $model = new self;


        //用户操作权限检测
        $shop_id = Activity::checkOptionAuth(0, $oper_id,$oper_type);
        $params["shop_id"] = $shop_id;


        //判断逻辑
        if($trans){
            self::beginTrans();
        }
        $res = true;
        try{
            $this->update_check($params,$row=null);
            $spec = $params["item_json"];
            unset($params["item_json"]);
//                    $params["status"] = "2";
            $result = $model->allowField(true)->save($params);
//                    $this->model = new \app\manystore\model\school\classes\activity\Activity;
//                    $this->itemmodel = new \app\manystore\model\school\classes\activity\ActivityItem();

            $activity = \app\manystore\model\school\classes\activity\Activity::where("title",$params["title"])->find();
            if(!$activity){
                $params["status"] = "3";
                $params["classes_activity_auth_id"] = $model["id"];
                $activity = new \app\manystore\model\school\classes\activity\Activity;
                $result2 = $activity->allowField(true)->save($params);
            }

            //添加课程规格
            foreach ($spec as $k=>$v){
                $v["classes_activity_auth_id"] = $model["id"];

                $v["classes_activity_id"] = $v["classes_activity_id"] ?? ($model->classes_activity_id ?? 0);

                $v["manystore_id"] = $model["manystore_id"];
                $v["shop_id"] = $model["shop_id"];

                unset($v["id"]);
                (new \app\common\model\school\classes\activity\ActivityItemAuth())->allowField(true)->save($v);
                if(isset($result2)){
                    (new \app\common\model\school\classes\activity\ActivityItem())->allowField(true)->save($v);
                }
            }

            //因为是批量添加，所有规格重新进行检测，防止出现时间重叠
            $specss = \app\common\model\school\classes\activity\ActivityItemAuth::where("classes_activity_auth_id",$model["id"])->select();
            foreach ($specss as $k=>$specs){
                $params =$specs->toArray();
                (new \app\common\model\school\classes\activity\ActivityItemAuth)->specCheck($params,null,$specs);
            }


            $this->update_classes($model["classes_activity_id"] ?? 0,$model);


            //调用事件
            $data = ['activity' => $model];
            \think\Hook::listen('activity_auth_need_after', $data);





            if($trans){
                self::commitTrans();
            }
        }catch (\Exception $e){
            if($trans){
                self::rollbackTrans();
            }
            throw new \Exception($e->getMessage());
        }
        return $model;
    }



    protected function update_classes($classes_activity_id,$row=null){
        if($classes_activity_id && $row){
            $activity = \app\common\model\school\classes\activity\Activity::where("id",$classes_activity_id)->find();
            if($activity && in_array($row["auth_status"],[0,2])){
                $activity["classes_activity_auth_id"] = $row["id"];
                $activity->save();
            }
        }
    }





    /** 小程序端操作课程修改
     * @param bool $check
     * @param bool $trans
     * @return bool
     * @throws \Exception
     */
    public function updateActivityAuthByOper($params,$ids,$check=false,$oper_type='user',$oper_id=0,$trans=false){

        $activityAuth = self::get($ids);
        if(!$activityAuth) throw new \Exception("活动不存在或已下架！");
//        $ids = $activityAuth["classes_activity_id"];
        //用户操作权限检测
        $shop_id = Activity::checkOptionAuth($activityAuth["classes_activity_id"], $oper_id,$oper_type);
        $params["shop_id"] = $shop_id;

        $row = self::where('id',$ids)->where("shop_id",$shop_id)->find();
        if(!$row) throw new \Exception("活动不存在！");





        //判断逻辑
        if($trans){
            self::beginTrans();
        }
        $res = true;
        try{
            //事务逻辑
            $this->update_check($params,$row);
            $spec = $params["item_json"] ?? [];

//            var_dump($spec);

            $delete_spec_ids =   $params["delete_spec_ids"] ?? [];
            unset($params["item_json"]);
            unset($params["delete_spec_ids"]);

            $result = $row->allowField(true)->save($params);

            //添加课程规格
            foreach ($spec as $k=>$v){
                $v["classes_activity_auth_id"] = $row["id"];
                $v["classes_activity_id"] = $v["classes_activity_id"] ?? ($row->classes_activity_id ?? 0);


                $v["manystore_id"] = $row["manystore_id"];
                $v["shop_id"] = $row["shop_id"];
                //有id更新，否则新增
                if(isset($v["id"]) && $v["id"]){
                    \app\common\model\school\classes\activity\ActivityItemAuth::update((new \app\common\model\school\classes\activity\ActivityItemAuth)->checkAssemblyParameters($v));
                }else{

                    \app\common\model\school\classes\activity\ActivityItemAuth::create((new \app\common\model\school\classes\activity\ActivityItemAuth)->checkAssemblyParameters($v));
                }

            }
            //删除规格
            foreach ($delete_spec_ids as $k=>$delete_spec){


                (new \app\common\model\school\classes\activity\ActivityItemAuth)->updateCheck($delete_spec["id"]);

                $delete_spec->delete();
            }

            //因为是批量添加，所有规格重新进行检测，防止出现时间重叠
            $specss = \app\common\model\school\classes\activity\ActivityItemAuth::where("classes_activity_auth_id",$row["id"])->select();
            foreach ($specss as $k=>$specs){

                $params =$specs->toArray();
                (new \app\common\model\school\classes\activity\ActivityItemAuth)->specCheck($params,null,$specs);
            }


            $this->update_classes($row["classes_activity_id"],$row);

            //调用事件
            $data = ['activity' => $row];
            \think\Hook::listen('activity_auth_need_after', $data);


            if($trans){
                self::commitTrans();
            }
        }catch (\Exception $e){
            if($trans){
                self::rollbackTrans();
            }
            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
        }
        return $row;
    }




    /** 小程序端操作课程修改
     * @param bool $check
     * @param bool $trans
     * @return bool
     * @throws \Exception
     */
    public function deleteActivityAuthByOper($ids,$check=false,$oper_type='user',$oper_id=0,$trans=false){

        $activityAuth = self::get($ids);
        if(!$activityAuth) throw new \Exception("活动不存在或已下架！");
        //用户操作权限检测
        $shop_id = Activity::checkOptionAuth($activityAuth["classes_activity_id"], $oper_id,$oper_type);

        $row = self::where('id',$ids)->where("shop_id",$shop_id)->find();
        if(!$row) throw new \Exception("活动不存在！");


        $list = self::where("id", 'in', $ids)->select();
        foreach ($list as $item) {
            $this->updateCheck($item->id);
        }
        //判断逻辑
        if($trans){
            self::beginTrans();
        }
        $res = true;
        try{
            $count = 0;
            //事务逻辑
            //删除前检测
            foreach ($list as $k => $v) {
                \app\common\model\school\classes\activity\ActivityItemAuth::where("classes_activity_auth_id",$item->id)->delete();
                $count += $v->delete();
            }
            if($trans){
                self::commitTrans();
            }
        }catch (\Exception $e){
            if($trans){
                self::rollbackTrans();
            }
            throw new \Exception($e->getMessage().$e->getFile().$e->getLine());
        }
        return $count;
    }








    /**得到基础条件
     * @param $status
     * @param null $model
     * @param string $alisa
     */
    public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false)
    {

        if (!$model) {
            $model = new static;
            if ($alisa&&!$with) $model = $model->alias($alisa);
        }
        if ($alisa) $alisa = $alisa . '.';
        $tableFields = (new static)->getTableFields();
        foreach ($tableFields as $fields)
        {
            if(in_array($fields, ['status','title','address_city','address','address_detail','start_time','end_time','sign_start_time','sign_end_time']))continue;
//            if (isset($whereData[$fields]) && $whereData[$fields]) $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);

            if (isset($whereData[$fields]) && $whereData[$fields]){
                if(is_array($whereData[$fields])){
                    $model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]);
                }else{
                    $model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
                }

            }


        }
        if (isset($whereData['status']) && $whereData['status']) $model = $model->where("{$alisa}status", 'in', $whereData['status']);
        if (isset($whereData['not_status']) && $whereData['not_status']) $model = $model->where("{$alisa}status", 'not in', $whereData['not_status']);


        if (isset($whereData['keywords'])&&$whereData['keywords']) $model = $model->where("{$alisa}title|{$alisa}id|{$alisa}address|{$alisa}address_detail", '=', $whereData['keywords']);
        if (isset($whereData['time'])&&$whereData['time']){
            $model = $model->time(["{$alisa}createtime",$whereData['time']]);
        }

        return $model;
    }





    /** 活动详情
     * @param $id
     * @throws \think\exception\DbException
     */
    public function detail($id,$user_id=0,$oper_type='user',$trans=false){
        $self = $this->get($id,['shop','itemauths']);

        //是否收藏
        $self['is_collect'] = 0;
        //是否购买
        $self['have_buy'] = 0;
        if($user_id){
            //判断是否收藏
            $self['is_collect'] = Collect::where("user_id",$user_id)->where("classes_activity_id",$self["classes_activity_id"])->count() ? 1:0 ;

            //判断用户是否已报名
            $self['have_buy'] = Order::where("classes_activity_id",$self["classes_activity_id"])
                ->where("user_id",$user_id)
                ->where("status","not in",["-3","6","9"])
                ->count() ? 1 : 0 ;
        }

        //
//        $this->setViews($id,$user_id,$oper_type,$user_id,$trans);
//        $self->getRelation('user')->visible(['nickname','realname','mobile','avatar']);
        //参与人数 = 虚拟人数 + 平台人数
        return $self;
    }




    /**活动规格
     * @param $id
     *
     */
    public function spec($id,$user_id=0){
        //课程规格
        $spec_data = ActivityItemAuth::where("classes_activity_auth_id",$id)->where("status",'1')->order("weigh desc,id desc")->select();
        foreach ($spec_data as &$spec){
            //默认未下单预约
            $spec["user_reservation"] = null;
            if($user_id){
                $spec["user_reservation"] = Order::where("classes_activity_item_id",$spec['classes_activity_item_id'])->where("user_id",$user_id)->where("status","not in",["-3","6","9"])->find();
            }

        }
        return $spec_data;
    }




    /**
     * 获取所有活动列表
     */
    public static function getVaildList($params) {
        extract($params);
        $a = (new self)->getWithAlisaName().'.';
        $with = ['items',"itemauths"];
//        if (isset($has_shop) && $has_shop) {
        $with[] = 'shop';
//        }



//        $cate_list = Cate::where("status",'1')->column("name","id");
//        $label_list = Label::where("status",'1')->column("name","id");
//        $type_list = Type::where("status",'1')->column("name","id");
        $selfetch = self::with($with);
        // 查询自提点
        if($status == "-2"){
            $selfetch = $selfetch->where($a.'status', '1');
        }

//        if($auth_status == "-2"){
//            $selfetch = $selfetch->where("{$a}auth_status",1);
//        }



        $order  = $order?? 'normal';
        $per_page = $limit ?? 10;


        $field = "{$a}start_time,{$a}end_time,{$a}sign_start_time,{$a}sign_end_time,{$a}id,{$a}shop_id,{$a}title,{$a}headimage,{$a}images,{$a}address_type,{$a}province,{$a}city,{$a}district,{$a}address,{$a}address_detail,{$a}longitude,{$a}latitude,{$a}sale,{$a}price,{$a}status,{$a}weigh,{$a}recommend,{$a}hot,{$a}new,{$a}selfhot,{$a}createtime,{$a}feel,{$a}sign_num,{$a}verification_num";

        //得到距离
        if (isset($latitude) && isset($longitude) && $latitude && $longitude) {
            $field .= ', '.getDistanceBuilder($latitude, $longitude,$a);
        }else{
            $field .= ', 0 as distance';
        }

        //得到每个


        $selfetch = $selfetch->field($field);
        if (isset($keywords) && $keywords) {
            $selfetch = $selfetch->where("{$a}title|{$a}address|{$a}address_detail|{$a}address_city", 'like', '%' . $keywords . '%');
        }


        if(isset($start_time) && isset($end_time) && ($end_time || $start_time)) {
            if($start_time){
                $selfetch = $selfetch->where("{$a}start_time", '>=', $start_time);
            }
            if($end_time){
                $selfetch = $selfetch->where("{$a}end_time", '<=', $end_time);
            }
        }


        if(isset($sign_start_time) && isset($sign_end_time) && ($sign_end_time || $sign_start_time)) {
            if($sign_start_time){
                $selfetch = $selfetch->where("{$a}sign_start_time", '>=', $sign_start_time);
            }
            if($sign_end_time){
                $selfetch = $selfetch->where("{$a}sign_end_time", '<=', $sign_end_time);
            }
        }



        // is_expire 是否查过期：1只查过期,2只查不过期,0全查
        if(isset($is_expire) && $is_expire) {
            if($is_expire == 1){
                $selfetch = $selfetch->where("{$a}end_time", '<', time());
            }else{
                $selfetch = $selfetch->where("{$a}end_time", '>=', time());
            }

        }

        // is_expire 是否查过期：1只查过期,2只查不过期,0全查
        if(isset($is_sign_expire) && $is_sign_expire) {
            if($is_sign_expire == 1){
                $selfetch = $selfetch->where("{$a}sign_end_time", '<', time());
            }else{
                $selfetch = $selfetch->where("{$a}sign_end_time", '>=', time());
            }

        }




        if (isset($manystore_id) && $manystore_id) {
            $selfetch = $selfetch->where("{$a}manystore_id", 'in', ''.$manystore_id);
        }

        if (isset($shop_id) && $shop_id) {
            $selfetch = $selfetch->where("{$a}shop_id", 'in', ''.$shop_id);
        }

        if (isset($user_id) && $user_id) {
            $selfetch = $selfetch->where("{$a}user_id", 'in', ''.$user_id);
        }

        if (isset($my) && $my && isset($my_user_id) && $my_user_id) {
            $selfetch = $selfetch->where("{$a}user_id", 'in', ''.$my_user_id);
        }



//        if (isset($teacher_id) && $teacher_id) {
//            $selfetch = $selfetch->where("{$a}teacher_id", 'in', ''.$teacher_id);
//        }

//        if (isset($type) && $type) {
//            $selfetch = $selfetch->where("{$a}type", 'in', ''.$type);
//        }

        if (isset($address_type) && $address_type) {
            $selfetch = $selfetch->where("{$a}address_type", 'in', ''.$address_type);
        }


        if (isset($recommend) && $recommend) {
            $selfetch = $selfetch->where("{$a}recommend", 'in', ''.$recommend);
        }


        if (isset($hot) && $hot) {
            $selfetch = $selfetch->where("{$a}hot", 'in', ''.$hot);
        }


        if (isset($new) && $new) {
            $selfetch = $selfetch->where("{$a}new", 'in', ''.$new);
        }

        if (isset($feel) && $feel) {
            $selfetch = $selfetch->where("{$a}feel", 'in', ''.$feel);
        }

//        //设置订单信息
//        if(isset($auth_status) && $auth_status != -1 && $auth_status != -2){
//            $selfetch = $selfetch->where("{$a}auth_status", 'in', ''.$auth_status);
//        }

        //设置订单信息
        if(isset($status) && $status != -1 && $status != -2){
            $selfetch = $selfetch->where("{$a}status", 'in', ''.$status);
        }



        //区域搜索
        if (isset($province) && $province) {
            $selfetch = $selfetch->where("{$a}province", 'in', ''.$province);
        }


        if (isset($city) && $city) {
            $selfetch = $selfetch->where("{$a}city", 'in', ''.$city);
        }

        if (isset($district) && $district) {
            $selfetch = $selfetch->where("{$a}district", 'in', ''.$district);
        }










//        if (isset($self_label_tag) && $self_label_tag) {
//            $self_label_tag =  implode("|",explode(',',$self_label_tag));
//            $selfetch = $selfetch->whereRaw(" {$a}self_label_tag REGEXP '({$self_label_tag})'");
//        }
//
//        if (isset($classes_type) && $classes_type) {
//            $classes_type =  implode("|",explode(',',$classes_type));
//            $selfetch = $selfetch->whereRaw(" {$a}classes_type REGEXP '({$classes_type})'");
//        }




//        if (isset($classes_cate_ids) && $classes_cate_ids) {
//            $classes_cate_ids =  implode("|",explode(',',$classes_cate_ids));
//            $selfetch = $selfetch->whereRaw(" {$a}classes_cate_ids REGEXP '({$classes_cate_ids})'");
//        }
//
//        if (isset($classes_label_ids) && $classes_label_ids) {
//            $classes_label_ids =  implode("|",explode(',',$classes_label_ids));
//            $selfetch = $selfetch->whereRaw(" {$a}classes_label_ids REGEXP '({$classes_label_ids})'");
//        }
//        $collect_classes_lib_ids = [-1];
//        //需登录查询条件：
//        if(isset($my_user_id) && $my_user_id){
//            //得到我收藏的课程ids
//            $collect_classes_lib_ids = Collect::where("user_id",$my_user_id)->column("classes_activity_id");
//            //专查我的收藏
//            if(isset($collect) && $collect){
//                $selfetch = $selfetch->where("{$a}id","in",$collect_classes_lib_ids);
//            }
//
//        }

        if(isset($order) && $order == "distance"){
            $selfetch = $selfetch->where("{$a}longitude","not null")
                ->where("{$a}latitude","not null")
                ->where("{$a}longitude","<>","")
                ->where("{$a}latitude","<>","")
                ->where("{$a}longitude","<>","null")
                ->where("{$a}latitude","<>","null");
        }





        //排序
        switch ($order) {
            case "normal": //综合排序（推薦優先）
                $selfetch = $selfetch->order("{$a}recommend desc,{$a}hot desc,{$a}new desc,{$a}weigh desc,{$a}start_time desc,{$a}id desc");
                break;
            case "distance": //距离优先 权重
                $selfetch = $selfetch->order("distance asc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
                break;
            case "hot": //熱門优先
                $selfetch = $selfetch->order("{$a}hot desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
                break;

            case "new": //平台最新优先
                $selfetch = $selfetch->order("{$a}new desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
                break;
            case "selfhot": //机构热门优先
                $selfetch = $selfetch->order("{$a}selfhot desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
                break;

            case "recommend": //推薦优先
                $selfetch = $selfetch->order("{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc,{$a}sign_num desc");
                break;

            case "sale": //銷量优先
                $selfetch = $selfetch->order("{$a}sale desc,{$a}verification_num desc,{$a}sign_num desc,{$a}recommend desc,{$a}weigh desc");
                break;

            case "views": //浏览量优先
                $selfetch = $selfetch->order("{$a}views desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc");
                break;
            case "collect": //收藏量优先
                $selfetch = $selfetch->order("{$a}collect desc,{$a}recommend desc,{$a}weigh desc,{$a}verification_num desc,{$a}sale desc");
                break;
            default:
                throw new \Exception("不支持的排序类型");
        }

        if(isset($nearby) &&  $nearby) {
//            throw new \Exception("现版本不支持");
            $selfetch = $selfetch->having("distance <= {$nearby}");
        }




        $selfetch = $selfetch->page($page,$limit)->select();


        foreach ($selfetch as $row) {

//            $row->getRelation('manystore')->visible(['nickname']);
            $row->getRelation('shop')->visible(['name','logo']);
        }
        $rows = $selfetch;
//        $total = $selfetch->total();



        //额外附加数据
        foreach ($rows as &$row) { //迭代器魔术方法遍历，填值自动引用传值
            //设置是否已收藏
            $row->is_collect = in_array($row->id,$collect_classes_lib_ids) ? 1 : 0;

            if($row->is_collect){
                $row["collect"] = Collect::where("user_id",$my_user_id)->where("classes_activity_id",$row->classes_activity_id)->find();
            }
//            $classes_label_ids = $row['classes_label_ids'];
//            $classes_cate_ids = $row['classes_cate_ids'];
//            $classes_type_ids = $row['classes_type'];
//            $classes_label = $classes_cate =  $classes_type = [];
//            foreach (explode(",", $classes_label_ids) as $classes_label_id){
//                if(isset($label_list[$classes_label_id]))$classes_label[] = $label_list[$classes_label_id];
//            }
//            foreach (explode(",", $classes_cate_ids) as $classes_cate_id){
//                if(isset($cate_list[$classes_cate_id]))$classes_cate[] = $cate_list[$classes_cate_id];
//            }
//
//            foreach (explode(",", $classes_type_ids) as $classes_type_id){
//                if(isset($type_list[$classes_type_id]))$classes_type[] = $type_list[$classes_type_id];
//            }
//
//
//            $row['classes_label'] = $classes_label;
//            $row['classes_cate'] = $classes_cate;
//            $row['classes_type_name'] = $classes_type;

        }
        $result = array("data" => $rows);
        return $result;
    }






}
