DinnerCate.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Model;
  6. class DinnerCate extends Model
  7. {
  8. public function add(){
  9. $data = [
  10. 'id' => input('id/d',0),
  11. 'title' => input('title','','trim'),
  12. 'start' => input('start','','trim'),
  13. 'end' => input('end','','trim'),
  14. 'remark' => input('remark','','trim'),
  15. 'syesterday' => input('syesterday/d',0),
  16. 'eyesterday' => input('eyesterday/d',0),
  17. 'enable' => input('enable/d',1),
  18. 'color' => input('color','','trim'),
  19. 'show_start' => input('showStart','','trim'),
  20. 'show_end' => input('showEnd','','trim'),
  21. ];
  22. $logdata = json_encode($data);
  23. $result = validate('DinnerCate')->check($data,[],'');
  24. if(true !== $result){
  25. HelpHander::error(validate('DinnerCate')->getError());
  26. }
  27. if($data['syesterday'] > $data['eyesterday']){
  28. HelpHander::error('开始日期不能大于结束日期');
  29. }
  30. if($data['start'] > $data['end'] && $data['syesterday']==$data['eyesterday']){
  31. HelpHander::error('开始日期不能大于结束日期');
  32. }
  33. $id = $data['id'];
  34. unset($data['id']);
  35. if($id > 0){
  36. $data['update_time'] = date('Y-m-d H:i:s');
  37. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  38. }else{
  39. $data['create_time'] = date('Y-m-d H:i:s');
  40. $ret = $this->allowField(true)->save($data);
  41. }
  42. if(!$ret){
  43. HelpHander::error('操作失败');
  44. }
  45. if($id > 0){
  46. $content = '修改点餐分类';
  47. }else{
  48. $content = '添加点餐分类';
  49. }
  50. model('ActionLog')->add(20,$content,0,$logdata);
  51. return true;
  52. }
  53. public function info($id){
  54. $info = $this->where('id',$id)->find();
  55. if(!$info){
  56. HelpHander::error('数据不存在');
  57. }
  58. return $info->toArray();
  59. }
  60. public function lists($page,$size,$title,$enable){
  61. $map[] = ['del','=',0];
  62. if($title != ''){
  63. $map[] = ['title','like','%'.$title.'%'];
  64. }
  65. if($enable >= 0){
  66. $map[] = ['enable','=',$enable];
  67. }
  68. $lists = $this
  69. ->where($map)
  70. ->page($page,$size)
  71. ->order('id desc')
  72. ->select();
  73. $total = $this->where($map)->count();
  74. $data = [
  75. 'total' => $total,
  76. 'list' => $lists?$lists->toArray():[]
  77. ];
  78. return $data;
  79. }
  80. public function getList(){
  81. $map[] = ['del','=',0];
  82. $map[] = ['enable','=',1];
  83. $lists = $this
  84. ->where($map)
  85. ->field('id,title')
  86. ->order('id desc')
  87. ->select();
  88. return $lists?$lists->toArray():[];
  89. }
  90. public function allLists(){
  91. $lists = Db::name('dinner_cate')->where('del',0)->order('id asc')->select();
  92. return $lists?$lists:[];
  93. }
  94. public function del($id){
  95. $ret = $this->where('id',$id)->setField('del',1);
  96. if(!$ret){
  97. HelpHander::error('删除失败');
  98. }
  99. $logdata = json_encode(['id' => $id]);
  100. model('ActionLog')->add(20,'删除点餐分类',0,$logdata);
  101. return true;
  102. }
  103. }