<?php

namespace app\middleapi\controller;

use app\common\enum\user\AccountLogEnum;
use app\common\model\user\User;
use app\common\model\user\UserAccountLog;
use app\common\model\user\UserRole;
use app\common\service\FileService;
use app\common\controller\BaseLikeAdminController;

/***
 * 账户流水控制器
 * Class AccountLogController
 * @package app\adminapi\controller
 */
class AccountLogController extends BaseLikeAdminController
{


    /**
     * @notes 账户流水明细
     * @return \think\response\Json
     * @author 段誉
     * @date 2023/2/24 15:25
     */
    public function lists()
    {
        if(!$this->request->isPost()){
            return $this->fail('请求方式错误');
        }
        $params = $this->request->post(['page_no','page_size', 'start_time', 'end_time', 'user_info', 'type', 'change_type', 'company_id']);
        $where = [];
        if (isset($params['type']) && $params['type'] == 'um') {
            $where[] = ['change_type', 'in', AccountLogEnum::getUserMoneyChangeType()];
        }
        if (!empty($params['change_type'])) {
            $where[] = ['change_type', '=', $params['change_type']];
        }
        if (!empty($params['company_id'])) {
            $where[] = ['company_id', '=', $params['company_id']];
        }
        if (!empty($params['start_time'])) {
            $where[] = ['create_time', '>=', strtotime($params['start_time'])];
        }
        if (!empty($params['end_time'])) {
            $where[] = ['create_time', '<=', strtotime($params['end_time'])];
        }
        $pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
        $pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
        $lists = UserAccountLog::where($where)
            ->with(['company_info','user_info'])
            ->order('id', 'desc')
            ->page($pageNo, $pageSize)
            ->select()
            ->toArray();
        foreach ($lists as &$item) {
            $item['user_info']['avatar'] = FileService::getFileUrl($item['user_info']['avatar']);
            $item['user_info']['group_name']=UserRole::where('id',$item['user_info']['group_id'])->value('name'); 
            $item['change_type_desc'] = AccountLogEnum::getChangeTypeDesc($item['change_type']);
            $symbol = $item['action'] == AccountLogEnum::INC ? '+' : '-';
            $item['change_amount'] = $symbol . $item['change_amount'];
            // 用于导出
            $item['user_info_sn'] = $item['user_info']['sn'];
            $item['company_info_company_name'] = $item['company_info']['company_name'];
            $item['user_info_nickname'] = $item['user_info']['nickname'];
            $item['user_info_group_name'] = $item['user_info']['group_name'];
            $item['user_info_mobile'] = $item['user_info']['mobile'];
        }
        $count = UserAccountLog::where($where)->count();
        $result = [
            'lists' => $lists,
            'count' => $count,
            'page_no' => $pageNo,
            'page_size' => $pageSize
        ];
        return $this->success('请求成功',$result);
    }


    /**
     * @notes 用户余额变动类型
     * @return \think\response\Json
     * @author 段誉
     * @date 2023/2/24 15:25
     */
    public function getUmChangeType()
    {
        return $this->data(AccountLogEnum::getUserMoneyChangeTypeDesc());
    }

    public function getCompanyUserList()
    {
        $param = $this->request->param();
        $companyId = $param['company_id'] ?? 0;
        $userList = User::where(['company_id' => $companyId])->field('id,nickname,avatar,sn,mobile,group_id,deposit,income')->select()->toArray();
        return $this->success('成功', $userList);
    }

}