DeliveryAppointmentService.php 8.9 KB

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