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

use app\admin\model\IntelligenceCategory;
use app\admin\model\IntelligenceProduct;

class CategoryServer
{
    /**
     * Notes:获取智能产品分类列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getCategoryList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  IntelligenceCategory::getCategoryList($page, $limit, $keywords, 0);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['category_create_time'] = date('Y-m-d H:i:s',$v['category_create_time']);
                if (!empty($v['category_update_time'])){
                    $list[$k]['category_update_time'] = date('Y-m-d H:i:s',$v['category_update_time']);
                }
                if (empty($keywords)){
                    [$data,$counts] = IntelligenceCategory::getCategoryList($page, $limit, $keywords, $v['category_id']);
                    if (!empty($data)){
                        foreach ($data as $key => $val){
                            $data[$key]['category_create_time'] = date('Y-m-d H:i:s',$val['category_create_time']);
                            if (!empty($val['category_update_time'])){
                                $data[$key]['category_update_time'] = date('Y-m-d H:i:s',$val['category_update_time']);
                            }
                        }
                    }
                    $list[$k]['children'] = $data;
                }
            }
        }

        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取所有智能产品
     * @return array
     * User: ZQ
     * Date: 2022/9/19
     */
    public static function getCategoryAll($type)
    {
        $data = IntelligenceCategory::categoryAll(0);
        if ($type == 1){
            if (!empty($data)){
                foreach ($data as $k => $v){
                    $data[$k]['children'] = IntelligenceCategory::categoryAll($v['value']);
                }
            }

        }
        return $data;
    }

    /**
     * Notes:修改产品
     * @param string $category_name
     * @param int $category_id
     * @param int $category_pid
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateCategory($category_id, $category_name, $category_pid)
    {
        IntelligenceCategory::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $category_id;
            $data = [];
            if (!empty($category_name)){
                $data['category_name'] = $category_name;
            }
            if ($category_pid == 0){
                $data['category_pid'] = 0;
            }else{
                if (!empty($category_pid)){
                    $data['category_pid'] = end($category_pid);
                }
            }
            if ($data['category_pid'] != 0){
                $data['category_level'] = 2;
            }else{
                $data['category_level'] = 1;
            }
            $data['category_update_time'] = time();
            $result = IntelligenceCategory::where($where)->update($data);
            if ($result !== false){
                IntelligenceCategory::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            IntelligenceCategory::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除产品
     * @param int $category_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function delCategory($category_id)
    {
        IntelligenceCategory::affairBegin();
        try {
            $where = [];
            $where['category_id'] = $category_id;
            $data['category_del'] = 1;
            $result = IntelligenceCategory::where($where)->update($data);
            if (!empty($result)){
                IntelligenceCategory::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            IntelligenceCategory::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加产品
     * @param string $category_name
     * @param int $category_pid
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertCategory(string $category_name, $category_pid)
    {

        IntelligenceCategory::affairBegin();
        try {
            $data = [];
            $data['category_name'] = $category_name;
            $data['category_pid'] = end($category_pid);
            if ($data['category_pid'] == 0){
                $data['category_level'] = 1;
            }else{
                $data['category_level'] = 2;
            }
            $data['category_create_time'] = time();
            $result =  IntelligenceCategory::insertGetId($data);
            if (!empty($result)){
                IntelligenceCategory::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            IntelligenceCategory::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询产品信息
     * @param int $category_id
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function categoryInfo($category_id)
    {
        $where = [];
        $where['category_id'] = $category_id;
        $result = IntelligenceCategory::where($where)->first();
        if (!empty($result)){
            $result['category_create_time'] = date('Y-m-d H:i:s',$result['category_create_time']);
            if (!empty($result['product_update_time'])){
                $result['category_update_time'] = date('Y-m-d H:i:s',$result['category_update_time']);
            }
        }
        return $result;
    }

}
