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

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

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

    /**
     * Notes: 添加套餐项目
     * @param string $goods_name
     * @param array $goods_rules
     * @return int
     * User: YCP
     * Date: 2022/10/18
     */
    public static function insertPackagegoods(array $params, int $admin_id)
    {

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

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

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

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

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

        }*/
        return $result;
    }


}
