<?php

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

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\Admin\YeWu\CheckItemService;
use Illuminate\Support\Facades\DB;

class CheckItemController extends Controller
{
    //获取检查项目类别列表
    public function GetClassList()
    {
        $searchInfo = request('searchInfo');
        $s = new CheckItemService;
        return $s->GetClassList($searchInfo);
    }

    //获取检查项目列表
    public function GetItemList()
    {
        $page = request('page');
        $pageSize = request('pageSize');
        $searchInfo = request('searchInfo');
        $s = new CheckItemService;
        return $s->GetItemList($searchInfo, $page, $pageSize);
    }

    //绑定设备
    public function BindDevice()
    {
        $item_id = request('item_id');
        $device_ids = request('device_ids');
        $s = new CheckItemService;
        return $s->BindDevice($item_id, $device_ids);
    }

    //保存检查项目信息
    public function Save()
    {
        $Info = request('Info');
        $s = new CheckItemService;
        return $s->Save($Info);
    }

    //设置互斥
    public function SetHuChi(Request $request)
    {
        $userid = $request->get('userid');//中间件产生的参数
        $id = request('id');
        $code1 = request('code1');
        $code2 = request('code2');
        $time = request('time');
        if(!isset($time)) return \Yz::echoError1("时间不能为空");
        $i=null;
        $u=null;
        if($id==0){
            $q = DB::table('s_huchi')
                ->where('is_del', 0)
                ->where(function ($query) use ($code1, $code2) {
                    $query->where('code1', $code1)->where('code2', $code2);
                })
                ->orWhere(function ($query) use ($code1, $code2) {
                    $query->where('code1', $code2)->where('code2', $code1);
                })
                ->get();

            if (count($q) > 0) return \Yz::echoError1("此条互斥已存在，请删除后再添加！");
            $i = DB::table('s_huchi')->insertGetId([
                'code1' => $code1,
                'code2' => $code2,
                'time' => $time,
                'is_del' => 0,
                'add_user' => $userid,
            ]);
        }else{

            $u=DB::table('s_huchi')->where(['id'=>$id,'is_del'=>0])->update([
               'time'=>$time
            ]);
            if($u){
                $i= $id;
            }
        }



        if ($i) {
            return \Yz::Return(true, '设置成功', ['code2'=>$code2,'id'=>$i]);
        } else {
            return \Yz::echoError1("操作失败");
        }

    }

    public function DelHuChi()
    {
        $id = request('id');
        $u = DB::table('s_huchi')
            ->where('id', $id)
            ->update([
                'is_del' => 1
            ]);

        if ($u) {
            return \Yz::Return(true, '操作成功', []);
        } else {
            return \Yz::echoError1("操作失败");
        }
    }

    //获取某个项目已经设置的互斥列表
    public function GetHuChiList()
    {
        $code = request('code');
         $q=DB::select("SELECT
    s_huchi.code1,
    COALESCE(ci1.item_name, '未找到') AS code1_item_name,
    s_huchi.code2,
    COALESCE(ci2.item_name, '未找到') AS code2_item_name,time,s_huchi.id
FROM
    s_huchi
LEFT JOIN
    s_check_item AS ci1
ON
    s_huchi.code1 = ci1.item_code
LEFT JOIN
    s_check_item AS ci2
ON
    s_huchi.code2 = ci2.item_code
WHERE
    s_huchi.is_del = 0 AND
    (s_huchi.code1 = ? OR s_huchi.code2 = ?)",[$code,$code]);
        foreach ($q as $key=> $row) {
            $r=[];
            if ($row->code1 == $code) {
                $r=$row;
            } else if ($row->code2 == $code) {
                $jsonString = json_encode($row);
                $array = json_decode($jsonString, true);
                $r=$array;
                $r['code1_item_name'] = $array['code2_item_name'];
                $r['code2_item_name'] = $array['code1_item_name'];
                $r['code1'] =  $array['code2'];
                $r['code2'] =  $array['code1'];

            }
            $q[$key]=$r;
        }
        return \Yz::Return(true, '操作成功', $q);

    }
}
