<?php

namespace App\Http\Controllers;

use App\Commodity;
use App\CommodityBarcode;
use App\Events\ResetProcessStatisticStartDateEvent;
use App\LaborReport;
use App\Owner;
use App\Process;
use App\ProcessDaily;
use App\ProcessDailyParticipant;
use App\ProcessesContent;
use App\ProcessMethod;
use App\ProcessStatistic;
use App\Services\OwnerService;
use App\Services\ProcessService;
use App\Sign;
use App\Tutorial;
use App\User;
use App\UserDetail;
use App\UserDutyCheck;
use App\UserLabor;
use Carbon\Carbon;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Validator;
use Ramsey\Uuid\Uuid;

class ProcessController extends Controller
{
    /**
     * Display a listing of the resource.
     * @param Request $request
     * @param OwnerService $ownerService
     * @return void
     */
    public function index(Request $request,OwnerService $ownerService)
    {
        if(!Gate::allows('二次加工管理-查询')){ return redirect(url('/'));  }
        $paginateParams = $request->input();
        $processes = app('ProcessService')->paginate($paginateParams);
        $owners=$ownerService->getIntersectPermitting();
        return view('process.index',['processes'=>$processes,'owners'=>$owners,'paginateParams'=>$paginateParams]);
    }

    //获取每日参与人
    public function getDailyParticipant(Request $request){
        if(!Gate::allows('二次加工管理-登记工时')){ return redirect(url('/'));  }
        $today=Carbon::now()->format('Y-m-d');
        $id=$request->input('id');
        $processDailies=ProcessDaily::with(['processDailyParticipants','process'])->orderBy('date','DESC')
            ->where('process_id',$id)->get();
        $process=Process::query()->with('automaticSupplementSign')->find($id);
        if (count($processDailies)>0){
            if ((($process->amount)-($process->completed_amount))<=0 ||
                $process->status=='交接完成' || $process->automaticSupplementSign)$result=true;
            else $result=$this->createDeficiencyData($processDailies,$today);
            if ($result){
                $processDailies=ProcessDaily::with(['processDailyParticipants','process'])
                    ->orderBy('date','DESC')->where('process_id',$id)->get();
            }
            //$processDailies=$this->countManHour($processDailies);
        }
        return $processDailies;
    }

    /*//根据参与人查找打卡记录计算工时信息
    public function countManHour($processDailies){
        foreach ($processDailies as $processDaily){
            $date=$processDaily->date;
            foreach ($processDaily->processDailyParticipants as $processDailyParticipant){
                $processDailyParticipant->started_at=Carbon::parse($processDailyParticipant->started_at)->format('H:i');
                $processDailyParticipant->ended_at=Carbon::parse($processDailyParticipant->ended_at)->format('H:i');
                $processDailyParticipant=$this->countParticipantManHour($processDailyParticipant,$date);
            }
        }
        return $processDailies;
    }*/

    /*//计算单参与人工时信息
    public function countParticipantManHour($processDailyParticipant,$date){
        $user_id=$processDailyParticipant->user_id;
        $userDutyCheckStart=UserDutyCheck::select('id','checked_at')->where('user_id',$user_id)
            ->where('checked_at','like',$date.'%')->where('type','登入')->orderBy('id')->first();
        $userDutyCheckEnd=UserDutyCheck::select('id','checked_at')->where('user_id',$user_id)
            ->where('checked_at','like',$date.'%')->where('type','登出')->orderBy('id','desc')->first();
        //跨日情况寻找下一天
        if (!$userDutyCheckEnd){
            $date=date("Y-m-d",strtotime("+1 day",strtotime($date)));
            $userDutyCheckEnd=UserDutyCheck::select('id','checked_at')->where('user_id',$user_id)
                ->where('checked_at','like',$date.'%')->where('type','登出')->orderBy('id','desc')->first();
        }
        if (!$userDutyCheckStart || !$userDutyCheckEnd){
            return $processDailyParticipant;
        }
        $dateStart=Carbon::parse($userDutyCheckStart->checked_at);
        $dateEnd=Carbon::parse($userDutyCheckEnd->checked_at);
        $hour=($dateEnd->diffInSeconds($dateStart))/3600; //打卡工时
        if ($processDailyParticipant->dinner_duration)$hour=$hour-(($processDailyParticipant->dinner_duration)/60); //减晚饭时间
        $hour=$this->isHour($userDutyCheckStart,$hour); //去除休息时间
        $processDailyParticipant->hour=round($hour,2);
        if ($hour&&$processDailyParticipant->hour_count){
            $diff=abs(round($processDailyParticipant->hour_count-$hour,2));
            $processDailyParticipant->diff=$diff;
        }

        //计件工 保留
        // if ($processDailyParticipant->unit_count){
        //   return $processDailyParticipant;
        //}
        if ($hour&&$hour>8){
            $processDailyParticipant->billingHour=8;
            return $processDailyParticipant;
        }
        if ($hour&&$hour<=8){
            $processDailyParticipant->billingHour=round($hour,2);
        }
        return $processDailyParticipant;
    }*/

/*    //打卡工时减休息时间
    private function isHour($userDutyCheckStart,$hour){

        $date=$userDutyCheckStart->checked_at;
        $date=Carbon::parse($date)->format('H');
        if ((int)$date<=11){
            $hour=$hour-1;
        }
        return $hour;
    }*/
    //生成二次加工单缺失时间记录及本日记录
    private function createDeficiencyData($processDailies,$today){
        $processDailiesArr=[];
        foreach ($processDailies as $processDaily){
            $processDailiesArr[$processDaily->date]=$processDaily;
        }
        $processDailyOne=$processDailies[count($processDailies)-1];
        $startDate=Carbon::parse($processDailyOne->date);
        $diffDay=$startDate->diffInDays($today,true);
        $remain=$processDailyOne->remain;
        $processId=$processDailyOne->process_id;
        for ($i=1;$i<=$diffDay;$i++){
            $date=date("Y-m-d",strtotime('+'.strval($i)." day",strtotime($startDate)));
            if (!isset($processDailiesArr[$date])){
                $processDaily=new ProcessDaily([
                    'process_id'=>$processId,
                    'date'=>$date,
                    'output'=>0,
                    'remain'=>$remain
                ]);
                $processDaily->save();
            }
            if (isset($processDailiesArr[$date])){
                $remain=$remain-($processDailiesArr[$date]->output);
            }
        }
        return true;
    }

