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

use app\admin\model\MedicalCareDoctor;
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 MedicalCareProductServer
{
    /**
     * Notes:获取产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: QJF
     * Date: 2022/9/17
     */
    public static function getProductList( $page, int $limit, $keywords)
    {
        [$list, $count] =  MedicalCareProduct::getProductList($page, $limit, $keywords);

        return compact('list', 'page', 'limit', 'count');
    }
    /**
     * Notes: 添加产品
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/23
     */
    public static function insertProduct(array $param)
    {
        MedicalCareProduct::affairBegin();
        try {
            $param['product_create_time'] = time();
            $param['product_update_time'] = time();
            $attr = $param['attr'];
            unset($param['attr']);
            $result =  MedicalCareProduct::insertGetId($param);
            //规格插入
            MedicalCareProductAttr::insertProductAttr($attr,$result,$param['product_type']);
            if (!empty($result)){
                MedicalCareProduct::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }


        /**
     * Notes:修改产品
     * @param array $param
     * @return int
     * User: QJF
     * Date: 2022/9/23
     */
    public static function updateProduct($param)
    {
        MedicalCareProduct::affairBegin();
        try {
            $where = [];
            $where['product_id'] = $param['product_id'];
            $param['product_update_time'] = time();
            $attr = $param['attr'];
            unset($param['attr']);
            $result = MedicalCareProduct::where($where)->update($param);
            //删除原有规格
            MedicalCareProductAttr::insertProductAttrDel($param['product_id']);
            //规格插入
            MedicalCareProductAttr::insertProductAttr($attr,$param['product_id'],$param['product_type']);
            if ($result !== false){
                MedicalCareProduct::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            MedicalCareProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:删除产品
     * @param int $role_id
     * @return int
     * User: QJF
     * Date: 2022/9/23
     */
    public static function delProduct($product_id)
    {
        MedicalCareProduct::affairBegin();
        try {
            $where = [];
            $where['product_id']         = $product_id;
            $data['product_is_del']      = 1;
            $data['product_update_time'] = time();
            $result = MedicalCareProduct::where($where)->update($data);
            if (!empty($result)){
                MedicalCareProduct::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MedicalCareProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:上/下架
     * @param int $role_id
     * @return int
     * User: QJF
     * Date: 2022/9/23
     */
    public static function showProduct($product_id,$product_is_show)
    {
        MedicalCareProduct::affairBegin();
        try {
            $where = [];
            $where['product_id']         = $product_id;
            $data['product_is_show']      = $product_is_show;
            $data['product_update_time'] = time();
            $result = MedicalCareProduct::where($where)->update($data);
            if (!empty($result)){
                MedicalCareProduct::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MedicalCareProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
    /**
     * Notes:产品详情
     * @param int $package_id
     * @return int
     * User: QJF
     * Date: 2022/9/23
     */
    public static function infoProduct($product_id)
    {
        MedicalCareProduct::affairBegin();
        try {
            $where = [];
            $where['product_id'] = $product_id;
            $info = MedicalCareProduct::with(['sku_type','sku','Shop','Postage'])->where($where)->first();
            if(!empty($info))
            {
                if(!empty($info['sku_type']))
                {
                    foreach ($info['sku_type'] as $kl => $val)
                    {
                        $sku_type_value = explode('-!-', $val['attr_values']);
                        $sku_type_value_arr = array();
                        for ($j = 0; $j < count($sku_type_value); $j++)
                        {
                            $sku_type_value_arr[$j]['id'] = $val['attr_value_id'].$j;
                            $sku_type_value_arr[$j]['name'] = $sku_type_value[$j];
                        }
                        $info['sku_type'][$kl]['attr_values'] = $sku_type_value_arr;
                    }
                }
                if(!empty($info['sku']))
                {
                    foreach ($info['sku'] as $kj => $item)
                    {
                        $sk_data = array();
                        $attr_value = explode(',', $item['attr_value_sku']);
                        $sku_image = explode('-|-', $item['attr_value_image']);
                        $info['sku'][$kj]['pic'] = $item['attr_value_image'];
                        for ($i = 0; $i < count($attr_value); $i++){
                            $info['sku'][$kj]['value' . $i] = $attr_value[$i];
                            $sk_data[$i] = array('id' => $item['attr_value_id'], 'name' => $info['sku_type'][$i]['attr_name'], 'value_id' => $item['attr_value_id'] . $i, 'value_name' => $attr_value[$i]);
                        }
                        $info['sku'][$kj]['sku_data'] = $sk_data;
                        $sku_data = array();
                        for ($l = 0; $l < count($sku_image); $l++){
                            $sku_data[$l]['url'] = $sku_image[$l];
                            $sku_data[$l]['uid'] = $item['sku_id'] . $l;
                        }
                        $info['sku'][$kj]['sku_images'] = $sku_data;
                    }
                }
            }
            if (!empty($info)){
                MedicalCareProduct::affairCommit();
                return $info;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            MedicalCareProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }
}
