ManageTitle.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace app\common\model;
  3. use think\Model;
  4. class ManageTitle extends Model
  5. {
  6. // 表名
  7. protected $table = 'manage_title';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'int';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. // 追加属性
  14. protected $append = [
  15. 'status_text',
  16. 'sex_text'
  17. ];
  18. public function getStatusList()
  19. {
  20. return ['normal' => '显示','hidden' => '隐藏'];
  21. }
  22. public function getSexList()
  23. {
  24. return ['1' => '男频', '2' => '女频', '3'=> '活动'];
  25. }
  26. public function getStatusTextAttr($value, $data)
  27. {
  28. $value = $value ? $value : $data['status'];
  29. $list = $this->getStatusList();
  30. return isset($list[$value]) ? $list[$value] : '';
  31. }
  32. public function getSexTextAttr($value, $data)
  33. {
  34. $value = $value ? $value : $data['sex'];
  35. $list = $this->getSexList();
  36. return isset($list[$value]) ? $list[$value] : '';
  37. }
  38. /**
  39. * 随机获取一个标题
  40. *
  41. * @author liues@dianzhong.com
  42. * @date 2018-08-24 10:21:33
  43. * @param array $sex
  44. * @return mixed
  45. */
  46. public function getRandTitle(array $sex = [1,2,3]){
  47. $where = ['status' => 'normal', 'sex' => ['in', $sex]];
  48. $ids = $this->where($where)->column('id');
  49. if($ids){
  50. $id = $ids ? array_rand($ids) : 0;
  51. $title = $this->where(['id' => $ids[$id]])->value('title');
  52. return $title;
  53. }
  54. return null;
  55. }
  56. }