<?php
namespace app\adminVue\controller;
use think\facade\Db;
use app\mobile\service\PositionDevice;

class V2MachineCheck extends V2Base
{
    private function run($work) {try{return json(['code'=>0,'data'=>$work()]);}catch(\InvalidArgumentException $e){$this->fail($e->getMessage());}}
    private function binding() {return hash('sha256',$this->requestToken());}
    private function manager() {return (int)($this->actor['rightid']??0)>98;}
    private function admin() {if(!$this->manager())$this->fail('仅管理员可同步点检基础数据',403);}
    public function options() {return $this->run(function(){return [
        'canSync'=>$this->manager(),'latest'=>V2MachineSync::latest(),
        'projects'=>Db::name('team_base')->where('mid',1)->field('id,title')->order('id')->select()->toArray(),
        'lines'=>Db::name('team_base')->where('mid',2)->field('id,title,mark')->order('id')->select()->toArray(),
        'models'=>Db::name('me_base')->where('mid',2)->field('id,title,mark,status')->order('id')->select()->toArray(),
        'categories'=>Db::name('me_base')->where('mid',1)->field('id,title')->order('id')->select()->toArray(),
    ];});}
    public function index() {return $this->run(function(){
        $kind=$this->request->get('kind','devices');if(!in_array($kind,['devices','clauses'],true))throw new \InvalidArgumentException('数据类型无效');
        $table=V2MachineSync::TABLES[$kind];$q=Db::name($table);$page=max(1,(int)$this->request->get('page',1));$limit=max(1,min(100,(int)$this->request->get('limit',30)));
        $kw=$this->request->get('keyword','');if(!is_string($kw)||mb_strlen($kw)>100)throw new \InvalidArgumentException('关键字最多100字');
        foreach(['status','type','project','line','is_check'] as $f){if($kind==='clauses'&&!in_array($f,['status','type'],true))continue;$v=$this->request->get($f,'');if($v==='')continue;if(!is_scalar($v)||!ctype_digit((string)$v))throw new \InvalidArgumentException('筛选条件无效');if($f==='is_check'&&(int)$v===0)$q->whereIn($f,[0,2]);else $q->where($f,(int)$v);}
        if(trim($kw)!=='')$q->whereLike($kind==='devices'?'m_name|fixed_number':'content|method|standard','%'.addcslashes(trim($kw),'%_\\').'%');
        return ['total'=>(clone $q)->count(),'list'=>$q->order($kind==='clauses'?'type,order,id':'id desc')->limit(($page-1)*$limit,$limit)->select()->toArray()];
    });}
    public function upload() {$this->admin();return $this->run(function(){
        @set_time_limit(180);$file=$this->request->file('file');if(!$file||strtolower($file->getOriginalExtension())!=='xlsx'||$file->getSize()>10*1024*1024)throw new \InvalidArgumentException('请选择不超过10MB的xlsx文件');
        return V2MachineSync::prepare($file->getPathname(),$this->actor,$this->binding(),$file->getOriginalName());
    });}
    public function preview() {$this->admin();return $this->run(function(){
        $kind=$this->request->get('kind','');$action=$this->request->get('action','diff');$keyword=$this->request->get('keyword','');
        if(!is_string($kind)||($kind!==''&&!isset(V2MachineSync::TABLES[$kind]))||!in_array($action,['diff','all','new','update','disable','same'],true)||!is_string($keyword)||mb_strlen($keyword)>100)throw new \InvalidArgumentException('预览筛选无效');
        return V2MachineSync::preview($this->request->get('ticket'),$this->binding(),$kind,$action,$keyword,max(1,(int)$this->request->get('page',1)),max(1,min(100,(int)$this->request->get('limit',20))));
    });}
    public function commit() {$this->admin();return $this->run(function(){return V2MachineSync::commit($this->request->post('ticket'),$this->binding());});}
    public function discard() {$this->admin();return $this->run(function(){return V2MachineSync::discard($this->request->post('ticket'),$this->binding());});}
    public function positionOptions() {return $this->run(function(){return ['teams'=>PositionDevice::teams($this->actor)];});}
    public function positionData() {return $this->run(function(){return PositionDevice::data(PositionDevice::team((int)$this->request->get('teamId'),$this->actor));});}
    public function positionSave() {return $this->run(function(){return PositionDevice::save((int)$this->request->post('teamId'),$this->actor,$this->request->post(),'管理端岗位设备管理');});}
    public function positionLogs() {return $this->run(function(){return PositionDevice::logs(PositionDevice::team((int)$this->request->get('teamId'),$this->actor));});}
}
