<?php
namespace wstmart\app\model;
use think\Db;
use wstmart\common\model\Messages as CMessages;
/**
 * ============================================================================
 * 商城消息
 */
class Messages extends CMessages{
   /**
    * 获取列表
    */
    public function pageQuery(){
        $from = input('from/d');
        $userId = (int)session('WST_USER.userId');
        $where['receiveUserId'] = (int)$userId;
        $where['dataFlag'] = 1;
        if(isset($from)){
            $where['from'] = $from;
        }
        $page = $this->where($where)->order('msgStatus asc,id desc')->cache(true,30)->paginate(input('pagesize/d'))->toArray();
        // foreach ($page['Rows'] as $key => $v){
        // 	$page['Rows'][$key]['msgContent'] = WSTMSubstr(strip_tags(htmlspecialchars_decode($v['msgContent'])),0,140);
        // }
        foreach ($page['Rows'] as &$v){
            if($v['from'] == 1){
                $msgJson = json_decode($v['msgJson']);
                //dump($msgJson);die;
                $shopId = Db::name('orders')->where(['orderId'=>$msgJson->dataId])->cache(true,86400)->value('shopId');
                $shopImg = Db::name('shops')->where(['shopId'=>$shopId])->cache(true,86400)->value('shopImg');
                // Db::name('order o')
                //     ->join('__SHOPS s','o.shopId=s.shopId','inner join')
                //     ->field('shopId,shopImg')
                //     ->where(['o.orderId'=>$msgJson['dataId']])
                //     ->cache(true,86400)
                //     ->find();
                $v['img'] = WSTImg($shopImg,3);
            }else{
                $v['img'] = '';
            }
        }
        return $page;
    }
   /**
    *  获取某一条消息详情
    */
    public function getById(){
    	$userId = (int)session('WST_USER.userId');
        $id = (int)input('msgId');
        $data = $this->get(['id'=>$id,'receiveUserId'=>$userId]);
        if(!empty($data)){
          //$data['msgContent'] = htmlspecialchars_decode($data['msgContent']);
          if($data['msgStatus']==0)
            model('Messages')->where('id',$id)->setField('msgStatus',1);
        }
        return $data;
    }

    /**
     * 删除
     */
    public function del(){
    	$userId = (int)session('WST_USER.userId');
        $id = input('id/d');
        $data = [];
        $data['dataFlag'] = -1;
        $result = $this->update($data,['id'=>$id,'receiveUserId'=>$userId]);
        if(false !== $result){
            return WSTReturn("删除成功", 1);
        }else{
            return WSTReturn($this->getError(),-1);
        }
    }
    /**
    * 批量删除
    */
    public function batchDel(){
    	$userId = (int)session('WST_USER.userId');
        $ids = input('ids/a');
        $data = [];
        $data['dataFlag'] = -1;
        $result = $this->update($data,['id'=>['in',$ids],'receiveUserId'=>$userId]);
        if(false !== $result){
            return WSTReturn("删除成功", 1);
        }else{
            return WSTReturn($this->getError(),-1);
        }
    }
    /**
    * 标记为已读
    */
    public function batchRead(){
    	$userId = (int)session('WST_USER.userId');
        $ids = input('ids/a');
        $data = [];
        $data['msgStatus'] = 1;
        $result = $this->update($data,['id'=>['in',$ids],'receiveUserId'=>$userId]);
        if(false !== $result){
            return WSTReturn("操作成功", 1);
        }else{
            return WSTReturn($this->getError(),-1);
        }
    }

    
}