DeliveryAppointmentService.php 6.8 KB

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