<?php
/**
 * 商户Controller
 * Created by PhpStorm.
 * User: haoren
 * Date: 2020/09/25
 * Time: 15:00
 */

namespace JinDouYun\Controller\Merchant;

use JinDouYun\Model\Finance\MMerchantFlow;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Controller\BaseController;

use JinDouYun\Model\Merchant\MMerchant;

class Merchant extends BaseController
{
    private $objMMerchant;

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

    /**
     * 获取参数
     */
    public function commonFieldFilter()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = [
            'name'             => isset($params['name']) ? $params['name'] : '',
            'category'         => isset($params['category']) ? $params['category'] : '',
            'contactName'      => isset($params['contactName']) ? $params['contactName'] : '',
            'userCenterMobile' => isset($params['userCenterMobile']) ? $params['userCenterMobile'] : '',
            'contactMobile'    => isset($params['contactMobile']) ? $params['contactMobile'] : '',
        ];
        foreach ($data as $key => $value) {
            if (empty($value) && $value !== 0) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        isset($params['warehouseId']) && $data['warehouseId'] = !empty($params['warehouseId']) ? $params['warehouseId'] : '';
        isset($params['expireTime']) && $data['expireTime'] = !empty($params['expireTime']) ? $params['expireTime'] : '';
        isset($params['desc']) && $data['desc'] = !empty($params['desc']) ? $params['desc'] : '';
        isset($params['provinceCode']) && $data['provinceCode'] = !empty($params['provinceCode']) ? $params['provinceCode'] : '';
        isset($params['cityCode']) && $data['cityCode'] = !empty($params['cityCode']) ? $params['cityCode'] : '';
        isset($params['districtCode']) && $data['districtCode'] = !empty($params['districtCode']) ? $params['districtCode'] : '';
        isset($params['address']) && $data['address'] = !empty($params['address']) ? $params['address'] : '';
        isset($params['latitude']) && $data['latitude'] = !empty($params['latitude']) ? $params['latitude'] : '';
        isset($params['longitude']) && $data['longitude'] = !empty($params['longitude']) ? $params['longitude'] : '';
        isset($params['note']) && $data['note'] = !empty($params['note']) ? $params['note'] : '';
        isset($params['settleType']) && $data['settleType'] = !empty($params['settleType']) ? $params['settleType'] : '';
        isset($params['settle']) && $data['settle'] = !empty($params['settle']) ? $params['settle'] : '';
        isset($params['auditType']) && $data['auditType'] = !empty($params['auditType']) ? $params['auditType'] : '';
        isset($params['audit']) && $data['audit'] = !empty($params['audit']) ? $params['audit'] : '';
        isset($params['settingType']) && $data['settingType'] = !empty($params['settingType']) ? $params['settingType'] : '';
        isset($params['setting']) && $data['setting'] = !empty($params['setting']) ? $params['setting'] : '';
        return $data;
    }

