<?php
/**
 * Copy Right IJH.CC
 * Each engineer has a duty to keep the code elegant
 * $Id$
 */
if(!defined('__CORE_DIR')){
    exit("Access Denied");
}
class Ctl_Biz_Msg extends Ctl_Biz
{
    
    public function items($params)
    {
        //0:所有消息 1:订单消息, 2:评价消息,3:投诉消息,4:系统消息
        $limit = 10;
        $page = max((int)$params['page'], 1);
        $filter = array('shop_id'=>$this->shop_id);
        if(in_array($params['type'], array(1, 2, 3, 4))){
            $filter['type'] = $params['type']; 
        }
        if(in_array($params['is_read'], array(0, 1))){
            $filter['is_read'] = $params['is_read']; 
        }        
        if(!$items = K::M('member/message')->items($filter, null, $page, $limit, $count)){
            $items = array();
        }
        $this->msgbox->set_data('data', array('items'=>array_values($items), 'total_count'=>$count));        
    }

    public function read($params)
    {

        if($ids = K::M('verify/check')->ids($params['msg_id'])){
            $this->msgbox->add('未指消息ID', 211);
        }else if(!$items = K::M('shop/msg')->items_by_ids($ids)){
            $this->msgbox->add('未指要操作的消息', 212);
        }else{
            $mids = array();
            foreach($items as $k=>$v){
                if(($v['shop_id'] == $this->shop_id) && !$v['is_read']){
                    $mids[$v['msg_id']] = $v['msg_id'];
                } 
            }
            if($mids){
                K::M('shop/msg')->update($mids, array('is_read'=>1));
            }
            $this->msgbox->add('success');
        }
    }
    
}