<?php
/**
 * 我的账户控制器
 */
namespace app\modules\engineer\v1\controllers;
use app\modules\common\Helper;
use app\modules\common\Verify;
use app\modules\logic\services\UserService;
use app\modules\logic\services\AccountService;
use app\modules\logic\services\WxBindService;
use app\modules\logic\model\NoticeModel;
use app\modules\logic\model\OrderModel;
use app\modules\logic\model\AccountCashModel;
use app\modules\logic\model\AccountLogModel;
use app\modules\logic\model\UsersModel;
use Yii;
use yii\web\Controller;

class AccountController extends MiddleController {
	/**
	 * 返回
	 * @param $code
	 * @param string $info
	 * @param string $controller
	 * @param string $total
	 */
	protected function response($code, $info = '', $total = '', $controller = 'account') {
		return parent::response($code, $info, $total, $controller);
	}
	/**
	 * [actionMain 获取用户账户明细]
	 * @apiDoc
	 * @api    v1/account/main
	 * @group  account
	 * @name   账户详情
	 * @desc   技师APP-账户详情
	 * @method POST
	 * @return json
		{
		"code": "000000",
		"msg": "获取成功",
		"info": {
		"account": {
		    "total_amount": "0.00",
		    "frost_amount": "0.00",
		    "use_amount": "0.00",
		    "total_gold": "0.00",
		    "user_id": "1"
		},
		"settle_money": "18.00",
		"bind_wx_info": {
		    "wx_openid": "",
		    "wx_nickname": ""
		}
		}
		}
	 */
	public function actionMain(){
		if (empty($this->user_id)) {
            return $this->response('para_miss_user', '未能识别用户标志');
        }
        $user_id = $this->user_id;
        $account_info = AccountService::accountMoney($user_id);
        if(isset($account_info['credit'])) unset($account_info['credit']);
        //计算结算中金额
        $Order = new OrderModel();
        $settle_money = $Order->find()->where(['settle_status'=>0,'re_user_id'=>$user_id])->sum('tech_price');
        $account_info['settle_money'] = !empty($settle_money)?$settle_money:0;
        $Users = new UsersModel();
        $res = $Users->getWidgetRow(['cols'=>['wx_nick','wx_openid'],'user_id'=>$user_id]);
        //$bind_service = new WxBindService($this->user_id);
        $account_info['bind_wx_info'] = [
        	'wx_openid'=>empty($res['wx_openid']) ? '' : $res['wx_openid'],
        	'wx_nickname'=>empty($res['wx_nick']) ? '' : $res['wx_nick'],
        ];
        return $this->response('deal_succ', '获取成功', $account_info);
	}

