<?php
namespace app\common\model;

use app\hander\HelpHander;
use think\Db;
use think\Model;

class PostSalary extends Model
{

    public function add(){
        $data = [
            'id' => input('id/d',0),
            'title' => input('title','','trim'),
            'job' => input('job','','trim'),
            'type' => input('type/d',0),
            'money' => input('money/f',0),
            'company_id' => input('companyId/d',0),
            'fixed_performance' => input('fixedPerformance/f',0),
            'month_performance' => input('monthPerformance/f',0),
            'year_performance' => input('yearPerformance/f',0),
            'fixed_performance2' => input('fixedPerformance2/f',0),
            'month_performance2' => input('monthPerformance2/f',0),
            'year_performance2' => input('yearPerformance2/f',0),
        ];
        $logdata = json_encode($data);

        $result = validate('PostSalary')->check($data,[],'');
        if(true !== $result){
            HelpHander::error(validate('PostSalary')->getError());
        }
        $id = $data['id'];
        unset($data['id']);
        if($id > 0){
            $data['update_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data,['id'=>$id]);
        }else{
            $data['create_time'] = date('Y-m-d H:i:s');
            $ret = $this->allowField(true)->save($data);
        }
        if(!$ret){
            HelpHander::error('操作失败');
        }

        if($id > 0){
            $content = '修改岗位等级';
        }else{
            $content = '添加岗位等级';
        }
        model('ActionLog')->add(6,$content,0,$logdata);

        return true;
    }

    public function info($id){
        $info = $this->where('id',$id)->find();
        if(!$info){
            HelpHander::error('数据不存在');
        }
        return $info->toArray();
    }

    public function lists($page,$size,$type,$companyId){
        $map[] = ['del','=',0];
        if($companyId > 0){
            $map[] = ['company_id','=',$companyId];
        }
        if(in_array($type,[1,2,3])){
            $map[] = ['type','=',$type];
        }
        $lists = $this
            ->where($map)
            ->page($page,$size)
            ->order('id desc')
            ->select();
        $lists = $lists?$lists->toArray():[];
        foreach ($lists as $k=>$v){
            $lists[$k]['company'] = Db::name('company')->where('id',$v['company_id'])->value('title');
        }
        $total = $this->where($map)->count();
        $data = [
            'total' => $total,
            'list' => $lists
        ];
        return $data;
    }

    public function del($id){
        $ret = $this->where('id',$id)->setField('del',1);
        if(!$ret){
            HelpHander::error('删除失败');
        }
        $logdata = json_encode(['id' => $id]);
        model('ActionLog')->add(6,'删除岗位等级',0,$logdata);
        return true;
    }

    public function all(){
        $map[] = ['del','=',0];
        $lists = $this
            ->where($map)
            ->field('id,title,type,company_id')
            ->order('id desc')
            ->select();

        return $lists?$lists->toArray():[];
    }

    public function center(){
        $map[] = ['del','=',0];
        $map[] = ['company_id','=',1];
        $lists = $this
            ->where($map)
            ->field('id,title,job,type')
            ->order('type asc,id asc')
            ->select();

        return $lists?$lists->toArray():[];
    }

}