2024-09-19 11:32:39 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace common\jobs;
|
|
|
|
|
|
|
|
|
|
|
|
use common\helpers\FuncHelper;
|
2024-12-19 19:20:27 +08:00
|
|
|
|
use common\models\oldDb\ProductOrder;
|
|
|
|
|
|
use common\models\oldDb\ProductOrderLog;
|
2024-09-19 11:32:39 +08:00
|
|
|
|
use common\services\JacErpService;
|
|
|
|
|
|
use yii\helpers\Json;
|
|
|
|
|
|
use yii\queue\JobInterface;
|
|
|
|
|
|
use yii\queue\Queue;
|
|
|
|
|
|
|
|
|
|
|
|
//产品订单同步
|
|
|
|
|
|
class ProductOrderSyncJob extends BaseJob implements JobInterface
|
|
|
|
|
|
{
|
|
|
|
|
|
public $orderId;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @param Queue $queue which pushed and is handling the job
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function execute($queue)
|
|
|
|
|
|
{
|
2025-04-02 10:55:31 +08:00
|
|
|
|
ProductOrderLog::saveLog($this->orderId,'开始同步订单');
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 开始数据库事务
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$t = \Yii::$app->db->beginTransaction();
|
|
|
|
|
|
try {
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 设置请求参数
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$this->setRequest();
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 查询符合条件的订单
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$ProductOrder = ProductOrder::find()->with(['prescription','store'])->where([
|
|
|
|
|
|
'id' => $this->orderId,
|
2025-04-01 19:11:22 +08:00
|
|
|
|
'is_sync_erp' => 0,
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'is_pay' => 1,
|
|
|
|
|
|
'is_online' => 0,
|
|
|
|
|
|
'status' => 1 //已付款待发货
|
|
|
|
|
|
])->one();
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查订单是否存在且未被取消
|
2024-09-19 11:32:39 +08:00
|
|
|
|
if (!$ProductOrder || !$ProductOrder->prescription || $ProductOrder->cancel_status == 1 || !$ProductOrder->store) {
|
|
|
|
|
|
throw new \Exception('订单不存在或已取消');
|
|
|
|
|
|
}
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 检查处方是否已审核
|
2024-09-19 11:32:39 +08:00
|
|
|
|
if($ProductOrder->prescription->status != 1){
|
|
|
|
|
|
throw new \Exception('处方未审核');
|
|
|
|
|
|
}
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 解析处方内容
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$prescriptionContent = Json::decode($ProductOrder->prescription->content);
|
|
|
|
|
|
$recipe = $prescriptionContent['repice'][0];
|
|
|
|
|
|
$recipeContent = Json::decode($recipe['content']);
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据煎服或外配规则生成加工内容
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$processContent = '';
|
2025-02-20 11:00:22 +08:00
|
|
|
|
if($recipe['process_rule_id'] != 0){
|
|
|
|
|
|
$processContent = '煎服,每天'.$recipe['deployment'].'次。'.$recipe['process_rule'];
|
|
|
|
|
|
} elseif ($recipe['package_method_id'] != 0) {
|
|
|
|
|
|
$processContent = '外配,每天'.$recipe['deployment'].'次。'. $recipe['process_rule'];
|
2024-09-19 11:32:39 +08:00
|
|
|
|
}
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 准备同步到ERP的参数
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$params = [
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 生成聊天ID,格式为 'XK_' 加用户ID、上级ID和注册ID
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'cht_id' => 'XK_'.$ProductOrder->user_id.'_'.$ProductOrder->up_id.'_'.$ProductOrder->prescription->register_id,
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 处方序列号
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'ps_seq' => $ProductOrder->prescription->prescription_no,
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 患者姓名
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'pat_name' => $prescriptionContent['patient']['name'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 性别,根据患者性别代码决定
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'sex' => $prescriptionContent['patient']['sex'] == 1 ? '男':'女',
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 年龄,通过身份证号码计算得出
|
2025-04-07 10:59:59 +08:00
|
|
|
|
// 'age' => FuncHelper::getAgeFromIdNo($prescriptionContent['patient']['id_card']),
|
|
|
|
|
|
'age' => $prescriptionContent['patient']['age'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 总剂量
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'tot_posts' => $recipe['dosage'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 单剂量
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'tisane_posts' => $recipe['dosage'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 用法说明
|
2025-02-20 11:00:22 +08:00
|
|
|
|
'usage' => $processContent,//`, 每次'.$recipe['volume'].'ml
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 医生姓名
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'doctor' => $prescriptionContent['doctor']['name'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 科室名称
|
|
|
|
|
|
'department' => $prescriptionContent['doctor']['depart']['name'],
|
|
|
|
|
|
// 剂量重量,即加工内容的条目数
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'post_weight' => count($recipeContent),
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 单剂量金额,总价格除以总剂量并四舍五入到两位小数
|
2025-04-07 10:33:41 +08:00
|
|
|
|
// 'post_amount' => round($recipe['total_price']/$recipe['dosage'],2),
|
|
|
|
|
|
'post_amount' => $recipe['total_price'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 诊断信息
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'diagnose' => $prescriptionContent['clinical_diagnose'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 业务类型代码
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'yw_type' => '1',
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 提交日期,格式为年-月-日
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'submit_date' => date('Y-m-d'),
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 领取编号,由用户ID、随机数和上级ID组合后取前10位
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'get_no' => substr($ProductOrder->user_id.rand(1000,999).$ProductOrder->up_id,0,10)
|
|
|
|
|
|
];
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 根据配送方式添加详细信息
|
2024-09-19 11:32:39 +08:00
|
|
|
|
if($ProductOrder->delivery_method){ //到店取货
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 设置配送方式为默认值'0'
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$params['send_way'] = '0';
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 使用患者移动电话作为联系方式
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$params['telphone'] = $prescriptionContent['patient']['mobile'];
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 配送地址设置为门店位置
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$params['addr'] = $ProductOrder->store->position;
|
|
|
|
|
|
} else { //快递到家
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 设置发货方式为1,此处假设1代表某种特定的发货方式,例如快递
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$params['send_way'] = '1';
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 将订单的联系电话设置为(express_mobile字段的值)
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$params['telphone'] = $ProductOrder->express_mobile;
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 组合订单的地址信息,包括地区和详细地址
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$params['addr'] = $ProductOrder->express_region.$ProductOrder->express_address;
|
|
|
|
|
|
}
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 准备订单详情
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$details = [];
|
|
|
|
|
|
foreach ($recipeContent as $value) {
|
2025-04-01 19:09:18 +08:00
|
|
|
|
$unitName = !empty($value['unit'])? $value['unit']['name'] : 'g';
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$detail = [
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 药品序列号
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'drug_seq' => $value['drug_id'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 历史采购代码,拼接'XK'前缀以区分
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'his_prc_cod' => 'XK'.$value['id'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 药品名称
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'drug_name' => $value['name'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 规格,拼接数量和单位名称
|
2025-04-01 19:09:18 +08:00
|
|
|
|
'spec' => $value['number'].$unitName,
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 投标数量,即药品的数量
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'post_quantity' => $value['number'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 备注,根据是否有订单选择对应的使用方式名称或'无'
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'note' => $value['order']?$value['useWay']['name']:'无',
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 药品价格
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'drug_price' => $value['price'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 批发价格
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'wholesale_price' => $value['buy_price'],
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 单位名称
|
2025-04-01 19:10:00 +08:00
|
|
|
|
'unit' => $unitName,
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 汤剂配方代码
|
2024-09-19 11:32:39 +08:00
|
|
|
|
'tisane_prc_code' => $value['drug_number']
|
|
|
|
|
|
];
|
|
|
|
|
|
$details[] = $detail;
|
|
|
|
|
|
}
|
|
|
|
|
|
$params['details'] = $details;
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 记录日志
|
2025-04-02 10:56:41 +08:00
|
|
|
|
// \Yii::info(__METHOD__."——同步订单至江奥川erp(".$ProductOrder->store->erp_id.")原始数据: ".Json::encode($params));
|
|
|
|
|
|
// ProductOrderLog::saveLog($this->orderId,__METHOD__."——同步订单至江奥川erp(".$ProductOrder->store->erp_id.")原始数据: ".Json::encode($params));
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 调用ERP服务同步订单
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$result = JacErpService::getInstance($ProductOrder->store->erp_id)->syncOrder($params);
|
|
|
|
|
|
if(!$result){
|
|
|
|
|
|
throw new \Exception('订单同步江奥川ERP失败:'.$this->getErrorMsg());
|
|
|
|
|
|
}
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 订单更新为已同步erp
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$ProductOrder->is_sync_erp = 1;
|
|
|
|
|
|
$ProductOrder->save();
|
|
|
|
|
|
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 提交事务
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$t->commit();
|
2025-04-02 10:56:41 +08:00
|
|
|
|
ProductOrderLog::saveLog($this->orderId,'同步成功');
|
2024-09-19 11:32:39 +08:00
|
|
|
|
} catch (\Exception $exception) {
|
2025-04-02 09:18:42 +08:00
|
|
|
|
// 回滚事务
|
2024-09-19 11:32:39 +08:00
|
|
|
|
$t->rollBack();
|
2025-04-02 09:18:42 +08:00
|
|
|
|
|
|
|
|
|
|
// 记录异常日志
|
2024-09-19 11:32:39 +08:00
|
|
|
|
ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getMessage());
|
2025-04-01 18:50:43 +08:00
|
|
|
|
ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getLine());
|
2025-04-01 19:05:53 +08:00
|
|
|
|
ProductOrderLog::saveLog($this->orderId,'订单自动同步江奥川ERP('.$ProductOrder->store->erp_id.')异常:'.$exception->getFile());
|
2024-09-19 11:32:39 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|