<?php

namespace app\lumaapi\model;

use think\Db;
use think\Exception;
use think\Model;

class MsgModel extends Model
{

    private $code;//编号
    private $user_id;//读取用户id
    private $user_info;//用户信息
    private $uid;//发送用id
    private $resmsg;//模板消息
    private $money;//金额
    private $paytype;//支付类型
    private $databaseData;//数据库数据

    /**
     * 添加提醒消息
     */
    public static function addMsg($code = 0, $user_id = 0, $money = 0, $msg = null,$uid=null,$data=[],$type = null)
    {
        $msgModel = new MsgModel();
        $msgModel->code = $code;//模板编码
        $msgModel->user_id = $user_id;//用户id
        $msgModel->money = $money;//金额
        $msgModel->resmsg = $msg;//提醒消息
        $msgModel->uid = $uid;
        $msgModel->paytype = $type;
        $msgModel->databaseData = $data;
        if (empty($msgModel->code)) return "缺失参数1";
        //截取最后一位
        $last = substr($msgModel->code, -1);
        if ($last == -1) {
            if (empty($msgModel->msg)) return "缺失参数2";
        } else {
            //获取用户信息
            $msgModel->user_info = MemberModel::getUserByUserid($msgModel->user_id);
            if (empty($msgModel->user_info)) return "错误参数3";
        }

        $result = $msgModel->readTemplate();
        return $result;
    }

    /**
     * 根据code获取模板信息
     * @return [type] [description]
     */
    public function readTemplate()
    {
        $data = [];
        $url = '';
        $title = '';
        $text = '';
        $data['type'] = 3;//默认为个人消息
        $data['ctime'] = time();
        $change = $this->money > 0 ? '增加' : '减少';
        $this->money = abs($this->money);
        switch ($this->code) {
            case 100001:
                $data['type'] = 1;
                $title = '系统通知';
                $text = $this->resmsg;
                break;
            case 200001:
                $data['type'] = 2;
                $title = '后台通知';
                $text = $this->resmsg;
                break;
            case 300001:
                $data['type'] = 3;$data['source']=0;
                $title = '会员通知';
                $text = $this->resmsg;
                break;
            case 300002:
                $title = '资金变化通知';
                $text = '您的资金' .$change . $this->money . '元';
                $data['type']=3; $data['source']=1;
                $data['mold']=$change=="增加"?1:2;
                break;
            case 300005:
                $title = '积分变化通知';
                $text = '您的积分' . $change . $this->money . '分';
                $data['type']=3; $data['source']=2;
                $data['mold']=$change=="增加"?1:2;
                break;
            case 400000:
                $title = '点赞变化通知';
                $text = $this->resmsg;
                $data['type']=3;$data['source']=3;
                $data['mold']=1;//点赞成功
                break;
            case 400001:
                $title = '点赞变化通知';
                $text = $this->resmsg;
                $data['type']=3;$data['source']=3;
                $data['mold']=2;//取消点赞
                break;
            case 400005:
                $title = '评论变化通知';//评论成功
                $text = $this->resmsg;
                $data['type']=3;$data['source']=4;
                $data['mold']=1;
                break;
            case 400006:
                $title = '评论变化通知';//删除评论
                $text = $this->resmsg;
                $data['type']=3;$data['source']=4;
                $data['mold']=2;
                break;
            case 500000:
                $title = '关注变化通知';//关注成功
                $text = $this->resmsg;
                $data['type']=3;$data['source']=5;
                $data['mold']=1;
                break;
            case 500001:
                $title = '关注变化通知';//取消关注
                $text = $this->resmsg;
                $data['type']=3;$data['source']=5;
                $data['mold']=2;
                break;
            default:
                # code...
                return false;
                break;
        }
        $content = [
            'url' => $url,
            'uid' => $this->uid,
            'title' => $title,
            'content' => $text,
            'money' => $this->user_info['money'],
            'score' => $this->user_info['score'],
            'mold'  =>isset($data['mold'])?$data['mold']:0,
        ];
        unset($data['mold']);
        $data['title'] = $title;
        $data['user_id'] = $this->user_id;
        $data['msg_text'] = json_encode($content);
        $data['db_msg'] = json_encode($this->databaseData);
        Db::name('msg')->insert($data);
        return true;
    }

    /**
     * 获取未读消息总数
     * @param $user_id
     * @return int|string
     * @throws Exception
     * @author: Sh4wnZec
     * @editTime: 2020/1/8 15:32
     */
    public static function getAdvicesCount($user_id,$source=['IN','0'],$type=['IN' ,'1,2,3'])
    {
        if(!$user_id){
            return 0;
        }
        $where = ['user_id' => $user_id , 'is_read' => 0 ,'source'=>$source, 'type' => $type];
        $res = Db::name('msg')->where($where)->count();
        //如果消息数大于99条，气泡只显示99（气泡装不下三位数）
        return $res>99 ? 99 : $res;
    }

    /**
     * 获取个人消息与商家消息
     * @param $where
     * @param $field
     * @param $page
     * @param $pagesize
     * Created by azq
     * User: Administrator
     * Date: 2020/11/18
     * Time: 9:47
     */
    public static function getAdvicesList($where , $field , $page){
        $list = self::getMsgInformSelect($where , $field , $page);
        foreach ($list as $k => $v) {
            //msg_text转换格式
            $list[$k]['msg_text'] = json_decode($v['msg_text'], 1);
            $list[$k]['db_msg'] = json_decode($v['db_msg'], 1);
            //如果is_read为O,更新为1
            if ($v['is_read'] == 0)
                $v['is_read'] = Db::name('msg')->where(['id'=>$v['id']])->update(['is_read' => 1]);
        }
        return $list;
    }



    /**
     * 根据条件获取列表
     * @param $where
     * @param $field
     * @param string $order
     * @param int $page
     * @param int $pagesize
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getMsgInformSelect($where = [], $field = '*', $page = 1, $order = 'ctime desc')
    {
        return Db::name('msg')->where($where)->field($field)->order($order)->fetchSql(false)->page($page)->select();
    }







}