<?php

namespace App\Http\Controllers\API\Admin\YeWu;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class DeleteOperationLogController extends Controller
{
    public function GetList(Request $request)
    {
        $page = request('page');
        $pageSize = request('pageSize');
        $searchInfo = request('searchInfo');

        $query = DB::table('delete_operation_log');

        if (!empty($searchInfo['name'])) {
            $query->where('name', 'like', '%' . $searchInfo['name'] . '%');
        }
        if (!empty($searchInfo['id_card_num'])) {
            $encrypt = \App\Lib\HSM::HsmEncrypt($searchInfo['id_card_num']);
            if ($encrypt['status']) {
                $query->where('id_card_num', $encrypt['data']);
            }
        }
        if (!empty($searchInfo['operator_name'])) {
            $query->where('operator_name', 'like', '%' . $searchInfo['operator_name'] . '%');
        }
        if (!empty($searchInfo['table_name'])) {
            $query->where('table_name', $searchInfo['table_name']);
        }
        if (!empty($searchInfo['type'])) {
            $query->where('type', $searchInfo['type']);
        }
        if (!empty($searchInfo['dateRange'])) {
            $query->whereBetween('created_at', [$searchInfo['dateRange'][0] . ' 00:00:00', $searchInfo['dateRange'][1] . ' 23:59:59']);
        }
        if (!empty($searchInfo['org_id'])) {
            $org = DB::table('medical_institution')->where('id', $searchInfo['org_id'])->first();
            if ($org) {
                $query->where('current_org_name', $org->org_name);
            }
        }

        $count = $query->count();
        $list = $query->orderBy('id', 'desc')
            ->offset(($page - 1) * $pageSize)
            ->limit($pageSize)
            ->get();

        foreach ($list as $item) {
            if (!empty($item->id_card_num)) {
                $decrypt = \App\Lib\HSM::HsmDecrypt($item->id_card_num);
                if ($decrypt['status']) {
                    $item->id_card_num = $decrypt['data'];
                }
            }
        }

        return \Yz::Return(true, '查询成功', ['list' => $list, 'count' => $count]);
    }

    public function ExportList(Request $request)
    {
        $searchInfo = request('searchInfo');

        $query = DB::table('delete_operation_log');

        if (!empty($searchInfo['name'])) {
            $query->where('name', 'like', '%' . $searchInfo['name'] . '%');
        }
        if (!empty($searchInfo['id_card_num'])) {
            $encrypt = \App\Lib\HSM::HsmEncrypt($searchInfo['id_card_num']);
            if ($encrypt['status']) {
                $query->where('id_card_num', $encrypt['data']);
            }
        }
        if (!empty($searchInfo['operator_name'])) {
            $query->where('operator_name', 'like', '%' . $searchInfo['operator_name'] . '%');
        }
        if (!empty($searchInfo['table_name'])) {
            $query->where('table_name', $searchInfo['table_name']);
        }
        if (!empty($searchInfo['type'])) {
            $query->where('type', $searchInfo['type']);
        }
        if (!empty($searchInfo['dateRange'])) {
            $query->whereBetween('created_at', [$searchInfo['dateRange'][0] . ' 00:00:00', $searchInfo['dateRange'][1] . ' 23:59:59']);
        }
        if (!empty($searchInfo['org_id'])) {
            $org = DB::table('medical_institution')->where('id', $searchInfo['org_id'])->first();
            if ($org) {
                $query->where('current_org_name', $org->org_name);
            }
        }

        $list = $query->orderBy('id', 'desc')->get();

        foreach ($list as $item) {
            if (!empty($item->id_card_num)) {
                $decrypt = \App\Lib\HSM::HsmDecrypt($item->id_card_num);
                if ($decrypt['status']) {
                    $item->id_card_num = $decrypt['data'];
                }
            }
        }

        return \Yz::Return(true, '查询成功', ['list' => $list]);
    }
}
