<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台（PHP版）
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码，建议反馈是我们前进的动力
// | 开源版本可自由商用，可去除界面版权logo
// | gitee下载：https://gitee.com/likeshop_gitee/likeadmin
// | github下载：https://github.com/likeshop-github/likeadmin
// | 访问官网：https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------

namespace app\adminapi\validate\land;


use app\common\validate\BaseValidate;
use think\facade\Db;


/**
 * Land验证器
 * Class LandValidate
 * @package app\adminapi\validate\land
 */
class LandValidate extends BaseValidate
{

     /**
      * 设置校验规则
      * @var string[]
      */
    protected $rule = [
        'id' => 'require',
        'product_id' => 'require',
        'title' => 'require',
        'address' => 'require',
        'residual_area' => 'checkArea',
        'province_code' => 'require',
        'province_name' => 'require',
        'city_code' => 'require',
        'city_name' => 'require',
        'county_code' => 'require',
        'county_name' => 'require',
        'town_code' => 'require',
        'town_name' => 'require',
        'village_code' => 'require',
        'village_name' => 'require',
        'group_code' => 'require',
        'group_name' => 'require',
        'longitude' => 'require',
        'latitude' => 'require',
        'master_name' => 'require',
        'master_phone' => 'require',
    ];


    /**
     * 参数描述
     * @var string[]
     */
    protected $field = [
        'id' => 'id',
        'title' => '土地名称',
        'address' => '详细地址',
        'total_area' => '土地面积',
        'residual_area' => '剩余面积',
        'province_code' => '省编码',
        'province_name' => '省名称',
        'city_code' => '市编码',
        'city_name' => '市名称',
        'county_code' => '县编码',
        'county_name' => '县名称',
        'town_code' => '镇编码',
        'town_name' => '镇名称',
        'village_code' => '村编码',
        'village_name' => '村名称',
        'group_code' => '组编码',
        'group_name' => '组名称',
        'longitude' => '经度',
        'latitude' => '纬度',
        'master_name' => '土地负责人',
        'master_phone' => '负责人电话',
        'status' => '土地状态',
        'pic' => '土地图片',
    ];


    /**
     * @notes 添加场景
     * @return LandValidate
     * @author likeadmin
     * @date 2023/11/22 16:35
     */
    public function sceneAdd()
    {
        return $this->only(['title','address','residual_area','master_name','master_phone']);
    }


    /**
     * @notes 编辑场景
     * @return LandValidate
     * @author likeadmin
     * @date 2023/11/22 16:35
     */
    public function sceneEdit()
    {
        return $this->only(['id','title','address','residual_area','master_name','master_phone']);
    }


    /**
     * @notes 删除场景
     * @return LandValidate
     * @author likeadmin
     * @date 2023/11/22 16:35
     */
    public function sceneDelete()
    {
        return $this->only(['id'])->append('id', 'require|checkLand');
    }


    /**
     * @notes 详情场景
     * @return LandValidate
     * @author likeadmin
     * @date 2023/11/22 16:35
     */
    public function sceneDetail()
    {
        return $this->only(['id']);
    }

    public function checkLand($value, $rule, $data)
    {
        $landProduct = Db::name('land_product')->where('land_id', $value)->findOrEmpty();
        if (!empty($landProduct)) {
            return '存在关联产品，无法删除';
        }
        return true;
    }

    /**
     * @notes 绑定产品
     * @return LandValidate
     * @author likeadmin
     * @date 2023/11/22 16:35
     */
    public function sceneBind()
    {
        return $this->only(['id', 'product_id']);
    }

    public function checkArea($value, $rule, $data)
    {
        if (!empty($data['total_area']) && !empty($data['residual_area'])) {
            if ($data['residual_area'] > $data['total_area']) {
                return '剩余面积不能大于总面积';
            }
        }
        return true;
    }

}