<?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 HomeGoodsServer
{

    /**
     * Notes:获取项目列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/31
     */
    public static function getGoodsList(int $page,int $limit,string $keywords,$category_id)
    {
        [$list, $count] =  HomeGoods::getGoodsList($page,$limit,$keywords,$category_id);
        return compact('list','count');
    }

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

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

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

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

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


}
