AddController.class.php 7.37 KB
<?php
/**
 * 【销售活动-后台】添加活动接口
 *
 * User: WJY
 * Date: 2017-11-02
 */

namespace Apicp\Controller\Activity;

use Common\Common\AttachOperation;
use Common\Common\Helper;
use Common\Common\User;
use Common\Model\ActivityModel;
use Common\Service\ActivityService;
use Common\Service\CommentService;
use Common\Service\PacketrecordService;
use Common\Service\PacketService;
use Common\Service\RightService;
use Think\Exception;

class AddController extends \Apicp\Controller\AbstractController
{

    /** @var ActivityService */
    protected $activity_s;
    /** @var PacketService */
    protected $packet_s;
    /** @var CommentService */
    protected $comment_s;
    /** @var RightService */
    protected $right_s;
    /** @var PacketrecordService */
    protected $packetrecord_s;
    /** @var User */
    protected $user_s;

    public function before_action($action = '')
    {

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

            return false;
        }

        // 实例化活动信息表
        $this->activity_s = new ActivityService();
        // 实例化红包表
        $this->packet_s = new PacketService();
        // 实例化评论表
        $this->comment_s = new CommentService();
        // 实例化权限表
        $this->right_s = new RightService();
        // 实例化红包记录表
        $this->packetrecord_s = new PacketrecordService();
        // 实例化用户表
        $this->user_s = new User();

        return true;
    }

    public function Index_post()
    {

        $params = I('post.');

        // 进行活动添加操作
        $ac_id = $this->add_activity($params);

        $this->_result = [
            'ac_id' => $ac_id,
        ];

        return true;
    }

    /**
     * 新增活动(后端)
     *
     * @param array $reqData 请求数据
     *
     * @return mixed
     */
    protected function add_activity($reqData)
    {
        $ac_id = 0;
        // 获取活动数据,类型为添加
        $activity = $this->activity_s->fetch_activity($reqData);

        // 验证数据
        $this->activity_s->validate_for_add($activity);

        // 开始时间不能小于当前时间
        if ($activity['begin_time'] <= MILLI_TIME) {

            E('_ERR_BEGINTIME_LT_NOWTIME');
        }

        // 结束时间不能小于当前时间
        if ($activity['end_time'] > 0 && $activity['end_time'] <= MILLI_TIME) {

            E('_ERR_ENDTIME_LT_NOWTIME');
        }

        // 是否是立即发布活动
        if (ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {

            $activity['publish_time'] = MILLI_TIME;
        }

        // 将最后更新时间设为当前时间
        $activity['last_time'] = MILLI_TIME;
        // 将是否开启红包放在外层
        $activity['is_show_red'] = $activity['red_content']['is_show_red'];
        // 重新定义权限数组
        $right = $activity['right'];
        unset($activity['right']);

        try {
            $this->activity_s->start_trans();

            // 活动数据入库
            $ac_id = $this->activity_s->insert($activity);

            // 是否开启红包
            if (ActivityService::RED_OPENED == $activity['is_red_open']) {
                // 是否为随机比例红包
                if (ActivityService::RAND_PRO_RED == $activity['red_content']['red_type']) {

                    $packetdate = [
                        'ac_id' => $ac_id,
                        'type' => $activity['red_content']['red_type'],
                        'red_base_num' => $activity['red_content']['red_base_num'],
                        'max_total' => $activity['red_content']['red_get_num'],
                        'red_content' => serialize($activity['red_content']['rule_list']),
                        'warn_mobile' => $activity['red_content']['warn_mobile'],
                        'is_show_red' => $activity['red_content']['is_show_red']
                    ];
                    // 存储红包配置数据
                    $pid = $this->packet_s->insert($packetdate);

                    // 已发布的活动
                    if (ActivityService::DRAFT_STATUS != $activity['activity_status']) {
                        // 生成红包记录
                        $this->packetrecord_s->create_packet_record($ac_id, $pid, $activity['red_content']['rule_list'],
                            $activity['red_content']['red_base_num']);
                    }
                }
            } else {
                // 若不开启红包,删除红包祝福语
                $activity['packet_bless'] = '';
                // 干掉抢红包图标信息
                $activity['is_show_red'] = ActivityService::SHOW_RED_CLOSE;
            }

            // 权限入库
            if (ActivityService::IS_ALL != $activity['is_all']) {
                // 如果权限不是全公司
                $this->right_s->save_data(['ac_id' => $ac_id], $right);
                // 获取活动参与权限总数入库
                $unjoin_num = $this->comment_s->count_unjoin(['ac_id' => $ac_id], []);
                $this->activity_s->update($ac_id, ['join_total' => $unjoin_num['unjoin_num']]);
            } else {
                // 如果权限是全公司
                $should_join_num = $this->user_s->listByConds();
                $this->activity_s->update($ac_id, ['join_total' => $should_join_num['total']]);
            }

            $this->activity_s->commit();
        } catch (Exception $e) {

            $this->activity_s->rollback();
            E('_ERR_ADD_FAILED');
        }

        // 如果是已发布状态
        if (ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {
            $activity['right'] = $right;
            //【发送消息】状态为发布状态并且开启发送消息时则发送消息
            if (ActivityModel::NOTICE_ON == $activity['is_notice'] && $ac_id) {
                // 组装发送消息的数据
                $params = $this->activity_s->assemble_msg_params($ac_id, $activity);

                // 发送消息
                $this->activity_s->send_msg($params, ActivityService::MSG_ACTIVITY_PUBLISH);

            }

            // 如果开启审核且是已发布状态
            if ($activity['is_check_open'] == ActivityService::IS_CHECK_OPEN) {

                // 给审核人发送消息
                $send_params = $this->activity_s->send_check_msg_params($ac_id, $activity);
                // 给审核人发送消息
                $this->activity_s->send_msg($send_params, ActivityService::MSG_ACTIVITY_PUBLISH_CHECK_USER);

            }

            // 如果开启移动端首页推荐
            if (Helper::SALE_INDEX_RECOMMEND == $activity['is_recommend']) {
                $this->activity_s->send_recommend($ac_id, $activity);
            }
        }

        // 附件操作
        $attach_serv = new AttachOperation();
        $attach_ids = [];
        // 封面图
        if (!empty($activity['cover_id'])) {
            $attach_ids[] = $activity['cover_id'];
        }
        // 内容中的封面图
        if (!empty($activity['content'])) {
            $content_at_ids = $attach_serv->match_at_ids($activity['content']);
            $attach_ids = array_merge($attach_ids, $content_at_ids);
        }

        $attach_serv->insert_attach(
            APP_DIR,
            'activity',
            $ac_id,
            ['attach_ids' => $attach_ids]
        );

        return $ac_id;
    }
}