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

use app\admin\model\LifeFarmGoods;
use app\admin\model\SystemMenu;
use support\Redis;

class FarmGoodsServer
{
    /**
     * Notes:获取绿色蔬菜列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/11/4
     */
    public static function getFarmGoodsList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeFarmGoods::getFarmGoodsList($page, $limit, $keywords);
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes: 添加绿色蔬菜
     * @param string $goods_name
     * @param array $goods_rules
     * @return int
     * User: YCP
     * Date: 2022/11/4
     */
    public static function insertFarmGoods(array $params, int $admin_id)
    {

        LifeFarmGoods::affairBegin();
        try {
            $params['goods_create_time'] = time();
            $result =  LifeFarmGoods::insertGetId($params);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加生态农场绿色蔬菜-编号: ' . $result;
                plog('life-farmgoods-create', '悦活-生态农场绿色蔬菜', $msg);
                LifeFarmGoods::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFarmGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:修改绿色蔬菜
     * @param string $goods_name
     * @param int $goods_id
     * @return int
     * User: YCP
     * Date: 2022/11/4
     */
    public static function updateFarmGoods(array $params, int $admin_id)
    {
        LifeFarmGoods::affairBegin();
        try {
            
            $where = [];
            $where['goods_id'] = $params['goods_id'];
            $info = LifeFarmGoods::where($where)->first();
            if(empty($info) || $info === false)
            {
                throw new \Exception('信息不存在');
            }
            $params['goods_update_time'] = time();
            $result = LifeFarmGoods::where($where)->update($params);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改生态农场绿色蔬菜-编号: ' . $params['goods_id'];
                plog('life-farmgoods-update', '悦活-修改生态农场绿色蔬菜', $msg);
                LifeFarmGoods::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFarmGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除绿色蔬菜
     * @param int $goods_id
     * @return int
     * User: YCP
     * Date: 2022/11/4
     */
    public static function delFarmGoods($goods_id,$admin_id)
    {
        LifeFarmGoods::affairBegin();
        try {
            $where = [];
            $where['goods_id'] = $goods_id;
            $data['goods_is_del'] = 1;
            $result = LifeFarmGoods::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除生态农场绿色蔬菜-编号: ' . $goods_id;
                plog('life-farmgoods-delete', '悦活-删除生态农场绿色蔬菜', $msg);
                LifeFarmGoods::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeFarmGoods::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询绿色蔬菜
     * @param int $goods_id
     * @return int
     * User: YCP
     * Date: 2022/11/4
     */
    public static function farmGoodsInfo($goods_id)
    {
        $where = [];
        $where['goods_id'] = $goods_id;
        $result = LifeFarmGoods::with(['Shop','Category','MedicalCarePostage'])->where($where)->first();
        if(empty($result) || $result === false)
        {
            throw new \Exception('信息不存在');
        }
        return $result;
    }

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


}
