AddController.class.php 3.12 KB
<?php
/**
 * 【手机端】07-发布照片接口
 * Created by PhpStorm.
 * @author:wanghuan
 * @date:2017-08-31
 */

namespace Api\Controller\Pic;

use Common\Common\AttachOperation;
use Common\Service\PlanService;
use Common\Service\PlanPicService;
use Common\Service\RightUsersService;

class AddController extends \Api\Controller\AbstractController
{

    /** @var PlanService */
    protected $plan_s;
    /** @var PlanPicService */
    protected $pic_s;
    /** @var RightUsersService */
    protected $right_users_s;

    public function before_action($action = '')
    {

        if (!parent::before_action($action)) {

            return false;
        }

        // 实例化培训计划service
        $this->plan_s = new PlanService();
        // 实例化照片service
        $this->pic_s = new PlanPicService();
        // 实例化员工名单service
        $this->right_users_s = new RightUsersService();

        return true;
    }

    /**
     * 发布照片
     */
    public function Index_post()
    {

        // 培训id
        $ed_id = I('post.ed_id', 0, 'intval');
        // 培训id为空
        if (!$ed_id) {

            E('_EMPTY_ED_ID');
        }

        // 培训详情
        $this->get_education($ed_id);

        // 适用范围查询条件
        $right_cond = [
            'ru_uid' => $this->uid,
            'ed_id' => $ed_id
        ];
        // 查询当前员工适用范围数据
        $user_right = $this->right_users_s->get_by_conds($right_cond);
        // 当前员工不在适用范围
        if (empty($user_right)) {

            E('_ERR_ED_NO_RIGHT');
        }

        // 照片墙id
        $plan_id = I('post.plan_id', 0, 'intval');
        // 照片墙id为空
        if (!$plan_id) {

            E('_EMPTY_PIC_PLAN_ID');
        }

        // 照片墙信息
        $plan = $this->plan_s->get($plan_id);
        if (!$plan) {

            E('_ERR_PIC_PLAN_EXIST');
        }

        // 照片描述
        $pic_name = I('post.pic_name');
        // 照片描述为空
        if (!$pic_name) {

            E('_EMPTY_PIC_NAME');
        }

        // 照片附件id
        $pic_at_id = I('post.pic_at_id');
        // 照片附件id为空
        if (!$pic_at_id) {

            E('_EMPTY_PIC_AT_ID');
        }

        // 待新增数据
        $data = [
            'pic_name' => $pic_name,
            'pic_at_id' => $pic_at_id,
            'pic_uid' => $this->uid,
            'plan_id' => $plan_id,
            'ed_id' => $ed_id
        ];

        // 查询照片数据
        $pic = $this->pic_s->get_by_conds($data);
        // 已发布过名称相同照片提示:已发布过相同描述的照片(避免连击导致多次插入)
        if (!empty($pic)) {

            E('_ERR_PIC_EXIST');
        }

        // 新增
        $result = $this->pic_s->insert($data);
        // 新增失败
        if (!$result) {

            E('_EMPTY_ADD_PIC_FAIL');
        }

        $attach_serv = new AttachOperation();
        $attach_serv->insert_attach(
            APP_DIR,
            'plan',
            $plan_id,
            ['attach_ids' => [$pic_at_id]]
        );

        return true;
    }
}