<?php
/**
 * 积分商品管理Model
 * Created by PhpStorm.
 * User: haoren
 * Date: 2021/03/25
 * Time: 16:00
 */

namespace JinDouYun\Model\Integral;

use Exception;

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

use JinDouYun\Dao\Integral\DIntegralGoods;
use JinDouYun\Dao\Integral\DIntegralGoodsExchange;
use JinDouYun\Dao\Integral\DIntegralRule;
use JinDouYun\Dao\Customer\DCustomerIntegralDesc;
use JinDouYun\Dao\UserCenter\DUserCenter;

use JinDouYun\Model\MBaseModel;
use JinDouYun\Model\Customer\MCustomer;

class MIntegralGoods extends MBaseModel
{
    private $objDIntegralGoods;
    private $objDIntegralGoodsExchange;
    private $objDIntegralRule;
    private $userCenterId;
    private $enterpriseId;

    public function __construct($enterpriseId, $userCenterId = false)
    {
        $this->enterpriseId = $enterpriseId;
        $this->userCenterId = $userCenterId;
        $this->objDIntegralGoods = new DIntegralGoods('default');
        $this->objDIntegralGoodsExchange = new DIntegralGoodsExchange('default');
        $this->objDIntegralRule = new DIntegralRule('default');
        $this->objDIntegralGoods->setTable('qianniao_integral_goods_' . $enterpriseId);
        $this->objDIntegralGoodsExchange->setTable('qianniao_integral_goods_exchange_' . $enterpriseId);
        $this->objDIntegralRule->setTable('qianniao_integral_rule_' . $enterpriseId);
    }

