ImportOrderFromCAnalysisData.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\command;
  4. use app\admin\model\OrderImportFromC as OIFCModel;
  5. use think\console\Command;
  6. use think\console\Input;
  7. use think\console\input\Argument;
  8. use think\console\input\Option;
  9. use think\console\Output;
  10. use think\facade\Cache;
  11. use think\facade\Db;
  12. use think\facade\Validate;
  13. //C端订单导入-数据解析
  14. class ImportOrderFromCAnalysisData extends Command
  15. {
  16. protected function configure()
  17. {
  18. // 指令配置
  19. $this->setName('analysisData')
  20. ->setDescription('批量解析C端导入的订单数据');
  21. }
  22. //【脚本1】解析用户导入的数据
  23. protected function execute(Input $input, Output $output)
  24. {
  25. //解析信息存到extend表,其中customer_code字段根据订单编号去customer_info查,其他根据商品编号查询
  26. $key = 'import_order_cacalysis_c_key';
  27. $c_data_id = Cache::store('redis')->get($key);
  28. if($c_data_id==1)return '';
  29. Cache::store('redis')->set($key,1,180);
  30. Db::startTrans();
  31. try {
  32. $rs = Db::name('order_import_from_c')
  33. ->where(['is_del' => OIFCModel::$is_del_normal, 'status' => OIFCModel::$status_wait_validate])
  34. // ->lock(true)
  35. ->findOrEmpty();
  36. if (empty($rs)) throw new \Exception('无符合条件的记录');
  37. //1.数据校验
  38. $val = Validate::rule([
  39. 'platform_code|平台订单号' => 'require',
  40. 'platform_id|平台ID' => 'require|number',
  41. 'platform_time|平台下单时间' => 'require|date',
  42. 'sale_source|销售渠道' => 'require',
  43. 'plat_code|平台商品编号' => 'require',
  44. 'price|单价' => 'require|float',
  45. 'num|数量' => 'require|number|gt:0',
  46. 'before_discount_all_price|总金额' => 'require|float',
  47. 'mode|收费模式' => 'require',
  48. 'contactor|收货人' => 'require',
  49. 'mobile|联系电话' => 'require',
  50. 'addr|联系地址' => 'require',
  51. ]);
  52. $update_c_data = [
  53. 'updatetime' => date('Y-m-d H:i:s'),
  54. 'updateid' => 0,
  55. 'updater' => 'system',
  56. ];
  57. if (!$val->check($rs)) {
  58. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  59. $update_c_data['remark'] = '数据错误,' . $val->getError();
  60. }
  61. //平台订单号和平台商品编号的组合不能重复
  62. $temp = Db::name('order_import_from_c')
  63. ->where([
  64. 'is_del' => OIFCModel::$is_del_normal,
  65. 'platform_code' => $rs['platform_code'],
  66. 'plat_code' => $rs['plat_code'],
  67. ])->where('id', '<>', $rs['id'])->find();
  68. if (!empty($temp)) {
  69. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  70. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',平台订单号和平台商品编号重复' : '平台订单号和平台商品编号重复';
  71. }
  72. $extend_insert_data = [
  73. 'order_import_from_c_id' => $rs['id'],
  74. 'createrid' => 0,
  75. 'creater' => 'system',
  76. 'discount_reason' => $rs['activity_name'],
  77. 'platform_code' => $rs['platform_code'],
  78. 'po_code' => $rs['po_code'],
  79. 'platform_time' => $rs['platform_time'],
  80. 'price' => $rs['price'],
  81. 'num' => $rs['num'],
  82. 'before_discount_all_price' => $rs['before_discount_all_price'],
  83. 'order_remark' => $rs['order_remark'],
  84. 'contactor' => $rs['contactor'],
  85. 'mobile' => $rs['mobile'],
  86. 'addr' => $rs['addr'],
  87. 'type' => 1,//1系统解析,2用户确认
  88. 'addtime' => date('Y-m-d H:i:s'),
  89. 'sale_source' => $rs['sale_source'],
  90. 'mode' => $rs['mode'],
  91. ];
  92. //组织extend的数据
  93. $good_platform_info = Db::name('good_platform')
  94. ->field('id,spuCode,platform_code,skuCode,plat_code')
  95. ->where([
  96. 'is_del' => 0,
  97. 'plat_code' => $rs['plat_code'],
  98. 'platform_code' => $rs['platform_id'],
  99. 'exam_status' => 3//exam_status==3 已上线
  100. ])->find();
  101. //商品信息
  102. if (!empty($good_platform_info)) {
  103. $extend_insert_data['skuCode'] = $good_platform_info['skuCode'];
  104. $extend_insert_data['platform_id'] = $good_platform_info['platform_code'];
  105. $extend_insert_data['plat_code'] = $good_platform_info['plat_code'];
  106. $good_info = Db::name('good')
  107. ->field('id,good_name,companyNo,supplierNo')
  108. ->where(['is_del' => 0])
  109. ->where('spuCode', $good_platform_info['spuCode'])
  110. ->find();
  111. if (!empty($good_info)) {
  112. $extend_insert_data['companyNo'] = $good_info['companyNo'];
  113. $extend_insert_data['supplierNo'] = $good_info['supplierNo'];
  114. $extend_insert_data['good_name'] = $good_info['good_name'];
  115. } else {
  116. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  117. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在上线商品表中查不到spuCode对应的商品信息' : '在上线商品表中查不到spuCode对应的商品信息';
  118. }
  119. //商品价格和总价
  120. if ($rs['price'] > 0) {
  121. //活动
  122. if ($rs['activity_name']) {
  123. // $activity_info = Db::name('good_activity')
  124. // ->alias('ga')
  125. // ->field('ga.id,ga.activity_name,ga.activity_code,a.moq_num,a.activity_price')
  126. // ->where(['ga.activity_name' => $rs['activity_name'], 'ga.is_del' => 0])
  127. // ->leftJoin('activity_info a', 'a.activity_code=ga.activity_code AND a.is_del=0')
  128. // ->find();
  129. //同一个活动会有多个商品
  130. $activity_info = Db::name('activity_info')
  131. ->alias('ai')
  132. ->field('ai.id,ga.activity_name,ga.activity_code,ai.moq_num,ai.activity_price')
  133. ->where(['ai.skuCode' => $extend_insert_data['skuCode'], 'ga.activity_name' => $rs['activity_name'], 'ai.is_del' => 0,"ga.status"=>6])
  134. ->leftJoin('good_activity ga', 'ga.activity_code=ai.activity_code AND ga.is_del=0')
  135. ->find();
  136. if (empty($activity_info)) {
  137. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  138. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',该活动不存在' : '该活动不存在';
  139. $extend_insert_data['activity_code'] = '';
  140. $extend_insert_data['activity_name'] = '';
  141. $extend_insert_data['price'] = 0;
  142. $extend_insert_data['before_discount_all_price'] = 0;
  143. } else {
  144. $extend_insert_data['activity_code'] = $activity_info['activity_code'];
  145. $extend_insert_data['activity_name'] = $activity_info['activity_name'];
  146. //是否满足活动的起订量和价格
  147. if ($rs['num'] < $activity_info['moq_num']) {
  148. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  149. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',不满足活动的最小起订量' . $activity_info['moq_num'] : '不满足活动的最小起订量' . $activity_info['moq_num'];
  150. $extend_insert_data['num'] = 0;
  151. }
  152. if ($rs['price'] < $activity_info['activity_price']) {
  153. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  154. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',不满足活动价' . $activity_info['activity_price'] : '不满足活动价' . $activity_info['activity_price'];
  155. $extend_insert_data['price'] = 0;
  156. $extend_insert_data['before_discount_all_price'] = 0;
  157. }
  158. }
  159. } else {
  160. $good_ladder = Db::name("good_ladder")
  161. ->field('id,sale_price')
  162. ->where(["skuCode" => $good_platform_info['skuCode'], "is_del" => 0, "status" => 1])
  163. ->where([["min_num", "<=", $rs['num']]])
  164. ->order("min_num desc")
  165. ->find();
  166. if (empty($good_ladder)) {
  167. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  168. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',找不到商品售价' : '找不到商品售价';
  169. } else {
  170. if ($rs['price'] < $good_ladder['sale_price']) {
  171. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  172. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',售价低于系统标准价' . $good_ladder['sale_price'] : '售价低于系统标准价' . $good_ladder['sale_price'];
  173. $extend_insert_data['price'] = 0;//价格错误
  174. $extend_insert_data['before_discount_all_price'] = 0;//总价也错误
  175. }
  176. }
  177. }
  178. //判断总价是否计算正确
  179. if (bccomp((string)($rs['num'] * $rs['price']), (string)$rs['before_discount_all_price']) != 0) {
  180. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  181. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',导入总价错误' : '导入总价错误';
  182. $extend_insert_data['before_discount_all_price'] = 0;
  183. }
  184. } else {
  185. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  186. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',售价不能为0' : '售价不能为0';
  187. $extend_insert_data['price'] = 0;
  188. $extend_insert_data['before_discount_all_price'] = 0;
  189. }
  190. //销售渠道
  191. $sale_source_temp = Db::name('platform_source')
  192. ->field('id')
  193. ->where(['source' => $rs['sale_source'], 'is_del' => 0, 'platform_id' => $rs['platform_id']])
  194. ->find();
  195. if (empty($sale_source_temp)) {
  196. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  197. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',销售渠道不存在' : '销售渠道不存在';
  198. $extend_insert_data['sale_source'] = '';
  199. }
  200. } else {
  201. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  202. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在商品平台表中查不到平台商品编号在对应平台ID的spuCode' : '在商品平台表中查不到平台商品编号在对应平台ID的spuCode';
  203. $extend_insert_data['plat_code'] = '';
  204. $extend_insert_data['sale_source'] = '';
  205. $extend_insert_data['activity_code'] = '';
  206. $extend_insert_data['activity_name'] = '';
  207. $extend_insert_data['price'] = 0;
  208. $extend_insert_data['before_discount_all_price'] = 0;
  209. $extend_insert_data['platform_id'] = 0;
  210. }
  211. //收费模式
  212. $mode_temp = in_array($rs['mode'], ['卡券支付', '现金支付', '混合支付']);
  213. if ($mode_temp) $extend_insert_data['mode'] = $rs['mode'];
  214. else {
  215. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  216. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',收费模式错误' : '收费模式错误';
  217. $extend_insert_data['mode'] = '';
  218. }
  219. //企业客户
  220. $platfrom = Db::name('good_platform')
  221. ->alias('gp')
  222. ->leftJoin('platform p', 'p.id=gp.platform_code AND p.is_del=0')
  223. ->where('gp.plat_code', $rs['plat_code'])
  224. ->where('gp.platform_code', $rs['platform_id'])
  225. ->where('gp.exam_status', 3)//exam_status==3已上线
  226. ->value("platform_name",'');
  227. $co = Db::connect("mysql_sys")->name('customer_org1') ->where(["name"=>$platfrom,"is_del"=>0])->find();
  228. if ($co) {
  229. $customer_code = Db::connect("mysql_sys")->name('customer_info')
  230. ->where(['itemid' => $co['id'], 'companyName' => $co['name'] . '客户', 'is_del' => 0])
  231. ->value('companyNo', '');
  232. if ($customer_code) $extend_insert_data['customer_code'] = $customer_code;
  233. else {
  234. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  235. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在企业客户管理组织中找不到对应的企业' : '在企业客户管理组织中找不到对应的企业';
  236. }
  237. } else {
  238. $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  239. $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',在企业客户管理组织中找不到平台名称对应的组织' : '在企业客户管理组织中找不到平台名称对应的组织';
  240. }
  241. //省市区
  242. if ($rs['addr']) {
  243. $p_c_a = get_address($rs['addr']);
  244. $province = Db::name('province')
  245. ->field('province_code,name')
  246. ->where('name', $p_c_a['province'])
  247. ->findOrEmpty();
  248. $city = Db::name('city')
  249. ->field('city_code,name')
  250. ->where(['name' => $p_c_a['city'], 'province_code' => $province['province_code'] ?? ''])
  251. ->findOrEmpty();
  252. $area = Db::name('area')
  253. ->field('area_code,name')
  254. ->where(['name' => $p_c_a['district'], 'city_code' => $city['city_code'] ?? ''])
  255. ->findOrEmpty();
  256. $extend_insert_data['addr_code'] = implode([$province['province_code'] ?? '', $city['city_code'] ?? '', $area['area_code'] ?? ''], ',');
  257. //将地址中省市区部分替换掉
  258. if (!empty($province) && $province['name'] !== '') {
  259. $extend_insert_data['addr'] = str_replace($province['name'], '', $extend_insert_data['addr']);
  260. if (!empty($city) && $city['name'] !== '') {
  261. $extend_insert_data['addr'] = str_replace($city['name'], '', $extend_insert_data['addr']);
  262. if (!empty($area) && $area['name'] != '') $extend_insert_data['addr'] = str_replace($area['name'], '', $extend_insert_data['addr']);
  263. }
  264. }
  265. // 地址非必填,能解析就解析,解析不出来就不管了
  266. // if (is_null($province) || is_null($city) || is_null($area)) {
  267. // $update_c_data['status'] = OIFCModel::$status_wait_confirm;
  268. // $update_c_data['remark'] = isset($update_c_data['remark']) ? $update_c_data['remark'] . ',地址解析失败' : '地址解析失败';
  269. // } else $extend_insert_data['addr_code'] = implode([$province, $city, $area], ',');
  270. }
  271. Db::name('order_import_from_c_extend')->insertGetId($extend_insert_data);
  272. $is = false;
  273. if (!isset($update_c_data['status'])) {
  274. $is = true;
  275. $update_c_data['status'] = OIFCModel::$status_wait_relation;
  276. }
  277. $res = Db::name('order_import_from_c')
  278. ->where([
  279. 'id' => $rs['id'],
  280. 'is_del' => OIFCModel::$is_del_normal,
  281. 'status' => OIFCModel::$status_wait_validate
  282. ])->update($update_c_data);
  283. if($res==false)throw new \Exception($rs['id'].'处理失败');
  284. Db::commit();
  285. // if ($is && $res) Cache::store("redis")->handler()->lPush('import_order_from_c_key', $rs['id']);//校验无误,入队列
  286. // 指令输出
  287. // $output->writeln(date('Y-m-d H:i:s') . '|解析成功');
  288. } catch (\Exception $exception) {
  289. Db::rollback();
  290. // 指令输出
  291. $output->writeln(date('Y-m-d H:i:s') . '|解析失败,' . $exception->getMessage() . '|' . $exception->getFile() . '|' . $exception->getLine());
  292. }
  293. Cache::store('redis')->set($key,0,180);
  294. }
  295. }