InventoryExchangeLogic.php 8.1 KB

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