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

//use app\admin\model\HomeGoods;
use app\admin\model\HomeBanner;
use app\admin\model\SystemMenu;
use support\Redis;

class HomeBannerServer
{

    /**
     * Notes:获取banner列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/11/1
     */
    public static function getBannerList()
    {
        [$list, $count] =  HomeBanner::getBannerList();
        return compact('list','count');
    }

    /**
     * Notes: 添加项目
     * @param string $goods_name
     * @param array $goods_rules
     * @return int
     * User: YCP
     * Date: 2022/11/1
     */
    public static function insertBanner(array $params, int $admin_id)
    {

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

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

    /**
     * Notes:删除项目
     * @param int $goods_id
     * @return int
     * User: YCP
     * Date: 2022/11/1
     */
    public static function delBanner($banner_id,$admin_id)
    {
        HomeBanner::affairBegin();
        try {
            $where = [];
            $where['banner_id'] = $banner_id;
            $data['banner_is_del'] = 1;
            $result = HomeBanner::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除banner-编号: ' . $banner_id;
                plog('life-homebanner-delete', '居家-删除banner', $msg);
                HomeBanner::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            HomeBanner::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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


}
