Company.php 13.2 KB
<?php
namespace app\mobile\controller;

use think\Validate;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use app\common\controller\Api;
use app\mobile\model\CompanyUser;
use app\mobile\model\CourseOrder;
use app\mobile\model\CourseAppraise;
use app\mobile\model\SecretOrder;
use app\mobile\model\Package;
use app\mobile\model\PackageOrder;
use addons\epay\library\Service;

/**
 * 我的(企业)接口
 * @ApiWeigh (65)
 */
class Company extends Api
{
	protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];
    protected $company_id = 0;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\mobile\model\Company;
        if(!$this->company_id){
            $user = $this->auth->getUser();
            if($user['group_id'] == 0){
                $this->error('您还不是企业管理员');
            }
            $this->company_id = $this->model->where('user_id',$user['id'])->value('id');
        }
    }

    /**
     * @ApiWeigh (99)
     * @ApiTitle    (我的密卷)
     * @ApiSummary  (我的密卷)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1, 
        "msg": "成功", 
        "time": "1600499807", 
        "data": [
            {
                "id": 10, //密卷订单ID
                "pay_price": "500.00", //实际支付金额
                "people_num": 20, //密卷规格人数(斜杠后数字)
                "is_top": "1", //是否顶配:0=否,1=是
                "secret": {
                    "id": 1, //密卷ID
                    "title": "测试密卷", 
                    "do_num": 20
                }, 
                "user_count": 3 //当前企业下员工含管理员数量(斜杠前数字)
            }
        ]
    })
     */
    public function mySecret(){
        // 查找所有密卷
        $list = SecretOrder::with(['secret'])
            ->where('company_id',$this->company_id)
            ->where('user_id',$this->auth->id)
            ->where('pay_status','1')
            ->select();
        $user_count = CompanyUser::where('company_id',$this->company_id)
                ->where('status','1')
                ->count();
        foreach ($list as $v) {
            // 当前企业下员工含管理员人数
            $v['user_count'] = $user_count + 1;
            $v->visible(['id','pay_price','people_num','is_top','secret'])->append(['user_count']);
            $v->getRelation('secret')->visible(['id','title']);
        }
        $this->success('成功',$list);
    }

    /**
     * @ApiWeigh (97)
     * @ApiTitle    (我的课程)
     * @ApiSummary  (我的课程)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600081718",
        "data": [{
            "id": 9, //课程订单ID
            "pay_price": "50.00", //实际支付金额
            "people_num": 20, //限制人数(斜杠后数字)
            "is_top": "0", //是否顶配:0=否,1=是
            "course": { //课程信息
                "id": 1, //课程ID
                "title": "测试课程", //课程标题
                "cover": "" //课程封面图
            },
            "user_count": 0 //当前企业下员工含管理员数量(斜杠前数字)
            "is_have_appraise": 0 //是否已评价:0=否,1=是
        }]
    })
     */
    public function myCourse(){
        // 查找所有密卷
        $list = CourseOrder::with(['course'])
            ->where('company_id',$this->company_id)
            ->where('user_id',$this->auth->id)
            ->where('pay_status','1')
            ->select();
        $user_count = CompanyUser::where('company_id',$this->company_id)
                ->where('status','1')
                ->count();
        foreach ($list as $v) {
            // 当前企业下员工含管理员人数
            $v['user_count'] = $user_count + 1;
            // 是否已评价
            $have_appraise = CourseAppraise::where('user_id',$this->auth->id)
                ->where('course_id',$v['course_id'])
                ->where('course_order_id',$v['id'])
                ->field('id')
                ->find();
            $v['is_have_appraise'] = !empty($have_appraise) ? 1 : 0;
            $v->visible(['id','pay_price','people_num','is_top','course'])->append(['user_count','is_have_appraise']);
            $v->getRelation('course')->visible(['id','cover','title']);
        }
        $this->success('成功',$list);
    }

    /**
     * @ApiWeigh (95)
     * @ApiTitle    (企业成员管理)
     * @ApiSummary  (企业成员管理)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="status", type="string", required=false, description="0=未审核,1=已通过")
     * @ApiParams   (name="page", type="inter", required=false, description="当前页(默认1)")
     * @ApiParams   (name="page_num", type="inter", required=false, description="每页显示数据个数(默认10)")
     *
     * @ApiReturn({
        "code": 1, 
        "msg": "成功", 
        "time": "1600511589", 
        "data": {
            "total": 2, //数据总数
            "list": [
                {
                    "id": 1, //申请ID
                    "name": "何先生", //申请人名称
                    "createtime": "2020.09.12" //申请时间
                }
            ]
        }
    })
     */
    public function companyUser()
    {
        $status = $this->request->param('status','0');
        $page = $this->request->param('page', 1, 'intval');
        $page_num = $this->request->param('page_num', 10, 'intval');
        $data = CompanyUser::where('company_id',$this->company_id)
            ->where('status',$status)
            ->paginate($page_num,false,['page'=>$page])
            ->each(function($v){
                $v['createtime'] = date('Y.m.d',$v['createtime']);
                $v->visible(['id','name','createtime']);
            })->toArray();
        $this->success('成功',['total'=>$data['total'],'list'=>$data['data']]);
    }

    /**
     * @ApiWeigh (93)
     * @ApiTitle    (企业成员管理-同意加入)
     * @ApiSummary  (企业成员管理-同意加入)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_user_id", type="string", required=false, description="申请ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": null
    })
     */
    public function companyUserAgree()
    {
        $company_user_id = $this->request->param('company_user_id');
        empty($company_user_id) && $this->error('缺少必需参数');
        $info = CompanyUser::get($company_user_id);
        empty($info) && $this->error('申请信息不存在');
        $info->save(['status'=>'1']);
        $this->success('同意加入成功');
    }

    /**
     * @ApiWeigh (91)
     * @ApiTitle    (企业成员管理-拒绝加入)
     * @ApiSummary  (企业成员管理-拒绝加入)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_user_id", type="string", required=false, description="申请ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": null
    })
     */
    public function companyUserRefuse()
    {
        $company_user_id = $this->request->param('company_user_id');
        empty($company_user_id) && $this->error('缺少必需参数');
        $info = CompanyUser::get($company_user_id);
        empty($info) && $this->error('申请信息不存在');
        $info->save(['status'=>'2']);
        $this->success('拒绝成功');
    }

    /**
     * @ApiWeigh (89)
     * @ApiTitle    (企业成员管理-移出团队)
     * @ApiSummary  (企业成员管理-移出团队)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="company_user_id", type="string", required=false, description="申请ID")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": null
    })
     */
    public function companyUserRemove()
    {
        $company_user_id = $this->request->param('company_user_id');
        empty($company_user_id) && $this->error('缺少必需参数');
        $info = CompanyUser::get($company_user_id);
        empty($info) && $this->error('申请信息不存在');
        $info->delete();
        $this->success('移出成功');
    }

    /**
     * @ApiWeigh (87)
     * @ApiTitle    (企业成员管理-企业邀请码)
     * @ApiSummary  (企业成员管理-企业邀请码)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": {
            "invite_code": 376581, //企业邀请码
        }
    })
     */
    public function inviteCode()
    {
        $invite_code = $this->model->where('id',$this->company_id)->value('invite_code');
        $this->success('成功',compact('invite_code'));
    }

    /**
     * @ApiWeigh (85)
     * @ApiTitle    (企业成员管理-更新企业邀请码)
     * @ApiSummary  (企业成员管理-更新企业邀请码)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": {
            "invite_code": 376581, //企业邀请码
        }
    })
     */
    public function setInviteCode()
    {
        $invite_code = (new User)->setInviteCode($this->company_id);
        $this->success('更新邀请码成功',compact('invite_code'));
    }

    /**
     * @ApiWeigh (83)
     * @ApiTitle    (企业套餐)
     * @ApiSummary  (企业套餐)
     * @ApiMethod   (POST)
     *
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="is_buy", type="inter", required=false, description="0=未购买,1=已购买")
     * @ApiParams   (name="order", type="inter", required=false, description="排序方式:0=最新,1=购买最多,2=价格最低,3=优惠最大")
     *
     * @ApiReturn({
        "code": 1,
        "msg": "成功",
        "time": "1600167441",
        "data": {
            "invite_code": 376581, //企业邀请码
        }
    })
     */
    public function package()
    {
        $user_id = $this->auth->id;
        $is_buy = $this->request->param('is_buy',0);
        $order = $this->request->param('order',0);
        switch ($order) {
            case 0:
                $order = ['p.createtime' => 'desc'];
                break;
            case 1:
                $order = ['buy_num' => 'desc'];
                break;
            case 2:
                $order = ['p.current_price' => 'asc'];
                break;
            case 3:
                $order = ['preferential_price' => 'asc'];
                break;
        }
        $package_order = Db::name('mobile_package_order')
            ->where('pay_status','1')
            ->field('id,package_id,people_num')
            ->order(['is_top'=>'desc','people_num'=>'desc'])
            ->limit(1)
            ->buildSql();
        $list = Package::alias('p')
            ->join(['select id,package_id,people_num from mobile_package_order where pay_status = 1 and user_id = '.$user_id.'and pay_status = 1 order by is_top,people_num desc limit 1'=>'po'],'p.id = po.package_id','left')
            ->join('mobile_package_cart pc','pc.package_id = p.id and pc.user_id='.$user_id,'left')
            ->field("
                p.*,
                if(po.id > 0,1,0) is_buy,
                if(pc.id > 0,1,0) is_add_cart,
                IFNULL((select SUM(current_price) from fa_mobile_secret where id in (p.secret_ids)),0) +
                IFNULL((select SUM(current_price) from fa_mobile_course where id in (p.course_ids)),0) original_price,
                p.buy_num_virtual + p.buy_num_real buy_num,
                IFNULL((select SUM(current_price) from fa_mobile_secret where id in (p.secret_ids)),0) +
                IFNULL((select SUM(current_price) from fa_mobile_course where id in (p.course_ids)),0) - p.current_price preferential_price
            ")
            ->order($order)
            ->having("is_buy = {$is_buy}")
            ->select();
            // halt(Package::getLastSql());
        foreach ($list as $v) {
            $v->visible([
                'id',
                'title',
                'current_price',
            ])->append([
                'is_buy',
                'is_add_cart',
                'original_price',
                'buy_num',
                'preferential_price',
                'secret_list',
                'course_list',
            ]);
        }
        $this->success('成功',$list);
    }
}