<?php
/**
 * 应付单管理模块
 * Created by PhpStorm.
 * User: tpl
 * Date: 2019/10/30
 * Time: 13:54
 */
namespace JinDouYun\Controller\Finance;

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

use JinDouYun\Controller\BaseController;
use JinDouYun\Model\Finance\MPay;

class Pay extends BaseController
{
    private $objMPay;

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

    /**
     * 添加和编辑应付单管理公共字段处理方法
     *
     * @return array
     */
    public function commonFieldFilter(){

        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $PayData = [
              'supplierId'=> isset($params['supplierId']) ? $params['supplierId'] : '',//int(11) DEFAULT NULL COMMENT '客户Id',
              'supplierName'=> isset($params['supplierName']) ? $params['supplierName'] : '',//varchar(50) DEFAULT NULL COMMENT '客户名称',
              'sourceNo'=> isset($params['sourceNo']) ? $params['sourceNo'] : '',//varchar(50) DEFAULT NULL COMMENT '订单号',
              'financeTypeId'=> isset($params['financeTypeId']) ? $params['financeTypeId'] : '',//int(10) DEFAULT NULL COMMENT '财务类型id',
              'financeType'=> isset($params['financeType']) ? $params['financeType'] : '',//int(10) DEFAULT NULL COMMENT '财务类型id',
              'discountMoney'=> isset($params['discountMoney']) ? $params['discountMoney'] : '',//float(10,2) DEFAULT NULL COMMENT '优惠金额',
              'PayMoney'=> isset($params['PayMoney']) ? $params['PayMoney'] : '',//float(10,2) DEFAULT NULL COMMENT '实际应付金额',
              'shopId'=> isset($params['shopId']) ? $params['shopId'] : '',//int(11) DEFAULT NULL COMMENT '商铺Id',
              'shopName'=> isset($params['shopName']) ? $params['shopName'] : '',//varchar(50) DEFAULT NULL COMMENT '商铺名称',
                'offsetStatus' => getArrayItem($params, 'offsetStatus',''),
              'auditStatus'=> isset($params['auditStatus']) ? $params['auditStatus'] : '',//tinyint(2) DEFAULT NULL COMMENT '单据状态',
              'receiptTypeId'=> isset($params['receiptTypeId']) ? $params['receiptTypeId'] : '',//tinyint(2) DEFAULT NULL COMMENT '单据状态',
        ];

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

    /**
     * 添加应付单
     */
    public function addPay()
    {
        $PayData = $this->commonFieldFilter();

        $result = $this->objMPay ->addPay($PayData);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }

    }

    /**
     * 获取指定应付单信息
     */
    public function getPayInfo()
    {
        $params = $this->request->getRawJson();
        $where = [];
        if(!empty($params['id'])) $where['id'] = $params['id'];
        if(!empty($params['createTime'])) $where['createTime'] = $params['createTime'];
        if(!empty($params['purchaseId'])) $where['purchaseId'] = $params['purchaseId'];

        if(empty($where)){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $result = $this->objMPay->getPayInfo($params);

        if($result->isSuccess()){
            $this->sendOutput($result->getData());
        }else{
            $this->sendOutput($result->getData(), $result->getErrorCode());
        }
    }

    /**
     * 应付单审核
     */
    public function updatePayStatus()
    {
        $params = $this->request->getRawJson();
        if( empty($params['id']) || empty($params['createTime'])){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $result = $this->objMPay->updatePayStatus($params);
        if($result->isSuccess()){
            parent::sendOutput($result->getData());
        }else{
            parent::sendOutput($result->getData(), $result->getErrorCode());
        }
    }


    /**
     * 后台所有应付单列表
     */
    public function getAllPay()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        if(isset($params['supplierId'])) {
            $selectParams['supplierId'] = $params['supplierId'];
        }
        if(isset($params['payReceiptIds'])) {
            $selectParams['payReceiptIds'] = $params['payReceiptIds'];
        }
        if(isset($params['offsetStatus'])) {
            $selectParams['offsetStatus'] = $params['offsetStatus'];
        }
        if(isset($params['auditStatus'])) {
            $selectParams['auditStatus'] = $params['auditStatus'];
        }
        if(isset($params['warehouseId'])) {
            $selectParams['warehouseId'] = $params['warehouseId'];
        }
        if(isset($params['sourceNo'])) {
            $selectParams['sourceNo'] = $params['sourceNo'];
        }
        if(isset($params['start'])) {
            $selectParams['start'] = $params['start'];
        }
        if(isset($params['end'])) {
            $selectParams['end'] = $params['end'];
        }

        $isExport = false;
        if(isset($params['isExport'])) {
            $isExport = $params['isExport'];
        }

        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $result = $this->objMPay->getAllPay($selectParams, $isExport);

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

    /**
     * 搜索
     */
    /*
    public function search()
    {
        $params = $this->request->getRawJson();
        if( empty($params) ){
            $this->sendOutput('参数为空', ErrorCode::$paramError );
        }

        $selectParams = [
            'keyword' => isset($params['keyword']) ? $params['keyword'] : '',
            'shopId' => isset($params['shopId']) ? $params['shopId'] : '',
            'start' => isset($params['start']) ? $params['start'] : '',
            'end' => isset($params['end']) ? $params['end'] : '',
            'receiptTypeId' => isset($params['receiptTypeId']) ? $params['receiptTypeId'] : '',
            'auditStatus' => isset($params['auditStatus']) ? $params['auditStatus'] : '',
            'isExport' => isset($params['isExport']) ? $params['isExport'] : '',
        ];

        $pageParams = pageToOffset($params['page']?:1, $params['pageSize']?:10);
        $selectParams['limit'] = $pageParams['limit'];
        $selectParams['offset'] = $pageParams['offset'];
        $result = $this->objMPay->search($selectParams);

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