InventoryShoppingLogic.php 6.9 KB

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