2023-02-13 14:36:25 +08:00

188 lines
7.1 KiB
PHP

<?php
namespace app\admin\controller\nk;
use app\admin\BaseController;
use think\facade\Db;
use think\facade\View;
/**
* 农产品成熟周期
*
* @icon fa fa-circle-o
*/
class Maturity extends BaseController
{
/**
* 构造函数
*/
public function __construct()
{
$this->adminInfo = get_login_admin();
$this->url=[
'/admin/nk.maturity/index',
'/admin/nk.maturity/add',
'/admin/nk.maturity/edit',
'/admin/nk.maturity/del',
'/admin/nk.maturity/read',
];
}
/**
* 数据列表
*/
public function index()
{
if (request()->isAjax()) {
$params= get_params();
$where[]= ['status','=',1];
if (isset($params['keywords']) && !empty($params['keywords'])){
$where[]= ['title','like','%'.$params['keywords'].'%'];
}
if($this->adminInfo['position_id'] != 1){ //不是超级管理员
$www['admin_id'] = $this->adminInfo['id'];
$user_address = Db::table('fa_szxc_information_useraddress')->where($www)->find();
if ($user_address){
if($user_address['auth_range'] == 1){
$where[] = ['village_id','=',$user_address['village_id']];
}elseif ($user_address['auth_range'] == 2){
$where[] = ['street_id','=',$user_address['street_id']];
}elseif ($user_address['auth_range'] == 3){
$where[] = ['area_id','=',$user_address['area_id']];
}else{
$where[] = ['village_id','=',$user_address['village_id']];
}
}
}
$total = Db::table('cms_agriculture_products')
->where($where)
->count();
$list = Db::table('cms_agriculture_products')
->withAttr('area_id',function ($value,$data){
return Db::table('fa_geo_area')->where('area_code',$data['area_id'])->value('area_name');
})
->withAttr('street_id',function ($value,$data){
return Db::table('fa_geo_street')->where('street_code',$data['street_id'])->value('street_name');
})
->withAttr('village_id',function ($value,$data){
return Db::table('fa_geo_village')->where('village_id',$data['village_id'])->value('village_name');
})
->withAttr('image',function ($value,$data){
return Db::table('fa_szxc_planting_type')->where('id',$data['szxc_planting_type_id'])->value('image');
})
->withAttr('status',function ($value,$data){
if($value == 1){
return '正常';
}else{
return '禁用';
}
})
->where($where)
->page($params['page'])
->limit($params['limit'])
->order('id desc')
->select();
$result = ['total' => $total, 'data' => $list];
return table_assign(0, '', $result);
}
return view('',['url'=>$this->url]);
}
public function add(){
if (request()->isAjax()) {
$param= get_params();
$adds=Db::table('fa_szxc_information_useraddress')->where('admin_id',$this->adminInfo['id'])->find();
$param['area_id']=$adds['area_id'];
$param['street_id']=$adds['street_id'];
$param['village_id']=$adds['village_id'];
$param['add_time']=date('Y-m-d H:i:s');
$param['cycle']=json_encode(['start_time'=>$param['start_time'],'end_time'=>$param['end_time'],'zhouqi'=>$param['zhouqi']]);
$param['title'] = Db::table('fa_szxc_planting_type')->where('id',$param['szxc_planting_type_id'])->value('name');
$res=Db::table('cms_agriculture_products')->strict(false)->field(true)->insertGetId($param);
if ($res){
return to_assign(0,'操作成功',['aid'=>$res]);
}
return to_assign(1, '操作失败,原因:'.$res);
}else{
View::assign('url', $this->url);
View::assign('editor', get_system_config('other','editor'));
$szxc_planting_type = Db::table('fa_szxc_planting_type')->select();
View::assign('szxc_planting_type', $szxc_planting_type);
return view();
}
}
public function edit(){
$param= get_params();
if (request()->isAjax()) {
$param['title'] = Db::table('fa_szxc_planting_type')->where('id',$param['szxc_planting_type_id'])->value('name');
$param['cycle']=json_encode(['start_time'=>$param['start_time'],'end_time'=>$param['end_time'],'zhouqi'=>$param['zhouqi']]);
$res=Db::table('cms_agriculture_products')->where('id',$param['id'])->strict(false)->field(true)->update($param);
if ($res){
return to_assign();
}else{
return to_assign(1, '操作失败,原因:'.$res);
}
}else{
$id = isset($param['id']) ? $param['id'] : 0;
$detail = Db::table('cms_agriculture_products')
->withAttr('cycle',function($value,$data){
return json_decode($value,true);
})
->where('id',$id)->find();
View::assign('editor', get_system_config('other','editor'));
if (!empty($detail)) {
View::assign('detail', $detail);
$szxc_planting_type = Db::table('fa_szxc_planting_type')->select();
View::assign('szxc_planting_type', $szxc_planting_type);
return view();
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
}
/**
* 查看信息
*/
public function read()
{
$param= get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$detail = Db::table('cms_agriculture_products')->where('id',$id)
->withAttr('cycle',function($value,$data){
return json_decode($value,true);
})
->find();
if (!empty($detail)) {
View::assign('detail', $detail);
$szxc_planting_type = Db::table('fa_szxc_planting_type')->select();
View::assign('szxc_planting_type', $szxc_planting_type);
return view();
}
else{
throw new \think\exception\HttpException(404, '找不到页面');
}
}
/**
* 删除
*/
public function del()
{
$param= get_params();
$id = isset($param['id']) ? $param['id'] : 0;
$type = isset($param['type']) ? $param['type'] : 0;
$res = Db::table('cms_agriculture_products')->where('id',$id)->delete();
if ($res){
return to_assign();
}else{
return to_assign(1, '操作失败,原因:'.$res);
}
}
}