Bonus.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\promotion;
  10. use app\admin\service\Service;
  11. use app\admin\model\promotion\PromotionBonus as PromotionBonusModel;
  12. use app\common\models\users\UsersBonus as UsersBonusModel;
  13. /**
  14. * 优惠劵服务类
  15. * Class Bonus
  16. * @package app\admin\service\promotion
  17. */
  18. class Bonus extends Service {
  19. /**
  20. * 列表
  21. * @param $data
  22. * @return array
  23. * @throws \think\db\exception\DataNotFoundException
  24. * @throws \think\db\exception\DbException
  25. * @throws \think\db\exception\ModelNotFoundException
  26. */
  27. public static function getList($data){
  28. $count = PromotionBonusModel::withSearch("name",['name'=>$data['key']["title"]??''])->count();
  29. $result = PromotionBonusModel::withSearch("name",['name'=>$data['key']["title"]??''])->page($data["page"]??1,$data["limit"]??10)->order("id","desc")->select()->toArray();
  30. foreach($result as $key=>$item){
  31. $result[$key]['total'] = $item["used"] . ' / ' . $item["giveout"];
  32. $result[$key]['time'] = $item["start_time"] . ' ~ ' . $item["end_time"];
  33. }
  34. return [ "count"=>$count, "data"=>$result ];
  35. }
  36. /**
  37. * 详情
  38. * @param $id
  39. * @return array[]
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public static function detail($id){
  45. $row = PromotionBonusModel::where("id",$id)->find();
  46. return [ "data"=>$row??[] ];
  47. }
  48. /**
  49. * 保存数据
  50. * @param $data
  51. * @return PromotionBonusModel|bool|\think\Model
  52. * @throws \Exception
  53. */
  54. public static function save($data){
  55. $data["start_time"] = strtotime($data["start_time"]);
  56. $data["end_time"] = strtotime($data["end_time"]);
  57. if($data["start_time"] > $data["end_time"]){
  58. throw new \Exception("开始时间不能小于结束时间",0);
  59. }
  60. if(PromotionBonusModel::where("id",$data["id"])->count()){
  61. return PromotionBonusModel::where("id",$data["id"])->save($data);
  62. }
  63. return PromotionBonusModel::create($data);
  64. }
  65. /**
  66. * 删除
  67. * @param $id
  68. * @return bool
  69. */
  70. public static function delete($id){
  71. if(PromotionBonusModel::where("id",$id)->delete()){
  72. return UsersBonusModel::where("bonus_id",$id)->delete();
  73. }
  74. return false;
  75. }
  76. /**
  77. * 更新字段值
  78. * @return PromotionBonusModel
  79. */
  80. public static function setFields(){
  81. $data = self::getFields();
  82. return PromotionBonusModel::where("id",$data["id"])->update([$data["name"]=>$data["value"]]);
  83. }
  84. }