<?php

namespace app\api\controller;

use think\facade\Lang;

/**
 * ============================================================================
 * 
 * ============================================================================
 * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司，并保留所有权利。
 * 网站地址: https://www.valimart.net/
 * ----------------------------------------------------------------------------
 * 
 * ============================================================================
 * 浏览历史控制器
 */
class Membergoodsbrowse extends MobileMember {

    public function initialize() {
        parent::initialize(); // TODO: Change the autogenerated stub
        Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/membergoodsbrowse.lang.php');
    }

    /**
     * 我的足迹
     */
    public function browse_list() {
        $goodsbrowse_model = model('goodsbrowse');
		$page = intval(input('param.page'));
		$per_page = intval(input('param.per_page'));
        //查询浏览记录
        $condition = array();
        $condition[] = array('member_id','=',$this->member_info['member_id']);
		$timearray = $goodsbrowse_model->getGoodsbrowseList($condition,'goodsbrowse_time',0,'goodsbrowse_time desc');
		foreach($timearray as $key => $val){
			$timearray[$key]['goodsbrowse_time'] = date('Y-m-d',$val['goodsbrowse_time']);
		}
		
		$timearray = array_unique(array_column($timearray,'goodsbrowse_time'));//去除日期数组里面的重复日期
		$count = count($timearray);//日期数组长度
		$page_total = ceil($count/$per_page);//总页数
		if(($count - ($per_page * $page)) > 0){
			$hasmore = true;
		}else{
			$hasmore = false;
		}
		
		$start = $per_page * ($page - 1);//数组起始位置
		$pagetimearray = array_slice($timearray,$start,$per_page);//获取页数日期数组
		$browsegoodslist = array();
		foreach($pagetimearray as $key => $val){
			$browsegoodslist[$key]['time'] =$val;
			$condition   = array();
			$condition[] = array('member_id' ,'=', $this->member_info['member_id']);
			$condition[] = array('goodsbrowse_time' ,'>=', strtotime($val));
			$condition[] = array('goodsbrowse_time' ,'<=', strtotime($val)+86399);
			$goodsidarray = $goodsbrowse_model->getGoodsbrowseList($condition,'goods_id');
			$goodsidarray = array_column($goodsidarray,'goods_id');
			$goodsidarray = implode(',',$goodsidarray);
			$goods_list = model('goods')->getGoodsList(array(array('goods_id','in', $goodsidarray)), 'goods_id, goods_name, goods_advword, goods_promotion_price, goods_promotion_type, goods_marketprice, goods_image, store_id, gc_id, gc_id_1, gc_id_2, gc_id_3');
			foreach($goods_list as $k => $v){
				$goods_list[$k]['goods_image_url'] = goods_cthumb($v['goods_image'], 480, $v['store_id']);
			}
			$browsegoodslist[$key]['goods_list'] =$goods_list;
		}
        $result = array_merge(array('goodsbrowse_list' => $browsegoodslist),array('hasmore' => $hasmore),array('page_total' => $page_total));
        ds_json_encode(10000, '', $result);
    }

    /**
     * 清空足迹
     */
    public function browse_clearall() {
        //清除缓存中浏览记录
        dcache($this->member_info['member_id'], 'goodsbrowse');
        model('goodsbrowse')->delGoodsbrowse(array('member_id' => $this->member_info['member_id']));
        ds_json_encode(10000, '', 1);
    }

}
