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

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

class PackageServer
{
    /**
     * Notes:获取套餐列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/18
     */
    public static function getPackageList(int $page, int $limit, string $keywords,$package_type)
    {
        [$list, $count] =  LifePackage::getPackageList($page, $limit, $keywords,$package_type);
        /*if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['package_create_time'] = date('Y-m-d H:i:s',$v['package_create_time']);
                if (!empty($v['package_update_time'])){
                    $list[$k]['package_update_time'] = date('Y-m-d H:i:s',$v['package_update_time']);
                }
            }
        }*/
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes: 添加套餐
     * @param string $package_name
     * @param array $package_rules
     * @return int
     * User: YCP
     * Date: 2022/10/18
     */
    public static function insertPackage(array $params, int $admin_id)
    {

        LifePackage::affairBegin();
        try {
            //检测套餐名是否重复
            if(LifePackage::checkPackageName($params['package_name']))
            {
                throw new \Exception($params['package_name'] . '套餐名已存在');
            }
            $params['package_create_time'] = time();
            $result =  LifePackage::insertGetId($params);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加悦活套餐-编号: ' . $result;
                plog('life-package-create', '悦活-添加套餐', $msg);
                LifePackage::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifePackage::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:修改套餐
     * @param string $package_name
     * @param int $package_id
     * @return int
     * User: YCP
     * Date: 2022/10/18
     */
    public static function updatePackage(array $params, int $admin_id)
    {
        LifePackage::affairBegin();
        try {
            
            $where = [];
            $where['package_id'] = $params['package_id'];
            $info = LifePackage::where($where)->first();
            if(empty($info) || $info === false)
            {
                throw new \Exception('套餐信息不存在');
            }
            $params['package_update_time'] = time();
            $result = LifePackage::where($where)->update($params);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改悦活套餐-编号: ' . $params['package_id'];
                plog('life-package-update', '悦活-修改套餐', $msg);
                LifePackage::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifePackage::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除套餐
     * @param int $package_id
     * @return int
     * User: YCP
     * Date: 2022/10/18
     */
    public static function delPackage($package_id,$admin_id)
    {
        LifePackage::affairBegin();
        try {
            $where = [];
            $where['package_id'] = $package_id;
            $data['package_is_del'] = 1;
            $result = LifePackage::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除套餐-编号: ' . $package_id;
                plog('life-farm-package-delete', '悦活-删除套餐', $msg);
                LifePackage::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifePackage::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询套餐
     * @param int $package_id
     * @return int
     * User: YCP
     * Date: 2022/10/18
     */
    public static function packageInfo($package_id)
    {
        $where = [];
        $where['package_id'] = $package_id;
        $result = LifePackage::where($where)->first();
        if(empty($result) || $result === false)
        {
            throw new \Exception('套餐信息不存在');
        }
        $result['package_images'] = explode(",",$result['package_images']);

        /*if (!empty($result)){
            $result['package_create_time'] = date('Y-m-d H:i:s',$result['package_create_time']);
            if (!empty($result['package_update_time'])){
                $result['package_update_time'] = date('Y-m-d H:i:s',$result['package_update_time']);
            }

        }*/
        return $result;
    }


}
