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



use app\admin\model\MerchantShop;
use app\admin\model\NewsCategory;
use app\admin\model\SystemRole;

class NewsCategoryServer
{
    /**
     * Notes:获取店铺列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/30
     */
    public static function getCategoryList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  NewsCategory::getCategory($page, $limit,$keywords);

        return compact('list', 'page', 'limit', 'count');
    }
    /**
     * Notes: 添加m店铺
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/28
     */
    public static function insertShop(array $param)
    {
        SystemRole::affairBegin();
        try {
            $param['shop_create_time'] = time();
            $result =  MerchantShop::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/23
     */
    public static function updateShop($param)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['shop_id'] = $param['shop_id'];
            $param['shop_update_time'] = time();
            $result = MerchantShop::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/23
     */
    public static function delShop($shop_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['shop_id']         = $shop_id;
            $data['shop_del']      = 1;
            $data['shop_update_time'] = time();
            $result = MerchantShop::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 $role_id
     * @return int
     * User: QJF
     * Date: 2022/9/28
     */
    public static function showShop($shop_id,$product_status)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['shop_id']         = $shop_id;
            $data['shop_status']      = $product_status;
            $data['shop_update_time'] = time();
            $result = MerchantShop::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 $package_id
     * @return int
     * User: QJF
     * Date: 2022/9/23
     */
    public static function infoShop($shop_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['shop_id'] = $shop_id;
            $result = MerchantShop::where($where)->first();
            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
