<?php

/**
 * Created by PhpStorm.
 * User: stanley-king
 * Date: 2017/4/2
 * Time: 下午6:31
 */
require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
require_once (BASE_ROOT_PATH . '/helper/goods_helper.php');
require_once (BASE_ROOT_PATH . '/helper/user_session/anotice.php');

class member_noticeControl extends mbMemberControl
{
    public function indexOp()
    {
        $mod_anotice = Model('arrival_notice');
        $notices = $mod_anotice->getArrivalNoticeList(array('member_id' => $_SESSION['member_id']));
        if(empty($notices))
        {
            return self::outsuccess(array('special_list' => null,
                'summary'  => null,
                'groupbuy' => null,
                'limitime' => null,
                'bundling' => null,
                'mobile_page' => mobile_page(0)));
        }
        else
        {
            $gids = [];
            foreach ($notices as $notice) {
                $gids[] = intval($notice['goods_id']);
            }
            $model_goods = Model('goods');
            $goods_list = $model_goods->getGoodsListByColorDistinct(array('goods_id' => array('in',$gids)),goods_helper::fieldstr);
            $page_count = $mod_anotice->gettotalpage();

            $helper = new goods_helper();
            $ret = $helper->summary($goods_list,$related_goods);

            $block = special_formater::format_goods($gids,"",$ret['sort_summary']);
            $blocks[] = $block;

            return self::outsuccess(array('special_list' => $blocks,
                'summary'  => $ret['summary'],
                'groupbuy' => $ret['groupbuy'],
                'limitime' => $ret['limitime'],
                'bundling' => $ret['bundling'],
                'mobile_page' => mobile_page($page_count)));
        }
    }

    public function addOp()
    {
        $goods_id = $_POST['goods_id'];
        if ($goods_id <= 0) {
            return self::outerr(errcode::ErrParamter,"请输入正确的参数.");
        }

        $goods_info = Model('goods')->getGoodsInfoByID($goods_id, 'goods_id,goods_name,goods_storage,goods_state');
        if (empty($goods_info)) {
            return self::outerr(errcode::ErrParamter,"该商品不存在.");
        }
        if($goods_info['goods_storage'] > 0 && $goods_info['goods_state'] == 1) {
            return self::outerr(errcode::ErrParamter,"该商品还有库存,不能添加通知提醒.");
        }


        $type = intval($goods_info['is_appoint']) == 1 ? 2 : 1;

        $mod_anotice = Model('arrival_notice');
        $where = array();
        $where['goods_id'] = $goods_info['goods_id'];
        $where['member_id'] = $_SESSION['member_id'];
        $where['an_type'] = $type;
        $notice_info = $mod_anotice->getArrivalNoticeInfo($where);

        if(empty($notice_info))
        {
            $insert = array();
            $insert['goods_id'] = $goods_info['goods_id'];
            $insert['goods_name'] = $goods_info['goods_name'];
            $insert['member_id'] = $_SESSION['member_id'];
            $insert['an_mobile'] = $_SESSION['member_mobile'];
            $insert['an_type'] = $type;
            $ret = $mod_anotice->addArrivalNotice($insert);
            if($ret == false) {
                Log::record("goods_common arrival_notice error.",Log::ERR);
            }
            $anotice  = new user_session\anotice();
            $anotice->add($goods_info['goods_id']);
        }

        return self::outsuccess(array('goods_id' => $goods_id));
    }
    public function delOp()
    {
        $goods_id = $_POST['goods_id'];
        if ($goods_id <= 0) {
            return self::outerr(errcode::ErrParamter,"请输入正确的参数.");
        }

        $mod_anotice = Model('arrival_notice');
        $ret = $mod_anotice->delArrivalNotice(array('goods_id' => $goods_id,'member_id' => $_SESSION['member_id']));
        $anotice  = new user_session\anotice();
        $anotice->del($goods_id);

        if($ret == false) {
            Log::record("{$_SESSION['member_id']} del arraval notice $goods_id = {$goods_id} faile",Log::ERR);
        }

        return self::outsuccess(array('goods_id' => $goods_id));
    }
}