SpecService.php 2.6 KB

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