    //驳回二次加工单
    public function reject($id){
        if(!Gate::allows('二次加工管理-接单与驳回')){ return redirect(url('/'));  }
        $process=Process::select('id','status')->find($id);
        $process->status="已驳回";
        $process->update();
        $process->createOperatorLog('驳回');
        app('LogService')->log(__METHOD__,"驳回二次加工单_".__FUNCTION__,json_encode($process),Auth::user()['id']);
        return $process;
    }
    //接单
    public function receive($id){
        if(!Gate::allows('二次加工管理-接单与驳回')){ return ["success"=>false,"data"=>"无权操作"];  }
        /** @var Process|\stdClass $process */
        $process=Process::query()->select('id','status','amount','designate_id')->find($id);
        if ($process && $process->designate_id && $process->designate_id!=Auth::id())return ["success"=>false,"data"=>"无权操作"];
        $amount=$process->amount;
        $today=Carbon::now()->format('Y-m-d');
        $processDaily=ProcessDaily::query()->where('date',$today)->where('process_id',$id)->first();
        if (!$processDaily){
            $processDaily=new ProcessDaily([
                'process_id'=>$id,
                'date'=>$today,
                'output'=>0,
                'remain'=>$amount,
            ]);
            $processDaily->save();
        }
        $process->status="待加工";
        $process->update();
        $process->createOperatorLog('接单');
        return ["success"=>true,"data"=>$process->status];
    }
    //完成
    public function accomplish(Request $request){
        if(!Gate::allows('二次加工管理-交接完成')){ return ['success'=>false,'data'=>'您无权进行该操作!'];  }
        $process=Process::with('processDailies')->find($request->id);
        if (!$process)return ['success'=>false];
        $result=$this->statistic($process);
//        if (!$result['success'])return $result;
        $process->update(['status'=>'交接完成']);
        $process->createOperatorLog('交接完成');
        $processStatistic=$result['data'];
        app('LogService')->log(__METHOD__,"完成二次加工单_".__FUNCTION__,json_encode($process)." || ".json_encode($processStatistic),Auth::user()['id']);
        app('ProcessService')->createInstantBill($process);
        return ['success'=>true,'data'=>'交接完成'];
    }
    private function statistic($process, $sign_end = false){
        if (count($process->processDailies)>0){
            $completed_amount=0;
            foreach ($process->processDailies as $processDaily){
                $completed_amount=$completed_amount+($processDaily->output);
            }
            $process->completed_amount=$completed_amount;
        }
        //统计：
        $revenue=($process->unit_price)*($process->completed_amount);   //收入合计
        $processDailies=ProcessDaily::with('processDailyParticipants')->where('process_id',$process->id)->where('output','>',0)->get();
        $duration_days=count($processDailies);      //完成天数
        $duration_man_hours=0;      //总工时
        $total_cost=0;              //合计成本
        foreach ($processDailies as $processDailyOne){
            foreach ($processDailyOne->processDailyParticipants as $processDailyParticipant){
                if (!$processDailyParticipant->unit_price && !$processDailyParticipant->hour_price) continue;
                $duration_man_hours += $processDailyParticipant->hour_count;
                if ($processDailyParticipant->unit_count){
                    $total_cost += ($processDailyParticipant->unit_count)*($processDailyParticipant->unit_price);
                    continue;
                }
                $total_cost += ($processDailyParticipant->hour_count)*($processDailyParticipant->hour_price);
            }
        };
        $processStatistic=ProcessStatistic::query()->find($process->id);
        $processStatistic->revenue=$revenue;
        $processStatistic->duration_days=$duration_days;
        $processStatistic->duration_man_hours=$duration_man_hours;
        if ($sign_end) $processStatistic->ended_at = date('Y-m-d H:i:s');
        if (!$revenue || !$total_cost){
            if (!$total_cost && $revenue){
                $processStatistic->total_cost = 0;
                $processStatistic->gross_profit = $revenue;
                $processStatistic->gross_profit_rate = 1;
            }
            $processStatistic->update();
            return ['success'=>false,'data'=>'该单未产生费用,无法计算毛利率!'];
        }
        $gross_profit=$revenue-$total_cost; //毛利润
        if ($gross_profit!=0)$gross_profit_rate=$gross_profit/$revenue;  //毛利率;
        else $gross_profit_rate=0;
        $processStatistic->total_cost=$total_cost;
        $processStatistic->gross_profit=$gross_profit;
        $processStatistic->gross_profit_rate=$gross_profit_rate;
        $processStatistic->update();
        app('LogService')->log(__METHOD__,"修改二次加工单统计单_".__FUNCTION__,json_encode($processStatistic),Auth::user()['id']);
        return ['success'=>true,'data'=>$processStatistic];
    }

    //修改当日产量
    public function updateDailyOutput(Request $request){
        if(!Gate::allows('二次加工管理-登记工时')){ return redirect(url('/'));  }
        $errors=$this->validator($request)->errors();
        if (count($errors)>0)return ['status'=>"error",'data'=>$errors];
        $id=$request->input('id');
        $output=$request->input('output');
        $processDaily=ProcessDaily::with('process')->find($id);
        $processDaily->output=$output;
        $processDaily->update();
        $result=$this->countRemains($processDaily);
        $processDailies=$result['processDailies'];
        app('LogService')->log(__METHOD__,"修改当日产量".__FUNCTION__,json_encode($processDaily),Auth::user()['id']);
        $response=[];
        $response['status']='success';
        $response['data']=$processDailies;
        if (isset($result['process'])){$response['process']=$result['process'];}
        return $response;
    }
    //每次修改，去查询该单下全部日期，逐个替换，前一天剩余减当天产量为当天剩余，无前天记录去拿单子预期产量
    private function countRemains($processDaily){
        if (!$processDaily || !$processDaily->process) return;
        $processDailies=ProcessDaily::where('process_id',$processDaily->process_id)->orderBy('date')->get();
        $completed_amount = 0;
        for ($i=0;$i<count($processDailies);$i++){
            if ($i==0&&$processDailies[$i]->date>=$processDaily->date){
                $processDailies[$i]->remain=($processDaily->process->amount)-$processDailies[$i]->output;
                $processDailies[$i]->update();
            }
            if ($i>0&&$processDailies[$i]->date>=$processDaily->date){
                $processDailies[$i]->remain=($processDailies[$i-1]->remain)-$processDailies[$i]->output;
                $processDailies[$i]->update();
            }
            $completed_amount += $processDailies[$i]->output;
        }
        if ($completed_amount) Process::query()->where('id',$processDaily->process_id)->update(['completed_amount'=>$completed_amount]);
        //同步二次加工单统计
        $processDailiesStatistic=$processDailies->where('output','>',0);
        $processStatistic=ProcessStatistic::find($processDaily->process_id);    //如非录入渠道建单不生成统计单，报错
        $processStatistic->top_capacity=$processDailiesStatistic->max('output');
        $processStatistic->bottom_capacity=$processDailiesStatistic->min('output');
        $processStatistic->average_capacity=$processDailiesStatistic->avg('output');
        $processStatistic->update();
        //修改二次加工单状态
        $result=[];
        /*if (count($processDailies)>0 && ($processDailies[(count($processDailies)-1)]->remain)<=0){
            $result['process']=$this->changeProcessCheck($processDaily->process_id);  //保留 目前小于0后等待人工确认再进入待验收
        }*/
        $result['processDailies']=$processDailies;
        return $result;
    }

