InventoryExchangeLogic.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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::alias('a')
  103. ->field('a.*,b.good_cover_img,b.moq,b.step,b.type,b.price,b.good_banner_img,b.good_img,b.good_param,b.good_remark,c.unit,b.status as good_status')
  104. ->leftJoin('good b', 'b.id=a.good_id AND b.is_del=' . CommonModel::$del_normal)
  105. ->leftJoin('unit c','c.id=b.unit_id AND c.is_del='.CommonModel::$del_normal)
  106. ->withAttr('good_banner_img', function ($val) {
  107. return explode(',', $val);
  108. })->withAttr('good_img', function ($val) {
  109. return explode(',', $val);
  110. })->withAttr('good_param', function ($val) {
  111. return json_decode($val);
  112. })
  113. ->where(['a.id' => $id])
  114. ->findOrEmpty()
  115. ->toArray();
  116. if (empty($res)) throw new ValidateException('该兑换商品库存为空');
  117. return json_show(CommonModel::$success, '获取兑换商品库存详情成功', $res);
  118. }
  119. //编辑兑换商品库存
  120. public static function edit(array $data = []): Json
  121. {
  122. Db::startTrans();
  123. try {
  124. $rs = InventoryExchangeModel::field('id,inventory,good_id,good_code,good_name')
  125. ->lock(true)
  126. ->where('id', $data['id'])
  127. ->findOrEmpty();
  128. if ($rs->isEmpty()) throw new Exception('该兑换商品库存不存在');
  129. $date = date('Y-m-d H:i:s');
  130. $inventory = bcadd($rs->inventory, bcmul($data['flag'], $data['number']));
  131. if ($inventory < 0) throw new Exception('最终库存数少于0');
  132. Db::name('inventory_exchange_log')->insert([
  133. 'inventory_exchange_id' => $data['id'],
  134. 'before_inventory' => $rs->inventory,
  135. 'after_inventory' => $inventory,
  136. 'flag' => $data['flag'],
  137. 'good_id' => $rs->good_id,
  138. 'good_code' => $rs->good_code,
  139. 'good_name' => $rs->good_name,
  140. 'source' => CommonModel::$source_admin,
  141. 'createrid' => self::$uid,
  142. 'creater' => self::$uname,
  143. 'addtime' => $date,
  144. ]);
  145. InventoryExchangeModel::where('id', $data['id'])
  146. ->save(['updatetime' => $date, 'updaterid' => self::$uid, 'updater' => self::$uname, 'inventory' => $inventory]);
  147. Db::commit();
  148. return json_show(CommonModel::$success, '编辑兑换商品库存成功');
  149. } catch (Exception $exception) {
  150. Db::rollback();
  151. return json_show(CommonModel::$error_param, '编辑兑换商品库存失败,' . $exception->getMessage());
  152. }
  153. }
  154. //兑换商品库存变动记录
  155. public static function log(array $data = []): Json
  156. {
  157. $count = Db::name('inventory_exchange_log')
  158. ->where('inventory_exchange_id', $data['id'])
  159. ->count('id');
  160. $res = Db::name('inventory_exchange_log')
  161. ->field('id,before_inventory,after_inventory,flag,good_id,good_code,good_name,source,creater,addtime')
  162. ->where('inventory_exchange_id', $data['id'])
  163. ->page($data['page'], $data['size'])
  164. ->order('id', 'desc')
  165. ->select()
  166. ->toArray();
  167. return $res ? json_show(CommonModel::$success, '获取兑换商品库存变动记录成功', ['count' => $count, 'list' => $res]) : json_show(CommonModel::$success, '获取兑换商品库存变动记录失败');
  168. }
  169. }