    /**
     * 入驻申请添加
     */
    public function addApply()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }

        $data = [
            'userCenterId'  => isset($params['userCenterId']) ? $params['userCenterId'] : '',
            'name'          => isset($params['name']) ? $params['name'] : '',
            'contactName'   => isset($params['contactName']) ? $params['contactName'] : '',
            'contactMobile' => isset($params['contactMobile']) ? $params['contactMobile'] : '',
        ];
        foreach ($data as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数为空', ErrorCode::$paramError);
            }
            unset($params[$key]);
        }

        $data['desc'] = isset($params['desc']) ? $params['desc'] : '';
        $data['license'] = isset($params['license']) ? $params['license'] : '';
        $data['category'] = isset($params['category']) ? $params['category'] : '';
        $data['provinceCode'] = isset($params['provinceCode']) ? $params['provinceCode'] : '';
        $data['cityCode'] = isset($params['cityCode']) ? $params['cityCode'] : '';
        $data['districtCode'] = isset($params['districtCode']) ? $params['districtCode'] : '';
        $data['address'] = isset($params['address']) ? $params['address'] : '';
        $data['info'] = isset($params['info']) ? $params['info'] : '';
        $modelResult = $this->objMMerchant->addApply($data);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 入驻审核
     */
    public function auditApply()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            parent::sendOutput('id参数错误', ErrorCode::$paramError);
        }
        $params = $this->request->getRawJson();
        $data['auditStatus'] = $params['auditStatus'];
        foreach ($data as $key => $value) {
            if (empty($value)) {
                parent::sendOutput($key . '参数错误', ErrorCode::$paramError);
            }
        }
        $data['auditFailReason'] = isset($params['auditFailReason']) ? $params['auditFailReason'] : '';
        $modelResult = $this->objMMerchant->auditApply($data, ['id' => $id]);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 新增商户
     */
    public function addMerchant()
    {
        $data = self::commonFieldFilter();
        $modelResult = $this->objMMerchant->addMerchant($data);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 删除商户
     */
    public function delMerchant()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $update = [
            'deleteStatus' => StatusCode::$delete
        ];
        $where = [
            'id' => $id
        ];
        $modelResult = $this->objMMerchant->updateMerchant($update, $where);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 禁用/启用商户
     */
    public function enabledMerchant()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $update = [
            'enabledStatus' => StatusCode::$delete
        ];
        $where = [
            'id' => $id
        ];
        $modelResult = $this->objMMerchant->updateMerchant($update, $where);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 修改商户
     */
    public function updateMerchant()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = self::commonFieldFilter();
        $modelResult = $this->objMMerchant->updateMerchant($data, ['id' => $id]);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 商户列表
     */
    public function getAllMerchant()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            $this->sendOutput('参数为空', ErrorCode::$paramError);
        }
        $selectParams = [];
        $params['page'] = isset($params['page']) ? $params['page'] : 1;
        $params['pageSize'] = isset($params['pageSize']) ? $params['pageSize'] : 10;
        $pageParams = pageToOffset($params['page'], $params['pageSize']);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];

        if (isset($params['search']) && !empty($params['search'])) {
            $selectParams['keyword'] = $params['search'];
        }

        if (isset($params['deleteStatus']) && !empty($params['deleteStatus'])) {
            $selectParams['deleteStatus'] = $params['deleteStatus'];
        }

        if (isset($params['auditStatus']) && !empty($params['auditStatus'])) {
            $selectParams['auditStatus'] = $params['auditStatus'];
        }

        if (isset($params['enabledStatus']) && !empty($params['enabledStatus'])) {
            $selectParams['enabledStatus'] = $params['enabledStatus'];
        }

        //到期时间
        if (isset($params['starExpireTime']) && !empty($params['starExpireTime'])) {
            $selectParams['starExpireTime'] = $params['starExpireTime'];
        }
        if (isset($params['endExpireTime']) && !empty($params['endExpireTime'])) {
            $selectParams['endExpireTime'] = $params['endExpireTime'];
        }

        //创建时间
        if (isset($params['starCreateTime']) && !empty($params['starCreateTime'])) {
            $selectParams['starCreateTime'] = $params['starCreateTime'];
        }
        if (isset($params['endCreateTime']) && !empty($params['endCreateTime'])) {
            $selectParams['endCreateTime'] = $params['endCreateTime'];
        }

        //审核时间
        if (isset($params['starAuditTime']) && !empty($params['starAuditTime'])) {
            $selectParams['starAuditTime'] = $params['starAuditTime'];
        }
        if (isset($params['endAuditTime']) && !empty($params['endAuditTime'])) {
            $selectParams['endAuditTime'] = $params['endAuditTime'];
        }

        //删除时间
        if (isset($params['starDeleteTime']) && !empty($params['starDeleteTime'])) {
            $selectParams['starDeleteTime'] = $params['starDeleteTime'];
        }
        if (isset($params['endDeleteTime']) && !empty($params['endDeleteTime'])) {
            $selectParams['endDeleteTime'] = $params['endDeleteTime'];
        }

        $modelResult = $this->objMMerchant->getAllMerchant($selectParams);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        $returnData = $modelResult->getData();

        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize'  => $params['pageSize'],
            'pageTotal' => $returnData['total'],
        ];
        parent::sendOutput($returnData['data'], 0, $pageData);
    }
    
    /**
     * 商户列表不分页
     */
    public function getAllMerchantList()
    {
        $modelResult = $this->objMMerchant->getAllMerchantList();
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }


    /**
     * 商户详情
     */
    public function getInfoMerchant()
    {
        $id = $this->request->param('request_id');
        if (empty($id)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $where = [
            'id' => $id
        ];
        $modelResult = $this->objMMerchant->getInfoMerchant($where);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * 保存设置
     */
    public function saveSetting()
    {
        $params = $this->request->getRawJson();
        if (empty($params)) {
            parent::sendOutput('参数为空', ErrorCode::$paramError);
        }
        $data = isset($params['data']) ? $params['data'] : '';
        $type = isset($params['type']) ? $params['type'] : '';
        $modelResult = $this->objMMerchant->saveSetting($data, ['type' => $type]);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }


    /**
     * 获取设置
     */
    public function getSetting()
    {
        $params = $this->request->getRawJson();
        $data['type'] = $params['type'];
        $modelResult = $this->objMMerchant->getSetting($data);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * Doc: (des="获取商户基础配置")
     * User: XMing
     * Date: 2020/12/8
     * Time: 5:38 下午
     */
    public function getSettingField()
    {
        $params = $this->request->getRawJson();
        $fields = isset($params['fields']) ? $params['fields'] : '';
        $modelResult = $this->objMMerchant->getSettingField($fields);
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }

    /**
     * Doc: (des="资金变动记录")
     * User: XMing
     * Date: 2020/12/9
     * Time: 4:44 下午
     * @throws \Exception
     */
    public function log()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            parent::sendOutput('参数为空', ErrorCode::$paramError );
        }
        !empty($this->shopId) && $selectParams['shopId'] = $this->shopId;
        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $selectParams['isPlatform'] = isset($params['isPlatform']) ? $params['isPlatform'] : StatusCode::$standard;
        $objMMerchantFlow = new MMerchantFlow($this->onlineEnterpriseId,$this->onlineUserId);
        $result = $objMMerchantFlow->getAll($selectParams);
        if($result->isSuccess()){
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => $params['page'],
                'pageSize' => $params['pageSize'],
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2020/12/30
     * Time: 5:20 下午
     */
    public function statistics()
    {
        $modelResult = $this->objMMerchant->statistics();
        if (!$modelResult->isSuccess()) {
            parent::sendOutput($modelResult->getData(), $modelResult->getErrorCode());
        }
        parent::sendOutput($modelResult->getData());
    }
}
