CultureFoodController.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 食物管理
  5. * @author 晓宇
  6. * @version 0.0.1
  7. * @datetime 2019-03-29
  8. */
  9. class CultureFoodController extends CommonController
  10. {
  11. /**
  12. * [_initialize 前置操作-继承公共前置方法]
  13. */
  14. public function _initialize()
  15. {
  16. // 调用父类前置方法
  17. parent::_initialize();
  18. // 登录校验
  19. $this->Is_Login();
  20. // 权限校验
  21. $this->Is_Power();
  22. }
  23. /**
  24. * [Index 食物列表]
  25. */
  26. public function Index()
  27. {
  28. // 参数
  29. $param = array_merge($_POST, $_GET);
  30. // 模型模型
  31. $m = D('Culture/CultureFood');
  32. // 条件
  33. $where = $this->GetIndexWhere();
  34. // 分页
  35. $number = MyC('admin_page_number');
  36. $page_param = array(
  37. 'number' => $number,
  38. 'total' => $m->where($where)->count(),
  39. 'where' => $param,
  40. 'url' => U('Culture/CultureFood/Index'),
  41. );
  42. $page = new \My\Page($page_param);
  43. // 获取列表
  44. $field = array('food_id', 'food_name', 'thumb', 'description', 'sale_price', 'pet_type_id', 'pet_id', 'created_at', 'updated_at');
  45. $list = $this->SetDataHandle($m->field($field)->relation('FoodType')->where($where)->limit($page->GetPageStarNumber(), $number)->order('food_id desc')->select());
  46. // 参数
  47. $this->assign('param', $param);
  48. // 分页
  49. $this->assign('page_html', $page->GetPageHtml());
  50. // 数据列表
  51. $this->assign('list', $list);
  52. $this->display('Index');
  53. }
  54. /**
  55. * [SetDataHandle 数据处理]
  56. * @param [array] $data [食物数据]
  57. * @return [array] [处理好的数据]
  58. */
  59. private function SetDataHandle($data)
  60. {
  61. // 获取宠物类型
  62. foreach ($data as $k => &$value) {
  63. if($value['pet_type_id']){
  64. $value['pet_type_name'] = D("Culture/CulturePetType")->where(["pet_type_id" => $value['pet_type_id']])->getField("pet_type_name");
  65. }
  66. if($value['pet_id']){
  67. $value['pet_name'] = D("Culture/CulturePet")->where(["pet_id" => $value['pet_id']])->getField("pet_name");
  68. }
  69. }
  70. return $data;
  71. }
  72. /**
  73. * [GetIndexWhere 食物列表条件]
  74. */
  75. private function GetIndexWhere()
  76. {
  77. $where = array();
  78. // 模糊
  79. if(!empty($_REQUEST['keyword']))
  80. {
  81. $like_keyword = array('like', '%'.I('keyword').'%');
  82. $where[] = array(
  83. 'food_name' => $like_keyword,
  84. );
  85. }
  86. // 是否更多条件
  87. if(I('is_more', 0) == 1)
  88. {
  89. // 表达式
  90. if(!empty($_REQUEST['time_start']))
  91. {
  92. $where['created_at'][] = array('gt', I('time_start') . ' 00:00:00');
  93. }
  94. if(!empty($_REQUEST['time_end']))
  95. {
  96. $where['created_at'][] = array('lt', I('time_end') . ' 23:59:59');
  97. }
  98. }
  99. $where['deleted_at'] = ["exp", "is null"];
  100. return $where;
  101. }
  102. /**
  103. * [SaveInfo 食物添加/编辑页面]
  104. */
  105. public function SaveInfo()
  106. {
  107. // 食物信息
  108. $data = empty($_REQUEST['food_id']) ? array() : D('Culture/CultureFood')->find(I('food_id'));
  109. $this->assign('data', $data);
  110. // 食物类型信息
  111. $map['deleted_at'] = ["exp", "is null"];
  112. $pet_types = D('Culture/CulturePetType')->field("pet_type_id, pet_type_name")->where($map)->select();
  113. $this->assign('pet_types', $pet_types);
  114. if($data['pet_type_id']){
  115. // 获取宠物信息
  116. $map['pet_type_id'] = $data['pet_type_id'];
  117. $pets = D('Culture/CulturePet')->field("pet_id, pet_name")->where($map)->select();
  118. $this->assign('pets', $pets);
  119. }
  120. $this->display('SaveInfo');
  121. }
  122. /**
  123. * [Save 食物添加/编辑]
  124. */
  125. public function Save()
  126. {
  127. // 是否ajax请求
  128. if(!IS_AJAX)
  129. {
  130. $this->error(L('common_unauthorized_access'));
  131. }
  132. // 添加
  133. if(empty($_POST['food_id']))
  134. {
  135. $this->Add();
  136. // 编辑
  137. } else {
  138. $this->Edit();
  139. }
  140. }
  141. /**
  142. * [Add 食物添加]
  143. */
  144. private function Add()
  145. {
  146. // 食物模型
  147. $m = D('Culture/CultureFood');
  148. // 数据自动校验
  149. if($m->create($_POST, 1))
  150. {
  151. $m->created_at = date('Y-m-d H:i:s');
  152. $m->updated_at = date('Y-m-d H:i:s');
  153. // 数据添加
  154. if($m->add())
  155. {
  156. $this->ajaxReturn(L('common_operation_add_success'));
  157. } else {
  158. $this->ajaxReturn(L('common_operation_add_error'), -100);
  159. }
  160. } else {
  161. $this->ajaxReturn($m->getError(), -1);
  162. }
  163. }
  164. /**
  165. * [Edit 食物编辑]
  166. */
  167. private function Edit()
  168. {
  169. // 食物模型
  170. $m = D('Culture/CultureFood');
  171. // 数据自动校验
  172. if($m->create($_POST, 2))
  173. {
  174. $m->updated_at = date('Y-m-d H:i:s');
  175. // 更新数据库
  176. if($m->where(array('food_id'=>I('food_id')))->save())
  177. {
  178. $this->ajaxReturn(L('common_operation_edit_success'));
  179. } else {
  180. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  181. }
  182. } else {
  183. $this->ajaxReturn($m->getError(), -1);
  184. }
  185. }
  186. /**
  187. * [Delete 食物删除]
  188. */
  189. public function Delete()
  190. {
  191. // 是否ajax请求
  192. if(!IS_AJAX)
  193. {
  194. $this->error(L('common_unauthorized_access'));
  195. }
  196. // 参数处理
  197. $id = I('id');
  198. // 删除数据
  199. if(!empty($id))
  200. {
  201. // 食物模型
  202. $m = D('Culture/CultureFood');
  203. // 食物是否存在
  204. $scene = $m->where(array('food_id'=>$id))->count();
  205. if(empty($scene))
  206. {
  207. $this->ajaxReturn(L('common_user_no_exist_error'), -2);
  208. }
  209. // 删除食物
  210. $update_data['deleted_at'] = date('Y-m-d H:i:s');
  211. $state = $m->where(array('food_id'=>$id))->save($update_data);
  212. if($state !== false)
  213. {
  214. $this->ajaxReturn(L('common_operation_delete_success'));
  215. } else {
  216. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  217. }
  218. } else {
  219. $this->ajaxReturn(L('common_param_error'), -1);
  220. }
  221. }
  222. /**
  223. * 获取宠物接口
  224. */
  225. public function getPet()
  226. {
  227. $pet_type_id = I('pet_type_id');
  228. if(!$pet_type_id){
  229. $response_data['code'] = 0;
  230. $response_data['msg'] = "缺少参数";
  231. echo json_encode($response_data, JSON_UNESCAPED_UNICODE);
  232. exit();
  233. }
  234. $map['pet_type_id'] = $pet_type_id;
  235. $map['deleted_at'] = ["exp", "is null"];
  236. $pets = D("Culture/CulturePet")->field("pet_id as id, pet_name as name")->where($map)->select();
  237. $response_data['code'] = 0;
  238. $response_data['msg'] = "获取成功";
  239. $response_data['data'] = $pets;
  240. echo json_encode($response_data, JSON_UNESCAPED_UNICODE);
  241. exit();
  242. }
  243. }