<?php
/**
 * 商铺管理模块
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/10/31
 * Time: 15:02
 */
namespace JinDouYun\Controller\Shop;

use JinDouYun\Model\Enterprise\MEnterprise;
use JinDouYun\Model\Shop\MShopProject;
use JinDouYun\Model\Shop\MShopSubscribe;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Cache\ShopCache;
use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Shop\MShop;
use JinDouYun\Cache\TempSaveCache;

class ShopProject extends BaseController
{
    private $objMShopProject;
    private $objShopCache;
    private $objTempSaveCache;
    private $objSub;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMShopProject = new MShopProject($this->onlineEnterpriseId, $this->onlineUserId);
        $this->objShopCache = new ShopCache();
        $this->objTempSaveCache = new TempSaveCache();
        $this->objSub = new MShopSubscribe($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 添加和编辑商铺管理公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter(){

        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $Data = [
            'name' => isset($params['name']) ? $params['name'] : '',
            'image' => isset($params['image']) ? $params['image'] : '',
            'slider_image' => isset($params['slider_image']) ? $params['slider_image'] : '',
            'price' => isset($params['price']) ? $params['price'] : '',
            'ot_price' => isset($params['ot_price']) ? $params['ot_price'] : '',
            'cost_price' => isset($params['cost_price']) ? $params['cost_price'] : '',
            'service_time' => isset($params['service_time']) ? $params['service_time'] : '',
            'commission' => isset($params['commission']) ? $params['commission'] : '',
        ];

        //非暂存则验空
        if (!isset($params['tempSave']) || $params['tempSave'] == false) {
            foreach($Data as $key => $value){
                if(empty($value) && $value !== 0){
                    $this->sendOutput($key.'参数错误', ErrorCode::$paramError );
                }
            }
        }
        $Data['result']= isset($params['result']) ? $params['result'] : false;
        $Data['info']= isset($params['info']) ? $params['info'] : false;
        $Data['shop_id']= isset($params['shop_id']) ? $params['shop_id'] : false;
        return $Data;
    }

    /**
     * 列表
     */
    public function list()
    {
        $params = $this->request->params();

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        $selectParams['enterprise_id'] = $this->onlineEnterpriseId;

//        if ($this->shopId){
//            $selectParams['shop_id'] = $this->shopId;
//        }
        //项目昵称
        if(isset($params['name']) && !empty($params['name'])){
            $selectParams['name'] = $params['name'];
        }

        if(isset($params['is_show']) && !empty($params['is_show'])){
            $selectParams['is_show'] = $params['is_show'] == 1? 0 : 1;
        }


        $result = $this->objMShopProject->list($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            foreach ($returnData['data'] as &$item)
            {
                $item['sold'] = $this->objSub->count([['project', 'like', '%'.$item['id'].'%']]);
            }
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($result->getData(), ErrorCode::$dberror);
        }
    }


    /**
     * 添加项目
     * @throws \Exception
     */
    public function add()
    {
        $addStaffData = $this->commonFieldFilter();
        $addStaffData['enterprise_id'] = $this->onlineEnterpriseId;
//        if ($this->shopId){
//            $addStaffData['shop_id'] = $this->shopId;
//        }

        $result = $this->objMShopProject->insert($addStaffData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }


    /**
     * 详情
     * @return void
     */
    public function details()
    {
        $where = [];
        $id = $this->request->param('id');
        if(!empty($id)){
            $where['id'] = $id;
        }
        $result = $this->objMShopProject->details($where);
        if ($result->isSuccess()) {

            $result = $result->getData();
            $result['sold'] = $this->objSub->count([['project', 'like', '%'.$result['id'].'%']]);
            $result['result'] = html_entity_decode($result['result']);
            parent::sendOutput($result);
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 修改
     * @return void
     */
    public function update()
    {
        $id['id'] = $this->request->param('id');
        if (empty($id['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $params = $this->commonFieldFilter();

        $result = $this->objMShopProject->update($params, $id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    public function delete()
    {
        $id['id'] = $this->request->param('id');
        if (empty($id['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $result = $this->objMShopProject->delete($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * @return void
     */
    public function set_shop()
    {
        $id['id'] = $this->request->param('id');
        $data['is_show'] = $this->request->param('is_show');
        if (empty($id['id'])) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }


        $result = $this->objMShopProject->update($data, $id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

}