<?php
/**
 * 账户明细管理模块
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/10/30
 * Time: 14:02
 */

namespace JinDouYun\Model\Finance;

use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ResultWrapper;
use JinDouYun\Model\MBaseModel;
use JinDouYun\Dao\Finance\DAccountDetail;

class MAccountDetail extends MBaseModel {

    private $objDAccountDetail;
    private $enterpriseId;
    private $userCenterId;
    private $cutTable = 100;

    public function __construct($enterpriseId, $userCenterId)
    {
        $this->userCenterId = $userCenterId;
        $this->enterpriseId = $enterpriseId;
        parent::__construct($enterpriseId, $userCenterId);
        $this->objDAccountDetail = new DAccountDetail('finance');
    }

    /**
     * 添加账户明细
     * @param $params
     * @return ResultWrapper
     * @throws \Exception
     */
    public function addAccountDetail($params)
    {
        $tableName = $this->objDAccountDetail->getTableName('qianniao_account_detail_' . $this->enterpriseId, $params['accountId'], $this->cutTable);
        $this->objDAccountDetail->setTable($tableName);

        $AccountDetailId = $this->objDAccountDetail->insert($params);

        if($AccountDetailId === false){
            return ResultWrapper::fail($this->objDAccountDetail->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($AccountDetailId);
        }
    }

    /**
     * 获取所有账户明细数据
     * @param array $selectParams 过滤条件
     * @return ResultWrapper
     * @throws \Exception
     */
    public function getAllAccountDetail($selectParams)
    {
        $limit = $selectParams['limit'];
        unset($selectParams['limit']);
        $offset = $selectParams['offset'];
        unset($selectParams['offset']);

        $accountId = $selectParams['accountId'];
        unset($selectParams['accountId']);
        $shopId = $selectParams['shopId'];
        unset($selectParams['shopId']);
        $start = $selectParams['start'];
        unset($selectParams['start']);
        $end = $selectParams['end'];
        unset($selectParams['end']);

        $tableName = $this->objDAccountDetail->getTableName('qianniao_account_detail_' . $this->enterpriseId, $accountId, $this->cutTable);
        $this->objDAccountDetail->setTable($tableName);

        $where = "accountId = " . $accountId . ' AND createTime>=' . $start . ' AND createTime<=' . $end;
        if($shopId) {
            $where .= ' And shopId='.$shopId;
        }
        $dbResult = $this->objDAccountDetail->select($where, '*', 'createTime asc', $limit, $offset);

        if($dbResult === false){
            return ResultWrapper::fail($this->objDAccountDetail->error(), ErrorCode::$dberror);
        }

        $total = $this->objDAccountDetail->count($where);

        $return = [
            'data'  => self::format($dbResult),
            'total' => ($total)?intval($total):0,
        ];

        return ResultWrapper::success($return);

    }

    public function format($data) {
        foreach ($data as $k=>$v) {
            $data[$k]['accountCode'] = createCode(StatusCode::$code['account']['prefix'], $v['accountId'], StatusCode::$code['account']['length']);

            switch ($v['financeType']){
                case '线上支付收款':
                case '预存收款':
                case '销售收款':
                case '银行打款收款':
                    $data[$k]['sourceNo'] = StatusCode::$noPrefix[17].'-'.$v['sourceNo'];
                    break;
                case '采购预付':
                case '采购付款':
                    $data[$k]['sourceNo'] = StatusCode::$noPrefix[19].'-'.$v['sourceNo'];
                    break;
                case '退款单退款':
                    $data[$k]['sourceNo'] = StatusCode::$noPrefix[20].'-'.$v['sourceNo'];
                    break;
            }
        }
        return $data;
    }
    
}