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


use app\admin\model\PackageGoods;
use app\admin\model\PackageGoodsCategory;
use app\admin\model\SystemRole;

class PackageGoodsCategoryServer
{
    /**
     * Notes:获取角色列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/17
     */
    public static function getPackageGoodsCategoryList(int $page, int $limit, $keywords)
    {
        [$list, $count] =  PackageGoodsCategory::getPackageGoodsCategoryList($page, $limit, $keywords);

        return compact('list', 'page', 'limit', 'count');
    }
    /**
     * Notes: 添加产品分类
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/17
     */
    public static function insertPackageGoodsCategory(array $param)
    {
        SystemRole::affairBegin();
        try {
            $param['category_create_time'] = time();
            $param['category_update_time'] = time();
            $result =  PackageGoodsCategory::insertGetId($param);
            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:修改产品分类
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/21
     */
    public static function updatePackageGoodsCategory($param)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $param['category_id'];
            $param['category_update_time'] = time();
            $result = PackageGoodsCategory::where($where)->update($param);
            if ($result !== false){
                SystemRole::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:任务
     * @param int $role_id
     * @return int
     * User: QJF
     * Date: 2022/9/19
     */
    public static function delPackageGoodsCategory($category_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $category_id;
            $data['category_delete_time'] = time();
            $result = PackageGoodsCategory::where($where)->update($data);
            if (!empty($result)){
                SystemRole::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:获取权益包详情
     * @param int $category_id
     * @return int
     * User: QJF
     * Date: 2022/9/19
     */
    public static function infoPackageCategory($category_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $category_id;
            $result = PackageGoodsCategory::where($where)->first();
            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
