DeliveryAppointmentService.php 8.9 KB

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