    /**
     * 积分商品添加
     * @param $params
     * @return ResultWrapper
     * @throws Exception
     */
    public function addIntegralGoods($params)
    {
        $params['deleteStatus'] = StatusCode::$standard;
        $params['enableStatus'] = StatusCode::$standard;
        $params['createTime'] = time();
        $params['updateTime'] = time();
        $dbResult = $this->objDIntegralGoods->insert($params);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 积分商品删除
     * @param $where
     * @return ResultWrapper
     */
    public function deleteIntegralGoods($where)
    {
        $params = [
            'deleteStatus' => StatusCode::$delete,
            'updateTime' => time(),
        ];
        $dbResult = $this->objDIntegralGoods->update($params, $where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        $returnData = $dbResult;
        return ResultWrapper::success($returnData);
    }

    /**
     * 积分商品禁用
     * @param $where
     * @return ResultWrapper
     */
    public function enableIntegralGoods($where)
    {
        $dbResult = $this->objDIntegralGoods->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        $update = [
            'enableStatus' => StatusCode::$standard,
            'updateTime' => time(),
        ];
        if($dbResult['enableStatus'] == StatusCode::$standard){
            $update['enableStatus'] = StatusCode::$delete;
        }

        $dbResult = $this->objDIntegralGoods->update($update, $where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        $returnData = $dbResult;
        return ResultWrapper::success($returnData);
    }

    /**
     * 积分商品修改
     * @param $updateData
     * @param $where
     * @return ResultWrapper
     */
    public function updateIntegralGoods($updateData, $where)
    {
        $dbResult = $this->objDIntegralGoods->get($where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$paramError);
        }
        $goods = $dbResult;
        unset($dbResult);

        $dbResult = $this->objDIntegralGoods->update($updateData, $where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }

        return ResultWrapper::success($dbResult);
    }

    /**
     * 积分商品列表
     * @param $selectParams
     * @return ResultWrapper
     * @throws Exception
     */
    public function getAllIntegralGoods($selectParams)
    {
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $offset = $selectParams['offset'];
        unset($selectParams['offset']);

        $selectParams['deleteStatus'] = StatusCode::$standard;
        isset($selectParams['search']) && $selectParams['search'] = ['name' => $selectParams['search']];
        $selectParams = self::formatSqlWhere($selectParams);

        $dbResult = $this->objDIntegralGoods->select($selectParams,'*', 'sort desc,createTime desc', $limit, $offset);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }

        $countResult = $this->objDIntegralGoods->count($selectParams);
        if ($countResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }

        $return = [
            'data' => self::formatIntegralGoods($dbResult),
            'total' => ($countResult) ? intval($countResult) : 0,
        ];
        return ResultWrapper::success($return);
    }

    /**
     * 积分商品详情
     * @param $where
     * @return ResultWrapper
     * @throws Exception
     */
    public function getIntegralGoodsInfo($where)
    {
        $where['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDIntegralGoods->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success(self::formatIntegralGoods($dbResult));
    }

    /**
     * 积分商品格式化
     * @param $params
     * @return array
     */
    public function formatIntegralGoods($params)
    {
        if(isset($params['id'])){
            $data = [$params];
        }else{
            $data = $params;
        }
        foreach($data as &$value){
            $value['images'] = json_decode($value['images'], true);
            $value['categoryName'] = $value['category'] == StatusCode::$delete ? '虚拟商品' : '实物商品';
            $value['details'] = htmlspecialchars_decode($value['details'], ENT_QUOTES);
        }
        unset($value);
        if(isset($params['id'])){
            $return = array_shift($data);
        }else{
            $return = $data;
        }
        return $return;
    }

    /**
     * 查询积分商品数据
     * @param $where
     * @return ResultWrapper
     */
    public function getIntegralGoods($where)
    {
        $where['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDIntegralGoods->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 查询积分商品数据
     * @param $where
     * @return ResultWrapper
     */
    public function selectIntegralGoods($where)
    {
        $where['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDIntegralGoods->select($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 积分商品兑换添加
     * @param $params
     * @return ResultWrapper
     */
    public function addIntegralGoodsExchange($params)
    {
        //查询商品
        $dbResult = $this->objDIntegralGoods->get(['id' => $params['goodsId'], 'deleteStatus' => StatusCode::$standard]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }
        $goods = $dbResult;
        unset($dbResult);
        if(empty($goods)){
            return ResultWrapper::fail('商品不存在', ErrorCode::$paramError);
        }
        if($goods['enableStatus'] == StatusCode::$delete){
            return ResultWrapper::fail('商品已下架', ErrorCode::$paramError);
        }
/*        if($params['num'] > $goods['num']){
            return ResultWrapper::fail('商品可兑换数量不足', ErrorCode::$paramError);
        }*/
        if($params['num'] > $goods['limit']){
            return ResultWrapper::fail('每位用户限购'.$goods['limit'], ErrorCode::$paramError);
        }
        $params['integral'] = $goods['integral'];
        $params['amount'] = bcmul($params['num'], $params['integral'], 2);

        //查询客户
        $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
        $where = [
            'userCenterId' => isset($params['userCenterId']) ? $params['userCenterId'] : $this->userCenterId,
            'deleteStatus' => StatusCode::$standard
        ];
        $modelResult = $objMCustomer->getCustomerData($where);
        if(!$modelResult->isSuccess()){
            return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
        }
        $customer = $modelResult->getData();
        unset($modelResult);
        if(empty($customer)){
            return ResultWrapper::fail('用户不存在', ErrorCode::$paramError);
        }

        $whereGoodsExchange = [
            'customerId' => $customer['id'],
            'goodsId' => $goods['id'],
            'shopId' => $params['shopId'],
            'status' => [4,5]
        ];
        $sql = 'select sum(num) as num from qianniao_integral_goods_exchange_'.$this->enterpriseId.' where '.self::formatSqlWhere($whereGoodsExchange);
        $dbResult = $this->objDIntegralGoodsExchange->query($sql);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
        }
        $exchangeNum = $dbResult[0]['num'];
        unset($dbResult);
        if(bcadd($exchangeNum, $params['num']) > $goods['limit']){
            return ResultWrapper::fail('每位用户限购'.$goods['limit'], ErrorCode::$paramError);
        }

        if($customer['integral'] < $params['amount']){
            return ResultWrapper::fail('积分不足', ErrorCode::$paramError);
        }

        $beginStatus = $this->objDIntegralGoodsExchange->beginTransaction();

        //扣除积分
        $updateCustomer = [
            'integral' => bcsub($customer['integral'], $params['amount'], 2),
            'updateTime' => time()
        ];
        $modelResult = $objMCustomer->updateCustomer($updateCustomer, ['id' => $customer['id']]);
        if(!$modelResult->isSuccess()){
            $this->objDIntegralGoodsExchange->rollBack();
            return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
        }
        unset($modelResult);

        $params['status'] = StatusCode::$delete;
        $params['no'] = createOrderSn(StatusCode::$source['manage'], 99, $params['userCenterId']);
        $params['createTime'] = time();
        $params['updateTime'] = time();
        $params['customerId'] = $customer['id'];
        $dbResult = $this->objDIntegralGoodsExchange->insert($params);
        if($dbResult === false){
            $this->objDIntegralGoodsExchange->rollBack();
            return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
        }
        $exchangeId = $dbResult;
        unset($dbResult);

        //增加记录
        $insertIntegralDesc = [
            'userCenterId' => $customer['userCenterId'],
            'customerId' => $customer['id'],
            'shopId' => isset($params['shopId']) ? $params['shopId'] : 0,
            'originId' => $exchangeId,
            'originNo' => $params['no'],
            'title' => '积分兑换商品',
            'amount' => $params['amount'],
            'changeAmount' => $updateCustomer['integral'],
            'type' => StatusCode::$delete,
            'source' => 4,
            'createTime' => time(),
            'updateTime' => time(),
        ];
        $objDCustomerIntegralDesc = new DCustomerIntegralDesc();
        $objDCustomerIntegralDesc->set_Table('qianniao_customer_integral_desc_'.$this->enterpriseId);
        $dbResult = $objDCustomerIntegralDesc->insert($insertIntegralDesc);
        if($dbResult === false){
            $this->objDIntegralGoodsExchange->rollBack();
            return ResultWrapper::fail($objDCustomerIntegralDesc->error(), ErrorCode::$dberror);
        }

        //修改商品
        $updateGoods = [
            'num' => bcsub($goods['num'], $params['num']),
            'changeNum' => bcadd($goods['changeNum'], $params['num']),
            'updateTime' => time()
        ];
        $dbResult = $this->objDIntegralGoods->update($updateGoods, ['id' => $goods['id']]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoods->error(), ErrorCode::$dberror);
        }

        $beginStatus && $this->objDIntegralGoodsExchange->commit();
        return ResultWrapper::success($exchangeId);
    }

    /**
     * 积分商品兑换修改
     * @param $update
     * @param $where
     * @return ResultWrapper
     */
    public function updateIntegralGoodsExchange($update, $where)
    {
        $dbResult = $this->objDIntegralGoodsExchange->get($where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
        }
        $exchange = $dbResult;
        unset($dbResult);
        if(isset($update['status']) && $update['status'] == 6 && $exchange['status'] != 4){
            return ResultWrapper::fail('订单已完成', ErrorCode::$paramError);
        }

        if(isset($update['status']) && $update['status'] == 5 && $exchange['status'] != 4){
            return ResultWrapper::fail('订单已失效', ErrorCode::$paramError);
        }

        if($update['status'] == 6){
            //返还积分
            $objMCustomer = new MCustomer($this->enterpriseId, $this->userCenterId);
            $modelResult = $objMCustomer->getCustomerData(['id' => $exchange['customerId']]);
            if(!$modelResult->isSuccess()){
                return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
            }
            $customer = $modelResult->getData();
            unset($modelResult);
            $updateCustomer = [
                'integral' => bcadd($customer['integral'], $exchange['amount'], 2),
                'updateTime' => time(),
            ];
            $modelResult = $objMCustomer->updateCustomer($updateCustomer, ['id' => $customer['id']]);
            if(!$modelResult->isSuccess()){
                return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
            }
            //增加记录
            $insertIntegralDesc = [
                'userCenterId' => $customer['userCenterId'],
                'customerId' => $customer['id'],
                'shopId' => isset($exchange['shopId']) ? $exchange['shopId'] : 0,
                'originId' => $exchange['id'],
                'originNo' => $exchange['no'],
                'title' => '积分兑换商品失效,返还积分',
                'amount' => $exchange['amount'],
                'changeAmount' => $updateCustomer['integral'],
                'type' => StatusCode::$standard,
                'source' => 5,
                'createTime' => time(),
                'updateTime' => time(),
            ];
            $objDCustomerIntegralDesc = new DCustomerIntegralDesc();
            $objDCustomerIntegralDesc->set_Table('qianniao_customer_integral_desc_'.$this->enterpriseId);
            $dbResult = $objDCustomerIntegralDesc->insert($insertIntegralDesc);
            if($dbResult === false){
                return ResultWrapper::fail($objDCustomerIntegralDesc->error(), ErrorCode::$dberror);
            }
        }

        $update['updateTime'] = time();
        $dbResult = $this->objDIntegralGoodsExchange->update($update,$where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }


    /**
     * 积分商品兑换列表
     * @return ResultWrapper
     */
    public function getAllIntegralGoodsExchange($params)
    {
        $sql = 'select e.*,g.name as integralGoodsName,g.category,g.images,c.name as customerName from qianniao_integral_goods_exchange_'.$this->enterpriseId.' e left join qianniao_integral_goods_'.$this->enterpriseId.' g on g.id = e.goodsId left join qianniao_customer_'.$this->enterpriseId.' c on c.id = e.customerId';
        $whereSql = '';

        if(isset($params['shopId'])){
            $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.shopId = '.$params['shopId'];
        }

        if(isset($params['userCenterId'])){
            $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.userCenterId = '.$params['userCenterId'];
        }

        if(isset($params['search'])){
            $whereSql .= (!empty($whereSql) ? ' and ' : '').' (c.name like "%'.$params['search'].'%" or g.name like "%'.$params['search'].'%")';
        }

        if(isset($params['status'])){
            $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.status = '.$params['status'];
        }

        !empty($whereSql) && $whereSql = ' where '.$whereSql;

        $orderSql = ' order by e.createTime desc';

        $dbResult = $this->objDIntegralGoodsExchange->query($sql.$whereSql.$orderSql);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
        }
        $data = $dbResult;
        unset($dbResult);
        $countSql = 'select count(*) as count  from qianniao_integral_goods_exchange_'.$this->enterpriseId.' e left join qianniao_integral_goods_'.$this->enterpriseId.' g on g.id = e.goodsId left join qianniao_customer_'.$this->enterpriseId.' c on c.id = e.customerId';
        $dbResult = $this->objDIntegralGoodsExchange->query($countSql.$whereSql);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
        }
        $count = $dbResult;
        unset($dbResult);
        $return = [
            'data' => self::formatIntegralGoodsExchange($data),
            'total' => isset($count[0]['count']) ? $count[0]['count'] : 0
        ];
        return ResultWrapper::success($return);
    }

    /**
     * 积分商品兑换详情
     * @param $where
     * @return ResultWrapper
     */
    public function getIntegralGoodsExchangeInfo($params)
    {
        $sql = 'select e.*,g.name as integralGoodsName,g.category,g.images,c.name as customerName from qianniao_integral_goods_exchange_'.$this->enterpriseId.' e left join qianniao_integral_goods_'.$this->enterpriseId.' g on g.id = e.goodsId left join qianniao_customer_'.$this->enterpriseId.' c on c.id = e.customerId';
        $whereSql = '';

        if(isset($params['id'])){
            $whereSql .= (!empty($whereSql) ? ' and ' : '').' e.id = '.$params['id'];
        }

        !empty($whereSql) && $whereSql = ' where '.$whereSql;

        $limitSql = ' limit 1';

        $dbResult = $this->objDIntegralGoodsExchange->query($sql.$whereSql.$limitSql);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralGoodsExchange->error(), ErrorCode::$dberror);
        }
        $data = array_shift($dbResult);
        unset($dbResult);
        return ResultWrapper::success(self::formatIntegralGoodsExchange($data));
    }

    /**
     * 积分商品兑换格式化
     * @param $params
     * @return array|mixed
     */
    public function formatIntegralGoodsExchange($params)
    {
        if(isset($params['id'])){
            $data = [$params];
        }else{
            $data = $params;
        }
        $userCenterIds = array_column($data, 'userCenterId');

        $userCenterData = [];
        if(!empty($userCenterIds)){
            $objDUserCenter = new DUserCenter();
            $dbResult = $objDUserCenter->select(['id' => $userCenterIds]);
            if($dbResult){
                foreach($dbResult as $value){
                    $userCenterData[$value['id']] = $value['mobile'];
                }
            }
            unset($dbResult);
        }

        foreach($data as &$value){
            $value['images'] = !empty($value['images']) ? json_decode($value['images'], true) : [];
            $value['categoryName'] = $value['category'] == StatusCode::$delete ? '虚拟商品' : '实物商品';
            $value['address'] = !empty($value['address']) ? json_decode($value['address'], true) : [];
            $value['mobile'] = isset($userCenterData[$value['userCenterId']]) ? $userCenterData[$value['userCenterId']] : '';
        }
        unset($value);
        if(isset($params['id'])){
            $return = array_shift($data);
        }else{
            $return = $data;
        }
        return $return;
    }

    /**
     * 积分规则添加
     * @param $params
     * @return ResultWrapper
     */
    public function addIntegralRule($params)
    {
        $dbResult = $this->objDIntegralRule->update(['enableStatus' => StatusCode::$delete, 'updateTime' => time()]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }

        $params['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDIntegralRule->insert($params);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 积分规则删除
     * @param $where
     * @return ResultWrapper
     */
    public function deleteIntegralRule($where)
    {
        $params = [
            'deleteStatus' => StatusCode::$delete,
            'updateTime' => time(),
        ];
        $dbResult = $this->objDIntegralRule->update($params, $where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }
        $returnData = $dbResult;
        return ResultWrapper::success($returnData);
    }

    /**
     * 积分规则禁用
     * @param $where
     * @return ResultWrapper
     */
    public function enableIntegralRule($where)
    {
        $dbResult = $this->objDIntegralRule->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }
        $update = [
            'enableStatus' => StatusCode::$standard,
            'updateTime' => time(),
        ];
        if($dbResult['enableStatus'] == StatusCode::$standard){
            $update['enableStatus'] = StatusCode::$delete;
        }else{
            $result = $this->objDIntegralRule->update(['enableStatus' => StatusCode::$delete, 'updateTime' => time()],['enableStatus' => StatusCode::$standard]);
            if ($result === false) {
                return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
            }
        }

        $dbResult = $this->objDIntegralRule->update($update, $where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }
        $returnData = $dbResult;
        return ResultWrapper::success($returnData);
    }

    /**
     * 积分规则修改
     * @param $updateData
     * @param $where
     * @return ResultWrapper
     */
    public function updateIntegralRule($updateData, $where)
    {
        $dbResult = $this->objDIntegralRule->get($where);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$paramError);
        }
        $rule = $dbResult;
        unset($dbResult);

        $dbResult = $this->objDIntegralRule->update($updateData, $where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }

        return ResultWrapper::success($dbResult);
    }

