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

use app\admin\model\HealthyProduct;

class HealthyProductServer
{
    /**
     * Notes:获取健康产品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function getProductList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  HealthyProduct::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:修改健康产品
     * @param string $product_name
     * @param int $product_id
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function updateProduct($product_id, $product_name, $product_pack_price, $product_postage_id, $product_category_id, $product_weight, $product_label, $product_status, $product_image, $product_image_list, $product_limit, $product_notice, $product_guarantee, $product_type, $product_package, $product_rule, $product_shop_id, $product_price, $product_detail, $product_sort, $admin_id)
    {
        HealthyProduct::affairBegin();
        try {
            $where = [];
            $where['product_id'] = $product_id;
            $data = [];
            $data['product_name'] = $product_name;
            $data['product_pack_price'] = $product_pack_price;
            $data['product_postage_id'] = $product_postage_id;
            $data['product_category_id'] = $product_category_id;
            $data['product_weight'] = $product_weight;
            $data['product_label'] = $product_label;
            $data['product_image'] = $product_image;
            $data['product_image_list'] = $product_image_list ? implode(',',$product_image_list) : '';
            $data['product_limit'] = $product_limit;
            $data['product_notice'] = $product_notice;
            $data['product_guarantee'] = $product_guarantee;
            $data['product_type'] = $product_type;
            $data['product_package'] = json_encode($product_package);
            $data['product_rule'] = $product_rule;
            $data['product_status'] = $product_status;
            $data['product_price'] = $product_price;
            $data['product_shop_id'] = $product_shop_id;
            $data['product_detail'] = $product_detail;
            $data['product_sort'] = $product_sort;
            $data['product_update_time'] = time();
            $result = HealthyProduct::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改万悦健康产品-编号: ' . $product_id;
                plog('healthy-product-update', '健康服务-健康产品-修改健康产品', $msg);
                HealthyProduct::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            HealthyProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除健康产品
     * @param int $product_id
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function delProduct($product_id,$admin_id)
    {
        HealthyProduct::affairBegin();
        try {
            $where = [];
            $where['product_id'] = $product_id;
            $data['product_del'] = 1;
            $result = HealthyProduct::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除万悦健康产品-编号: ' . $product_id;
                plog('healthy-product-delete', '健康服务-健康产品-删除健康产品', $msg);
                HealthyProduct::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            HealthyProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加健康产品
     * @param string $product_name
     * @param array $product_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function insertProduct($product_name, $product_pack_price, $product_postage_id, $product_category_id, $product_weight, $product_label, $product_status, $product_image, $product_image_list, $product_limit, $product_notice, $product_guarantee, $product_type, $product_package, $product_rule, $product_shop_id, $product_price, $product_detail, $product_sort, $admin_id)
    {

        HealthyProduct::affairBegin();
        try {
            $data = [];
            $data['product_name'] = $product_name;
            $data['product_pack_price'] = $product_pack_price;
            $data['product_postage_id'] = $product_postage_id;
            $data['product_category_id'] = $product_category_id;
            $data['product_weight'] = $product_weight;
            $data['product_label'] = $product_label;
            $data['product_image'] = $product_image;
            $data['product_image_list'] = $product_image_list ? implode(',',$product_image_list) : '';
            $data['product_limit'] = $product_limit;
            $data['product_notice'] = $product_notice;
            $data['product_guarantee'] = $product_guarantee;
            $data['product_type'] = $product_type;
            $data['product_package'] = json_encode($product_package);
            $data['product_rule'] = $product_rule;
            $data['product_status'] = $product_status;
            $data['product_price'] = $product_price;
            $data['product_shop_id'] = $product_shop_id;
            $data['product_detail'] = $product_detail;
            $data['product_sort'] = $product_sort;
            $data['product_create_time'] = time();
            $result =  HealthyProduct::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加万悦健康产品-编号: ' . $result;
                plog('healthy-product-create', '健康服务-健康产品-添加健康产品', $msg);
                HealthyProduct::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            HealthyProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询健康产品
     * @param int $product_id
     * @return int
     * User: ZQ
     * Date: 2022/10/31
     */
    public static function productInfo($product_id)
    {
        $where = [];
        $where['product_id'] = $product_id;
        $result = HealthyProduct::where($where)
            ->leftJoin('merchant_shop_category','category_id','=','healthy_product.product_category_id')
            ->leftJoin('merchant_shop','shop_id','=','healthy_product.product_shop_id')
            ->first(['healthy_product.*','merchant_shop_category.category_name','merchant_shop.shop_name']);
        if (!empty($result)){
            $result['product_package'] = json_decode($result['product_package'],true);
            $result['product_image_list'] = explode(',',$result['product_image_list']);
            $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;
    }

    /**
     * Notes:修改健康产品状态
     * @param string $product_name
     * @param int $product_status
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function updateStatus($product_id, $product_status)
    {
        HealthyProduct::affairBegin();
        try {
            $where = [];
            $where['product_id'] = $product_id;
            $data = [];
            $data['product_status'] = $product_status;
            $result = HealthyProduct::where($where)->update($data);
            if ($result !== false){
                HealthyProduct::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            HealthyProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:审核健康产品
     * @param string $product_name
     * @param int $product_status
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function autoProduct($product_id, $product_auto,$product_msg)
    {
        HealthyProduct::affairBegin();
        try {
            $where = [];
            $where['product_id'] = $product_id;
            $data = [];
            $data['product_auto'] = $product_auto;
            $data['product_msg'] = $product_msg;
            $result = HealthyProduct::where($where)->update($data);
            if ($result !== false){
                HealthyProduct::affairCommit();
                return true;
            }
            throw new \Exception('审核失败！');
        }catch (\Exception $exception){
            HealthyProduct::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
