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

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

class FarmlandServer
{
    /**
     * Notes:获取地块列表
     * @param string $keywords
     * @param int $page
     * @param int $limit
     * @return array
     * User: YCP
     * Date: 2022/10/17
     */
    public static function getlandList(int $page, int $limit, string $keywords)
    {
        [$list, $count] =  LifeFarmLand::getlandList($page, $limit, $keywords);
        if (!empty($list)){
            foreach ($list as $k => $v){
                $list[$k]['land_create_time'] = date('Y-m-d H:i:s',$v['land_create_time']);
                if (!empty($v['land_update_time'])){
                    $list[$k]['land_update_time'] = date('Y-m-d H:i:s',$v['land_update_time']);
                }
            }
        }
        return compact('list', 'page', 'limit', 'count');
    }

    /**
     * Notes: 添加地块
     * @param string $land_name
     * @param array $land_rules
     * @return int
     * User: YCP
     * Date: 2022/10/17
     */
    public static function insertland($land_name, $land_price, $land_longitude, $land_latitude, $admin_id)
    {

        LifeFarmLand::affairBegin();
        try {
            //检测地块名是否重复
            if(LifeFarmLand::checkLandName($land_name))
            {
                throw new \Exception($land_name . '地块名已存在');
            }
            //检测经纬度是否重复
            if(LifeFarmLand::checkLandPoint($land_longitude,$land_latitude))
            {
                throw new \Exception('该坐标已存在');
            }
            $data = [];
            $data['land_name'] = $land_name;
            $data['land_price'] = $land_price;
            $data['land_longitude'] = $land_longitude;
            $data['land_latitude'] = $land_latitude;
            $data['land_create_time'] = time();
            $result =  LifeFarmLand::insertGetId($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '添加一亩心田地块-编号: ' . $result;
                plog('life-farm-land-create', '悦活-生态农场-一亩心田-添加地块', $msg);
                LifeFarmLand::affairCommit();
                return $result;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFarmLand::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:修改地块
     * @param string $land_name
     * @param int $land_id
     * @return int
     * User: YCP
     * Date: 2022/10/17
     */
    public static function updateland($land_id, $land_name, $land_price, $land_longitude, $land_latitude, $admin_id)
    {
        LifeFarmLand::affairBegin();
        try {
            
            $where = [];
            $where['land_id'] = $land_id;
            $info = LifeFarmLand::where($where)->first();
            if(empty($info) || $info === false)
            {
                throw new \Exception('地块信息不存在');
            }
            $data = [];
            $data['land_name'] = $land_name;
            $data['land_price'] = $land_price;
            $data['land_longitude'] = $land_longitude;
            $data['land_latitude'] = $land_latitude;
            $data['land_update_time'] = time();
            $result = LifeFarmLand::where($where)->update($data);
            if ($result !== false){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '修改一亩心田地块-编号: ' . $land_id;
                plog('life-farm-land-update', '悦活-生态农场-一亩心田-修改地块', $msg);
                LifeFarmLand::affairCommit();
                return true;
            }
            throw new \Exception('操作失败！');
        }catch (\Exception $exception){
            LifeFarmLand::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:删除地块
     * @param int $land_id
     * @return int
     * User: YCP
     * Date: 2022/10/17
     */
    public static function delland($land_id,$admin_id)
    {
        LifeFarmLand::affairBegin();
        try {
            $where = [];
            $where['land_id'] = $land_id;
            $data['land_is_del'] = 1;
            $result = LifeFarmLand::where($where)->update($data);
            if (!empty($result)){
                $msg = '管理员：' . $admin_id . '在：' . date("Y-m-d H:i:s", time()) . '删除一亩心田地块-编号: ' . $land_id;
                plog('life-farm-land-delete', '悦活-生态农场-一亩心田-删除地块', $msg);
                LifeFarmLand::affairCommit();
                return true;
            }else{
                return false;
            }
        }catch (\Exception $exception){
            LifeFarmLand::affairRollback();
            throw new \Exception($exception->getMessage(), 500);
        }
    }

    /**
     * Notes:查询地块
     * @param int $land_id
     * @return int
     * User: YCP
     * Date: 2022/10/17
     */
    public static function landInfo($land_id)
    {
        $where = [];
        $where['land_id'] = $land_id;
        $result = LifeFarmLand::where($where)->first();
        if(empty($result) || $result === false)
        {
            throw new \Exception('地块信息不存在');
        }
        if (!empty($result)){
            $result['land_create_time'] = date('Y-m-d H:i:s',$result['land_create_time']);
            if (!empty($result['land_update_time'])){
                $result['land_update_time'] = date('Y-m-d H:i:s',$result['land_update_time']);
            }

        }
        return $result;
    }


}
