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

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

class ProductServer
{
    /**
     * Notes:获取智能产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getProductList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  IntelligenceProduct::getProductList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['product_create_time'] = date('Y-m-d H:i:s',$v['product_create_time']);
                if (!empty($v['product_update_time'])){
                    $list[$k]['product_update_time'] = date('Y-m-d H:i:s',$v['product_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取所有智能产品
     * @return array
     * User: ZQ
     * Date: 2022/9/19
     */
    public static function getProductAll()
    {
        return IntelligenceProduct::getProductAll();
    }

    /**
     * Notes:修改产品
     * @param string $product_name
     * @param int $product_id
     * @param int $product_category_id
     * @param string $product_content
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateProduct($product_id, $product_category_id, $product_name, $product_content)
    {
        IntelligenceProduct::affairBegin();
        try {
            $where = [];
            $where['product_id'] = $product_id;
            $data = [];
            if (!empty($product_category_id)){
                $data['product_category_id'] = end($product_category_id);
            }
            if (!empty($product_content)){
                $data['product_content'] = $product_content;
            }
            if (!empty($product_name)){
                $data['product_name'] = $product_name;
            }
            $data['product_update_time'] = time();
            $result = IntelligenceProduct::where($where)->update($data);
            if ($result !== false){
                IntelligenceProduct::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            IntelligenceProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

    /**
     * Notes: 添加产品
     * @param string $product_name
     * @param string $product_content
     * @param int $product_category_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertProduct(string $product_name, $product_category_id, $product_content)
    {

        IntelligenceProduct::affairBegin();
        try {
            $data = [];
            $data['product_name'] = $product_name;
            if (!empty($product_content)){
                $data['product_content'] = $product_content;
            }
            $data['product_category_id'] = end($product_category_id);
            $data['product_create_time'] = time();
            $result =  IntelligenceProduct::insertGetId($data);
            if (!empty($result)){
                IntelligenceProduct::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            IntelligenceProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

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

}