	/**
	 * [actionCash 提现操作]
	 * @apiDoc
	 * @api    v1/account/cash
	 * @group  account
	 * @name   提现
	 * @desc   技师APP->提现
	 * @method POST
	 * @param money string 提现金额 req
	 * @return json
	 * {
     * "code": "000000",
     * "msg": "您的提现申请已经成功提交，预计1~3个工作日到账",
     * "info": ""
     * }
	 */
	public function actionCash(){
		if (empty($this->user_id)) {
            return $this->response('para_miss_user', '用户id为空');
        }
        $user_id = $this->user_id;
        if (!isset($this->para['money']) || empty($this->para['money']) || !is_numeric($this->para['money'])) {
            return $this->response('para_miss', '提现金额不正确');
        }
        
        $account_info = AccountService::accountMoney($user_id);
        $money = $this->para['money'];
        if ($money > $account_info['account']['use_amount']) {
            return $this->response('para_error', '用户可提现金额不足');
        }
		//$bind_service = new WxBindService($this->user_id);
		$Users = new UsersModel();
        $res = $Users->getWidgetRow(['cols'=>['wx_nick','wx_openid'],'user_id'=>$user_id]);
		if (empty($res['wx_openid'])) {
		    return $this->response('para_miss', '微信未绑定');
		}
        $order_nid = "C" . Helper::GenCashOrderNid();
        $cash_fee = 0;
        $data_new['user_id'] = $user_id;
        $data_new['nid'] = $order_nid;
        $data_new['status'] = 0;
        $data_new['total'] = $money;
        $data_new['money'] = $money - $cash_fee;
        $data_new['fee'] = $cash_fee;
        $data_new['wx_openid'] = empty($res['wx_openid']) ? '' : $res['wx_openid'];
        $data_new['wx_nickname'] = empty($res['wx_nick']) ? '' : $res['wx_nick'];
        $data_new['addip'] = Helper::getUserIp();
        $data_new['add_time'] = time();//app端;
        $data_new['cash_type'] = 1;
        $remark = "申请提现,到账" . $data_new['money'] . "元,手续费" . $cash_fee . "元，";
        $cash_result = AccountService::addUserCash($data_new, $remark);
        if ($cash_result > 0) {
            return $this->response('deal_succ', '您的提现申请已经成功提交，预计1~3个工作日到账');
        } else {
            return $this->response('server_err', '您的提现失败，请重新操作');
        }
	}
	/**
	 * [actionCashlist 提现明细]
	 * @apiDoc
	 * @api    v1/account/cashlist
	 * @group  account
	 * @name   提现明细
	 * @desc   技师APP-提现列表
	 * @method POST
	 * @param currentPage string 当前页 noreq 1
	 * @param pageSize string 每页条数 noreq 15
	 * @return json
		{
		"code": "000000",
		"msg": "获取成功",
		"info": [
		{
		    "id": "1",
		    "money": "100.00",
		    "total": "100.00",
		    "fee": "0.00",
		    "add_time": "2019-09-25 11:50:28",
		    "status": "处理中"
		}
		]
		}
	 */
	public function actionCashlist(){
		
		if (empty($this->user_id)) {
            return $this->response('para_miss_user', '用户id为空');
        }
        $user_id = $this->user_id;
        $currentPage = 1;
		$pageSize = 15;
		if (isset($this->para['currentPage']) && !empty($this->para['currentPage']) && is_numeric($this->para['currentPage'])) {
			$currentPage = $this->para['currentPage'];
		}
		if (isset($this->para['pageSize']) && !empty($this->para['pageSize']) && is_numeric($this->para['pageSize'])) {
			$pageSize = $this->para['pageSize'];
		}

        $AccountCash = new AccountCashModel();
        $_where = ['cols'=>['id','money','total','fee','add_time','status'],'user_id'=>$user_id];
        $list = $AccountCash->getWidgetPages($_where, $pageSize, $pageSize * ($currentPage - 1));
        if (!empty($list)) {
			foreach ($list as &$value) {
				$value['add_time'] = date("Y-m-d H:i:s", $value['add_time']);
				$value['status'] = Helper::getCashStatusByKey($value['status']);
			}
		}
		return $this->response('deal_succ', '获取成功', $list);
	}
	/**
	 * [actionMoneylist 收支明细]
	 * @apiDoc
	 * @api    v1/account/moneylist
	 * @group  account
	 * @name   收支明细
	 * @desc   技师APP-收支明细
	 * @method POST
	 * @return json
		{
		"code": "000000",
		"msg": "获取成功",
		"info": [
		{
		    "money": "-0.01",
		    "remark": "支付订单",
		    "nid": "201909132334265050535098426",
		    "created": "2019-09-23 19:24:58",
		    "op_type": "pay_order"
		},
		{
		    "money": "-0.01",
		    "remark": "支付订单",
		    "nid": "201909132334265050535098426",
		    "created": "2019-09-23 19:26:36",
		    "op_type": "pay_order"
		},
		{
		    "money": "-0.01",
		    "remark": "支付订单",
		    "nid": "201909132334265050535098426",
		    "created": "2019-09-23 19:33:23",
		    "op_type": "pay_order"
		}
		]
		}
	 */
	public function actionMoneylist(){
		if (empty($this->user_id)) {
            return $this->response('para_miss_user', '用户id为空');
        }
        $user_id = $this->user_id;
        $currentPage = 1;
		$pageSize = 15;
		if (isset($this->para['currentPage']) && !empty($this->para['currentPage']) && is_numeric($this->para['currentPage'])) {
			$currentPage = $this->para['currentPage'];
		}
		if (isset($this->para['pageSize']) && !empty($this->para['pageSize']) && is_numeric($this->para['pageSize'])) {
			$pageSize = $this->para['pageSize'];
		}

        $AccountLog = new AccountLogModel();
        $_where = ['cols'=>['money','remark','nid','created','op_type'],'user_id'=>$user_id,'money_type'=>'money'];
        $list = $AccountLog->getWidgetPages($_where, $pageSize, $pageSize * ($currentPage - 1));
        if (!empty($list)) {
			foreach ($list as &$value) {
				$value['created'] = date("Y-m-d H:i:s", $value['created']);
			}
		}
		return $this->response('deal_succ', '获取成功', $list);
	}
	/**
	 * [actionCashdt 提现提现详情]
	 * @apiDoc
	 * @api    v1/account/cashdt
	 * @group  account
	 * @name   提现详情
	 * @desc   技师APP-提现详情
	 * @method POST
	 * @param id int 提现ID req
	 * @return json
		{
		"code": "000000",
		"msg": "获取成功",
		"info": {
		"id": "1",
		"money": "100.00",
		"total": "100.00",
		"fee": "0.00",
		"add_time": "2019-09-25 11:50:28",
		"status": "处理中",
		"verify_time": "1970-01-01 08:00:00",
		"wx_nickname": "bbb",
		"nid": "xxx"
		}
		}
	 */
	public function actionCashdt(){
		if (empty($this->user_id)) {
            return $this->response('para_miss_user', '用户id为空');
        }
        if (empty($this->para['id']) || !is_numeric($this->para['id'])) {
            return $this->response('para_miss', 'ID不能为空');
        }
        $id = $this->para['id'];
        $AccountCash = new AccountCashModel();
        $row = $AccountCash->getWidgetRow(['cols'=>['id','money','total','fee','add_time','status','verify_time','wx_nickname','nid'],'id'=>$id]);
        if($row){
        	$row['add_time'] =  date("Y-m-d H:i:s", $row['add_time']);
        	$row['verify_time'] =  isset($row['verify_time'])?date("Y-m-d H:i:s", $row['verify_time']):'-';
        	$row['status'] =  Helper::getCashStatusByKey($row['status']);
        	return $this->response('deal_succ', '获取成功', $row);
        }else{
        	return $this->response('deal_false',"参数错误");
        }
	}
}