SignController.class.php 6.23 KB
<?php
/**
 * 【手机端】11-签到接口
 * Created by PhpStorm.
 * @author:wanghuan
 * @date:2017-09-01
 */

namespace Api\Controller\Education;

use Common\Service\EducationService;
use Common\Service\RightUsersService;
use Common\Service\SignService;
use Common\Service\PlanService;
use Common\Service\PlanDoService;

class SignController extends \Api\Controller\AbstractController
{

    /** @var SignService */
    protected $sign_s;
    /** @var PlanService */
    protected $plan_s;
    /** @var PlanDoService */
    protected $plan_do_s;
    /** @var RightUsersService */
    protected $right_users_s;

    public function before_action($action = '')
    {

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

            return false;
        }

        // 实例化签到service
        $this->sign_s = new SignService();
        // 实例化计划service
        $this->plan_s = new PlanService();
        // 实例化计划完成记录service
        $this->plan_do_s = new PlanDoService();
        // 实例化员工名单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');
        }

        // 培训详情
        $education = $this->get_education($ed_id);
        // 验证当前用户是否有签到权限
        $this->validate_sign_right($education);

        // 培训签到场次查询条件
        $plan_cond = [
            'ed_id' => $ed_id,
            'plan_type' => PlanService::PLAN_TYPE_SIGN,
            'plan_use_status' => PlanService::PLAN_STATUS_OPEN
        ];
        // 培训签到场次数据
        $plans = $this->plan_s->list_by_conds($plan_cond);

        // 培训没有设置签到,二维码已过期
        if (empty($plans)) {

            E('_ERR_QC_INVALID');
        }

        // 签到时间
        $now_time = MILLI_TIME;
        // 有效签到场次总数
        $sign_enable_count = 0;
        // 当前签到场次id
        $plan_id = 0;
        foreach ($plans as $value) {
            // 判断统计有效签到场次
            if ($value['plan_sign_end'] > $now_time) {

                $sign_enable_count += 1;
            }

            // 获取当前签到场次id
            if ($value['plan_sign_begin'] <= $now_time && $value['plan_sign_end'] >= $now_time) {

                $plan_id = $value['plan_id'];
            }
        }

        // 所有场次签到均已结束:签到时间已结束
        if (!$sign_enable_count) {

            E('_ERR_SIGN_TIME_END');
        }

        // 没有查询到当前签到场次:没有进行中签到场次
        if (!$plan_id) {

            E('_ERR_NO_SIGNING');
        }

        // 计划详情
        $plan_info = $this->plan_s->get($plan_id);
        // 计划状态为失效或者当前时间小于计划启用时间:计划未启用
        if ($now_time < $plan_info['plan_use_time']) {

            E('_ERR_SIGN_STATUS_CLOSE');
        }

        // 用户签到场次查询条件
        $plan_sign_cond = [
            'sign_uid' => $this->uid,
            'ed_id' => $ed_id,
            'plan_id' => $plan_id
        ];
        // 用户签到场次信息
        $user_plan_sign = $this->sign_s->get_by_conds($plan_sign_cond);

        // 已签到
        if (!empty($user_plan_sign)) {

            E('_ERR_SIGN_EXIST');
        }

        // 签到表待新增数据
        $plan_sign_cond['sign_on_time'] = $now_time;

        try {
            // 开始事务
            $this->sign_s->start_trans();

            // 签到表插入该用户的签到数据
            $result = $this->sign_s->insert($plan_sign_cond);

            // 签到成功
            if ($result) {
                // plan_do表待加数据
                $plan_do_data = [
                    'pd_uid' => $this->uid,
                    'plan_id' => $plan_id,
                    'ed_id' => $ed_id
                ];
                // plan_do表中添加数据
                $this->plan_do_s->insert($plan_do_data);
            } else { // 签到失败

                $this->sign_s->rollback();
            }

            $this->sign_s->commit();
        } catch (\Think\Exception $e) {

            $this->sign_s->rollback();
            E('_ERR_SIGN_FAIL');

            return false;
        }

        // 签到失败
        if (!$result) {

            E('_ERR_SIGN_FAIL');
        }

        return true;
    }

    /**
     * 验证用户是否有签到权限
     *
     * @param array $education
     */
    protected function validate_sign_right($education = [])
    {

        // 是否收费
        $ed_is_charge = $education['ed_is_charge'];
        // 是否审核
        $ed_is_check = $education['ed_is_check'];

        // 报名查询条件
        $right_cond = [
            'ru_uid' => $this->uid,
            'ed_id' => $education['ed_id']
        ];
        // 用户报名数据
        $right_users = $this->right_users_s->get_by_conds($right_cond);
        // 用户不在适用范围
        if (empty($right_users)) {
            // 没有签到权限
            E('_ERR_SIGN_NO_RIGHT');
        }

        // 开启报名
        if (EducationService::EDUCATION_SIGN_OPEN == $education['ed_is_sign_up']) {

            // 不收费、不审核、未报名,没有签到权限
            if (EducationService::EDUCATION_UN_CHARGE == $ed_is_charge
                && EducationService::EDUCATION_UN_CHECK == $ed_is_check
                && RightUsersService::SIGN_STATUS_SIGNED != $right_users['ru_sign_status']) {

                E('_ERR_SIGN_NO_RIGHT');
            }

            // 开启收费、未支付,没有签到权限
            if (EducationService::EDUCATION_CHARGE == $ed_is_charge
                && RightUsersService::SIGN_CHARGE_PAY != $right_users['ru_pay_status']) {

                E('_ERR_SIGN_NO_RIGHT');
            }

            // 开启审核、审核未通过,没有签到权限
            if (EducationService::EDUCATION_CHECK == $ed_is_check
                && RightUsersService::SIGN_CHECK_PASS != $right_users['ru_check_status']) {

                E('_ERR_SIGN_NO_RIGHT');
            }
        }
    }
}