<?php

namespace app\api\controller;

use app\common\model\StockConfig;
use app\common\model\ShopList;
use app\common\model\StockLog;
use think\exception\ValidateException;
use app\api\validate\Stock as StockValidate;
use app\common\model\StockDetail;
use think\facade\Db;

class Index extends Base
{

      protected $noNeedLogin = ['getAppVersion','getArr'];

      //首页数据分组统计品种
      public function index(StockLog $stockLog)
      {
            $data= $stockLog->where('user_id', $this->userinfo['id'])
                              ->field('type,count(id) as count')
                              ->group('type')
                              ->select();
            $this->success('ok', $data);
      }

      //库存统计
      public function statistics(StockConfig $stockConfig, StockDetail $stockDetail)
      {     
            $result = [];
            $list = $stockConfig->where('status', $stockConfig::StatusNormal)->field('id,type_id,title')
            ->orderRaw("field('variety_name','packing_box','material')")->select();
            foreach ($list as $item) {
                  switch ($item['type_id']) {
                        case $stockConfig::VarietyName:
                              $result['variety_name'][] = [
                                    'name' => $item->title,
                                    'num'  => $stockDetail->where('key', $item['id'])->value('num')
                              ];
                              break;
                        case $stockConfig::PackingBox:
                             $result['packing_box'][] = [
                              'name' => $item->title,
                              'num'  => $stockDetail->where('key', $item['id'])->value('num')
                             ];
                             break;
                        case $stockConfig::Material:
                             $result['material'][] = [
                              'name' => $item->title,
                              'num'  => $stockDetail->where('key', $item['id'])->value('num')
                             ];
                             break;
                  }
            }
            $this->success('ok', $result);
      }

      //app版本升级配置
      public function getAppVersion(){
            $this->success('ok', 
            ['version' => site_config('addonsd.app_version'), 
            'url' => site_config('addonsd.app_address'), 
            'content' => site_config('addonsd.app_describe')]);
      }

      /**
     * @return void 全部平台
      */
      public function getPlatform()
      {
            $list  = site_config('addonsd.platform_list');
            $this->success('提交成功', $list);
      }

      //获取店铺
      public function getShop(ShopList $shopList){
            $platform_id = $this->request->post('platform_id/d');
            if(empty($platform_id)){
                  $this->error('参数有误');
            }
            return $this->success('ok', $shopList->where('platform', $platform_id)->column('name', 'id'));   
      }

      //获取规格
      public function getSpec(ShopList $shopList){

            $shop_id = $this->request->post('shop_id/d');
            if( empty($shop_id)){
                  $this->error('参数有误');
            }
            $spec = $shopList->where('id', $shop_id)->value('type_spec');
            $shopList = $spec?json_decode($spec, true): [];
            return $this->success('ok', $shopList);  
      }

      //获取规格
      public function getArr(){

            $list  = site_config('addonsd.arr_1');
            $this->success('提交成功', $list);
      }

   
}