UserMedalDeleteController.class.php 3.57 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * 勋章列表
 * User: zhoutao
 * Reader: zhoutao 2017-05-31 10:07:35
 * Date: 2017-05-24 15:43:49
 */

namespace Apicp\Controller\Medal;

use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Msg;
use Common\Model\MedalModel;
use Common\Service\MedalLogService;
use Common\Service\MedalService;
use Common\Service\MemberMedalService;

class UserMedalDeleteController extends AbstractController
{
    /**
     * UserMedalDelete
     * @author tangxingguo
     * @desc 人员勋章删除接口
     * @param String uid:true 人员ID
     * @param Array ml_ids 勋章记录ID
     * @param String description 描述
     * @return Array
     */
    public function index()
    {
        $rules = [
            'uid' => 'require',
            'ml_ids' => 'require|array',
            'description' => 'max:80',
        ];

        // 验证请求数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $postData = $validate->postData;
        $uid = $postData['uid'];
        $mlIds = $postData['ml_ids'];
        $desc = $postData['description'];

        $medalServ = new MedalService();
        $medalLogServ = new MedalLogService();
        $memMedalServ = new MemberMedalService();

        // 取勋章记录
        $logList = $medalLogServ->list_by_conds(['ml_id' => $mlIds]);
        if (!empty($logList)) {
            // 取人员勋章信息
            $imIds = array_column($logList, 'im_id');
            $userMedalList = $memMedalServ->list_by_conds(['im_id' => $imIds, 'mem_uid' => $uid]);

            // 勋章信息
            $medalList = $medalServ->list_by_conds(['im_id' => $imIds]);
            if (!empty($medalList)) {
                $medalList = array_combine_by_key($medalList, 'im_id');
            }

            // 统计每个勋章待删除的个数
            $imIdCount = array_count_values($imIds);

            // 删除 或 减少人员勋章
            foreach ($userMedalList as $v) {
                // 获得的勋章数 > 删除的勋章数
                if ($v['im_total'] > $imIdCount[$v['im_id']]) {
                    $data = ['im_total = im_total - ?' => $imIdCount[$v['im_id']]];
                    $memMedalServ->update_by_conds(['im_id' => $v['im_id'], 'mem_uid' => $uid], $data);

                // 获得的勋章数 <= 删除的勋章数
                } else {
                    $memMedalServ->delete_by_conds(['im_id' => $v['im_id'], 'mem_uid' => $uid]);
                }

                // 勋章存在,发送消息
                if (isset($medalList[$v['im_id']])) {
                    $this->_sendAwardNotice($uid, $medalList[$v['im_id']]['name'], $desc);
                }
            }

            // 删除勋获取记录
            $medalLogServ->delete($mlIds);
        }
    }

    /**
     * @desc 消息推送
     * @author tangxingguo
     * @param array $uid 用户信息
     * @param array $medalName 勋章名称
     * @param string $desc 描述
     * @return bool;
     */
    private function _sendAwardNotice($uid, $medalName, $desc = '')
    {
        if (!empty($desc)) {
            $desc = "\n内容:{$desc}";
        } else {
            return;
        }

        $msgServ = &Msg::instance();
        $msg_data = [
            [
                'title' => "您的【{$medalName}】勋章已被管理员移除",
                'description' => "移除时间:" . rgmdate(MILLI_TIME, 'Y-m-d H:i') . $desc,
                'url' => oaUrl('Frontend/Index/Medal/Index'),
            ]
        ];

        $msgServ->sendNews($uid, null, null, $msg_data);
    }
}