ProjectDonationUser.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\common\model\project;
  3. use liuniu\BaseModel;
  4. use think\Model;
  5. class ProjectDonationUser extends BaseModel
  6. {
  7. // 表名
  8. protected $name = 'project_donation_user';
  9. // 自动写入时间戳字段
  10. protected $autoWriteTimestamp = false;
  11. // 定义时间戳字段名
  12. protected $createTime = false;
  13. protected $updateTime = false;
  14. protected $deleteTime = false;
  15. // 追加属性
  16. protected $append = [
  17. 'anonymous_text',
  18. 'invoice_text',
  19. 'user_type_text'
  20. ];
  21. public function getAnonymousList()
  22. {
  23. return ['0' => __('Anonymous 0'), '1' => __('Anonymous 1')];
  24. }
  25. public function getInvoiceList()
  26. {
  27. return ['0' => __('Invoice 0'), '1' => __('Invoice 1')];
  28. }
  29. public function getUserTypeList()
  30. {
  31. return ['0' => __('User_type 0'), '1' => __('User_type 1')];
  32. }
  33. public function getAnonymousTextAttr($value, $data)
  34. {
  35. $value = $value ? $value : (isset($data['anonymous']) ? $data['anonymous'] : '');
  36. $list = $this->getAnonymousList();
  37. return isset($list[$value]) ? $list[$value] : '';
  38. }
  39. public function getInvoiceTextAttr($value, $data)
  40. {
  41. $value = $value ? $value : (isset($data['invoice']) ? $data['invoice'] : '');
  42. $list = $this->getInvoiceList();
  43. return isset($list[$value]) ? $list[$value] : '';
  44. }
  45. public function getUserTypeTextAttr($value, $data)
  46. {
  47. $value = $value ? $value : (isset($data['user_type']) ? $data['user_type'] : '');
  48. $list = $this->getUserTypeList();
  49. return isset($list[$value]) ? $list[$value] : '';
  50. }
  51. /**
  52. * 设置默认用户
  53. * @param $id 地址id
  54. * @param $uid 用户uid
  55. * @return bool
  56. */
  57. public static function setDefault($id,$uid,$type=0,$cid=0)
  58. {
  59. self::beginTrans();
  60. $model = new self;
  61. if ($cid>0) $model = $model->where('cid',$cid);
  62. $res1 =$model::where('uid',$uid)->where('type',$type)->update(['is_default'=>0]);
  63. $res2 = $model::where('id',$id)->where('uid',$uid)->where('type',$type)->update(['is_default'=>1]);
  64. $res =$res1 !== false && $res2 !== false;
  65. self::checkTrans($res);
  66. return $res;
  67. }
  68. /**
  69. * 获取默认用户
  70. * @param $uid
  71. * @param int $type
  72. * @param string $field
  73. * @return array|\think\Model|null
  74. * @throws \think\db\exception\DataNotFoundException
  75. * @throws \think\db\exception\DbException
  76. * @throws \think\db\exception\ModelNotFoundException
  77. */
  78. public static function getDefault($uid,$type=0,$field="*",$cid=0)
  79. {
  80. return self::where('cid',$cid)->where('uid',$uid)->where('type',$type)->where('is_default',1)->field($field)->find();
  81. }
  82. }