WithdrawCate.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. namespace app\finance\model;
  3. use think\Model;
  4. class WithdrawCate extends Model
  5. {
  6. public function add($param)
  7. {
  8. if ($this->valueExists((int) $param['type'], (int) $param['value'])) {
  9. return to_assign(1, '该类型下「值」已存在,请使用其它值');
  10. }
  11. try {
  12. $param['create_time'] = time();
  13. $insertId = self::strict(false)->field(true)->insertGetId($param);
  14. add_log('add', $insertId, $param);
  15. } catch (\Exception $e) {
  16. return to_assign(1, '操作失败,原因:' . $e->getMessage());
  17. }
  18. return to_assign(0, '操作成功', ['return_id' => $insertId]);
  19. }
  20. public function edit($param)
  21. {
  22. if ($this->valueExists((int) $param['type'], (int) $param['value'], (int) $param['id'])) {
  23. return to_assign(1, '该类型下「值」已存在,请使用其它值');
  24. }
  25. try {
  26. self::where('id', $param['id'])->strict(false)->field(true)->update($param);
  27. add_log('edit', $param['id'], $param);
  28. } catch (\Exception $e) {
  29. return to_assign(1, '操作失败,原因:' . $e->getMessage());
  30. }
  31. return to_assign(0, '操作成功', ['return_id' => $param['id']]);
  32. }
  33. /**
  34. * 校验同 type 下 value 是否已被占用(编辑时排除自身)
  35. */
  36. protected function valueExists(int $type, int $value, int $excludeId = 0): bool
  37. {
  38. $query = self::where(['type' => $type, 'value' => $value, 'delete_time' => 0]);
  39. if ($excludeId > 0) {
  40. $query->where('id', '<>', $excludeId);
  41. }
  42. return $query->count() > 0;
  43. }
  44. public function getById($id)
  45. {
  46. return self::find($id);
  47. }
  48. public function delById($id)
  49. {
  50. try {
  51. self::where('id', $id)->update(['delete_time' => time()]);
  52. add_log('delete', $id);
  53. } catch (\Exception $e) {
  54. return to_assign(1, '操作失败,原因:' . $e->getMessage());
  55. }
  56. return to_assign();
  57. }
  58. public static function getByType($type)
  59. {
  60. return self::where(['type' => $type, 'status' => 1, 'delete_time' => 0])
  61. ->order('sort asc, id asc')
  62. ->select();
  63. }
  64. }