<?php
/**
 * Created by PhpStorm.
 * User: Gss
 * Date: 2021/4/7 0007
 * Time: 15:26
 */

namespace JinDouYun\Model\Merchant;

use JinDouYun\Dao\Merchant\DMerchantApply;
use JinDouYun\Dao\Merchant\DMerchantDetail;

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

class MMerchantDetail
{
    private $objDMerchantDetail;
    private $objDMerchantApply;
    private $enterpriseId;
    private $userCenterId;

    public function __construct($enterpriseId, $userCenterId)
    {
        $this->userCenterId = $userCenterId;
        $this->enterpriseId = $enterpriseId;

        $this->objDMerchantDetail = new DMerchantDetail('finance');
        $this->objDMerchantApply = new DMerchantApply();

        $this->objDMerchantDetail->setTable('qianniao_merchant_detail_' . $enterpriseId);
    }

    /*
     * 新增收支记录
     * */
    public function addMerchantDetail($params)
    {
        $beginTransactionStatus = $this->objDMerchantDetail->beginTransaction();
        $merchantDetailId = $this->objDMerchantDetail->insert($params);
        if ($merchantDetailId === false) {
            return ResultWrapper::fail($this->objDMerchantDetail->error(), ErrorCode::$dberror);
        }

        if($beginTransactionStatus){
            $this->objDMerchantDetail->commit();
        }
        return ResultWrapper::success($merchantDetailId);
    }

    /*
     * 所有收支记录
     * */
    public function getAllMerchantDetail($selectParams)
    {
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $offset = $selectParams['offset'];
        unset($selectParams['offset']);

        $returnData = [
            'data'  => [],
            'total' => 0,
        ];

        $whereSql = '';
//        if (isset($selectParams['operatorName']) && !empty($selectParams['operatorName'])) {
//            $where = empty($whereSql) ? ' WHERE ' : ' AND ';
//            $whereSql .= $where . ' operatorName like "%' . $selectParams['operatorName'] . '%"';
//        }
        if (isset($selectParams['merchantId']) && !empty($selectParams['merchantId'])) {
            $where = empty($whereSql) ? ' WHERE ' : ' AND ';
            $whereSql .= $where . ' merchantId = ' . $selectParams['merchantId'];
        }
        if (isset($selectParams['type']) && !empty($selectParams['type'])) {
            $where = empty($whereSql) ? ' WHERE ' : ' AND ';
            $whereSql .= $where . ' type = ' . $selectParams['type'];
        }
        if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
            $where = empty($whereSql) ? ' WHERE ' : '  AND ';
            $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
        }

        $sql = 'SELECT * FROM ' .$this->objDMerchantDetail->get_Table().$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
        $dbResult = $this->objDMerchantDetail->query($sql);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->objDMerchantDetail->error(), ErrorCode::$dberror);
        }
        if(empty($dbResult)){
            return ResultWrapper::success($returnData);
        }
        $totalSql = 'SELECT COUNT(1) as count FROM ' .$this->objDMerchantDetail->get_Table() . $whereSql;
        $dbTotalResult = $this->objDMerchantDetail->query($totalSql);

        if ($dbTotalResult === false) {
            return ResultWrapper::fail($this->objDMerchantDetail->error(), ErrorCode::$dberror);
        }
        if(empty($dbTotalResult)){
            return ResultWrapper::success([]);
        }
        $return = [
            'data'  => $dbResult,
            'total' => $dbTotalResult[0]['count']
        ];
        return ResultWrapper::success($return);

    }



}