    /**
     * 积分规则列表
     * @param $selectParams
     * @return ResultWrapper
     * @throws Exception
     */
    public function getAllIntegralRule($selectParams)
    {
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $offset = $selectParams['offset'];
        unset($selectParams['offset']);

        $selectParams['deleteStatus'] = StatusCode::$standard;
        isset($selectParams['search']) && $selectParams['search'] = ['title' => $selectParams['search']];
        $selectParams = self::formatSqlWhere($selectParams);

        $dbResult = $this->objDIntegralRule->select($selectParams,'*', 'createTime desc', $limit, $offset);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }

        $countResult = $this->objDIntegralRule->count($selectParams);
        if ($countResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }

        $return = [
            'data' => self::formatIntegralRule($dbResult),
            'total' => ($countResult) ? intval($countResult) : 0,
        ];
        return ResultWrapper::success($return);
    }

    /**
     * 积分规则详情
     * @param $where
     * @return ResultWrapper
     * @throws Exception
     */
    public function getIntegralRuleInfo($where)
    {
        $where['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDIntegralRule->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success(self::formatIntegralRule($dbResult));
    }

    /**
     * 积分规则格式化
     * @param $params
     * @return array
     */
    public function formatIntegralRule($params)
    {
        if(isset($params['id'])){
            $data = [$params];
        }else{
            $data = $params;
        }

        foreach($data as &$value){
            $value['goods'] = json_decode($value['goods'], true);
        }
        unset($value);
        if(isset($params['id'])){
            $return = array_shift($data);
        }else{
            $return = $data;
        }
        return $return;
    }

    /**
     * 查询积分规则数据
     * @param $where
     * @return ResultWrapper
     */
    public function getIntegralRule($where)
    {
        $where['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDIntegralRule->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }

    /**
     * 查询积分规则数据
     * @param $where
     * @return ResultWrapper
     */
    public function selectIntegralRule($where)
    {
        $where['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDIntegralRule->select($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDIntegralRule->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult);
    }


}