Currency.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. namespace app\home\model;
  3. use think\Model;
  4. class Currency extends Model
  5. {
  6. public function add($param)
  7. {
  8. if ($this->codeExists((string) $param['code'])) {
  9. return to_assign(1, '该币种代码已存在,请勿重复添加');
  10. }
  11. try {
  12. $param['code'] = strtoupper(trim((string) $param['code']));
  13. $param['create_time'] = time();
  14. $insertId = self::strict(false)->field(true)->insertGetId($param);
  15. add_log('add', $insertId, $param);
  16. } catch (\Exception $e) {
  17. return to_assign(1, '操作失败,原因:' . $e->getMessage());
  18. }
  19. return to_assign(0, '操作成功', ['return_id' => $insertId]);
  20. }
  21. public function edit($param)
  22. {
  23. if ($this->codeExists((string) $param['code'], (int) $param['id'])) {
  24. return to_assign(1, '该币种代码已存在,请勿重复添加');
  25. }
  26. try {
  27. $param['code'] = strtoupper(trim((string) $param['code']));
  28. self::where('id', $param['id'])->strict(false)->field(true)->update($param);
  29. add_log('edit', $param['id'], $param);
  30. } catch (\Exception $e) {
  31. return to_assign(1, '操作失败,原因:' . $e->getMessage());
  32. }
  33. return to_assign(0, '操作成功', ['return_id' => $param['id']]);
  34. }
  35. /**
  36. * 校验币种代码是否已被占用(编辑时排除自身)
  37. */
  38. protected function codeExists(string $code, int $excludeId = 0): bool
  39. {
  40. $code = strtoupper(trim($code));
  41. $query = self::where(['code' => $code, 'delete_time' => 0]);
  42. if ($excludeId > 0) {
  43. $query->where('id', '<>', $excludeId);
  44. }
  45. return $query->count() > 0;
  46. }
  47. public function getById($id)
  48. {
  49. return self::find($id);
  50. }
  51. public function delById($id)
  52. {
  53. try {
  54. self::where('id', $id)->update(['delete_time' => time()]);
  55. add_log('delete', $id);
  56. } catch (\Exception $e) {
  57. return to_assign(1, '操作失败,原因:' . $e->getMessage());
  58. }
  59. return to_assign();
  60. }
  61. /**
  62. * 业务表单下拉用:取所有启用币种,按 sort 排序
  63. */
  64. public static function getEnabled()
  65. {
  66. return self::where(['status' => 1, 'delete_time' => 0])
  67. ->order('sort asc, id asc')
  68. ->select();
  69. }
  70. }