DeliveryAppointmentService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. namespace App\Services;
  3. use App\CommodityBarcode;
  4. use App\DeliveryAppointmentDetail;
  5. use App\Services\common\QueryService;
  6. use App\Traits\ServiceAppAop;
  7. use App\DeliveryAppointment;
  8. use App\Warehouse;
  9. use Illuminate\Database\Eloquent\Builder;
  10. use Illuminate\Support\Facades\Auth;
  11. use phpDocumentor\Reflection\Types\Integer;
  12. class DeliveryAppointmentService
  13. {
  14. use ServiceAppAop;
  15. protected $modelClass=DeliveryAppointment::class;
  16. /**
  17. * 搜索QUERY
  18. *
  19. * @param array $params
  20. *
  21. * @return Builder
  22. */
  23. public function query(array $params)
  24. {
  25. $owners = app("UserService")->getPermittingOwnerIds(Auth::user());
  26. return app(QueryService::class)->query($params,DeliveryAppointment::query(),[
  27. 'created_at_start' => ['alias' => 'created_at' , 'startDate' => ' 00:00:00'],
  28. 'appointment_date_start' => ['alias' => 'appointment_date' , 'startDate' => ' 00:00:00'],
  29. 'created_at_end' => ['alias' => 'created_at' , 'endDate' => ' 23:59:59'],
  30. 'appointment_date_end' => ['alias' => 'appointment_date' , 'endDate' => ' 23:59:59'],
  31. "owner_id"=>['multi' => ','],
  32. "asn_number"=>['like' => '','timeLimit' => 15],
  33. "id"=>['multi' => ','],
  34. ])->orderByDesc("id")->whereIn("owner_id",$owners)->with(["cars.car","details.commodity.barcodes"]);
  35. }
  36. /**
  37. * 根据吨,立方,SKU减产系数计算产能
  38. *
  39. * @param double $tonne
  40. * @param double $cubicMeter
  41. * @param int $amount
  42. * @param int $rpcc reduced_production_capacity_coefficient
  43. *
  44. * @return double
  45. */
  46. public function calculateCapacity($tonne, $cubicMeter, $amount, $rpcc)
  47. {
  48. $need = max(($tonne*config("appointment.production_capacity.tonne")),
  49. ($cubicMeter*config("appointment.production_capacity.cubic_meter")));
  50. $coefficient = config("appointment.production_capacity.coefficient");
  51. if ($amount && ($need/$amount < $coefficient)){
  52. $need *= 1+(($coefficient-($need/$amount))*($rpcc/100));
  53. }
  54. return $need;
  55. }
  56. /**
  57. * 根据提供的详情数组插入详情
  58. *
  59. * @param DeliveryAppointment|object $parent
  60. * @param array $details
  61. * @param string $name
  62. * @param string $amount
  63. * @param string $barCode
  64. */
  65. public function insertDetails($parent, array $details, $name="name", $amount="amount", $barCode="bar_code")
  66. {
  67. $codes = [];
  68. $names = [];
  69. $insert = [];//插入的元素
  70. foreach ($details as $index=>$detail){
  71. if (isset($detail["commodity_id"]) && $detail["commodity_id"]){
  72. $insert[] = [
  73. "delivery_appointment_id" => $parent->id,
  74. "commodity_id" => $detail["commodity_id"],
  75. "bar_code" => null,
  76. "name" => null,
  77. "amount" => $detail[$amount],
  78. ];
  79. unset($details[$index]);continue;
  80. }
  81. if ($detail[$barCode])$codes[] = $detail[$barCode];
  82. else $names[] = $detail[$name];
  83. }
  84. $map = [];//存储条码与ID的映射
  85. if ($codes || $names){
  86. CommodityBarcode::query()->with("commodity")->whereHas("commodity",function ($query)use($parent){
  87. /** @var Builder $query */
  88. $query->where("owner_id",$parent->owner_id);
  89. })->whereIn("code",$codes)->orWhereHas("commodity",function ($query)use($names){
  90. /** @var Builder $query */
  91. $query->whereIn("name",$names);
  92. })->get()->each(function ($commodity)use(&$map){
  93. $map[$commodity->code] = $commodity->commodity_id;
  94. $map[$commodity->commodity->name] = $commodity->commodity_id;
  95. });
  96. }
  97. foreach ($details as $detail){
  98. $code = $detail[$barCode];
  99. $insert[] = [
  100. "delivery_appointment_id" => $parent->id,
  101. "commodity_id" => $code ? ($map[$code] ?? null) : ($map[$detail[$name]] ?? null),
  102. "bar_code" => $code,
  103. "name" => $detail[$name],
  104. "amount" => $detail[$amount],
  105. ];
  106. }
  107. if ($insert)DeliveryAppointmentDetail::query()->insert($insert);
  108. }
  109. /**
  110. * 获取一个KEY值用于验证二维码 21-4-2 切换为永久二维码
  111. *
  112. * @return string
  113. */
  114. public function getKey()
  115. {
  116. $y = 'w';//date("y");
  117. $m = 'a';//date("m");
  118. $d = 's';//date("d");
  119. $k1 = substr(env("APP_KEY"),7,2);
  120. $k2 = substr(env("APP_KEY"),10,2);
  121. $k3 = substr(env("APP_KEY"),15,2);
  122. return md5($k3.$y.$k2.$m.$k1.$d);
  123. }
  124. /**
  125. * 验证key值
  126. *
  127. * @param string $key
  128. * @return bool
  129. */
  130. public function checkKey(string $key):bool
  131. {
  132. if ($key!=$this->getKey())return false;
  133. return true;
  134. }
  135. /**
  136. * 获取当前时段
  137. *
  138. * @param int|null $hour
  139. *
  140. * @return int|bool
  141. */
  142. public function getPeriod($hour=null)
  143. {
  144. if (!$hour)$hour = date("H");
  145. foreach (DeliveryAppointment::PERIOD as $key=>$period){
  146. $arr = explode("-",$period);
  147. if (count($arr)!=2)continue;
  148. if ($hour>=$arr[0] && $hour<$arr[1])return $key;
  149. }
  150. return false;
  151. }
  152. /**
  153. * 验证预约单完整性修改状态
  154. *
  155. * @param integer $id
  156. */
  157. public function checkFull($id)
  158. {
  159. /** @var DeliveryAppointment|\stdClass $delivery */
  160. $delivery = DeliveryAppointment::query()->withCount(["cars"=>function($query){
  161. /** @var Builder $query */
  162. $query->whereNull("delivery_time")->where("status","!=",2);
  163. }])->find($id);
  164. if ($delivery->cars_count == 0)$delivery->update(["status"=>2]);
  165. }
  166. /**
  167. * 获取指定时段可用产能
  168. *
  169. * @param string $date
  170. * @param int $period
  171. * @param integer $warehouseId
  172. *
  173. * @return double
  174. */
  175. public function getAvailableCapacity($date,$period,$warehouseId)
  176. {
  177. $result = DeliveryAppointment::query()->selectRaw("appointment_date,date_period,SUM(capacity) AS capacity")
  178. ->where("appointment_date",$date)
  179. ->where("date_period",$period)
  180. ->where("warehouse_id",$warehouseId)
  181. ->where("status",0)
  182. ->groupBy(["appointment_date","date_period"])
  183. ->lockForUpdate()->first();
  184. if (!$result)return 0;
  185. /** @var \stdClass $warehouse */
  186. $warehouse = Warehouse::query()->find($warehouseId);
  187. if (!$warehouse)return 0;
  188. $total = $warehouse->production_capacity*DeliveryAppointment::HOUR[$period];
  189. return $total-$result->capacity;
  190. }
  191. }