<?php
/*
 本代码由 焕岱科技 创建
 技术支持 WX：15285007200
 严禁反编译、逆向等任何形式的侵权行为，违者将追究法律责任
*/


namespace app\admin\controller\bbfxshop\workorder;

use app\admin\controller\bbfxshop\Base;
use think\Db;


class User extends Base
{
    
    public $model = null;
    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\common\model\bbfxshop\Workorder;
    }
    
    public function all(){
        $where = array("uniacid"=>UNIACID);
        
        $list = Db::name("bbfx_workorder_user")->where($where)->order('id desc')->select();
        
        __success("操作成功",['list'=>$list]);
    }
    
    public function getList(){
        $params = $this->param();
        
        $pindex = max(1, intval($params["page"]));
        $psize = intval($params["limit"])?intval($params["limit"]):10;
        $category_id = intval($params['category_id']);
        $where = ["a.uniacid"=>UNIACID];
        if($category_id > 0){
            $where['a.category_id'] = $category_id;
        }
        
        $list = Db::name("bbfx_workorder_user")->alias("a")->join("bbfx_workorder_category c","a.category_id=c.id","LEFT")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->field("a.*,c.title as category_name,b.nickname,b.avatar")->order('a.id desc')->limit(($pindex - 1) * $psize,$psize)->select();
        $total = Db::name("bbfx_workorder_user")->alias("a")->join("bbfx_workorder_category c","a.category_id=c.id","LEFT")->join("bbfx_user b","a.user_id=b.id","LEFT")->where($where)->count();
        
        foreach ($list as $key => &$val){
            $val['image'] = cdnurl($val['image']);
            $val['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
            if(empty($val['avatar'])){
                $val['avatar'] = "/assets/images/avatar.jpg";
            }
            if(empty($val['nickname'])){
                $val['nickname'] = $val['name'];
            }
            $val['avatar'] = cdnurl($val['avatar']);
        }
        unset($val);
        
        __success("操作成功",['total'=>$total,'list'=>$list]);
    }
    
    public function detail(){
        $params = $this->param();
        
        $info = Db::name("bbfx_workorder_user")->where("id",$params['id'])->find();
        
        if(empty($info)){
            __error("数据不存在");
        }
        $info['image'] = cdnurl($info['image']);
        
        __success('操作成功',$info);
    }
    
    public function post(){
        $params = $this->param();
        $data = array(
            "uniacid"=>UNIACID,
            "name"=>$params['name'],
            "user_id"=>intval($params['user_id']),
            "category_id"=>intval($params['category_id']),
            "status"=>intval($params['status']),
            "updatetime"=>time()
        );
        if($data['user_id'] <= 0){
            __error("请选择用户");
        }
        if($data['category_id'] <= 0){
            __error("请选择分类");
        }

        $user = Db::name("bbfx_workorder_user")->where(['uniacid'=>UNIACID,'user_id'=>$data['user_id']])->find();
        if(!empty($user)){
            if(empty($params['id']) || (!empty($params['id']) && $params['id'] != $user['id'])){
                __error("该用户已被绑定");
            }
        }

        if(!empty($params['id'])){
            $res = Db::name("bbfx_workorder_user")->where("id",$params['id'])->update($data);
            $data['id'] = $params['id'];
        }
        else{
            $data['createtime'] = time();
            $res = Db::name("bbfx_workorder_user")->insertGetId($data);
            $data['id'] = $res;
        }
        
        if($res){
            \app\admin\model\AdminLog::record("","",$data,"bbfx_workorder_user");
            __success("编辑成功");
        }
        else{
            __success("操作成功");
        }
    }
    
    public function delete(){
        $this->commonDelete('bbfx_workorder_user');
    }
}