<?php
namespace app\models\system;
use app\models\user\User;
use app\models\user\UserBill;
use app\models\user\UserLevel;
use crmeb\basic\BaseModel;
use crmeb\traits\ModelTrait;
use crmeb\services\{PHPExcelService};

class CardInfo extends BaseModel
{
    use ModelTrait;
    public function  getCreateTimeAttr($name)
    {
        return date("Y-m-d H:i:s",$name);
    }
    public function getUseTimeAttr($name)
    {
        return date("Y-m-d H:i:s",$name);
    }

    /**
     * 激活卡片
     * @param $card_number
     * @param $card_password
     * @param int $uid
     * @param int $type
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function verification($card_number,$card_password,$uid=0,$type=0,$store_id=0,$is_confirm=0)
    {
        $info = self::where('card_number',$card_number)->find();
        if(!$info) return self::setErrorInfo('卡券信息有误！',true);
        if($info['card_password']!=$card_password) return self::setErrorInfo('卡券密码错误！');
        if($is_confirm==0)
        {
            return true;
        }
        if($type == 0 && $store_id==0) return self::setErrorInfo('门店不能为空！',true);
        $data['use_uid'] = $uid;
        $data['use_time'] = time();
        $data['status'] = 1;
        self::where('card_number',$card_number)->update($data);
        switch (intval($info['type']))
        {
            case 0:
                SystemStoreMember::setstorecardno($uid,$store_id);
                $user = User::where('uid',$uid)->find();
                User::where('uid',$uid)->inc('consumer',$info['amount'])->update(['reg_store_id'=>$store_id]);
                UserLevel::setUserLevel($uid,1);
                UserBill::income('开通会员赠送消费券',$uid,'consumer','system_add_consumer',$info['amount'],$info['id'],bcadd($user['consumer'],$info['amount'],2),"开通会员赠送消费券".$info['amount']);
                if($user['spread_uid']>0)
                {
                    $sp_user = User::where('uid',$user['spread_uid'])->find();
                    $spread_consumer = sys_config('spread_consumer');
                    User::where('uid',$user['spread_uid'])->inc('consumer',$spread_consumer)->update();
                    UserBill::income('推荐会员赠送消费券',$user['spread_uid'],'consumer','spread_add_consumer',$spread_consumer,$info['id'],bcadd($sp_user['consumer'],$spread_consumer,2),"推荐会员赠送消费券".$spread_consumer);
                }
                break;
            case 1:
                $user = User::where('uid',$uid)->find();
                User::where('uid',$uid)->inc('now_money',$info['amount'])->update();
                UserBill::income('充值卡充值',$uid,'now_money','system_add',$info['amount'],$info['id'],bcadd($user['consumer'],$info['amount'],2),"充值卡充值".$info['amount']);
                break;
        }

        return true;
    }
    public static function lst($where)
    {
        $model = new self;
        if(isset($where['amount']) && $where['amount']>0)
        {
            $model = $model->where('amount','=',$where['amount']);
        }
        if(isset($where['type']) && $where['type']>-1)
        {
            $model = $model->where('type','=',$where['type']);
        }
        if(isset($where['status']) && $where['status']>-1)
        {
            $model = $model->where('status','=',$where['status']);
        }
        if (isset($where['card_id']) && $where['card_id'] >0) {
            $model = $model->where('card_id','=',$where['card_id']);
        }
        if (isset($where['data']) && $where['data'] != '') {
            list($startTime, $endTime) = explode(' - ', $where['data']);
            $model = $model->where('create_time', '>', strtotime($startTime));
            $model = $model->where('create_time', '<', strtotime($endTime) + 24 * 3600);
        }
        if(isset($where['key']) && $where['key']!='')
        {
            $model = $model->whereLike('card_number','%'.$where['key'].'%');
        }
        $model = $model->order("id desc");
        if(isset($where['excel']) && $where['excel']==1)
        {
            $export = [];
            foreach ($model->select() as $index => $item) {
                $export[] = [
                    $item['card_number']."\t",
                    $item['card_password'],
                    $item['amount']
                ];
            }
            PHPExcelService::setExcelHeader(['卡号', '密码', '面值'])
                ->setExcelTile('卡券导出', '卡券导出' . time(), ' 生成时间：' . date('Y-m-d H:i:s', time()))
                ->setExcelContent($export)
                ->ExcelSave();
        }
        $count = $model->value('count(id)')?:0;
        $data = $model->page($where['page'],$where['limit'])->select()->toarray();
        foreach ($data as &$v)
        {
            $v['title'] = Card::where('id',$v['card_id'])->value('title');
            $v['nickname'] = User::where('uid',$v['use_uid'])->value('nickname');
        }
        return compact('count','data');
    }

}
