<?php
namespace app\admin\server\medical;
use app\admin\model\MerchantShopCategory;

class ShopCategoryServer
{
    /**
     * Notes:获取书院课程列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/11/03
     */
    public static function getShopList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  MerchantShopCategory::getShopCategoryList($page, $limit, $keywords);
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:根据店铺获取分类列表
     * @param int $category_shop_id
     * @return array
     * User: ZQ
     * Date: 2022/11/08
     */
    public static function getCategoryList($category_shop_id)
    {
        $list =  MerchantShopCategory::getCategoryList($category_shop_id);
        return $list;
    }

    /**
     * Notes:修改书院课程
     * @param array $param
     * @param int $admin_id
     * @return int
     * User: QJF
     * Date: 2022/11/03
     */
    public static function updateShopCategory($param, $admin_id)
    {
        MerchantShopCategory::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $param['category_id'];
            $param['category_update_time'] = time();
            $result = MerchantShopCategory::where($where)->update($param);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改店铺分类-编号: ' . $param['category_id'];
                plog('life-shop-category-update', '通用-店铺分类-修改店铺分类', $msg);
                MerchantShopCategory::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MerchantShopCategory::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /*
     * Notes:删除店铺分类
     * @param int $category_id
     * @param int $admin_id
     * @return int
     * User: ZQ
     * Date: 2022/10/14
     */
    public static function delShopCategory($category_id,$admin_id)
    {
        MerchantShopCategory::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $category_id;
            $data['category_del'] = 1;
            $result = MerchantShopCategory::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除店铺分类-编号: ' . $category_id;
                plog('life-shop-category-delete', '通用-店铺分类-删除店铺分类', $msg);
                MerchantShopCategory::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MerchantShopCategory::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加店铺分类
     * @param array $param
     * @param int $admin_id
     * @return int
     * User: QJF
     * Date: 2022/11/03
     */
    public static function insertShopCategory($param,$admin_id)
    {
        MerchantShopCategory::affairBegin();
        try {
            $param['category_create_time'] = time();
            $result =  MerchantShopCategory::insertGetId($param);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加上铺分类-编号: ' . $result;
                plog('medical-medical-shop-category-create', '通用-店铺分类-添加店铺分类', $msg);
                MerchantShopCategory::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MerchantShopCategory::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询店铺分类
     * @param int $category_id
     * @return int
     * User: QJF
     * Date: 2022/11/03
     */
    public static function shopCategoryInfo($category_id)
    {
        MerchantShopCategory::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $category_id;
            $result = MerchantShopCategory::with(['Shop'])->where($where)->first();
            if (!empty($result)){
                MerchantShopCategory::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MerchantShopCategory::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
