0
0

ShopDay.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Db;
  4. class ShopDay extends Auth {
  5. public function index() {
  6. if (request()->isAjax()) {
  7. //分页参数
  8. $length = input('rows', 10, 'intval'); //每页条数
  9. $page = input('page', 1, 'intval'); //第几页
  10. $start = ($page - 1) * $length; //分页开始位置
  11. //排序
  12. $sortRow = input('sidx', 'id', 'trim'); //排序列
  13. $sort = input('sord', 'asc', 'trim'); //排序方式
  14. $order = $sortRow . ' ' . $sort;
  15. $day = input('day', '', 'trim');
  16. if ($day != '') {
  17. $map[] = ['day', '=', $day];
  18. }
  19. $cate = input('cate', '', 'trim');
  20. if ($cate != '') {
  21. $map[] = ['cate', '=', $cate];
  22. }
  23. $map[] = ['org_id', '=', $this->orgId];
  24. $map = empty($map) ? true : $map;
  25. //数据查询
  26. $lists = Db::name('shop_day')
  27. ->where($map)->limit($start, $length)
  28. ->order($order)->select();
  29. foreach ($lists as $k => $v) {
  30. $lists[$k]['goods_name'] = '';
  31. $ids = $v['goods'] ? explode(',', $v['goods']) : [];
  32. if ($ids) {
  33. $goodsName = Db::name('shop_goods')
  34. ->where('id', 'in', $ids)->where('del', 0)->column('title');
  35. $lists[$k]['goods_name'] = $goodsName ? implode(',', $goodsName) : '';
  36. }
  37. }
  38. //数据返回
  39. $totalCount = Db::name('shop_day')->where($map)->count();
  40. $totalPage = ceil($totalCount / $length);
  41. $result['page'] = $page;
  42. $result['total'] = $totalPage;
  43. $result['records'] = $totalCount;
  44. $result['rows'] = $lists;
  45. return json($result);
  46. }
  47. else {
  48. $this->assign('meta_title', '开业时间段列表');
  49. return $this->fetch();
  50. }
  51. }
  52. /**
  53. * 新增/编辑
  54. */
  55. public function add($id = 0) {
  56. if (request()->isPost()) {
  57. $model = new \app\common\model\ShopDay();
  58. $res = $model->updates();
  59. if ($res) {
  60. $this->success('操作成功', url('index'));
  61. }
  62. else {
  63. $this->error($model->getError());
  64. }
  65. }
  66. else {
  67. $meta_title = '新增时间段';
  68. if ($id) {
  69. $info = Db::name('shop_day')->where('id', $id)->find();
  70. $info['time'] = $info['start'] . ',' . $info['end'];
  71. $this->assign('info', $info);
  72. $meta_title = '编辑时间段';
  73. }
  74. //获取分类
  75. $map['org_id'] = $this->orgId;
  76. $map['del'] = 0;
  77. $map['enable'] = 1;
  78. $goods = Db::name('shop_goods')->field('id,title')
  79. ->where($map)->select();
  80. $this->assign('goods', $goods);
  81. $this->assign('meta_title', $meta_title);
  82. return $this->fetch();
  83. }
  84. }
  85. /**
  86. * 删除记录
  87. * @param int $id
  88. */
  89. public function del($id = 0) {
  90. if (!$id) {
  91. $this->error('参数错误');
  92. }
  93. $res = Db::name('shop_day')->delete($id);
  94. if ($res) {
  95. $this->success('删除成功');
  96. }
  97. else {
  98. $this->error('删除失败');
  99. }
  100. }
  101. }