Couponarea.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. namespace app\admin\controller\coupon;
  3. use app\admin\model\SystemAdmin;
  4. use app\common\model\CouponAreaModel;
  5. use app\common\model\CouponPlanModel;
  6. use app\common\model\ShopCategory;
  7. use app\admin\traits\Curd;
  8. use app\common\controller\AdminController;
  9. use app\common\model\ThirddataLogModel;
  10. use app\common\model\ThirddataModel;
  11. use EasyAdmin\annotation\ControllerAnnotation;
  12. use EasyAdmin\annotation\NodeAnotation;
  13. use think\App;
  14. use think\facade\Db;
  15. use think\Model;
  16. /**
  17. * (业务已取消)
  18. * Class Admin
  19. * @package app\admin\controller\system
  20. * @ControllerAnnotation(title="消费券分类管理")
  21. */
  22. class Couponarea extends AdminController
  23. {
  24. public function __construct(App $app)
  25. {
  26. parent::__construct($app);
  27. $this->model = new CouponAreaModel();
  28. }
  29. use Curd;
  30. protected $allowModifyFields = [
  31. 'can_buy',
  32. 'status'
  33. ];
  34. public function index ()
  35. {
  36. if ($this->request->isAjax()) {
  37. if (input('selectFields')) {
  38. return $this->selectList();
  39. }
  40. list($page, $limit, $where) = $this->buildTableParames();
  41. $count = $this->model
  42. ->where($where)
  43. ->count();
  44. // return 11;
  45. // return json_decode('[{"price":10, "fee":1},{"price":50, "fee":0.5},{"price":100, "fee":0}]');
  46. $list = $this->model
  47. ->withoutField('password')
  48. ->withAttr('buy_time', function ($val, $data){
  49. if (!$val){return '还未配置时间';}
  50. $curdata = json_decode($val);
  51. $cu_data['day'] = sr_getcurtime($curdata->day, 'Y-m-d');
  52. $arr_time = $curdata->time;
  53. for ($i = 0; $i < count($arr_time); $i++){
  54. $val1 = $curdata->time[$i];
  55. $key = 'time'.($i+1);
  56. $cu_data[$key] = (sr_getcurtime($val1->start_time, 'H:i:s') . ' - ' . sr_getcurtime($val1->end_time, 'H:i:s'));
  57. }
  58. return ($cu_data['day']. '日'. ($cu_data['time1']?('【'.$cu_data['time1'].'】'):'' ). '|'. (isset($cu_data['time2'])?('【'.$cu_data['time2'].'】'):''). '|' .(isset($cu_data['time3'])?('【'.$cu_data['time3'].'】'):''));
  59. })
  60. ->where($where)
  61. ->page($page, $limit)
  62. ->order($this->sort)
  63. ->select();
  64. $data = [
  65. 'code' => 0,
  66. 'msg' => '',
  67. 'count' => $count,
  68. 'data' => $list,
  69. ];
  70. return json($data);
  71. }
  72. return $this->fetch();
  73. }
  74. /**
  75. * @NodeAnotation(title="添加")
  76. */
  77. public function add()
  78. {
  79. if ($this->request->isAjax()){
  80. $row = request()->post();
  81. $row['create_time'] = sr_getcurtime(time());
  82. unset($row['file']);
  83. $modelplan = new CouponPlanModel();
  84. Db::startTrans();
  85. try {
  86. $a_id = $this->model->insertGetId($row);
  87. $modelplan->insert([
  88. 'area_id'=>$a_id,
  89. 'target_num'=>$row['begin_num'],
  90. 'create_time'=>sr_getcurtime(time()),
  91. 'less_num'=>$row['begin_num'],
  92. 'buy_least'=>10,
  93. 'buy_most'=>1000
  94. ]);
  95. Db::commit();
  96. }catch (\Exception $e){
  97. Db::rollback();
  98. $this->error('添加失败'. $e->getMessage());
  99. }
  100. $this->success('添加成功');
  101. }
  102. return $this->fetch();
  103. }
  104. /**
  105. * @NodeAnotation(title="修改")
  106. */
  107. public function edit ($id)
  108. {
  109. $row = $this->model->find($id);
  110. empty($row) && $this->error('数据不存在');
  111. if ($this->request->isAjax()) {
  112. $post = $this->request->post();
  113. $rule = [];
  114. $this->validate($post, $rule);
  115. try {
  116. if (!empty($post['content'])) {
  117. $post['content'] = htmlspecialchars_decode($post['content']);
  118. }
  119. $save = $row->save($post);
  120. } catch (\Exception $e) {
  121. $this->error('保存失败');
  122. }
  123. $save ? $this->success('保存成功') : $this->error('保存失败');
  124. }
  125. $this->assign('row', $row);
  126. return $this->fetch();
  127. }
  128. /**
  129. * @NodeAnotation(title="配置时间")
  130. */
  131. public function addtime($id){
  132. $row = $this->model->find($id);
  133. empty($row) && $this->error('数据不存在');
  134. if ($this->request->isAjax()) {
  135. $post = $this->request->post();
  136. $day = strtotime($post['day']);
  137. $beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));
  138. $endToday=mktime(24,0,0,date('m'),date('d'),date('Y'));
  139. $is_today = false;
  140. if ($beginToday<=$day && $endToday>$day){
  141. $is_today = true;
  142. }
  143. $timearr = [];
  144. if ($post['time1']){
  145. $arrtime1 = explode(' - ', $post['time1']);
  146. // sr_log('aaa'.$post['day'].' '. $arrtime1[0]);
  147. $arrtime1 = [strtotime($post['day'].' '. $arrtime1[0]), strtotime($post['day'].' '. $arrtime1[1])];
  148. array_push($timearr, ['start_time' => $arrtime1[0], 'end_time' => $arrtime1[1]]);
  149. }
  150. if ($post['time2']){
  151. $arrtime2 = explode(' - ', $post['time2']);
  152. $arrtime2 = [strtotime($post['day'].' '.$arrtime2[0]), strtotime($post['day'].' '.$arrtime2[1])];
  153. array_push($timearr, ['start_time' => $arrtime2[0], 'end_time' => $arrtime2[1]]);
  154. }
  155. if ($post['time3']){
  156. $arrtime3 = explode(' - ', $post['time3']);
  157. $arrtime3 = [strtotime($post['day'].' '.$arrtime3[0]), strtotime($post['day'].' '.$arrtime3[1])];
  158. array_push($timearr, ['start_time' => $arrtime3[0], 'end_time' => $arrtime3[1]]);
  159. }
  160. if (count($timearr) == 0){
  161. $this->error('请选择时间段');
  162. }
  163. foreach ($timearr as $key=>$value){
  164. if ($timearr[$key]['start_time'] > $timearr[$key]['end_time']){
  165. $this->error('时间段开始时间比如小于结束时间');
  166. }
  167. }
  168. if (!compareDate($timearr)){
  169. $this->error('时间段交叉,保存失败');
  170. }
  171. try {
  172. $save = $row->save([
  173. 'buy_time' => json_encode(['day'=>$day, 'time'=>$timearr]),
  174. 'is_today'=>($is_today?1:0)
  175. ]);
  176. } catch (\Exception $e) {
  177. $this->error('保存失败');
  178. }
  179. $save ? $this->success('保存成功') : $this->error('保存失败');
  180. }
  181. $curdata = json_decode($row['buy_time']);
  182. if ($curdata){
  183. $cu_data['day'] = sr_getcurtime($curdata->day, 'Y-m-d');
  184. $arr_time = $curdata->time;
  185. // dd($arr_time);
  186. for ($i = 0; $i < count($arr_time); $i++){
  187. $val = $curdata->time[$i];
  188. $key = 'time'.($i+1);
  189. $cu_data[$key] = (sr_getcurtime($val->start_time, 'H:i:s') . ' - ' . sr_getcurtime($val->end_time, 'H:i:s'));
  190. }
  191. $this->assign('row', $cu_data);
  192. }
  193. return $this->fetch();
  194. }
  195. }