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

use app\admin\model\MedicalCareDoctor;
use app\admin\model\MedicalCarePostage;
use app\admin\model\MedicalCareProduct;
use app\admin\model\MedicalCareProductAttr;
use app\admin\model\MedicalCareProductAttrValue;
use app\admin\model\Package;
use app\admin\model\SystemAdmin;
use app\admin\model\SystemMenu;
use app\admin\model\SystemRole;

class MedicalCarePostageServer
{
    /**
     * Notes:获取运费模板
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/28
     */
    public static function getPostageList( $page, int $limit, $keywords)
    {
        [$list, $count] =  MedicalCarePostage::getPostagetList($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 insertPostage(array $param)
    {
        SystemRole::affairBegin();
        try {
            $param['postage_create_time'] = time();
            $result =  MedicalCarePostage::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 updatePostage($param)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['postage_id'] = $param['postage_id'];
            $param['postage_update_time'] = time();
            $result = MedicalCarePostage::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 delPostage($postage_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['postage_id']         = $postage_id;
            $data['postage_is_delete']      = 1;
            $data['postage_update_time'] = time();
            $result = MedicalCarePostage::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 showPostage($postage_id,$product_status)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['postage_id']         = $postage_id;
            $data['postage_status']      = $product_status;
            $data['postage_update_time'] = time();
            $result = MedicalCarePostage::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 infoPostage($postage_id)
    {
        SystemRole::affairBegin();
        try {
            $where = [];
            $where['postage_id'] = $postage_id;
            $result = MedicalCarePostage::where($where)->first();
            if (!empty($result)){
                SystemRole::affairCommit();
                return $result;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            SystemRole::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
