<?php

namespace app\user\controller;

use app\user\model\UserScore as UserScoreModel;
use fastphp\base\Controller;

class UserScore extends Controller
{
    public function export()
    {
        
        $data = $_GET;
        
        $where = [];
        $where_str = '';
        if (!empty($data)) {
            if (!empty($data['st'])) {
                $date1 = date('Y-m-d', strtotime($data['st']));
                $date2 = date('Y-m-d', strtotime($data['et']));
                $where_str = " CreateTime>='" . $date1 . "' and CreateTime<='" . $date2 . "' ";
            }
        }
        $user_score_model = new UserScoreModel;

        $list = $user_score_model->field('UpdateTime,UserName,OperateUserName,ScoreTypeName,RestAmount1,ScoreValue,RestAmount2,Remark')->where($where)->where($where_str)->fetchAll();;
        
        \exportToCsv('test.csv',['创建时间','会员号','操作人员','资金项目','操作前余额','金额','操作后余额','备注'],$list);
    }

    public function get_all_list() {
        $data = $_GET;
        $page = 0;
        if (isset($data['PageIndex'])) {
            $page = ($data['PageIndex'] - 1) * 50;
        }
        $where_str = '';
        if (!empty($data)) {
            if (!empty($data['st'])) {
                $date1 = date('Y-m-d', strtotime($data['st']));
                $date2 = date('Y-m-d', strtotime($data['et']));
                $where_str = " CreateTime>='" . $date1 . "' and CreateTime<='" . $date2 . "' ";
            }
            
            if (!empty($data['type'])) {
                if (!empty($where_str)) {
                    $where_str .= ' AND ';
                }
                $where_str .= " ScoreType='" . $data['type'] . "' ";
            }
            
            if (!empty($data['keyWord'])) {
                if (!empty($where_str)) {
                    $where_str .= ' AND ';
                }
                $where_str .= " UserName='" . $data['keyWord'] . "' ";
            }
        }
        $user_score_model = new UserScoreModel;
        
        $list = $user_score_model->field('*')->where($where_str)->order('Id desc')->limit("$page,50")->fetchAll();;
        
        echo json_encode([
            'Code' => 10000,
            'Data' => $list,
            'TotalCount' => (int)$user_score_model->getCount($where_str)['count'],
        ]);
    }

    /**
     * @description: 获取资金明细列表
     * @param {*}
     * @return {*}
     */    
    public function getList() {
        $data = json_decode(file_get_contents("php://input"),true);
        $page = 0;
        if (isset($data['page'])) {
            $page = ($data['page'] - 1) * 50;
        }
        $user_score_model = new UserScoreModel;
        $list = $user_score_model->getListPage(['UserId'=>$this->userinfo['UserId']],'*','id desc',"$page,50");
        echo json_encode($list);
    }
}