    //剩余小于等于0，状态待验收
    private function changeProcessCheck($process_id){
        $process=Process::select('id','status')->find($process_id);
        if ($process->status=="加工中"){
            $process->status="待验收";
            $process->update();
            $processStatistic=ProcessStatistic::find($process_id);
            $processStatistic->ended_at=date('Y-m-d H:i:s');
            $processStatistic->update();
            app('LogService')->log(__METHOD__,"二次加工单待验收".__FUNCTION__,json_encode($process)." || ".json_encode($processStatistic),Auth::user()['id']);
        }
        return $process_id;
    }

    //新增参与人
    public function storeProcessDailyParticipant(Request $request){
        if(!Gate::allows('二次加工管理-登记工时')){ return redirect(url('/'));  }
        $errors=$this->validatorProcessDailyParticipant($request)->errors();
        if (count($errors)>0)return ['status'=>"error",'data'=>$errors];
        $user_id=$request->input('user_id');
        $started_at=$request->input('started_at');
        $ended_at=$request->input('ended_at');
        $daily_id=$request->input('daily_id');
        $hour_count=$request->input('hour_count');
        $unit_count=$request->input('unit_count');
        $hour_price=$request->input('hour_price');
        $unit_price=$request->input('unit_price');
        $dinner_duration=$request->input('dinner_duration');
        $remark=$request->input('remark');
        $userLabor=UserLabor::find($user_id);
        $processDaily=processDaily::find($daily_id);
        if ($userLabor->isOccupiedAt($processDaily->date,$ended_at)){
            ['status'=>"warning",'data'=>'该临时工此时间段已存在工作记录!'];
        };
        $processDailyParticipant=new ProcessDailyParticipant([
            'process_daily_id'=>$daily_id,
            'user_id'=>$user_id,
            'started_at'=>$started_at,
            'ended_at'=>$ended_at,
            'hour_price'=>$hour_price,
            'hour_count'=>$hour_count,
            'unit_price'=>$unit_price,
            'unit_count'=>$unit_count,
            'dinner_duration'=>$dinner_duration,
            'remark'=>$remark,
        ]);
        $processDailyParticipant->save();
        app('LogService')->log(__METHOD__,"添加新参与人".__FUNCTION__,json_encode($processDailyParticipant),Auth::user()['id']);
        $processDailyParticipant->load(['processDaily'=>function($query){
            $query->with('process');
        }]);

        // 重置统计开始时间
        event(new ResetProcessStatisticStartDateEvent($processDailyParticipant->processDaily->process));


        if ($processDailyParticipant->processDaily && $processDailyParticipant->processDaily->process &&
            $processDailyParticipant->processDaily->process->status=='交接完成'){
            $this->statistic($processDailyParticipant->processDaily->process);
        }
        $result=[];
        $processDaily=ProcessDaily::with('process')->select('process_id','date')->find($daily_id);
        //$date=$processDaily->date;
        //$processDailyParticipant=$this->countParticipantManHour($processDailyParticipant,$date);
        if ($processDaily->process && $processDaily->process->status=="待加工"){
            $process=Process::find($processDaily->process_id);
            $process->status="加工中";
            $process->update();
            $result['process']=$processDaily->process_id;
            $processStatistic=ProcessStatistic::find($processDaily->process_id);
            //$processStatistic->started_at=date('Y-m-d H:i:s');
            $processStatistic->started_at=$processDaily->date.' '.$started_at;
            $processStatistic->update();
            app('LogService')->log(__METHOD__,"二次加工单加工中".__FUNCTION__,json_encode($process)." || ".json_encode($processStatistic),Auth::user()['id']);
        }
        $result['status']="success";
        $result['data']=$processDailyParticipant;
        return $result;
    }

    //验证参与人
    public function verifyUserName(Request $request){
        $userName=$request->input('userName');
        if (!$userName) return ['success'=>false,'data'=>'未输入参与者!'];
        $date = $request->date;
        if (!$date){
            $processDailyParticipant=ProcessDailyParticipant::query()->find($request->id);
            $date=$processDailyParticipant->process_daily_date;
        }
        return $this->seekUserLabor($userName,$date);

    }

