<?php
namespace app\api\controller\v1;
use app\api\controller\Base;
use app\hander\HelpHander;
use think\App;
use think\Db;

class SuperviseRecord extends Base
{
    public function add(){
        $data = [
            'org_id'=>$this->orgId,
            'user_id'=>$this->userId,
            'supervise_id'=>input('superviseId',0),
            'content'=>input('content','','trim'),
            'create_time'=>date('Y-m-d H:i:s'),
        ];
        if($data['supervise_id'] < 1){
            HelpHander::error('参数错误');
        }


        $content = json_decode($data['content'],true);

        if(!$content || count($content) < 1){
            HelpHander::error('内容不能为空');
        }
        $score = 0;
        $score1 = 0;//总分
        foreach ($content as $k=>$v){
            $subscore = 0;

            foreach ($v['sub'] as $kk=>$vv){
                $score1+=$vv['score'];
                $subsubscoreAll = 0;
                foreach ($vv['sub'] as $kkk=>$vvv){
                    $subsubscoreAll +=$vvv['score'];
                }
               if($vv['scored'] == ''){
                   HelpHander::error('扣分不能为空');
               }else{
                   if($vv['scored'] < 0){
                       HelpHander::error('扣分不能为空');
                   }
               }

                if($vv['scored'] > $subsubscoreAll){
                    HelpHander::error('扣分不能大于考核总分值');
                }

                $subscore +=$vv['scored'];
            }
            $score +=$subscore;
        }

        $data['score'] = $score;
        $data['score1'] = $score1;
        $data['score2'] = $score1-$score;
        $add = Db::name('supervise_record')->insert($data);

        if(!$add){
            HelpHander::error('操作失败');
        }
        HelpHander::success([],'操作成功');
    }


    public function list(){
        $page = input('page',1);
        $size = input('size',10);

        $list = Db::name('supervise_record')
            ->field('id,score,score1,score2,create_time,org_id,supervise_id')
            ->where('user_id',$this->userId)
            ->where('del',0)
            ->page($page,$size)
            ->order('id desc')
            ->select();

        foreach ($list as $k=>$v){
            $list[$k]['org_name'] = Db::name('org')->where('id',$v['org_id'])->value('name');
            $list[$k]['supervise_name'] = Db::name('supervise')->where('id',$v['supervise_id'])->value('title');
            $list[$k]['score'] = $v['score2'];

        }

        HelpHander::success($list);
    }


    public function detail(){
        $id = input('id',0);

        $info = Db::name('supervise_record')->field('id,org_id,user_id,supervise_id,score,content,create_time')->where('id',$id)->find();

        if(!$info){
            HelpHander::success('参数错误');
        }

        $info['supervise_title'] = Db::name('supervise')->where('id',$info['supervise_id'])->value('title');
        $info['user_name'] = Db::name('user')->where('id',$info['user_id'])->value('real_name');
        $info['org_name'] = Db::name('org')->where('id',$info['org_id'])->value('name');
        $info['content'] = json_decode($info['content'] ,true);

        HelpHander::success($info);

    }



}