<?php

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

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

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

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

        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['current_org_name'])) {
            $query->where('current_org_name', 'like', '%' . $searchInfo['current_org_name'] . '%');
        }
        if (!empty($searchInfo['type'])) {
            $query->where('type', $searchInfo['type']);
        }
        if (!empty($searchInfo['source'])) {
            $query->where('source', $searchInfo['source']);
        }
        if (!empty($searchInfo['dateRange'])) {
            $query->whereBetween('created_at', [$searchInfo['dateRange'][0] . ' 00:00:00', $searchInfo['dateRange'][1] . ' 23:59:59']);
        }

        $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('conflict_record');

        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['current_org_name'])) {
            $query->where('current_org_name', 'like', '%' . $searchInfo['current_org_name'] . '%');
        }
        if (!empty($searchInfo['type'])) {
            $query->where('type', $searchInfo['type']);
        }
        if (!empty($searchInfo['source'])) {
            $query->where('source', $searchInfo['source']);
        }
        if (!empty($searchInfo['dateRange'])) {
            $query->whereBetween('created_at', [$searchInfo['dateRange'][0] . ' 00:00:00', $searchInfo['dateRange'][1] . ' 23:59:59']);
        }

        $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]);
    }
}