    //根据全名查询临时工
    public function seekUserLabor($userName,$date){
        $userDetails=UserDetail::with('userLabor')->where('type','临时工')->where('full_name',$userName)->get();
        if (!$userDetails)return ['success'=>false,'data'=>'未找到临时工信息!'];
        if (count($userDetails)==1){
            if (!$userDetails[0]->userLabor)return ['success'=>false,'data'=>'该员工非临时工身份!'];
            $laborReport=LaborReport::where('check_in_at','like',$date.'%')->where('user_id',$userDetails[0]->user_id)->first();
            if ($laborReport){
                $userDetails[0]->started_at=Carbon::parse($laborReport->thisRoundOnlineStartTime)->format('H:i');
                $userDetails[0]->ended_at=Carbon::parse($laborReport->thisRoundOnlineEndTime)->format('H:i');
            }
            return ['success'=>true,'data'=>$userDetails[0]];
        }
        //多个同名
        foreach ($userDetails as $userDetail){
            $userDutyCheck=UserDutyCheck::select('checked_at')->where("user_id",$userDetail->user_id)->orderBy('checked_at','DESC')->first();
            $laborReport=LaborReport::where('check_in_at','like',$date.'%')->where('user_id',$userDetail->user_id)->first();
            if ($laborReport) {
                $userDetail->started_at = Carbon::parse($laborReport->thisRoundOnlineStartTime)->format('H:i');
                $userDetail->ended_at = Carbon::parse($laborReport->thisRoundOnlineEndTime)->format('H:i');
            }
            if ($userDutyCheck)$userDetail->checked_at=$userDutyCheck->checked_at;
        }
        if (!$userDetails)return ['success'=>false,'data'=>'未找到临时工信息!'];
        return ['success'=>true,'data'=>$userDetails];
    }
    //修改参与人
    public function updateProcessDailyParticipant(Request $request){
        if(!Gate::allows('二次加工管理-登记工时')){ return redirect(url('/'));  }
        $id=$request->input('id');
        if (!$id) return ['success'=>false,'data'=>"修改失败，ID未传递"];
        $errors=$this->validatorProcessDailyParticipant($request)->errors();
        if (count($errors)>0)return ['success'=>false,'data'=>$errors];
        $processDailyParticipant=ProcessDailyParticipant::with(['processDaily'=>function($query){
            $query->with('process');
        },'userLabor'])->find($id);
        if (!$processDailyParticipant || !$processDailyParticipant->processDaily
            || !$processDailyParticipant->processDaily->process || !$processDailyParticipant->userLabor) return ['success'=>false,'data'=>"修改失败，该信息不存在"];
        if ($processDailyParticipant->userLabor->isOccupiedAt($processDailyParticipant->processDaily->date,$request->started_at)){
            ['success'=>false,'data'=>"该临时工此时间段已存在工作记录"];
        }
        $processDailyParticipant->user_id=$request->input('user_id');
        $processDailyParticipant->started_at=$request->input('started_at');
        $processDailyParticipant->ended_at=$request->input('ended_at');
        $processDailyParticipant->hour_count=$request->input('hour_count');
        $processDailyParticipant->unit_count=$request->input('unit_count');
        $processDailyParticipant->hour_price=$request->input('hour_price');
        $processDailyParticipant->unit_price=$request->input('unit_price');
        $processDailyParticipant->dinner_duration=$request->input('dinner_duration');
        $processDailyParticipant->remark=$request->input('remark');
        $processDailyParticipant->update();
        app('LogService')->log(__METHOD__,"修改参与人".__FUNCTION__,json_encode($processDailyParticipant),Auth::user()['id']);
        if ($processDailyParticipant->processDaily->process->status=='交接完成'){
            $this->statistic($processDailyParticipant->processDaily->process);
        }
        //$processDailyParticipant=$this->countParticipantManHour($processDailyParticipant,$processDailyParticipant->processDaily->date);
        return ['success'=>true,'data'=>$processDailyParticipant];
    }
    /*//参与人审核
    public function processDailyParticipantAudit($id){
        if(!Gate::allows('人事管理-任务审核')){ return redirect(url('/'));  }
        $processDailyParticipant=ProcessDailyParticipant::select('id','status')->find($id);
        if (!$processDailyParticipant) return ['success'=>false];
        $processDailyParticipant->status='已审核';
        $processDailyParticipant->update();
        app('LogService')->log(__METHOD__,"登记工时参与人审核".__FUNCTION__,json_encode($processDailyParticipant),Auth::user()['id']);
        return ['success'=>true,'processDailyParticipant'=>$processDailyParticipant];
    }*/
    //获取全部教程
    public function getTutorials($id){
        $process=Process::with('tutorials')->find($id);
        $tutorials_id=[];
        if ($process->tutorials){
            foreach ($process->tutorials as $tutorial){
                array_push($tutorials_id,$tutorial->id);
            }
        }
        $tutorials=Tutorial::query()->where('owner_id',$process->owner_id)->get();
        if ($tutorials_id && $tutorials) $tutorials=$tutorials->diff(Tutorial::whereIn('id',$tutorials_id)->get());
        return ['success'=>true,'data'=>$tutorials];
    }
    //添加教程关联
    public function selectedTutorial(Request $request){
        if(!Gate::allows('二次加工管理-教程管理')){ return ['success'=>false,'data'=>'您无权进行该操作!'];  }
        $process_id=$request->input('process_id');
        $tutorial_id=$request->input('tutorial_id');
        if (!$process_id || !$tutorial_id) return ['success'=>false,'data'=>'二次加工或教程传递错误!'];
        $tutorial=Tutorial::find($tutorial_id);
        if (!$tutorial)return ['success'=>false,'data'=>'未找到相关教程!'];
        $tutorial->processes()->syncWithoutDetaching([$process_id]);
        app('LogService')->log(__METHOD__,"添加教程关联".__FUNCTION__,json_encode($process_id),Auth::user()['id']);
        return ['success'=>true,'data'=>$tutorial];
    }
    //删除教程关联
    public function deleteTutorial(Request $request){
        if(!Gate::allows('二次加工管理-教程管理')){ return ['success'=>false,'data'=>'您无权进行该操作!'];  }
        $process_id=$request->input('process_id');
        $tutorial_id=$request->input('tutorial_id');
        if (!$process_id || !$tutorial_id) return ['success'=>false,'data'=>'二次加工或教程传递错误!'];
        DB::table('process_tutorial')->where('process_id',$process_id)->where('tutorial_id',$tutorial_id)->delete();
        app('LogService')->log(__METHOD__,"删除教程关联".__FUNCTION__,json_encode($process_id),Auth::user()['id']);
        return ['success'=>true];
    }
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if(!Gate::allows('二次加工管理-录入')){ return view("exception.authority");  }
        $processMethods=ProcessMethod::query()->get();
        $users = User::query()->whereNotNull("email")->whereHas("roles",function ($query){
            /** @var Builder $query */
            $query->whereHas("authorities",function($query){
                /** @var Builder $query */
                $query->where("alias_name","二次加工管理-接单与驳回");
            });
        })->orWhereIn("name",config("users.superAdmin"))->get();
        return view('process/create',['processMethods'=>$processMethods,'process'=>null,"users"=>$users]);
    }

    //货主ID获取教程
    public function ownerGetTutorials($owner_id){
        $tutorials=Tutorial::query()->where('owner_id',$owner_id)->get();
        return $tutorials;
    }

    public function store(Request $request)
    {
        if(!Gate::allows('二次加工管理-录入')){ return redirect(url('/'));  }
        $errors=$this->validatorProcess($request)->errors();
        if (count($errors)>0)return ['error'=>$errors,'data'=>"process"];
        $errors=[];
        foreach ($request->input('processContents') as $processContent){
            if ($processContent['addBtnShow'])continue;
            $error=$this->validatorProcessContent($processContent)->errors();
            if (count($error)>0)$errors[$processContent['wms_code']]=$error;
        }
        if (count($errors)>0)return ['error'=>$errors];
        $process=new Process([
            'code'=>Uuid::uuid1(),
            'owner_id'=>$request->input('owner_id'),
            'process_method_id'=>$request->input('process_method_id'),
            'unit_price'=>$request->input('unit_price'),
            'remark'=>$request->input('remark'),
            'balance_remark'=>$request->input('balance_remark'),
            'amount'=>$request->input('amount'),
            'designate_id'=>$request->input('designate_id'),
            'status'=>'待审核',
        ]);
        $process->save();
        $process->createOperatorLog('创建');
        $processContents=[];
        foreach ($request->input('processContents') as $processContent){
            $wmsCode_commoditySku=$processContent['wms_code'].'_'.$processContent['commodity_id'].'_'.$processContent['type'];
            array_push($processContents,$wmsCode_commoditySku);
        }
        $removeDuplicateProcessContents=array_unique($processContents);
        if (count($processContents)>count($removeDuplicateProcessContents)){
            return ['success'=>false,'data'=>'录入商品不可重复！'];
        }else{
            foreach ($request->input('processContents') as $processContent){
                if ($processContent['addBtnShow'])continue;
                $processContentOne=new ProcessesContent([
                    'process_id'=>$process->id,
                    'bill_type'=>$processContent['bill_type'],
                    'commodity_id'=>$processContent['commodity_id'],
                    'wms_code'=>$processContent['wms_code'],
                    'amount'=>$processContent['amount'],
                ]);
                if ($processContent['type'])$processContentOne->type='成品单';
                $processContentOne->save();
                if (isset($processContent['is_update_commodity_name'])){
                    Sign::query()->create([
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_name',
                        'mark'=>$processContent['commodity_name'],
                    ]);
                }
                if (isset($processContent['commodity_barcode'])){
                    Sign::query()->create([
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_barcode',
                        'mark'=>$processContent['commodity_barcode'],
                    ]);
                }
                if (isset($processContent['is_update_commodity_sku'])){
                    Sign::query()->create([
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_sku',
                        'mark'=>$processContent['commodity_sku'],
                    ]);
                }
            }
        }
        $process->code='P'.date ("Ymd").str_pad($process->id,3,"0",STR_PAD_LEFT);
        $process->update();
        $processStatistic=new ProcessStatistic([
            'process_id'=>$process->id,
        ]);
        $processStatistic->save();
        app('LogService')->log(__METHOD__,"录入二次加工单".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        //if ($request->input('tutorials'))$process->tutorials()->sync($request->input('tutorials'));
        return ['success'=>true];
    }
    //修改价格
    function updateUnitPrice(Request $request){
        if(!Gate::allows('二次加工管理-修改价格')){ return ['success'=>false,'data'=>'您无权进行该操作！'];  }
        $errors=Validator::make($request->input(),[
            'id' => 'required',
            'unit_price'=>['required','numeric'],
        ])->errors();
        if (count($errors)>0)return ['success'=>false,'data'=>'非法参数!'];
        $process=Process::with('signs')->find($request->id);
        if (!$process || $process->status!='交接完成' || count($process->signs)>0)return ['success'=>false,'data'=>'无法操作该二次加工单!'];
        $process->createSignUnitPrice($request->unit_price);
        $sign=$process->createSign('发起修改');
        app('LogService')->log(__METHOD__,"修改二次加工单价格_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return ['success'=>true,'sign'=>$sign];
    }

    //二次加工组审核   两个方法大体相同 拆分原因是为了精确区分权限
    function workGroupVerify(Request $request){
        if(!Gate::allows('二次加工管理-组长确认')){ return ['success'=>false,'data'=>'您无权进行该操作！'];  }
        app('LogService')->log(__METHOD__,"修改二次加工单价格二次加工组确认_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return $this->verify($request->id,'二次加工组确认');
    }
    //二次加工组审核
    function accountantVerify(Request $request){
        if(!Gate::allows('二次加工管理-财务确认')){ return ['success'=>false,'data'=>'您无权进行该操作！'];  }
        app('LogService')->log(__METHOD__,"修改二次加工单价格财务确认_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return $this->verify($request->id,'财务确认');
    }
    private function verify($id,$msg){
        $process=Process::with(['signs','processDailies'])->find($id);
        if (!$process || count($process->signs)<1)return false;
        $signMap=[];
        foreach ($process->signs as $sign){
            $signMap[$sign->mark]=true;
        }
        if (isset($signMap[$msg]))return ['success'=>false,'data'=>'该单已被确认过！'];
        if (isset($signMap['二次加工组确认'])){
            $signUnitPrice=$process->signUnitPrice;
            DB::beginTransaction();
            if ($signUnitPrice){
                $process->update(['unit_price' => $signUnitPrice->mark]);
                $this->statistic($process);
            }
            $process->createSign('财务确认');
            $process->destroySign();
            DB::commit();
            return ['success'=>true,'data'=>false];
        }
        if (isset($signMap['财务确认'])){
            $signUnitPrice=$process->signUnitPrice;
            DB::beginTransaction();
            if ($signUnitPrice){
                $process->update(['unit_price' => $signUnitPrice->mark]);
                $this->statistic($process);
            }
            $process->createSign('二次加工组确认');
            $process->destroySign();
            DB::commit();
            return ['success'=>true,'data'=>false];
        }
        $sign = $process->createSign($msg);
        return ['success'=>true,'data'=>$sign];
    }

    //审核
    function audit(Request $request){
        if(!Gate::allows('二次加工管理-审核')){ return ['success'=>false,'data'=>'您无权进行该操作！'];  }
        $process=Process::query()->find($request->id);
        if (!$process)return ['success'=>false];
        $process->update(['status'=>'待接单']);
        $process->createOperatorLog('审核');
        app('LogService')->log(__METHOD__,"二次加工单审核_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return ['success'=>true];
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(!Gate::allows('二次加工管理-编辑')){ return redirect(url('/'));  }
        $process=Process::with(['processesContents'=>function($query){
            return $query->with(['signCommodityName','signCommoditySku','signCommodityBarcode','commodity'=>function($query){
                return $query->with('barcodes');
            }]);
        }])->find($id);
        $processMethods=ProcessMethod::get();
        $users = User::query()->whereNotNull("email")->whereHas("roles",function ($query){
            /** @var Builder $query */
            $query->whereHas("authorities",function($query){
                /** @var Builder $query */
                $query->where("alias_name","二次加工管理-接单与驳回");
            });
        })->orWhereIn("name",config("users.superAdmin"))->get();
        return view('process.create',['process'=>$process,'processMethods'=>$processMethods,"users"=>$users]);
    }

    public function show($id){
        $process=Process::with(['processesContents'=>function($query){
            $query->with(['signCommodityName','signCommoditySku','signCommodityBarcode','commodity'=>function($query){
                $query->with('barcodes');
            }]);
        },'tutorials'])->find($id);
        return view('process.show',compact('process'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return array
     */
    public function update(Request $request, $id)
    {
        if(!Gate::allows('二次加工管理-编辑')){ return ['success'=>false,'data'=>'您无权进行该操作！'];  }
        $errors=$this->validatorProcess($request)->errors();
        if (count($errors)>0)return ['error'=>$errors,'data'=>"process"];
        $errors=[];
        foreach ($request->input('processContents') as $processContent){
            if ($processContent['addBtnShow'])continue;
            $error=$this->validatorProcessContent($processContent)->errors();
            if (count($error)>0)$errors[$processContent['wms_code']]=$error;
        }
        if (count($errors)>0)return ['error'=>$errors];
        $process=Process::query()->find($id);
        if (!$request->is_hide){
            $process->owner_id=$request->input('owner_id');
            $process->process_method_id=$request->input('process_method_id');
            $process->unit_price=$request->input('unit_price');
            $process->remark=$request->input('remark');
            $process->balance_remark=$request->input('balance_remark');
            $process->amount=$request->input('amount');
            $process->designate_id=$request->input('designate_id');
            if ($process->status=='已驳回')$process->status='待接单';
            $process->update();
            $process->createOperatorLog('编辑');
        }
        $processContents=[];
        foreach ($request->input('processContents') as $processContent){
            $wmsCode_commoditySku=$processContent['wms_code'].'_'.$processContent['commodity_sku'].'_'.$processContent['type'];
            array_push($processContents,$wmsCode_commoditySku);
        }
        $removeDuplicateProcessContents=array_unique($processContents);
        if (count($processContents)>count($removeDuplicateProcessContents)){
            return ['success'=>false,'data'=>'录入商品不可重复！'];
        }else{
            $ProcessesContents=ProcessesContent::where('process_id',$process->id)->get();
            $ids=array_column($ProcessesContents->toArray(),'id');
            Sign::query()->where('signable_type','processes_contents')->whereIn('signable_id',$ids)->delete();
            ProcessesContent::where('process_id',$process->id)->delete();
            foreach ($request->input('processContents') as $processContent){
                if ($processContent['addBtnShow'])continue;
                $processContentOne=new ProcessesContent([
                    'process_id'=>$process->id,
                    'bill_type'=>$processContent['bill_type'],
                    'commodity_id'=>$processContent['commodity_id'],
                    'wms_code'=>$processContent['wms_code'],
                    'amount'=>$processContent['amount'],
                ]);
                if ($processContent['type'])$processContentOne->type='成品单';
                $processContentOne->save();
                if (isset($processContent['is_update_commodity_name'])){
                    Sign::query()->updateOrCreate([
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_name',
                    ],[
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_name',
                        'mark'=>$processContent['commodity_name'],
                    ]);
                }
                if (isset($processContent['commodity_barcode'])){
                    Sign::query()->updateOrCreate([
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_barcode',
                    ],[
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_barcode',
                        'mark'=>$processContent['commodity_barcode'],
                    ]);
                }
                if (isset($processContent['is_update_commodity_sku'])){
                    Sign::query()->updateOrCreate([
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_sku',
                    ],[
                        'signable_type'=>'processes_contents',
                        'signable_id'=>$processContentOne->id,
                        'field'=>'commodity_sku',
                        'mark'=>$processContent['commodity_sku'],
                    ]);
                }
            }
        }
        app('LogService')->log(__METHOD__,"编辑二次加工单_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return ['success'=>true];
    }

    //删除二次加工内容单            弃用，保留，改为全部提交后一次性验证，不在操作时就允许删除
    public function deleteProcessContent($id){
        if(!Gate::allows('二次加工管理-删除')){ return redirect(url('/'));  }
        $processContent=ProcessesContent::find($id);
        if (!$processContent) return ['success'=>false,'data'=>'未找到对应二次加工内容单'];
        $processContent->delete();
        return ['success'=>true];
    }

    //回滚二次加工单状态
    function rollback(Request $request){
        if(!Gate::allows('二次加工管理-回滚')){ return ['success'=>false,'data'=>'您无权进行该操作！'];  }
        if (!$request->id)return ['success'=>false];
        $process=Process::query()->find($request->id);
        switch ($process->status){
            case '待接单':
                $process->status='待审核';break;
            case '待加工':
                $process->status='待接单';break;
            case '已驳回':
                $process->status='待审核';break;
            case '加工中':
                $process->status='待接单';break;
            case '待验收':
                $process->status='加工中';break;
            case '待交接':
                $process->status='待验收';break;
        }
        $process->save();
        $process->createOperatorLog('回滚');
        app('LogService')->log(__METHOD__,"回滚二次加工单_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return ['success'=>true,'data'=>$process->status];
    }
    //加工完成
    function processAccomplish(Request $request){
        if(!Gate::allows('二次加工管理-登记工时')){ return ['success'=>false,'data'=>'您无权进行该操作！'];  }
        $process= Process::query()->find($request->id);
        if (!$process)return ['success'=>false];
        $process->update([
            'status'=>'待验收'
        ]);
        $process->createOperatorLog('加工完成');
        app('LogService')->log(__METHOD__,"标记加工完成_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return ['success'=>true,'data'=>'待验收'];
    }

    //质量验收
    function checkAndAccept(Request $request){

        $process = Process::query()->with('processDailies')->find($request->id);
        if (!$process)return ['success'=>false];
        $process->load(['processesContents'=>function($query){
            $query->where('type','成品单');
        }]);
        if (count($process->processesContents) == 0)return ['success'=>false,'data'=>'没有成本单不得验收！'];
        $result=$this->statistic($process, true);
        //if (!$result['success'])return $result;
        $process->update([
            'status'=>'待交接'
        ]);
        $process->createOperatorLog('质量验收');
        app('LogService')->log(__METHOD__,"质量验收二次加工单_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return ['success'=>true,'data'=>'待交接'];
    }

    //回收站
    function recycle(Request $request){
        if(!Gate::allows('二次加工管理-删除')){ return redirect('/');  }
        $processes=Process::filterAuthorities()->onlyTrashed()->paginate($request->paginate??50);
        return view('process.recycle',compact('processes'));
    }

    //回收站恢复
    function recover(Request $request){
        if(!Gate::allows('二次加工管理-删除')){ return redirect('/');  }
        $checkData=$request->checkData;
        app('LogService')->log(__METHOD__,"回收站恢复二次加工单_".__FUNCTION__,json_encode($request->input()),Auth::user()['id']);
        return ['success'=>Process::withTrashed()->whereIn('id',$checkData)->restore()];
    }
    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return array
     */
    public function destroy($id)
    {
        if(!Gate::allows('二次加工管理-删除')){ return redirect('/');  }
        app('LogService')->log(__METHOD__,"删除二次加工单进回收站_".__FUNCTION__,json_encode($id),Auth::user()['id']);
        return ['success'=>Process::destroy($id)];
    }

    //修改二次加工单每日单的起始日期
    public function updateStartDate(Request $request){
        $start_date=$request->date;
        $process_id=$request->process_id;
        if (!$start_date || !$process_id)return ['success'=>false,'data'=>'起始日期为空或单据已不存在！'];
        $processDailiesId=[];
        $processDailies=ProcessDaily::where('process_id',$process_id)->get();
        $is_process_daily=false;
        foreach ($processDailies as $processDaily){
            if ($processDaily->date == $start_date){
                $is_process_daily=true;
            }
            if (Carbon::parse($processDaily->date)->lt(Carbon::parse($start_date))){
                array_push($processDailiesId,$processDaily->id);
                unset($processDaily);
            }
        }

        $process=Process::find($process_id);
        if (count($processDailiesId) > 0){
            ProcessDaily::destroy($processDailiesId);
            ProcessDailyParticipant::whereIn('process_daily_id',$processDailiesId)->delete();
            event(new ResetProcessStatisticStartDateEvent($process));
        }
        if (!$is_process_daily && $process){
            $ProcessDailyNow=ProcessDaily::create([
                'process_id'=>$process_id,
                'date'=>$start_date,
                'output'=>0,
                'remain'=>$process->amount,
            ]);
            $processDailies->push($ProcessDailyNow);
        }
        $today=Carbon::parse($start_date)->format('Y-m-d');
        $this->createDeficiencyData($processDailies,$today);
        $processDailies=ProcessDaily::with('processDailyParticipants')->where('process_id',$process_id)
            ->orderBy('date','DESC')->get();
        app('LogService')->log(__METHOD__,"修改二次加工单加工日记录_".__FUNCTION__,json_encode($request),Auth::user()['id']);
        return ['success'=>true,'data'=>$processDailies];
    }

    //修改二次加工单终止日期
    public function updateEndDate(Request $request){
        $end_date=$request->date;
        $process_id=$request->process_id;
        if (!$end_date || !$process_id)return ['success'=>false,'data'=>'终止日期为空或单据已不存在！'];
        $processDailiesId=[];
        $processDailies=ProcessDaily::where('process_id',$process_id)->orderBy('date','DESC')->get();
        foreach ($processDailies as $processDaily){
            if (Carbon::parse($processDaily->date)->gt(Carbon::parse($end_date))){
                array_push($processDailiesId,$processDaily->id);
                unset($processDaily);
            }
        }
        ProcessDaily::destroy($processDailiesId);
        ProcessDailyParticipant::whereIn('process_daily_id',$processDailiesId)->delete();
        $this->createDeficiencyData($processDailies,$end_date);
        $processDailies=ProcessDaily::with('processDailyParticipants')->where('process_id',$process_id)
            ->orderBy('date','DESC')->get();
        $this->createAutomaticSupplement($process_id);
        app('LogService')->log(__METHOD__,"修改二次加工单加工日记录_".__FUNCTION__,json_encode($request),Auth::user()['id']);
        return ['success'=>true,'data'=>$processDailies];
    }

    //增加填充标记
    private function createAutomaticSupplement($process_id){
        Sign::query()->firstOrCreate([
            'signable_type'=>'processes',
            'signable_id'=>$process_id,
            'field'=>'automatic_supplement',
            'mark'=>"停止自动补充每日记录单"
        ]);
    }

    //删除临时工工作记录
    public function destroyDailyParticipant($id){
        $pro = ProcessDailyParticipant::query()->with(["processDaily"=>function($query){
            /** @var Builder $query */
            $query->with("process");
        }])->find($id);
        if ($pro->delete()) {

            event(new ResetProcessStatisticStartDateEvent($pro->processDaily->process));

            return ['success'=>true];
        }
        return ['success'=>false , 'data'=>'记录不存在！'];
    }
    //导入商品数据
    public function importPasteData(Request $request){
        $commodities = explode("\n",$request->data); //拆分行
        $owner='';
        $processContents=[];
        $errors = [];
        foreach ($commodities as $i => $commodity){
            if ($i==0 && mb_strpos($commodity,'货主')!==false && mb_strpos($commodity,'品名')!==false
                && mb_strpos($commodity,'数量')!==false)continue;
            $commodity = explode("\t",$commodity); //拆分列
            $head="第".($i+1)."行";
            if (count($commodity)<count($request->rows)){
                $errors[$head] = ['数据不完整'];
                unset($commodity);
                continue;
            }
            $commodityData=[];
            $wmsCode='';
            $barcode='';
            $amount=0;
            $errors[$head] = [];
            foreach ($request->rows as $index => $row){
                if ($row == '货主'){
                    if ($commodity[$index] && !$owner){
                        $owner = $commodity[$index];
                    }
                    if (!$commodity[$index])array_push($errors[$head],"货主为空");
                }
                if ($row == '单据号'){
                    $wmsCode = $commodity[$index] ?? '';
                }
                if ($row == 'SKU'){
                    $commodityData['sku'] = $commodity[$index] ?? '';
                    if (!$commodity[$index])array_push($errors[$head],"SKU为空");
                }
                if ($row == '品名'){
                    $commodityData['name'] = $commodity[$index] ?? '';
                    if (!$commodity[$index])array_push($errors[$head],"品名为空");
                }
                if ($row == '条码'){
                    $barcode = $commodity[$index] ?? '';
                }
                if ($row == '数量'){
                    $amount = $commodity[$index] ?? '';
                    if (!$commodity[$index])array_push($errors[$head],"数量为空");
                    if (!is_numeric($commodity[$index]) || $commodity[$index] < 1)array_push($errors[$head],"数量为空或小于1");
                }
            }
            if (count($errors[$head]) > 0)continue;
            else unset($errors[$head]);
            if (!is_object($owner)){
                $str = $owner;
                if (!$str) throw new \Exception('未找到货主!');
                $owner=Owner::query()->where('code',$str)->orWhere('name',$str)->first();
                if (!$owner){
                    $owner = Owner::query()->create([
                        'code' => $str,
                        'name' => $str
                    ]);
                    app('LogService')->log(__METHOD__,"二次加工单录入导入商品数据时添加货主".__FUNCTION__,json_encode($owner),Auth::user()['id']);
                }
            }
            $goods = Commodity::query()->with('barcodes')->where('owner_id',$owner->id)//->whereNull('owner_id')  保留，暂时不知为何限定货主为空
                ->where('sku',$commodityData['sku'])->first();
            $commodity_barcodes = [];
            if (!$goods){
                $goods = Commodity::query()->create($commodityData);
                if ($barcode){
                    $commodityBarCode=CommodityBarcode::query()->create([
                        'code' => $barcode,
                        'commodity_id' => $goods->id
                    ]);
                    $commodity_barcodes = [$commodityBarCode];
                }
            }else {
                if ($barcode){
                    $commodity_barcodes = $goods->barcodes->toArray();
                    $commodityBarCode = $goods->barcodes->where('code',$barcode)->first();
                    if (!$commodityBarCode){
                        $commodityBarCode = CommodityBarcode::query()->create([
                            'code' => $barcode,
                            'commodity_id' => $goods->id
                        ]);
                        array_push($commodity_barcodes,$commodityBarCode);
                    }
                }
            }
            app('LogService')->log(__METHOD__,"二次加工单录入导入商品数据时添加商品及条码".__FUNCTION__,
                json_encode($goods)." || ".json_encode($commodity_barcodes),Auth::user()['id']);
            $processContent=['bill_type'=>'入库单','commodity_id'=>$goods->id,'wms_code'=>$wmsCode,'amount'=>$amount,
                'commodity_name'=>$goods->name,'commodity_barcodes'=>$commodity_barcodes,'commodity_sku'=>$goods->sku,
                'lineNo'=>1,'owner_id'=>$owner->id,'owner_name'=>$owner->name,'addBtnShow'=>false,'type'=>$request->type];
            array_push($processContents,$processContent);
        }
        return ['success'=>true,'data'=>$processContents,'errors'=>count($errors)>0?$errors:''];
    }


    //导出
    public function export(Request $request){
        if(!Gate::allows('二次加工管理-查询')){ return '没有权限';  }
        if ($request->checkAllSign){
            $params = $request->input();
            unset($params['checkAllSign']);
            $sql = app('ProcessService')->getSql($params);
        }else $sql = app('ProcessService')->getSql(["id"=>$request->data]);
        $post = Http::post(config('go.export.url'),['type'=>'process','sql'=>$sql]);
        if ($post->status() == 500){
            throw new Exception($post->header("Msg"));
        }
        return response($post,200, [
            "Content-type"=>"application/octet-stream",
            "Content-Disposition"=>"attachment; filename=二次加工记录-".date('ymdHis').'.xlsx',
        ]);
    }

    public function validatorProcessContent(array $processContent){
        $validator=Validator::make($processContent,[
            'commodity_id'=>['required','integer'],
            'bill_type'=>['required'],
            'amount'=>['required','integer']
        ],[
            'required'=>':attribute 不应为空',
            'min'=>':attribute 不得为0或为负',
            'integer'=>':attribute 必须为整数',
            'max'=>':attribute 输入值过大',
        ],[
            'commodity_id'=>'商品',
            'bill_type'=>'单据类型',
            'amount'=>'商品数量',
        ]);
        return $validator;
    }

    public function validatorProcess(Request $request){
        $validator=Validator::make($request->input(),[
            'owner_id'=>['required'],
            'process_method_id'=>['required','integer'],
            'amount'=>['required','integer'],
            'unit_price'=>['required','min:0','max:999999','numeric'],
        ],[
            'required'=>':attribute 不应为空',
            'min'=>':attribute 不得为0或为负',
            'numeric'=>':attribute 必须为数字',
            'max'=>':attribute 输入值过大',
            'integer' =>':attribute 必须为整数',
        ],[
            'owner_id'=>'货主',
            'process_method_id'=>'加工类型',
            'amount'=>'数量',
            'unit_price'=>'单价',
        ]);
        return $validator;
    }

    public function validator(Request $request){
        $validator=Validator::make($request->input(),[
            'id'=>['required','integer'],
            'output'=>'required|min:0|max:999999|numeric',
        ],[
            'required'=>':attribute 不应为空',
            'min'=>':attribute 不得为0或为负',
            'numeric'=>':attribute 必须为数字',
            'max'=>':attribute 输入值过大',
        ],[
            'output'=>'每日产量'
        ]);
        return $validator;
    }

    //参与人信息校验
    public function validatorProcessDailyParticipant(Request $request){
        $validator=Validator::make($request->input(),[
            'daily_id'=>'filled',
            'user_id'=>'required',
            'started_at'=>'required|date_format:H:i',
            'ended_at'=>'required|date_format:H:i',
            'hour_price'=>'nullable|min:0|max:999999|nullable|numeric',
            'unit_price'=>'nullable|min:0|max:999999|nullable|numeric',
            'dinner_duration'=>'nullable|integer',
            'hour_count'=>'required_with:hour_price|min:0|max:999999|nullable|numeric',
            'unit_count'=>'required_with:unit_price|min:0|max:999999|nullable|numeric',
            'remark'=>'nullable',
        ],[
            'required'=>':attribute 不应为空',
            'min'=>':attribute 不得为0或为负',
            'numeric'=>':attribute 必须为数字',
            'max'=>':attribute 输入值过大',
            'required_without'=>':attribute 计时与计件至少填一项',
            'date_format'=>':attribute 格式错误',
            'integer'=>':attribute 选择错误',
            'required_with'=>':attribute 必须填写',
        ],[
            'daily_id'=>'所属每日单',
            'user_id'=>'参与人',
            'started_at'=>'开始时间',
            'ended_at'=>'结束时间',
            'hour_price'=>'计时单价',
            'unit_price'=>'计件单价',
            'dinner_duration'=>'晚饭时间',
            'hour_count'=>'计时工时',
            'unit_count'=>'计件数量',
            'remark'=>'备注',
        ]);
        return $validator;
    }
}
