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

use app\admin\model\LifeGoods;

class GoodsServer
{
    /**
     * Notes:获取商品列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function getGoodsList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeGoods::getGoodsList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['goods_slide'] = explode(',',$v['goods_slide']);
                $list[$k]['goods_create_time'] = date('Y-m-d H:i:s',$v['goods_create_time']);
                if (!empty($v['goods_update_time'])){
                    $list[$k]['goods_update_time'] = date('Y-m-d H:i:s',$v['goods_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:修改商品
     * @param string $goods_name
     * @param int $goods_id
     * @return int
     * User: ZQ
     * Date: 2022/9/3
     */
    public static function updateGoods($goods_id,$goods_name,$goods_img,$goods_category_id,$goods_shop_id,$goods_is_new,$goods_price,$goods_oldprice,$goods_content,$goods_sell_num,$goods_postage_id,$goods_status,$goods_weight,$goods_sort,$goods_slide,$goods_pack_price,$food_attr,$food_varieties,$food_packaging,$food_storage,$goods_delivery_type,$goods_inventory,$admin_id)
    {
        LifeGoods::affairBegin();
        try {
            $where = [];
            $where['goods_id'] = $goods_id;
            $data = [];
            $data['goods_name'] = $goods_name;
            $data['goods_img'] = $goods_img;
            $data['goods_shop_id'] = $goods_shop_id;
            $data['goods_is_new'] = $goods_is_new;
            $data['goods_price'] = $goods_price;
            $data['goods_oldprice'] = $goods_oldprice;
            $data['goods_content'] = $goods_content;
            $data['goods_sell_num'] = $goods_sell_num;
            $data['goods_postage_id'] = $goods_postage_id;
            $data['goods_category_id'] = $goods_category_id;
            $data['goods_status'] = $goods_status;
            $data['goods_weight'] = $goods_weight;
            $data['goods_sort'] = $goods_sort;
            $data['goods_slide'] = implode(',',$goods_slide);
            $data['goods_pack_price'] = $goods_pack_price;
            $data['goods_attr'] = $food_attr;
            $data['goods_varieties'] = $food_varieties;
            $data['goods_packaging'] = $food_packaging;
            $data['goods_storage'] = $food_storage;
            $data['goods_delivery_type'] = $goods_delivery_type;
            $data['goods_inventory'] = $goods_inventory;
            $data['goods_update_time'] = time();
            $result = LifeGoods::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改健康超市商品-编号: ' . $goods_id;
                plog('life-goods-update', '悦活-健康超市-修改商品', $msg);
                LifeGoods::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除商品
     * @param int $goods_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function delGoods($goods_id,$admin_id)
    {
        LifeGoods::affairBegin();
        try {
            $where = [];
            $where['goods_id'] = $goods_id;
            $data['goods_is_del'] = 1;
            $result = LifeGoods::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除健康超市商品-编号: ' . $goods_id;
                plog('life-goods-delete', '悦活-健康超市-删除商品', $msg);
                LifeGoods::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加商品
     * @param string $goods_name
     * @param array $goods_rules
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function insertGoods($goods_name,$goods_img,$goods_category_id,$goods_shop_id,$goods_is_new,$goods_price,$goods_oldprice,$goods_content,$goods_sell_num,$goods_postage_id,$goods_status,$goods_weight,$goods_sort,$goods_slide,$goods_pack_price,$food_attr,$food_varieties,$food_packaging,$food_storage,$goods_delivery_type,$goods_inventory,$admin_id)
    {

        LifeGoods::affairBegin();
        try {
            $data = [];
            $data['goods_name'] = $goods_name;
            $data['goods_img'] = $goods_img;
            $data['goods_shop_id'] = $goods_shop_id;
            $data['goods_is_new'] = $goods_is_new;
            $data['goods_price'] = $goods_price;
            $data['goods_oldprice'] = $goods_oldprice;
            $data['goods_content'] = $goods_content;
            $data['goods_sell_num'] = $goods_sell_num;
            $data['goods_postage_id'] = $goods_postage_id;
            $data['goods_category_id'] = $goods_category_id;
            $data['goods_status'] = $goods_status;
            $data['goods_weight'] = $goods_weight;
            $data['goods_sort'] = $goods_sort;
            $data['goods_slide'] = implode(',',$goods_slide);
            $data['goods_pack_price'] = $goods_pack_price;
            $data['goods_attr'] = $food_attr;
            $data['goods_varieties'] = $food_varieties;
            $data['goods_packaging'] = $food_packaging;
            $data['goods_storage'] = $food_storage;
            $data['goods_delivery_type'] = $goods_delivery_type;
            $data['goods_inventory'] = $goods_inventory;
            $data['goods_create_time'] = time();
            $result =  LifeGoods::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加健康超市商品-编号: ' . $result;
                plog('life-goods-create', '悦活-健康超市-添加商品', $msg);
                LifeGoods::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询商品
     * @param int $goods_id
     * @return int
     * User: ZQ
     * Date: 2022/9/13
     */
    public static function goodsInfo($goods_id)
    {
        $where = [];
        $where['goods_id'] = $goods_id;
        $where['goods_is_del'] = 0;
        $result = LifeGoods::where($where)
            ->leftJoin('merchant_shop','shop_id','=','life_market_goods.goods_shop_id')
            ->leftJoin('category','category_id','=','life_market_goods.goods_category_id')
            ->first(['life_market_goods.*','merchant_shop.shop_name','category.category_name']);
        if (!empty($result)){
            $result['goods_slide'] = explode(',',$result['goods_slide']);
            $result['goods_create_time'] = date('Y-m-d H:i:s',$result['goods_create_time']);
            if (!empty($result['goods_update_time'])){
                $result['goods_update_time'] = date('Y-m-d H:i:s',$result['goods_update_time']);
            }

        }
        return $result;
    }

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

}
