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


use app\admin\controller\medical\Shop;
use app\admin\model\MedicalCareOrder;
use app\admin\model\MedicalCareOrderProduct;
use app\admin\model\MedicalCarePostage;
use app\admin\model\MerchantShop;
use app\admin\model\PackageOrderDetail;
use app\admin\model\SystemAdmin;
use app\admin\model\SystemMenu;
use app\admin\model\SystemRole;

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

        return compact('list', 'page', 'limit', 'count');
    }
    /**
     * Notes:获取店铺列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/11/08
     */
    public static function getHealthyShopList()
    {
        [$list] =  MerchantShop::getHealthyShopList();

        return compact('list');
    }

    /**
     * Notes:根据分类获取店铺列表
     * @param int $category_id
     * @return array
     * User: ZQ
     * Date: 2022/11/8
     */
    public static function getCategoryShop($category_id)
    {
        $list =  MerchantShop::getCategoryShop($category_id);

        return $list;
    }

    /**
     * Notes:店铺子集列表
     * @return array
     * User: ZQ
     * Date: 2022/11/8
     */
    public static function getCategorysShop()
    {
        $list =  MerchantShop::getCategorysShop();

        return $list;
    }

    /**
     * Notes: 添加m店铺
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/28
     */
    public static function insertShop(array $param)
    {
        MerchantShop::affairBegin();
        try {
            $param['shop_create_time'] = time();
            $result =  MerchantShop::insertGetId($param);
            if (!empty($result)){
                MerchantShop::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MerchantShop::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }


    /**
     * Notes:修改产品
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/23
     */
    public static function updateShop($param)
    {
        MerchantShop::affairBegin();
        try {
            $where = [];
            $where['shop_id'] = $param['shop_id'];
            $param['shop_update_time'] = time();
            $result = MerchantShop::where($where)->update($param);
            if ($result !== false){
                MerchantShop::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MerchantShop::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)
    {
        MerchantShop::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)){
                MerchantShop::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MerchantShop::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)
    {
        MerchantShop::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)){
                MerchantShop::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MerchantShop::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)
    {
        MerchantShop::affairBegin();
        try {
            $where = [];
            $where['shop_id'] = $shop_id;
            $result = MerchantShop::where($where)->first();
            if (!empty($result)){
                MerchantShop::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MerchantShop::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
