<?php
/**
 * 提成提现
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/10/31
 * Time: 15:02
 */
namespace JinDouYun\Controller\Department;

use JinDouYun\Dao\Department\DStaffReflect;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\ResultWrapper;
use Mall\Framework\Core\StatusCode;

use JinDouYun\Cache\ShopCache;
use JinDouYun\Controller\BaseController;
use JinDouYun\Cache\TempSaveCache;

class StaffReflect extends BaseController
{
    private $obj;
    private $objShopCache;
    private $objTempSaveCache;

    public function __construct($isCheckAcl = true, $isMustLogin = true)
    {
        parent::__construct($isCheckAcl, $isMustLogin);
        $this->objShopCache = new ShopCache();
        $this->objTempSaveCache = new TempSaveCache();
    }


    /**
     * 列表
     */
    public function list()
    {
        $params = $this->request->params();

        $pageParams = pageToOffset($params['page'] ?: 1, $params['pageSize'] ?: 10);
        $limit = $pageParams['limit'];
        $offset = $pageParams['offset'];

        $selectParams['a.en_id'] = $this->onlineEnterpriseId;
        if (isset($params['auditStatus']) and !empty($params['auditStatus'])) {
            $selectParams['a.auditStatus'] = $params['auditStatus'];
        }

        if (isset($params['staffId']) and !empty($params['staffId'])) {
            $selectParams['a.staffId'] = $params['staffId'];
        }

        if (isset($params['reflectStatus']) and !empty($params['reflectStatus'])) {
            $selectParams['a.reflectStatus'] = $params['reflectStatus'];
        }

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

        if (isset($params['end']) and !empty($params['end']) && isset($params['start']) and !empty($params['start'])) {
            $selectParams[] = ['a.createTime', '>=', $params['start']];
            $selectParams[] = ['a.createTime', '<=', $params['end']];
        }

        $db = new DStaffReflect('default');
        $join = 'Left join qianniao_staff_'.$this->onlineEnterpriseId.' as b on a.staffId = b.id';
        $list = $db->select($selectParams, 'a.*,b.staffName', 'a.id DESC', $limit, $offset, [], true, false, $join);
        if ($list){
            foreach ($list as &$item) {
                $item['reflectInfo'] = json_decode($item['reflectInfo']);
            }
        }

        $count = $db->count($selectParams);
        $pageData = [
            'pageIndex' => $params['page'],
            'pageSize' => $params['pageSize'],
            'pageTotal' => $count,
        ];
        parent::sendOutput($list, 0, $pageData);
    }


    /**
     * 打款审核
     * @return void
     */
    public function updateStatus()
    {
        $parma = $this->request->getRawJson();
        if (empty($parma['id']))  $this->sendOutput('id参数为空', ErrorCode::$paramError);
        if (empty($parma['status']))  $this->sendOutput('status参数为空', ErrorCode::$paramError);

        $db = new DStaffReflect('default');
        $data = $db->get(['id' => $parma['id']]);
        if (empty($data)) $this->sendOutput('提现记录不存在', ErrorCode::$paramError);

        if ($parma['status'] == 1){
            if ($data['auditStatus'] == 2) $this->sendOutput('提现已审核', ErrorCode::$paramError);
            $res = $db->update(['auditStatus' => 2]);
        }elseif ($parma['status'] == 2){
            $res = $db->update(['reflectStatus' => 3]);
        }elseif ($parma['status'] == 3){
            if ($data['reflectStatus'] == 5) $this->sendOutput('提现已打款', ErrorCode::$paramError);
            if ($data['auditStatus'] != 2) $this->sendOutput('订单未审核', ErrorCode::$paramError);
            $res = $db->update(['reflectStatus' => 5]);
        }
        if ($res) parent::sendOutput('成功');
        parent::sendOutput('失败', ErrorCode::$paramError);

    }
}