Files

137 lines
4.2 KiB
PHP

<?php
namespace app\common\service;
use app\common\model\order\Ships as OrderShips;
use app\common\traits\ServiceTrait;
class OrderShipService
{
use ServiceTrait;
/** @var OrderShips */
public static $Model = OrderShips::class;
/**
* @param $order_no
* @param $order_goods_spu_id
* @param $code
* @param $remark
* @return mixed
* @throws \Exception
*/
public static function createData($order_no, $order_goods_spu_id, $code, $remark = '')
{
$data = [
'order_no' => $order_no,
'order_goods_spu_id' => $order_goods_spu_id,
'code' => $code,
'remark' => $remark,
];
$res = self::create($data);
if (!$res) {
exception('创建发货单数据失败');
}
return $res;
}
/**
* 需求交付发货单(开发者发货:凭证截图 + 交付附件/链接 + 备注)
* 多图/多文件/多链接统一存英文逗号分隔的 URL 字符串:
* http://xxx/a.jpg,http://xxx/b.pdf
* 说明:源码通常在企业微信沟通群直接交付,平台只登记发货状态,
* 故附件与链接均为选填,仅凭证截图必填
* @param $order_no
* @param $order_goods_spu_id
* @param $demand_id
* @param array $images URL 数组
* @param array $files URL 数组(选填)
* @param string $remark
* @param array $links 交付链接数组(选填:Git仓库/网盘等持久获取方式)
* @return mixed
* @throws \Exception
*/
public static function createDemandDelivery($order_no, $order_goods_spu_id, $demand_id, array $images, array $files, $remark = '', array $links = [])
{
// 复用模型的规范化方法,统一为英文逗号分隔字符串
$imagesStr = OrderShips::toUrlString($images);
$filesStr = OrderShips::toUrlString($files);
$linksStr = OrderShips::toUrlString($links);
if (mb_strlen($imagesStr) > 1000 || mb_strlen($filesStr) > 1000 || mb_strlen($linksStr) > 1000) {
exception('附件数量过多或链接过长,请精简后重新发货');
}
$data = [
'order_no' => $order_no,
'order_goods_spu_id' => $order_goods_spu_id,
'demand_id' => $demand_id,
'images' => $imagesStr,
'files' => $filesStr,
'deliver_links' => $linksStr,
'remark' => $remark,
];
$res = self::create($data);
if (!$res) {
exception('创建发货单数据失败');
}
return $res;
}
/**
* 获取需求最新的发货单(未发货返回 null)
* @param int $demandId
* @return OrderShips|null
*/
public static function getDemandLastShip($demandId)
{
return self::where(['demand_id' => $demandId])->order('id', 'desc')->find();
}
/**
* 获取需求的交付内容(未发货返回 null)
* 结构:['images' => [{url,name}], 'files' => [{url,name}], 'remark' => '', 'delivery_time' => 时间戳]
* @param int $demandId
* @return array|null
*/
public static function getDemandDelivery($demandId)
{
$ship = self::getDemandLastShip($demandId);
if (!$ship) {
return null;
}
return [
'images' => $ship->images_list,
'files' => $ship->files_list,
'links' => $ship->deliver_links_list,
'remark' => $ship->remark,
'delivery_time' => $ship->createtime,
];
}
/**
* 判断需求是否已发货
* @param int $demandId
* @return bool
*/
public static function hasDemandDelivery($demandId)
{
return !empty(self::getDemandLastShip($demandId));
}
/**
* 批量获取已发货的需求ID(用于列表按钮展示,避免 N+1 查询)
* @param array $demandIds
* @return array
*/
public static function getDeliveredDemandIds(array $demandIds)
{
$demandIds = array_filter(array_unique(array_map('intval', $demandIds)));
if (!$demandIds) {
return [];
}
return self::where(['demand_id' => ['in', $demandIds]])->column('demand_id');
}
}