InventoryExchangeLogic.php 7.8 KB

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