<?php
namespace app\admin\server\index;

use app\admin\model\Activity;
use app\admin\model\SystemMenu;
use support\Redis;

class ActivityServer
{
    /**
     * Notes:获取活动列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2023/2/23
     */
    public static function getActivityList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  Activity::getActivityList($page, $limit, $keywords);

        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes: 添加活动
     * @param string $goods_name
     * @param array $goods_rules
     * @return int
     * User: YCP
     * Date: 2023/2/23
     */
    public static function insertActivity(array $params, int $admin_id)
    {

        Activity::affairBegin();
        try {
            $params['config_create_time'] = time();
            $result =  Activity::insertGetId($params);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加营销活动-编号: ' . $result;
                plog('life-activity-create', '添加营销活动', $msg);
                Activity::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            Activity::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:修改活动
     * @param string $goods_name
     * @param int $goods_id
     * @return int
     * User: YCP
     * Date: 2023/2/23
     */
    public static function updateActivity(array $params, int $admin_id)
    {
        Activity::affairBegin();
        try {     
            $where = [];
            $where['config_id'] = $params['config_id'];
            $info = Activity::where($where)->first();
            if(empty($info) || $info === false)
            {
                throw new \Exception('活动信息不存在');
            }
            $params['config_update_time'] = time();
            $result = Activity::where($where)->update($params);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改营销活动-编号: ' . $params['config_id'];
                plog('life-activity-update', '修改营销活动', $msg);
                Activity::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            Activity::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除活动
     * @param int $goods_id
     * @return int
     * User: YCP
     * Date: 2023/2/23
     */
    public static function delActivity($config_id,$admin_id)
    {
        Activity::affairBegin();
        try {
            $where = [];
            $where['config_id'] = $config_id;
            $data['config_is_del'] = 1;
            $result = Activity::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除营销活动-编号: ' . $config_id;
                plog('life-activity-delete', '删除营销活动', $msg);
                Activity::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            Activity::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询活动
     * @param int $goods_id
     * @return int
     * User: YCP
     * Date: 2022/10/18
     */
    public static function activityInfo($config_id)
    {
        $where = [];
        $where['config_id'] = $config_id;
        $result = Activity::where($where)->first();
        if(empty($result) || $result === false)
        {
            throw new \Exception('活动信息不存在');
        }
        $result['activity_url'] = "https://service.wanyuewellness.cn/h5/#/?act=".$config_id;
        return $result;
    }


}
