<?php
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-08-31 20:43
// +----------------------------------------------------------------------

namespace app\system\controller\v1;


use app\BaseController;
use app\model\system\ExpCost;
use app\model\system\Express;
use app\model\system\MemberLevel;
use app\model\system\Warehouse as WarehouseModel;
use app\model\system\SystemRole;
use app\Request;
use library\services\UtilService;
use library\utils\Dtree;
use library\utils\MakeSite;

class Warehouse extends  BaseController
{

    /**
     * 获取管理列表
     * @param Request $request
     */
    public function list(Request $request) {
        $pageSize = 50;
        [$page,$name] =  UtilService::getMore(
            [
                ['page',1],
                ['name','']
            ],$request,true
        );
        $where = [];
        if(!empty($name)) {
            $where['name'] = ["%{$name}%",'whereLike'];
        }
        $platform = (new \app\model\system\Platform());
        list($pageCount,$data) = (new WarehouseModel)->getList($page, $where,$pageSize,'id desc');
        foreach ($data as $k => $v) {
            $idsAr = explode(',',$v['platform_ids']);
            $platformAr = [];
            $data[$k]['platform']  = array_map(
                function ($item) use($platform){
                return  $platform->getPlatformId($item,'*');
            },$idsAr);
        }

        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $page
        ]);
    }


    /**
     * 保存数据
     * @param Request $request
     */
    public function  save(Request $request) {
        $post = UtilService::getMore(
            [
                ['id','0'],
                ['name','','empty','请输入仓库名称'],
                ['lx_name',''],
                ['lx_tel',''],
                ['address',''],
                ['exp_time',''],
                ['exp_star',''],
                ['tran_star',''],
                ['beijing',''],
                ['shanghai',''],
                ['qinghai',''],
                ['status','0'],
                ['seq','0'],
                ['platform_ids',''],
                ['password',''],
                ['username',''],
                ['status_js',0]
            ],
            $request
        );
        if(empty($post['password'])) {
            unset($post['password']);
        } else {
            $post['password'] = md5($post['password']);
        }
        $bool = (new WarehouseModel)->saveModel($post);
        if($bool)
            return app('json')->success("操作成功", []);
        else
            return app('json')->fail(WarehouseModel::getErrorInfo());
    }

    /**
     * 获取详情数据
     * @param Request $request
     * @return mixed
     */
    public function info(Request $request) {
        [$id] = UtilService::getMore([
            ['id','','empty','参数错误']
        ],$request,true);
        $info = WarehouseModel::get(compact('id'))->toArray();
        $info['password'] = '';
        return app('json')->success($info);
    }

    /**
     * 删除数据
     * @param Request $request
     */
    public function del(Request $request) {
        [$id] = UtilService::getMore(
            [
                ['id','0'],
            ],
            $request,true
        );
        WarehouseModel::del($id);
        ExpCost::where("warehouse_id",$id)->delete();
        return app('json')->success("删除成功", []);
    }

    /**
     * 快递配置
     * @param Request $request
     */
    public function exp(Request $request) {
        $pageSize = 20;
       [$id,$page] = UtilService::getMore(
            [
                ['id','','empty','参数错误'],
                ['page','1']
            ]
        ,$request,true);
        $warehouse = (new \app\model\system\Warehouse)->where('id',$id)->find();
        if(empty($warehouse)) {
            return app('json')->fail('数据失败');
        }
        $where  = [];
        $expCode = new ExpCost;
        list($pageCount,$data) = (new Express)
            ->getList($page, $where,$pageSize,'id desc');
        foreach ($data as $k=>$v) {
            $expData = [];
            $bool = false;
            $d = $expCode
                            ->field('id,money,status,level_id')
                            ->where('warehouse_id',$id)
                            ->where('express_id',$v['id'])
                            ->where('sassid',0)
                            ->where('level_id',-1)
                            ->find();
            if(empty($d)) {
                $expData[] = ['money'=>0,'id'=>0,'status'=>0,'level_id'=>-1];
            } else {
                $d['name'] = '快递低价';
                $expData[] = $d;
                if($d['status']) $bool = true;
            }
            $data[$k]['status'] = $bool;
            $data[$k]['expData'] = $expData;
            $data[$k]['warehouse_name'] = $warehouse['name'];
        }
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $page
        ]);
    }



    /**
     * 快递配置[全部数据]
     * @param Request $request
     */
    public function exp2(Request $request) {
        $pageSize = 20;
        [$sassid,$page] = UtilService::getMore(
            [
                ['sassid',''],
                ['page','1']
            ]
            ,$request,true);
        $where = [];
        if(!empty($sassid)) {
            $where['sassid'] = $sassid;
        }
        $where['level_id'] = -1;
        $level = (new MemberLevel)->select()->toArray();
        $expCost = new ExpCost;
        list($pageCount,$data) = $expCost->getList2($page, $where,$pageSize,'id desc');
        foreach ($data as $k=>$v) {
            $expData = [];
            $d = $expCost
                ->field('id,money,level_id,status')
                ->where('warehouse_id',$v['warehouse_id'])
                ->where('express_id',$v['express_id'])
                ->where('sassid',$v['sassid'])
                ->select();
            $bool = false;
            foreach ($level as $v) {
                $expData[$v['id']] = ['money'=>0,'id'=>0,'status'=>0,'level_id'=>$v['id']];
                foreach ($d as $v2) {
                    if($v['id'] == $v2['level_id']) {
                        $expData[$v['id']]['money'] = $v2['money'];
                        $expData[$v['id']]['id'] = $v2['id'];
                        if($v2['status']) $bool = true;
                    }
                }
            }
            $data[$k]['status'] = $bool;
            $data[$k]['expData'] = $expData;
        }

        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $page,
            'level'     => $level
        ]);

    }




    /**
     * 保存数据
     * @param Request $request
     */
    public function expSave(Request $request) {
        $pageSize = 20;
        [$warehouse_id,$id,$expData,$status,$money,$sassid] = UtilService::getMore(
            [
                ['warehouse_id','','empty','参数错误'],
                ['id','','empty','参数错误'],
                ['expData',[]],
                ['status',false],
                ['money',0],
                ['sassid',0]
            ]
            ,$request,true);

        $expCost = (new ExpCost);
        //修改底价
        $expCost
            ->where('sassid',$sassid)
            ->where('warehouse_id',$warehouse_id)
            ->where('express_id',$id)
            ->save(['money'=>$money]);
        //遍历数据
        foreach ($expData as $v) {
            $d['warehouse_id'] = $warehouse_id;
            $d['express_id'] = $id;
            $d['money'] = $v['money'];
            $d['level_id'] = $v['level_id'];
            $d['sassid'] = $sassid;
            $d['status'] = $status ? 1 : 0;
            $data = $expCost
                        ->where('sassid',$sassid)
                        ->where('warehouse_id',$warehouse_id)
                        ->where('express_id',$id)
                        ->where('level_id',$v['level_id'])
                        ->find();
            if( empty($data) ) {
                $expCost->insert($d);
            } else {
                $expCost->where('id',$data['id'])->save($d);
            }
        }
       return app('json')->success('操作成功');
    }

    /**
     * 更新分站数据
     * @param Request $request
     */
    public function updateSiteExp(Request $request) {
        [$sassid,$warehouse_id] = UtilService::getMore(
            [
                ['sassid',''],
                ['warehouse_id','']
            ]
            ,$request,true);
        $str = [];
        if(empty($sassid)) {
            $siteData = (new \app\model\system\Site)->select();
            foreach ($siteData as $v) {
                $makeSite = new MakeSite($v['sassid']);
                $makeSite->makeExpcos($warehouse_id);
                $str[] = $v['name'] . '更新成功';
            }
        } else {
            $w = (new \app\model\system\Warehouse)->select();
            foreach ($w as $v) {
                $makeSite = new MakeSite($sassid);
                $makeSite->makeExpcos($v['id']);
                $str[] = $v['name'].'更新成功';
            }

        }
        return app('json')->success(join(',',$str));
    }




}
