EditController.class.php 11 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 EditController extends \Apicp\Controller\AbstractController
{

    /** @var ActivityService */
    protected $activity_s;
    /** @var PacketService */
    protected $packet_s;
    /** @var CommentService */
    protected $comment_s;
    /** @var PacketrecordService */
    protected $packetrecord_s;
    /** @var RightService */
    protected $right_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->packetrecord_s = new PacketrecordService();
        // 实例化权限表
        $this->right_s = new RightService();
        // 实例化人员公共类
        $this->user_s = new User();

        return true;
    }

    public function Index_post()
    {

        $params = I('post.');
        // 开始编辑保存活动信息
        $ac_id = $this->update_activity($params);

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

        return true;
    }

    /**
     * 编辑活动(后端)
     *
     * @param array $reqData 请求数据
     *
     * @return bool
     */
    public function update_activity($reqData)
    {

        $ac_id = intval($reqData['ac_id']);

        // 活动ID不能为空
        if (empty($ac_id)) {

            E('_ERR_AC_ID_EMPTY');
        }

        // 活动不存在
        if (!$old_activity = $this->activity_s->get($ac_id)) {

            E('_ERR_ARTICLE_NOT_FOUND');
        }

        // 获取用户提交的活动数据
        $activity = $this->activity_s->fetch_activity($reqData);

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

        // 已发布的不能编辑保存成草稿
        if (ActivityModel::ACTIVITY_PUBLISH == $old_activity['activity_status'] &&
            ActivityModel::ACTIVITY_DRAFT == $activity['activity_status']) {

            E('_ERR_ACTIVITY_STATUS');
        }

        // 已发布的活动只能延长结束时间,不能减短
        if (ActivityModel::ACTIVITY_PUBLISH == $old_activity['activity_status']) {

            if ($old_activity['end_time'] > $activity['end_time'] || $activity['end_time'] < MILLI_TIME) {

                E('_ERR_ACTIVITY_ENDTIME');
            }
        }

        // 更新最后更新时间
        $activity['last_time'] = MILLI_TIME;

        // 红包是否开启放到外层
        $activity['is_show_red'] = $activity['red_content']['is_show_red'];

        // 用户提交的新的权限数据
        $right = $activity['right'];
        $right['is_all'] = $activity['is_all'];
        unset($activity['right']);

        // 获取已有权限
        $old_right = $this->right_s->list_by_conds(['ac_id' => $ac_id]);

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

            // 组装活动入库数组
            $activityList = $activity;

            // 去除不需要的元素
            unset($activityList['red_content']);
            unset($activityList['right']);

            // 这里是指草稿状态变为发布状态
            // 草稿不生成红包记录数据,已发布的活动不能修改红包,已发布的不能保存为草稿
            if (ActivityService::ACTIVITY_DRAFT != $activity['activity_status'] &&
                ActivityModel::ACTIVITY_PUBLISH != $old_activity['activity_status']) {

                // 草稿发布的时候,加入发布时间
                if (ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {

                    $activityList['publish_time'] = MILLI_TIME;
                }

                // 是否开启红包
                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'],
                        ];
                        // 生成红包
                        $pid = $this->packet_s->insert($packetdate);

                        // 生成随机红包
                        $this->packetrecord_s->create_packet_record($ac_id, $pid, $activity['red_content']['rule_list'],
                            $activity['red_content']['red_base_num']);
                    }
                } else {
                    // 干掉红包
                    $this->packet_s->delete_by_conds(['ac_id' => $ac_id]);
                    // 若不开启红包,删除红包祝福语
                    $activityList['packet_bless'] = '';
                }
                // 草稿编辑为草稿
            } elseif (ActivityModel::ACTIVITY_DRAFT == $activity['activity_status'] &&
                ActivityModel::ACTIVITY_PUBLISH != $old_activity['activity_status']) {

                // 是否开启红包
                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'],
                        ];
                        // 修改红包信息
                        $this->packet_s->update_by_conds(['ac_id' => $ac_id], $packetdate);
                    }
                } else {
                    // 干掉红包
                    $this->packet_s->delete_by_conds(['ac_id' => $ac_id]);
                    // 若不开启红包,删除红包祝福语
                    $activityList['packet_bless'] = '';
                }
            }

            // 活动数据入库
            $this->activity_s->update($ac_id, $activityList);


            // 权限与参与人数入库
            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']]);
            }

            // 如果是全公司则删除数据库已有的权限
            if (ActivityService::IS_ALL == $activity['is_all'] && !empty($old_right)) {

                $this->right_s->delete_by_conds(['ac_id' => $ac_id]);
            }

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

            $this->activity_s->rollback();

            E('_ERR_ADD_FAILED');
        }


        // 发送消息
        if (ActivityModel::NOTICE_ON == $activity['is_notice'] &&
            ActivityModel::ACTIVITY_PUBLISH == $activity['activity_status']) {

            //【1】如果是由草稿 改为发布
            if (ActivityModel::ACTIVITY_DRAFT == $old_activity['activity_status']) {

                // 组装发送消息的数据
                $activity['right'] = $right;
                $params = $this->activity_s->assemble_msg_params($ac_id, $activity);
                // 发送消息
                $this->activity_s->send_msg($params, ActivityService::MSG_ACTIVITY_PUBLISH);
                // 给审核人发送消息
                $send_params = $this->activity_s->send_check_msg_params($ac_id, $activity);
                // 如果开启移动端首页推荐
                if (Helper::SALE_INDEX_RECOMMEND == $activity['is_recommend']) {

                    $this->activity_s->send_recommend($ac_id, $activity);
                }

            } else {
                //【2】如果是由已发布改为发布

                // 获取需要发送消息的所有用户
                $msg_users = $this->right_s->right_to_all($old_right, $right);

                // 发送新消息
                if (!empty($msg_users['add'])) {

                    $activity['right']['uids'] = $msg_users['add'];
                    // 组装发送消息的数据
                    $params = $this->activity_s->assemble_msg_params($ac_id, $activity, 'edit');
                    // 发送发布活动消息
                    $this->activity_s->send_msg($params, ActivityService::MSG_ACTIVITY_PUBLISH);
                }
                // 给审核人发送消息
                $send_params = $this->activity_s->send_check_msg_params($ac_id, $activity, $old_activity);
            }

            // 如果开启审核
            if ($activity['is_check_open'] == ActivityService::IS_CHECK_OPEN) {

                // 如果开启审核且有审核人变更
                if (!empty($send_params['uids'])) {
                    // 给审核人发送消息
                    $this->activity_s->send_msg($send_params, ActivityService::MSG_ACTIVITY_PUBLISH_CHECK_USER);
                }
            }
        }

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

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

        return intval($ac_id);
    }
}