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

use app\admin\model\LifePlay;

class PlayServer
{
    /**
     * Notes:获取农庄美食/采摘游玩列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: ZQ
     * Date: 2022/10/18
     */
    public static function getPlayList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifePlay::getPlayList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['play_materials'] = json_decode($v['play_materials'],true);
                $list[$k]['play_condiment'] = json_decode($v['play_condiment'],true);
                $list[$k]['play_create_time'] = date('Y-m-d H:i:s',$v['play_create_time']);
                if (!empty($v['play_update_time'])){
                    $list[$k]['play_update_time'] = date('Y-m-d H:i:s',$v['play_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes:获取分类下的美食列表
     * @return int
     * User: ZQ
     * Date: 2022/10/18
     */
    public static function getPlayAll()
    {
        $where = [];
        $where['play_is_del'] = 0;
        $where['play_status'] = 0;
        $where['play_type'] = 1;
        $list = LifePlay::where($where)
            ->select(['play_id','play_name'])
            ->get()
            ->toArray();
        return $list;
    }

    /**
     * Notes:修改农庄美食/采摘游玩
     * @param string $play_name
     * @param int $play_id
     * @return int
     * User: ZQ
     * Date: 2022/10/18
     */
    public static function updatePlay($play_id, $play_name, $play_img, $play_price, $play_shop_id, $play_valid, $play_time, $play_type, $play_package, $play_label, $play_status, $play_content, $play_category, $admin_id)
    {
        LifePlay::affairBegin();
        try {
            $where = [];
            $where['play_id'] = $play_id;
            $data = [];
            $data['play_name'] = $play_name;
            $data['play_img'] = $play_img;
            $data['play_price'] = $play_price;
            $data['play_shop_id'] = $play_shop_id;
            $data['play_valid'] = $play_valid;
            $data['play_time'] = $play_time;
            $data['play_category'] = $play_category;
            $data['play_type'] = $play_type;
            $data['play_package'] = $play_package;
            $data['play_label'] = $play_label;
            $data['play_status'] = $play_status;
            $data['play_content'] = $play_content;
            $data['play_update_time'] = time();
            $result = LifePlay::where($where)->update($data);
            if ($result !== false){
                if ($play_category == 1){
                    $name = '农庄美食';
                }else{
                    $name = '采摘游玩';
                }
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改'.$name.'-编号: ' . $play_id;
                plog('life-play-update', '悦活-'.$name.'-修改'.$name, $msg);
                LifePlay::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifePlay::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除农庄美食/采摘游玩
     * @param int $play_id
     * @return int
     * User: ZQ
     * Date: 2022/10/18
     */
    public static function delPlay($play_id,$admin_id)
    {
        LifePlay::affairBegin();
        try {
            $where = [];
            $where['play_id'] = $play_id;
            $data['play_is_del'] = 1;
            $result = LifePlay::where($where)->update($data);
            if (!empty($result)){
//                $play_category = LifePlay::where($where)->first('play_category');
//                if ($play_category == 1){
//                    $name = '农庄美食';
//                }else{
//                    $name = '采摘游玩';
//                }
                $name = '农庄美食';
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除'.$name.'-编号: ' . $play_id;
                plog('life-play-delete', '悦活-'.$name.'-删除'.$name, $msg);
                LifePlay::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifePlay::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes: 添加农庄美食/采摘游玩
     * @param string $play_name
     * @param array $play_rules
     * @return int
     * User: ZQ
     * Date: 2022/10/18
     */
    public static function insertPlay($play_name, $play_img, $play_price, $play_shop_id, $play_valid, $play_time, $play_type, $play_package, $play_label, $play_status, $play_content, $play_category, $admin_id)
    {

        LifePlay::affairBegin();
        try {
            $data = [];
            $data['play_name'] = $play_name;
            $data['play_img'] = $play_img;
            $data['play_price'] = $play_price;
            $data['play_shop_id'] = $play_shop_id;
            $data['play_valid'] = $play_valid;
            $data['play_time'] = $play_time;
            $data['play_category'] = $play_category;
            $data['play_type'] = $play_type;
            $data['play_package'] = $play_package;
            $data['play_label'] = $play_label;
            $data['play_status'] = $play_status;
            $data['play_content'] = $play_content;
            $data['play_create_time'] = time();
            $result =  LifePlay::insertGetId($data);
            if (!empty($result)){
                if ($play_category == 1){
                    $name = '农庄美食';
                }else{
                    $name = '采摘游玩';
                }
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加'.$name.'-编号: ' . $result;
                plog('life-play-create', '悦活-'.$name.'-添加'.$name, $msg);
                LifePlay::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifePlay::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询农庄美食/采摘游玩
     * @param int $play_id
     * @return int
     * User: ZQ
     * Date: 2022/10/18
     */
    public static function playInfo($play_id)
    {
        $where = [];
        $where['play_id'] = $play_id;
        $result = LifePlay::where($where)->first();
        if (!empty($result)){
            $result['play_materials'] = json_decode($result['play_materials'],true);
            $result['play_condiment'] = json_decode($result['play_condiment'],true);
            $result['play_create_time'] = date('Y-m-d H:i:s',$result['play_create_time']);
            if (!empty($result['play_update_time'])){
                $result['play_update_time'] = date('Y-m-d H:i:s',$result['play_update_time']);
            }

        }
        return $result;
    }

    /**
     * Notes:修改农庄美食/采摘游玩状态
     * @param string $play_name
     * @param int $play_status
     * @return int
     * User: ZQ
     * Date: 2022/9/15
     */
    public static function updateStatus($play_id, $play_status)
    {
        LifePlay::affairBegin();
        try {
            $where = [];
            $where['play_id'] = $play_id;
            $data = [];
            $data['play_status'] = $play_status;
            $result = LifePlay::where($where)->update($data);
            if ($result !== false){
                LifePlay::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifePlay::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

}
