InventoryExchangeLogic.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace app\admin\logic;
  3. use app\model\AccountModel;
  4. use app\model\CommonModel;
  5. use app\model\GoodModel;
  6. use app\model\InventoryExchangeModel;
  7. use think\Exception;
  8. use think\exception\ValidateException;
  9. use think\facade\Db;
  10. use think\response\Json;
  11. class InventoryExchangeLogic extends BaseLogic
  12. {
  13. //获取兑换商品库存列表
  14. public static function list(array $data = []): Json
  15. {
  16. $db = InventoryExchangeModel::alias('a')
  17. ->leftJoin('account b', 'b.id=a.account_id AND b.is_del=' . CommonModel::$del_normal);
  18. if ($data['status'] != '') $db->where('b.status', $data['status']);
  19. if (($data['inventory_start'] != '') && ($data['inventory_end'] != '')) $db->whereBetween('a.inventory', [$data['inventory_start'], $data['inventory_end']]);
  20. if ($data['username'] != '') $db->whereLike('a.account_username', '%' . $data['username'] . '%');
  21. if ($data['name'] != '') $db->whereLike('a.account_name', '%' . $data['name'] . '%');
  22. if ($data['mobile'] != '') $db->whereLike('b.mobile', '%' . $data['mobile'] . '%');
  23. $count = $db->count('a.id');
  24. $list = $db->field('a.id,a.account_username,b.status,a.account_name,b.mobile,a.inventory,a.updatetime')
  25. ->page($data['page'], $data['size'])
  26. ->order(['a.id' => 'desc'])
  27. ->select()
  28. ->toArray();
  29. return json_show(CommonModel::$success, '获取兑换商品库存列表成功', ['count' => $count, 'list' => $list]);
  30. }
  31. //添加兑换商品库存
  32. public static function add(array $data = []): Json
  33. {
  34. Db::startTrans();
  35. try {
  36. $rs = InventoryExchangeModel::field('id')
  37. ->where([
  38. 'account_id' => $data['account_id'],
  39. 'good_id' => $data['good_id']
  40. ])
  41. ->findOrEmpty()
  42. ->isEmpty();
  43. if (!$rs) throw new Exception('该账号下的兑换商品库存已存在,不能重复添加');
  44. $account = AccountModel::field('id,username,name')
  45. ->where(['id' => $data['account_id'], 'is_del' => CommonModel::$del_normal])
  46. ->findOrEmpty();
  47. if ($account->isEmpty()) throw new Exception('该账号不存在');
  48. $good = GoodModel::field('id,good_code,good_name')
  49. ->where(['id' => $data['good_id'], 'is_del' => CommonModel::$del_normal])
  50. ->findOrEmpty();
  51. if ($good->isEmpty()) throw new Exception('该商品不存在');
  52. $date = date('Y-m-d H:i:s');
  53. $inventory_exchange_id = Db::name('inventory_exchange')->insertGetId([
  54. 'account_id' => $data['account_id'],
  55. 'account_username' => $account->username,
  56. 'account_name' => $account->name,
  57. 'good_id' => $data['good_id'],
  58. 'good_code' => $good->good_code,
  59. 'good_name' => $good->good_name,
  60. 'inventory' => $data['inventory'],
  61. 'createrid' => self::$uid,
  62. 'creater' => self::$uname,
  63. 'addtime' => $date,
  64. 'updaterid' => self::$uid,
  65. 'updater' => self::$uname,
  66. 'updatetime' => $date,
  67. ]);
  68. Db::name('inventory_exchange_log')->insert([
  69. 'inventory_exchange_id' => $inventory_exchange_id,
  70. 'before_inventory' => 0,
  71. 'after_inventory' => $data['inventory'],
  72. 'good_id' => $data['good_id'],
  73. 'good_code' => $good->good_code,
  74. 'good_name' => $good->good_name,
  75. 'source' => CommonModel::$source_admin,
  76. 'createrid' => self::$uid,
  77. 'creater' => self::$uname,
  78. 'addtime' => $date,
  79. ]);
  80. Db::commit();
  81. return json_show(CommonModel::$success, '添加兑换商品库存成功');
  82. } catch (Exception $exception) {
  83. Db::rollback();
  84. return json_show(CommonModel::$error_param, '添加兑换商品库存失败,' . $exception->getMessage());
  85. }
  86. }
  87. //获取兑换商品库存详情
  88. public static function read(int $id = 0): Json
  89. {
  90. $res = InventoryExchangeModel::field(true)
  91. ->where(['id' => $id])
  92. ->findOrEmpty()
  93. ->toArray();
  94. if (empty($res)) throw new ValidateException('该兑换商品库存为空');
  95. return json_show(CommonModel::$success, '获取兑换商品库存详情成功', $res);
  96. }
  97. //编辑兑换商品库存
  98. public static function edit(array $data = []): Json
  99. {
  100. Db::startTrans();
  101. try {
  102. $rs = InventoryExchangeModel::field('id,inventory,good_id,good_code,good_name')
  103. ->lock(true)
  104. ->where('id', $data['id'])
  105. ->findOrEmpty();
  106. if ($rs->isEmpty()) throw new Exception('该兑换商品库存不存在');
  107. $date = date('Y-m-d H:i:s');
  108. if ($rs->inventory != $data['inventory']) {
  109. Db::name('inventory_exchange_log')->insert([
  110. 'inventory_exchange_id' => $data['id'],
  111. 'before_inventory' => $rs->inventory,
  112. 'after_inventory' => $data['inventory'],
  113. 'good_id' => $rs->good_id,
  114. 'good_code' => $rs->good_code,
  115. 'good_name' => $rs->good_name,
  116. 'source' => CommonModel::$source_admin,
  117. 'createrid' => self::$uid,
  118. 'creater' => self::$uname,
  119. 'addtime' => $date,
  120. ]);
  121. }
  122. InventoryExchangeModel::where('id', $data['id'])
  123. ->save(array_merge($data, ['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname]));
  124. Db::commit();
  125. return json_show(CommonModel::$success, '编辑兑换商品库存成功');
  126. } catch (Exception $exception) {
  127. Db::rollback();
  128. return json_show(CommonModel::$error_param, '编辑兑换商品库存失败,' . $exception->getMessage());
  129. }
  130. }
  131. //兑换商品库存变动记录
  132. public static function log(array $data = []): Json
  133. {
  134. $count = Db::name('inventory_exchange_log')
  135. ->where('inventory_exchange_id', $data['id'])
  136. ->count('id');
  137. $res = Db::name('inventory_exchange_log')
  138. ->field('id,before_inventory,after_inventory,good_id,good_code,good_name,source,creater,addtime')
  139. ->where('inventory_exchange_id', $data['id'])
  140. ->page($data['page'], $data['size'])
  141. ->order('id', 'desc')
  142. ->select()
  143. ->toArray();
  144. return $res ? json_show(CommonModel::$success, '获取兑换商品库存变动记录成功', ['count' => $count, 'list' => $res]) : json_show(CommonModel::$success, '获取兑换商品库存变动记录失败');
  145. }
  146. }