<?php
/**
 * Created by PhpStorm.
 * User: Gss
 * Date: 2021/3/26 0026
 * Time: 10:50
 */

namespace JinDouYun\Controller\Customer;

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

use JinDouYun\Dao\BaseDao;

use JinDouYun\Model\Customer\MMemberBalanceDetail;

use JinDouYun\Controller\BaseController;

class MemberBalanceDetail extends BaseController
{
    private $objMMemberBalanceDetail;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objMMemberBalanceDetail = new MMemberBalanceDetail($this->onlineEnterpriseId, $this->onlineUserId);
    }

    /**
     * 添加和编辑会员明细公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter()
    {

        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $memberBalanceDetailData = [
            'customerId'        =>getArrayItem($params,'customerId',''),//int(10) DEFAULT NULL COMMENT '客户id',
            'type'              =>getArrayItem($params,'type',''),//int(1) NOT NULL COMMENT '金额类型   5收入/4支出',
            'purpose'           =>getArrayItem($params,'purpose',''),//varchar(255) NOT NULL COMMENT '用途',
            'money'             =>getArrayItem($params,'money',''),//decimal(15,4) NOT NULL COMMENT '变动金额',
            'orderIds'          =>getArrayItem($params,'orderIds',''),
        ];

        foreach ($memberBalanceDetailData as $key => $value) {
            if (empty($value) && $value !== 0) {
                $this->sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $memberBalanceDetailData['remark'] = getArrayItem($params,'remark','');
        $memberBalanceDetailData['createTime'] = time();
        $memberBalanceDetailData['updateTime'] = time();
        return $memberBalanceDetailData;
    }

    /**
     * 新增会员明细
     */
    public function addMemberBalanceDetail()
    {
        $memberBalanceDetailData = $this->commonFieldFilter();

        $result = $this->objMMemberBalanceDetail->addMemberBalanceDetail($memberBalanceDetailData);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        } else {
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 获取所有的明细
     */
    public function getAllMemberBalanceDetail()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }

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

        $returnData = $this->objMMemberBalanceDetail->getAllMemberBalanceDetail($params);

        if ($returnData->isSuccess()) {
            $returnData = $returnData->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize'  => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        } else {
            parent::sendOutput($returnData->getData(), ErrorCode::$dberror);
        }
    }

}