SpecService.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\api\service;
  4. use app\common\model\ShopList;
  5. use app\common\model\StockConfig;
  6. use app\common\model\ProductConfig;
  7. use app\common\model\StockDetail;
  8. /**
  9. * 录入发货
  10. */
  11. class SpecService{
  12. //判断价格为空
  13. public static function getIsZeroSpecsPrice(array $data): bool
  14. {
  15. $result = false;
  16. foreach ($data as $item) {
  17. if(empty($item['price'])) return false;
  18. }
  19. return $result;
  20. }
  21. //获取规格列表
  22. public static function getSpecsList(int $type_id, array $data): array
  23. {
  24. $result = [];
  25. foreach ($data as $item) {
  26. if($item['type_id'] == $type_id){
  27. $result[] = $item;
  28. }
  29. }
  30. return $result;
  31. }
  32. //录入规格列表
  33. public static function getDeliveryList(int $uid, array $data): array
  34. {
  35. $result = [];
  36. $customer_id = ShopList::where('id', $data['shop_id'])->value('customer_id');
  37. foreach ($data['variety'] as $item) {
  38. if(count($item) != 6 || empty(floatval($item['num']))) throw new \Exception('参数有误!');
  39. $specs = ProductConfig::where('id', $item['spec_id'])->field('weight,box_id')->find();
  40. if(!$specs) throw new \Exception('参数有误!');
  41. $weight = bcmul((string)$specs->weight, $item['num'], 2);
  42. $result[] = [
  43. 'user_id' => $uid,
  44. 'customer_id' => $customer_id,
  45. 'shop_id' => $data['shop_id'],
  46. 'plat_id' => $data['plat_id'],
  47. 'variety_id' => $item['variety_id'],
  48. 'spec_id' => $item['spec_id'],
  49. 'num' => $item['num'],
  50. 'weigh' => $weight,
  51. 'total_price' => bcmul($item['price'], $item['num'], 2)
  52. ];
  53. //根据品种扣除库存
  54. StockDetail::setStockConfigNum((int)$item['variety_id'], (string)-$weight, StockConfig::VarietyName);
  55. //根据规格扣除包装箱
  56. StockDetail::setStockConfigNum($specs->box_id, (string)-$item['num'], StockConfig::PackingBox);
  57. }
  58. return $result;
  59. }
  60. }