<?php


namespace JinDouYun\Controller\Market;


use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Market\MComBinPackage;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;

/**
 * @copyright   Copyright (c) https://www.qianniaovip.com All rights reserved
 * Description:
 * Class ComBinPackage
 * @package JinDouYun\Controller\Market
 */
class ComBinPackage extends BaseController
{

    /**
     * @var MComBinPackage
     */
    private $objMComBinPackage;

    /**
     * ComBinPackage constructor.
     * @param bool $isCheckAcl
     * @param bool $isMustLogin
     * @param bool $checkToken
     * @param false $getAreaCode
     * @param bool $checkShopToken
     * @param bool $checkSupplierToken
     * @throws \Exception
     */
    public function __construct($isCheckAcl = true, $isMustLogin = true, $checkToken = true, $getAreaCode = false, $checkShopToken = true, $checkSupplierToken = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode, $checkShopToken, $checkSupplierToken);
        $this->objMComBinPackage = new MComBinPackage($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 添加,编辑
     * @return array
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            self::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            "title"        => getArrayItem($params, 'title', ''),
            //varchar(30) DEFAULT '' COMMENT '活动名称',
            "isExpire"     => getArrayItem($params, 'isExpire', StatusCode::$delete),
            //tinyint(3) DEFAULT '4' COMMENT '是否具有时间限制 默认5 4不限时间 5限制时间',
            "shopId"       => getArrayItem($params, 'shopId', 0),
            //int(10) NOT NULL DEFAULT '0' COMMENT '商铺id',
            "shopName"     => getArrayItem($params, 'shopName', ''),
            //varchar(30) DEFAULT NULL COMMENT '商铺名称',
            "goodsData"    => getArrayItem($params, 'goodsData', null),
            //json DEFAULT NULL COMMENT '活动商品数据',
            "price"        => getArrayItem($params, 'price', 0),
            //decimal(10,2) DEFAULT '0.00' COMMENT '套餐价格',
            "originPrice"  => getArrayItem($params, 'originPrice', 0),
            //decimal(10,2) DEFAULT '0.00' COMMENT '套餐价格',
            "inventory"    => getArrayItem($params, 'inventory', 0),
            //decimal(10,2) DEFAULT NULL COMMENT '套餐设置库存',
            "isLimit"      => getArrayItem($params, 'isLimit', StatusCode::$delete),
            //tinyint(3) DEFAULT '4' COMMENT '是否开启每人限购 默认4 4不开启 5开启',
            "expressType"  => getArrayItem($params, 'expressType', StatusCode::$delete),
            //tinyint(3) DEFAULT '4' COMMENT '运费设置 默认4 4默认跟随商品 5包邮',
            "enableStatus" => getArrayItem($params, 'enableStatus', StatusCode::$delete),
            //tinyint(3) DEFAULT '4' COMMENT '是否启用 默认4 4禁用 5启用',
            "materielNum"  => getArrayItem($params,'materielNum',0),
        ];
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                self::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $goodsArr = [];
        foreach ($data['goodsData'] as $val){
            if (!in_array($val['goodsId'],$goodsArr)){
                $goodsArr[] = $val['goodsId'];
            }
        }
        $data['goodsIds'] = implode(',',$goodsArr);//产品ids
        $data['goodsData'] = json_encode($data['goodsData'],320);
        $data['startTime'] = getArrayItem($params, 'startTime', 0);
        $data['endTime'] = getArrayItem($params, 'endTime', 0);
        $data['limitNum'] = getArrayItem($params, 'limitNum', 0);
        return $data;
    }

    /**
     * Doc: (des="添加组合套餐")
     * User: XMing
     * Date: 2021/1/21
     * Time: 3:47 下午
     */
    public function add()
    {
        $data = $this->commonFieldFilter();
        $result = $this->objMComBinPackage->add($data);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2021/1/22
     * Time: 10:11 上午
     */
    public function edit()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $data = $this->commonFieldFilter();
        $result = $this->objMComBinPackage->edit($data,$id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }


    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2021/1/21
     * Time: 5:45 下午
     */
    public function getAll()
    {
        $params = $this->request->getRawJson();
        $pageParams = pageToOffset(isset($params['page']) ? $params['page'] : 1, isset($params['pageSize']) ? $params['pageSize'] : 10);
        $params['limit'] = $pageParams['limit'];
        $params['offset'] = $pageParams['offset'];
        $result = $this->objMComBinPackage->getAll($params);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => isset($params['page']) ? $params['page'] : 1,
                'pageSize'  => isset($params['pageSize']) ? $params['pageSize'] : 10,
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2021/1/21
     * Time: 6:39 下午
     * @throws \Exception
     */
    public function get()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $result = $this->objMComBinPackage->get($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * Doc: (des="启用/禁用")
     * User: XMing
     * Date: 2021/1/21
     * Time: 6:40 下午
     */
    public function enable()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            $this->sendOutput('参数错误', ErrorCode::$paramError);
        }
        $result = $this->objMComBinPackage->enable($id);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }
}