<?php
declare (strict_types=1);
namespace app\system\controller\v1;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-11-08 23:40
// +----------------------------------------------------------------------

use app\model\system\WlnPro;
use app\Request;
use library\lib\hupun;
use library\services\UtilService;

class Wln extends \app\BaseController {


    /**
     * 获取万能牛产品
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request){
        $pageSize = 10;
        $post =  UtilService::getMore(
            [
                ['page',1]
            ],$request
        );
        $where = [];
        $wlnpro = new WlnPro;
        list($pageCount,$data) = $wlnpro->getList($post['page'],$where,$pageSize,"*","w.id desc");
        return app('json')->success([
            'list'      => $data,
            'pageCount' => $pageCount,
            'pageSize'  => $pageSize,
            'page'      => $post['page']
        ]);
    }

    /**
     * 同步万能牛
     * @param Request $request
     */
    public function asyWln(Request $request) {
        [$id] =  UtilService::getMore(
            [
                ['id','','empty','参数错误']
            ],$request,true
        );
        $wlnpro = new WlnPro;
        $data = $wlnpro->where('id',$id)->find();
        if(empty($data)) {
            return app('json')->fail('找不到编号');
        }
        $proData = \app\model\system\Product::where('id',$data['pro_id'])->find();
        if(empty($proData)) {
            return app('json')->fail('找不到产品');
        }
        $imgAr = explode(',',$proData['img']);
        $save['code'] = $data['code'];
        $save['price'] = $proData['commission'];
        $save['quantity']  = $proData['count'];
        $save['csno'] = $data['code'];
        $save['url'] = 'http://www.lipinwawa.com/goods/detail/'.$proData['id'].'.html';
        $save['img'] = $imgAr[0];
        $save['status'] = $proData['status'];
        $save['time'] = time();
        $save['title'] = $proData['title'];
        $hupin = new hupun();
        $dv = $hupin->addProduct($save);
        if($dv['success']) {
            $wlnpro->where('id',$id)->save(['is_bool'=>1]);
            return app('json')->success('同步成功');
        }

        return app('json')->success($dv['error_code']);
    }


}