<?php
namespace app\common\model;

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

class UserBl extends Model
{

    public function add(){
        $data = [
            'id' => input('id/d',0),
            'p_bl' => input('pBl/f',0),
            'c_bl' => input('cBl/f',0),
            'p_bl_type' => input('pBlType/d',0),
            'c_bl_type' => input('cBlType/d',0),
            'p_bl_extra' => input('pBlExtra/f',0),
            'c_bl_extra' => input('cBlExtra/f',0),
            'p_free_bl' => input('pFreeBl/f',0),
            'c_free_bl' => input('cFreeBl/f',0),
        ];
        if($data['id'] <= 0){
            HelpHander::error('参数错误');
        }

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

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


    public function lists($page,$size,$companyId,$orgId){
        if($companyId > 0){
            $map[] = ['ur.company_id','=',$companyId];
        }
        $map = empty($map)?true:$map;
        $lists = Db::name('user_bl')
            ->alias('ur')
            ->join('company c','c.id = ur.company_id')
            ->where($map)
            ->field('ur.*,c.title as company')
            ->page($page,$size)
            ->order('ur.id asc')
            ->select();

        $total = Db::name('user_bl')
            ->alias('ur')
            ->join('company c','c.id = ur.company_id')
            ->where($map)->count();
        $data = [
            'total' => $total,
            'list' => $lists?$lists:[]
        ];
        return $data;
    }

}