<?php
/**
* Created by PhpStorm.
* User: kang
* Date: 2021/3/27
* Time: 12:11
*/
namespace JinDouYun\Model\Goods;

use JinDouYun\Model\MBaseModel;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Dao\Goods\DGoodsSupport;

class MGoodsSupport extends MBaseModel
{
    
    private $objDGoodsSupport;
    private $enterpriseId;
    private $userCenterId;
    
    public function __construct($enterpriseId, $userCenterId)
    {
        $this->userCenterId = $userCenterId;
        $this->enterpriseId = $enterpriseId;
        parent::__construct($enterpriseId, $userCenterId);
        $this->objDGoodsSupport = new DGoodsSupport('default');
    }
    
    /**
     * 添加商品服务
     * @param $params
     * @return ResultWrapper
     * @throws Exception
     */
    public function addGoodsSupport($addGoodsSupportData)
    {
        $dbResult = $this->objDGoodsSupport->get(['servicesName' => $addGoodsSupportData['servicesName']]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }
        if(!empty($dbResult)){
            return ResultWrapper::fail('该商品服务已存在', ErrorCode::$paramError);
        }
        unset($dbResult);
        $addGoodsServiceDataId = $this->objDGoodsSupport->insert($addGoodsSupportData);
        if($addGoodsServiceDataId === false){
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($addGoodsServiceDataId);
    }
    
    /**
     * 获取指定商品服务
     */
    public function getGoodsSupportInfo($GoodsSupportId)
    {
        
        if( empty($GoodsSupportId) ){
            return ResultWrapper::fail('接收不到要请求的商品服务', ErrorCode::$paramError);
        }
        $dbResult = $this->objDGoodsSupport->get($GoodsSupportId);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    
    /**
     * 编辑商品服务
     *
     * @param int|array $params 修改商品服务的数据
     *
     * @return ResultWrapper
     */
    public function editGoodsSupport($params)
    {
        if( empty($params['id']) ){
            return ResultWrapper::fail('没有指定要修改的商品服务', ErrorCode::$paramError);
        }
        $updateGoodsSupportId = $params['id'];
        unset($params['id']);
        $dbResult = $this->objDGoodsSupport->update($params, $updateGoodsSupportId);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    /**
     * 删除商品服务
     *
     * @param array $params  要删除商品服务
     *
     * @return ResultWrapper
     */
    public function delGoodsSupport($GoodsSupportId)
    {
        $updateData = [
            'deleteStatus' => StatusCode::$delete,
        ];
        $dbResult = $this->objDGoodsSupport->update($updateData, $GoodsSupportId);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    /**
     * 获取所有商品服务
     *
     * @param array $selectParams 过滤条件
     *
     * @return ResultWrapper
     */
    public function getAllGoodsSupport($selectParams)
    {
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $offset = $selectParams['offset'];
        unset($selectParams['offset']);
    
        $returnData = [
            'data'  => [],
            'total' => 0,
        ];

        $where = 'deleteStatus = '.StatusCode::$standard .'  and  enterprise ='.$this->enterpriseId;
        if( isset($selectParams['servicesName']) ){
            $where .= ' and servicesName like "%'.$selectParams['servicesName'].'%" ';
        }

        $dbResult = $this->objDGoodsSupport->select($where, '*', 'createTime desc',$limit,$offset);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }
        if( empty($dbResult) ){
            return ResultWrapper::success($returnData);
        }
        $dbTotalResult = $this->objDGoodsSupport->count($where);
        if ($dbTotalResult === false) {
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }
        $return = [
            'data'  => $dbResult,
            'total' => $dbTotalResult,
        ];
        return ResultWrapper::success($return);
    }
    
    /**
     * 获取goods中存储的商品服务数据
     */
    public function getGoodsSupport($SupportId)
    {
        if( empty($SupportId) ){
            return ResultWrapper::success([]);
        }
        $sql ='select servicesName,`describe`,Icon FROM qianniao_goods_service WHERE id IN ('.$SupportId.') and deleteStatus ='.StatusCode::$standard;
        $dbResult = $this->objDGoodsSupport->query($sql);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDGoodsSupport->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
}
