<?php


namespace JinDouYun\Controller\Purchase;


use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Purchase\MSupplierWithdraw;
use Mall\Framework\Core\ErrorCode;

/**
 * @copyright   Copyright (c) https://www.qianniaovip.com All rights reserved
 * Description:
 * Class SupplierWithdrawal
 * @package JinDouYun\Controller\Purchase
 */
class SupplierWithdrawal extends BaseController
{
    private $objMSupplierWithdraw;

    public function __construct($isCheckAcl = true, $isMustLogin = false, $checkToken = true, $getAreaCode = false, $checkShopToken = true, $checkSupplierToken = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin, $checkToken, $getAreaCode, $checkShopToken, $checkSupplierToken);
        $this->objMSupplierWithdraw = new MSupplierWithdraw($this->onlineUserId,$this->onlineEnterpriseId);
    }

    /**
     * Doc: (des="")
     * User: XMing
     * Date: 2020/12/18
     * Time: 6:00 下午
     */
    public function add()
    {
        $paramsData = $this->request->getRawJson();
        $params = [
            'type'     => isset($paramsData['type']) ? $paramsData['type'] : '',
            'accountContent' => isset($paramsData['accountContent']) ? $paramsData['accountContent'] : '',
            'money' => isset($paramsData['money']) ? $paramsData['money'] : '',
            //'nowMoney' => isset($paramsData['nowMoney']) ? $paramsData['nowMoney'] : '',
            'createTime' => time(),
            'updateTime' => time(),
        ];
        foreach ($params as $k => $v) {
            if (empty($v) && $v !== 0) {
                parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
            }
        }
        $result = $this->objMSupplierWithdraw->add($params,$this->supplierId);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), $result->getErrorCode());
    }

    /**
     * Doc: (des="提现记录")
     * User: XMing
     * Date: 2020/12/8
     * Time: 3:54 下午
     */
    public function getAll()
    {
        $paramsData = (array)$this->request->getRawJson();
        $selectParams = [
            'page'     => isset($paramsData['page']) ? $paramsData['page'] : 1,
            'pageSize' => isset($paramsData['pageSize']) ? $paramsData['pageSize'] : 10,
            'startTime' => isset($paramsData['startTime']) ? $paramsData['startTime'] : 0,
            'endTime' => isset($paramsData['endTime']) ? $paramsData['endTime'] : 0,
            'supplierId' => getArrayItem($paramsData,'supplierId',0),
        ];
        foreach ($selectParams as $k => $v) {
            if (empty($v) && $v != 0) {
                parent::sendOutput($k . '参数错误', ErrorCode::$paramError);
            }
        }
        isset($paramsData['type']) && $selectParams['type'] = $paramsData['type'];
        (isset($paramsData['auditStatus']) && !empty($paramsData['auditStatus'])) && $selectParams['auditStatus'] = $paramsData['auditStatus'];
        $pageParams = pageToOffset($selectParams['page'], $selectParams['pageSize']);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        !empty($this->supplierId) && $selectParams['supplierId'] = $this->supplierId;
        $result = $this->objMSupplierWithdraw->getAll($selectParams);
        if ($result->isSuccess()) {
            $returnData = $result->getData();
            $pageData = [
                'pageIndex' => getArrayItem($paramsData,'page',1),
                'pageSize'  => getArrayItem($paramsData,'pageSize',10),
                'pageTotal' => $returnData['total'],
            ];
            parent::sendOutput($returnData['data'], 0, $pageData);
        }
        parent::sendOutput($result->getData(), ErrorCode::$dberror);
    }

    /**
     * Doc: (des="更新提现记录状态")
     * User: XMing
     * Date: 2020/12/8
     * Time: 3:55 下午
     * @throws \Exception
     */
    public function updateAuditStatus()
    {
        $id = $this->request->param('request_id');
        if (!$id) {
            parent::sendOutput('请指定要审核的数据id', ErrorCode::$paramError);
        }
        $status = $this->request->param('status');
        $reason = $this->request->param('reason');
        if(!$status){
            parent::sendOutput('请指定要修改的状态', ErrorCode::$paramError);
        }
        $result = $this->objMSupplierWithdraw->updateAuditStatus($id,$status,$reason);
        if ($result->isSuccess()) {
            parent::sendOutput($result->getData());
        }
        parent::sendOutput($result->getData(), ErrorCode::$dberror);
    }

}