CulturePetController.class.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 宠物管理
  5. * @author 晓宇
  6. * @version 0.0.1
  7. * @datetime 2019-03-29
  8. */
  9. class CulturePetController 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/CulturePet');
  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/CulturePet/Index'),
  41. );
  42. $page = new \My\Page($page_param);
  43. // 获取列表
  44. $field = array('pet_id', 'pet_name', 'thumb', 'description', 'pet_type_id', 'sale_price', 'created_at', 'updated_at');
  45. $list = $this->SetDataHandle($m->field($field)->relation('PetType')->where($where)->limit($page->GetPageStarNumber(), $number)->order('pet_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. return $data;
  62. }
  63. /**
  64. * [GetIndexWhere 宠物列表条件]
  65. */
  66. private function GetIndexWhere()
  67. {
  68. $where = array();
  69. // 模糊
  70. if(!empty($_REQUEST['keyword']))
  71. {
  72. $like_keyword = array('like', '%'.I('keyword').'%');
  73. $where[] = array(
  74. 'pet_name' => $like_keyword,
  75. );
  76. }
  77. // 是否更多条件
  78. if(I('is_more', 0) == 1)
  79. {
  80. // 表达式
  81. if(!empty($_REQUEST['time_start']))
  82. {
  83. $where['created_at'][] = array('gt', I('time_start') . ' 00:00:00');
  84. }
  85. if(!empty($_REQUEST['time_end']))
  86. {
  87. $where['created_at'][] = array('lt', I('time_end') . ' 23:59:59');
  88. }
  89. }
  90. $where['deleted_at'] = ["exp", "is null"];
  91. return $where;
  92. }
  93. /**
  94. * [SaveInfo 宠物添加/编辑页面]
  95. */
  96. public function SaveInfo()
  97. {
  98. // 宠物信息
  99. $data = empty($_REQUEST['pet_id']) ? array() : D('Culture/CulturePet')->find(I('pet_id'));
  100. $this->assign('data', $data);
  101. // 宠物类型信息
  102. $map['deleted_at'] = ["exp", "is null"];
  103. $pet_type_list = D('Culture/CulturePetType')->field("pet_type_id, pet_type_name")->where($map)->select();
  104. $this->assign('pet_type_list', $pet_type_list);
  105. // 场景信息
  106. $scene_list = D('Culture/CultureScene')->field("scene_id, scene_name")->where($map)->select();
  107. $this->assign('scene_list', $scene_list);
  108. $this->display('SaveInfo');
  109. }
  110. /**
  111. * [Save 宠物添加/编辑]
  112. */
  113. public function Save()
  114. {
  115. // 是否ajax请求
  116. if(!IS_AJAX)
  117. {
  118. $this->error(L('common_unauthorized_access'));
  119. }
  120. // 添加
  121. if(empty($_POST['pet_id']))
  122. {
  123. $this->Add();
  124. // 编辑
  125. } else {
  126. $this->Edit();
  127. }
  128. }
  129. /**
  130. * [Add 宠物添加]
  131. */
  132. private function Add()
  133. {
  134. // 宠物模型
  135. $m = D('Culture/CulturePet');
  136. // 数据自动校验
  137. if($m->create($_POST, 1))
  138. {
  139. $m->created_at = date('Y-m-d H:i:s');
  140. $m->updated_at = date('Y-m-d H:i:s');
  141. // 数据添加
  142. if($m->add())
  143. {
  144. $this->ajaxReturn(L('common_operation_add_success'));
  145. } else {
  146. $this->ajaxReturn(L('common_operation_add_error'), -100);
  147. }
  148. } else {
  149. $this->ajaxReturn($m->getError(), -1);
  150. }
  151. }
  152. /**
  153. * [Edit 宠物编辑]
  154. */
  155. private function Edit()
  156. {
  157. // 宠物模型
  158. $m = D('Culture/CulturePet');
  159. // 数据自动校验
  160. if($m->create($_POST, 2))
  161. {
  162. $m->updated_at = date('Y-m-d H:i:s');
  163. // 更新数据库
  164. if($m->where(array('pet_id'=>I('pet_id')))->save())
  165. {
  166. $this->ajaxReturn(L('common_operation_edit_success'));
  167. } else {
  168. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  169. }
  170. } else {
  171. $this->ajaxReturn($m->getError(), -1);
  172. }
  173. }
  174. /**
  175. * [Delete 宠物删除]
  176. */
  177. public function Delete()
  178. {
  179. // 是否ajax请求
  180. if(!IS_AJAX)
  181. {
  182. $this->error(L('common_unauthorized_access'));
  183. }
  184. // 参数处理
  185. $id = I('id');
  186. // 删除数据
  187. if(!empty($id))
  188. {
  189. // 宠物模型
  190. $m = D('Culture/CulturePet');
  191. // 宠物是否存在
  192. $scene = $m->where(array('pet_id'=>$id))->count();
  193. if(empty($scene))
  194. {
  195. $this->ajaxReturn(L('common_user_no_exist_error'), -2);
  196. }
  197. // 删除宠物
  198. $update_data['deleted_at'] = date('Y-m-d H:i:s');
  199. $state = $m->where(array('pet_id'=>$id))->save($update_data);
  200. if($state !== false)
  201. {
  202. $this->ajaxReturn(L('common_operation_delete_success'));
  203. } else {
  204. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  205. }
  206. } else {
  207. $this->ajaxReturn(L('common_param_error'), -1);
  208. }
  209. }
  210. }