123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- <?php
- declare (strict_types=1);
- namespace app\command;
- use app\admin\model\OrderImportFromC as OIFCModel;
- use think\console\Command;
- use think\console\Input;
- use think\console\input\Argument;
- use think\console\input\Option;
- use think\console\Output;
- use think\facade\Cache;
- use think\facade\Db;
- use think\facade\Validate;
- //C端订单导入-数据解析
- class ImportOrderFromCAnalysisData extends Command
- {
- protected function configure()
- {
- // 指令配置
- $this->setName('analysisData')
- ->setDescription('批量解析C端导入的订单数据');
- }
- //【脚本1】解析用户导入的数据
- protected function execute(Input $input, Output $output)
- {
- //解析信息存到extend表,其中customer_code字段根据订单编号去customer_info查,其他根据商品编号查询
- $key = 'import_order_cacalysis_c_key';
- $c_data_id = Cache::store('redis')->get($key);
- if($c_data_id==1)return '';
- Cache::store('redis')->set($key,1,180);
- Db::startTrans();
- try {
- $rs = Db::name('order_import_from_c')
- ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_validate])
- // ->lock(true)
- ->findOrEmpty();
- if (empty($rs)) throw new \Exception('无符合条件的记录');
- //1.数据校验
- $val = Validate::rule([
- 'platform_code|平台订单号' => 'require',
- 'platform_id|平台ID' => 'require|number',
- 'platform_time|平台下单时间' => 'require|date',
- 'sale_source|销售渠道' => 'require',
- 'plat_code|平台商品编号' => 'require',
- 'price|单价' => 'require|float',
- 'num|数量' => 'require|number|gt:0',
- 'before_discount_all_price|总金额' => 'require|float',
- 'mode|收费模式' => 'require',
- 'contactor|收货人' => 'require',
- 'mobile|联系电话' => 'require',
- 'addr|联系地址' => 'require',
- ]);
- $update_c_data = [
- 'updatetime' => date('Y-m-d H:i:s'),
- 'updateid' => 0,
- 'updater' => 'system',
- ];
- if (!$val->check($rs)) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = '数据错误,' . $val->getError();
- }
- //平台订单号和平台商品编号的组合不能重复
- $temp = Db::name('order_import_from_c')
- ->where([
- 'is_del' => OIFCModel::$is_del_normal,
- 'platform_code' => $rs['platform_code'],
- 'plat_code' => $rs['plat_code'],
- ])->where('id', '<>', $rs['id'])->find();
- if (!empty($temp)) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',平台订单号和平台商品编号重复' : '平台订单号和平台商品编号重复';
- }
- $extend_insert_data = [
- 'order_import_from_c_id' => $rs['id'],
- 'createrid' => 0,
- 'creater' => 'system',
- 'discount_reason' => $rs['activity_name'],
- 'platform_code' => $rs['platform_code'],
- 'po_code' => $rs['po_code'],
- 'platform_time' => $rs['platform_time'],
- 'price' => $rs['price'],
- 'num' => $rs['num'],
- 'before_discount_all_price' => $rs['before_discount_all_price'],
- 'order_remark' => $rs['order_remark'],
- 'contactor' => $rs['contactor'],
- 'mobile' => $rs['mobile'],
- 'addr' => $rs['addr'],
- 'type' => 1,//1系统解析,2用户确认
- 'addtime' => date('Y-m-d H:i:s'),
- 'sale_source' => $rs['sale_source'],
- 'mode' => $rs['mode'],
- ];
- //组织extend的数据
- $good_platform_info = Db::name('good_platform')
- ->field('id,spuCode,platform_code,skuCode,plat_code')
- ->where([
- 'is_del' => 0,
- 'plat_code' => $rs['plat_code'],
- 'platform_code' => $rs['platform_id'],
- 'exam_status' => 3//exam_status==3 已上线
- ])->find();
- //商品信息
- if (!empty($good_platform_info)) {
- $extend_insert_data['skuCode'] = $good_platform_info['skuCode'];
- $extend_insert_data['platform_id'] = $good_platform_info['platform_code'];
- $extend_insert_data['plat_code'] = $good_platform_info['plat_code'];
- $good_info = Db::name('good')
- ->field('id,good_name,companyNo,supplierNo')
- ->where(['is_del' => 0])
- ->where('spuCode', $good_platform_info['spuCode'])
- ->find();
- if (!empty($good_info)) {
- $extend_insert_data['companyNo'] = $good_info['companyNo'];
- $extend_insert_data['supplierNo'] = $good_info['supplierNo'];
- $extend_insert_data['good_name'] = $good_info['good_name'];
- } else {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在上线商品表中查不到spuCode对应的商品信息' : '在上线商品表中查不到spuCode对应的商品信息';
- }
- //商品价格和总价
- if ($rs['price'] > 0) {
- //活动
- if ($rs['activity_name']) {
- // $activity_info = Db::name('good_activity')
- // ->alias('ga')
- // ->field('ga.id,ga.activity_name,ga.activity_code,a.moq_num,a.activity_price')
- // ->where(['ga.activity_name' => $rs['activity_name'], 'ga.is_del' => 0])
- // ->leftJoin('activity_info a', 'a.activity_code=ga.activity_code AND a.is_del=0')
- // ->find();
- //同一个活动会有多个商品
- $activity_info = Db::name('activity_info')
- ->alias('ai')
- ->field('ai.id,ga.activity_name,ga.activity_code,ai.moq_num,ai.activity_price')
- ->where(['ai.skuCode' => $extend_insert_data['skuCode'], 'ga.activity_name' => $rs['activity_name'], 'ai.is_del' => 0,"ga.status"=>6])
- ->leftJoin('good_activity ga', 'ga.activity_code=ai.activity_code AND ga.is_del=0')
- ->find();
- if (empty($activity_info)) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',该活动不存在' : '该活动不存在';
- $extend_insert_data['activity_code'] = '';
- $extend_insert_data['activity_name'] = '';
- $extend_insert_data['price'] = 0;
- $extend_insert_data['before_discount_all_price'] = 0;
- } else {
- $extend_insert_data['activity_code'] = $activity_info['activity_code'];
- $extend_insert_data['activity_name'] = $activity_info['activity_name'];
- //是否满足活动的起订量和价格
- if ($rs['num'] < $activity_info['moq_num']) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',不满足活动的最小起订量' . $activity_info['moq_num'] : '不满足活动的最小起订量' . $activity_info['moq_num'];
- $extend_insert_data['num'] = 0;
- }
- if ($rs['price'] < $activity_info['activity_price']) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',不满足活动价' . $activity_info['activity_price'] : '不满足活动价' . $activity_info['activity_price'];
- $extend_insert_data['price'] = 0;
- $extend_insert_data['before_discount_all_price'] = 0;
- }
- }
- } else {
- $good_ladder = Db::name("good_ladder")
- ->field('id,sale_price')
- ->where(["skuCode" => $good_platform_info['skuCode'], "is_del" => 0, "status" => 1])
- ->where([["min_num", "<=", $rs['num']]])
- ->order("min_num desc")
- ->find();
- if (empty($good_ladder)) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',找不到商品售价' : '找不到商品售价';
- } else {
- if ($rs['price'] < $good_ladder['sale_price']) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',售价低于系统标准价' . $good_ladder['sale_price'] : '售价低于系统标准价' . $good_ladder['sale_price'];
- $extend_insert_data['price'] = 0;//价格错误
- $extend_insert_data['before_discount_all_price'] = 0;//总价也错误
- }
- }
- }
- //判断总价是否计算正确
- if (bccomp((string)($rs['num'] * $rs['price']), (string)$rs['before_discount_all_price']) != 0) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',导入总价错误' : '导入总价错误';
- $extend_insert_data['before_discount_all_price'] = 0;
- }
- } else {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',售价不能为0' : '售价不能为0';
- $extend_insert_data['price'] = 0;
- $extend_insert_data['before_discount_all_price'] = 0;
- }
- //销售渠道
- $sale_source_temp = Db::name('platform_source')
- ->field('id')
- ->where(['source' => $rs['sale_source'], 'is_del' => 0, 'platform_id' => $rs['platform_id']])
- ->find();
- if (empty($sale_source_temp)) {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',销售渠道不存在' : '销售渠道不存在';
- $extend_insert_data['sale_source'] = '';
- }
- } else {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在商品平台表中查不到平台商品编号在对应平台ID的spuCode' : '在商品平台表中查不到平台商品编号在对应平台ID的spuCode';
- $extend_insert_data['plat_code'] = '';
- $extend_insert_data['sale_source'] = '';
- $extend_insert_data['activity_code'] = '';
- $extend_insert_data['activity_name'] = '';
- $extend_insert_data['price'] = 0;
- $extend_insert_data['before_discount_all_price'] = 0;
- $extend_insert_data['platform_id'] = 0;
- }
- //收费模式
- $mode_temp = in_array($rs['mode'], ['卡券支付', '现金支付', '混合支付']);
- if ($mode_temp) $extend_insert_data['mode'] = $rs['mode'];
- else {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',收费模式错误' : '收费模式错误';
- $extend_insert_data['mode'] = '';
- }
- //企业客户
- $platfrom = Db::name('good_platform')
- ->alias('gp')
- ->leftJoin('platform p', 'p.id=gp.platform_code AND p.is_del=0')
- ->where('gp.plat_code', $rs['plat_code'])
- ->where('gp.platform_code', $rs['platform_id'])
- ->where('gp.exam_status', 3)//exam_status==3已上线
- ->value("platform_name",'');
- $co = Db::connect("mysql_sys")->name('customer_org1') ->where(["name"=>$platfrom,"is_del"=>0])->find();
- if ($co) {
- $customer_code = Db::connect("mysql_sys")->name('customer_info')
- ->where(['itemid' => $co['id'], 'companyName' => $co['name'] . '客户', 'is_del' => 0])
- ->value('companyNo', '');
- if ($customer_code) $extend_insert_data['customer_code'] = $customer_code;
- else {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在企业客户管理组织中找不到对应的企业' : '在企业客户管理组织中找不到对应的企业';
- }
- } else {
- $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在企业客户管理组织中找不到平台名称对应的组织' : '在企业客户管理组织中找不到平台名称对应的组织';
- }
- //省市区
- if ($rs['addr']) {
- $p_c_a = get_address($rs['addr']);
- $province = Db::name('province')
- ->field('province_code,name')
- ->where('name', $p_c_a['province'])
- ->findOrEmpty();
- $city = Db::name('city')
- ->field('city_code,name')
- ->where(['name' => $p_c_a['city'], 'province_code' => $province['province_code'] ?? ''])
- ->findOrEmpty();
- $area = Db::name('area')
- ->field('area_code,name')
- ->where(['name' => $p_c_a['district'], 'city_code' => $city['city_code'] ?? ''])
- ->findOrEmpty();
- $extend_insert_data['addr_code'] = implode([$province['province_code'] ?? '', $city['city_code'] ?? '', $area['area_code'] ?? ''], ',');
- //将地址中省市区部分替换掉
- if (!empty($province) && $province['name'] !== '') {
- $extend_insert_data['addr'] = str_replace($province['name'], '', $extend_insert_data['addr']);
- if (!empty($city) && $city['name'] !== '') {
- $extend_insert_data['addr'] = str_replace($city['name'], '', $extend_insert_data['addr']);
- if (!empty($area) && $area['name'] != '') $extend_insert_data['addr'] = str_replace($area['name'], '', $extend_insert_data['addr']);
- }
- }
- // 地址非必填,能解析就解析,解析不出来就不管了
- // if (is_null($province) || is_null($city) || is_null($area)) {
- // $update_c_data['status'] = OIFCModel::$status_wait_confirm;
- // $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',地址解析失败' : '地址解析失败';
- // } else $extend_insert_data['addr_code'] = implode([$province, $city, $area], ',');
- }
- Db::name('order_import_from_c_extend')->insertGetId($extend_insert_data);
- $is = false;
- if (!isset($update_c_data['status'])) {
- $is = true;
- $update_c_data['status'] = OIFCModel::$status_wait_relation;
- }
- $res = Db::name('order_import_from_c')
- ->where([
- 'id' => $rs['id'],
- 'is_del' => OIFCModel::$is_del_normal,
- 'status' => OIFCModel::$status_wait_validate
- ])->update($update_c_data);
- if($res==false)throw new \Exception($rs['id'].'处理失败');
- Db::commit();
- // if ($is && $res) Cache::store("redis")->handler()->lPush('import_order_from_c_key', $rs['id']);//校验无误,入队列
- // 指令输出
- // $output->writeln(date('Y-m-d H:i:s') . '|解析成功');
- } catch (\Exception $exception) {
- Db::rollback();
- // 指令输出
- $output->writeln(date('Y-m-d H:i:s') . '|解析失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
- }
- Cache::store('redis')->set($key,0,180);
- }
- }
|