ImportOrderFromCAnalysisData.php 17 KB

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