DeliveryAppointmentService.php 7.3 KB

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