MSql.Class.php 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360
  1. <?php
  2. /**
  3. * 公共Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/11/18
  7. * Time: 09:22
  8. */
  9. namespace JinDouYun\Model\Common;
  10. use JinDouYun\Cache\Stock;
  11. use JinDouYun\Dao\Customer\DCustomer;
  12. use JinDouYun\Dao\Finance\DAccount;
  13. use JinDouYun\Dao\Finance\DReceiptRequisition;
  14. use JinDouYun\Dao\Goods\DGoods;
  15. use JinDouYun\Dao\GoodsManage\DGoodsBasic;
  16. use JinDouYun\Dao\GoodsManage\DSku;
  17. use JinDouYun\Dao\Purchase\DSupplier;
  18. use JinDouYun\Dao\Stock\DLockingShop;
  19. use JinDouYun\Dao\System\DReceiptTemplate;
  20. use JinDouYun\Model\Customer\MCustomer;
  21. use JinDouYun\Model\Department\MRoleAcl;
  22. use JinDouYun\Model\Finance\MPaid;
  23. use JinDouYun\Model\Finance\MReceived;
  24. use Mall\Framework\Core\ErrorCode;
  25. use Mall\Framework\Core\StatusCode;
  26. use Mall\Framework\Core\ResultWrapper;
  27. use JinDouYun\Dao\Stock\DInventory;
  28. use JinDouYun\Dao\Stock\DInventoryBatch;
  29. use JinDouYun\Model\MBaseModel;
  30. use Util\Common\ChineseCharacter;
  31. class MSql extends MBaseModel
  32. {
  33. private $objDInventory;
  34. public function __construct()
  35. {
  36. parent::__construct();
  37. $this->objDInventory = new DInventory();
  38. }
  39. public function batchRestoreSql()
  40. {
  41. $objDSku = new DSku();
  42. $objDSku->setTable('qianniao_sku_105');
  43. $sql = 'select * from `www.qianniao.vip`.`qianniao_sku_105` where deleteStatus = 5';
  44. $dbResult = $this->objDInventory->query($sql);
  45. $data = [];
  46. foreach($dbResult as $value){
  47. $data[$value['goodsId']][] = $value;
  48. }
  49. // V($data);
  50. foreach($data as $value){
  51. if($value[0]['isNew'] == 5){
  52. continue;
  53. }
  54. if(count($value) == 1){
  55. $value = $value[0];
  56. if($value['unitId'] == 1){
  57. $update = [
  58. 'isNew' => 5,
  59. 'unitId' => 3,
  60. 'unitName' => '袋'
  61. ];
  62. }else{
  63. $update = [
  64. 'isNew' => 5,
  65. ];
  66. }
  67. $objDSku->update($update,['id' => $value['id']]);
  68. }else{
  69. foreach($value as $key => $v){
  70. $array = $key == 0 ? $value[1] : $value[0];
  71. if($v['isMaster'] == 5){
  72. $update = [
  73. 'isMaster' => 4,
  74. 'conversion' => $array['conversion'],
  75. 'isNew' => 5,
  76. ];
  77. }else{
  78. $update = [
  79. 'isMaster' => 5,
  80. 'conversion' => 0,
  81. 'isNew' => 5,
  82. ];
  83. }
  84. $objDSku->update($update,['id' => $v['id']]);
  85. }
  86. }
  87. }
  88. exit;
  89. $whereSql = ' and skuId = 94';
  90. $whereSql = '';
  91. //查询所有库存数据
  92. $sql = 'select * from qianniao_inventory_details_56_1_203 where actionType = 5'.$whereSql;
  93. $dbResult = $this->objDInventory->query($sql);
  94. if($dbResult === false){
  95. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  96. }
  97. $inData = $dbResult;
  98. unset($dbResult);
  99. $sql = 'select * from qianniao_inventory_details_56_1_203 where actionType = 4'.$whereSql;
  100. $dbResult = $this->objDInventory->query($sql);
  101. if($dbResult === false){
  102. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  103. }
  104. $outData = $dbResult;
  105. unset($dbResult);
  106. $outFormat = [];
  107. foreach($outData as $value){
  108. $batch = json_decode($value['batch'], true)[0];
  109. $outFormat[$value['skuId']][$batch['batch']] = $value;
  110. }
  111. $addBatchData = [];
  112. //生成批次数据
  113. foreach($inData as $value){
  114. $batch = json_decode($value['batch'], true)[0];
  115. $sql = 'select inventoryNum from qianniao_inventory_56 where skuId = '.$value['skuId'];
  116. $dbResult = $this->objDInventory->query($sql);
  117. if($dbResult === false){
  118. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  119. }
  120. $inventoryData = $dbResult[0];
  121. unset($dbResult);
  122. if(isset($outFormat[$value['skuId']])){
  123. if((int)$outFormat[$value['skuId']][$batch['batch']]['inventoryChangeNum'] == (int)$inventoryData['inventoryNum']){
  124. $addBatchData[] = [
  125. 'originId' => $value['originId'],
  126. 'originNo' => $value['originNo'],
  127. 'warehouseId' => $value['warehouseId'],
  128. 'materielId' => $value['materielId'],
  129. 'materielCode' => $value['materielCode'],
  130. 'sourceNo' => $value['sourceNo'],
  131. 'skuId' => $value['skuId'],
  132. 'batchNo' => $batch['batch'],
  133. 'num' => $outFormat[$value['skuId']][$batch['batch']]['inventoryChangeNum'],
  134. 'averageCost' => $value['averageCost'],
  135. 'batchCost' => $batch['batchCost'],
  136. // 'productionData' => "",//生产日期
  137. // 'shelfLife' => "",//保质期
  138. 'batchStatus' => StatusCode::$standard,
  139. 'createTime' => $value['createTime'],
  140. 'updateTime' => $value['updateTime'],
  141. ];
  142. }
  143. }else{
  144. $addBatchData[] = [
  145. 'originId' => $value['originId'],
  146. 'originNo' => $value['originNo'],
  147. 'warehouseId' => $value['warehouseId'],
  148. 'materielId' => $value['materielId'],
  149. 'materielCode' => $value['materielCode'],
  150. 'sourceNo' => $value['sourceNo'],
  151. 'skuId' => $value['skuId'],
  152. 'batchNo' => $batch['batch'],
  153. 'num' => $value['inventoryChangeNum'],
  154. 'averageCost' => $value['averageCost'],
  155. 'batchCost' => $batch['batchCost'],
  156. // 'productionData' => "",//生产日期
  157. // 'shelfLife' => "",//保质期
  158. 'batchStatus' => StatusCode::$standard,
  159. 'createTime' => $value['createTime'],
  160. 'updateTime' => $value['updateTime'],
  161. ];
  162. }
  163. }
  164. $objDInventoryBatch = new DInventoryBatch();
  165. $objDInventoryBatch->setTable('qianniao_inventory_batch_56_1');
  166. $dbResult = $objDInventoryBatch->insert($addBatchData, true);
  167. if($dbResult === false){
  168. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  169. }
  170. return ResultWrapper::success($dbResult);
  171. }
  172. /**
  173. * sql执行
  174. * 该方法只执行需要拼接企业id的表 不要拼接企业id的表请自行手动执行
  175. * @param string $sqlParent
  176. */
  177. public function batchSql($sqlParent = '')
  178. {
  179. var_dump($sqlParent);
  180. //执行语句
  181. $sqlIndex = substr($sqlParent, stripos($sqlParent, "` ") + 2);
  182. //执行语句表所属数据库
  183. $dbName = substr($sqlParent, stripos($sqlParent, ' `') + 2, stripos($sqlParent, '`.' ) - stripos($sqlParent, ' `') - 2);
  184. //企业表所属数据库
  185. $dbIndexName = 'www.qianniao.vip';
  186. //库存数据库名
  187. $stockDbName = 'stock.qianniao.vip';
  188. //修改config
  189. //表名
  190. $sqlTable = substr($sqlParent, stripos($sqlParent, '.`') + 2, stripos($sqlParent, '` ' ) - stripos($sqlParent, '.`') - 2);
  191. $strNumIndex = 0;
  192. for($a = 1; $a <= 9; $a++){
  193. $strNum = stripos($sqlTable, (string)$a);
  194. if($strNum){
  195. if(!$strNumIndex || $strNum < $strNumIndex){
  196. $strNumIndex = $strNum;
  197. }
  198. }
  199. }
  200. if(!$strNumIndex){
  201. return ResultWrapper::fail('表名错误', ErrorCode::$dberror);
  202. }
  203. $tableNameIndex = substr($sqlTable, 0, $strNumIndex);
  204. //是否执行企业1表
  205. $oneEnterpriseTable = true;
  206. //从某个企业执行
  207. $startEnterpriseId = 0;
  208. //按照用户人数分表 例:tableName_1_1
  209. $userByTables = [
  210. 'qianniao_order_',
  211. 'qianniao_order_coupon_',
  212. 'qianniao_order_goods_',
  213. 'qianniao_order_receive_',
  214. 'qianniao_goods_collect_',
  215. 'qianniao_supplier_order_details_',
  216. 'qianniao_account_detail_',
  217. 'qianniao_customer_balance_',
  218. 'qianniao_supplier_balance_',
  219. ];
  220. //按照仓库id分表
  221. $warehouseTable = [
  222. 'qianniao_inventory_batch_'
  223. ];
  224. //按照季度分表 例:tableName_1_2020_3
  225. $dateByTable = [
  226. 'qianniao_commission_order_',
  227. 'qianniao_received_account_',
  228. 'qianniao_paid_',
  229. 'qianniao_paid_account_',
  230. 'qianniao_customerPriceAdjustmentSheet_',
  231. 'qianniao_customer_balance_detail_',
  232. 'qianniao_pay_receipt_',
  233. 'qianniao_receive_receipt_',
  234. 'qianniao_received_',
  235. 'qianniao_supplier_balance_detail_',
  236. 'qianniao_customerTypePriceAdjustmentSheet_',
  237. 'qianniao_priceAdjustmentSheet_',
  238. 'qianniao_refund_',
  239. ];
  240. //按照季度分表2 例:tableName_1_201
  241. $dateByTableTwo = [
  242. 'qianniao_inventory_locking_',
  243. ];
  244. //按照仓库id和季度分表 例:tableName_1_1_194
  245. $warehouseAndDateByTable = [
  246. 'qianniao_inventory_details_'
  247. ];
  248. $runTrue = 0;
  249. $runFalse = 0;
  250. $runTrueTableName = [];
  251. $runFalseTableName = [];
  252. $tableNum = 0;
  253. $existNum = 0;
  254. $existTable = [];
  255. if (empty($sqlIndex)) {
  256. return ResultWrapper::fail('请输入sql语句', ErrorCode::$dberror);
  257. }
  258. if (empty($dbIndexName)) {
  259. return ResultWrapper::fail('请输入数据库名', ErrorCode::$dberror);
  260. }
  261. if (empty($tableNameIndex)) {
  262. return ResultWrapper::fail('请输入表名', ErrorCode::$dberror);
  263. }
  264. //查询所有企业
  265. $whereSql = '';
  266. if ($startEnterpriseId) {
  267. $whereSql = ' where id >= ' . $startEnterpriseId;
  268. }
  269. $sql = 'select id from `' . $dbIndexName . '`.`qianniao_enterprise_1`' . $whereSql;
  270. $objDGoods = new DGoods();
  271. $dbResult = $objDGoods->query($sql);
  272. if ($dbResult === false) {
  273. return ResultWrapper::fail('查询企业时sql报错:'.$objDGoods->error(),ErrorCode::$dberror);
  274. }
  275. $enterpriseResult = $dbResult;
  276. unset($dbResult);
  277. unset($sql);
  278. if (empty($enterpriseResult)) {
  279. return ResultWrapper::fail('企业数据为空',ErrorCode::$dberror);
  280. }
  281. //按人数分表 因为用户数据量小 所以统一后缀加_1
  282. $tableSuffix = [];
  283. foreach ($userByTables as $value) {
  284. if ($tableNameIndex == $value) {
  285. $tableSuffix[] = '_1';
  286. break;
  287. }
  288. }
  289. $start = 2020;
  290. $end = (int)date('Y', time());
  291. //按日期季度分表
  292. foreach ($dateByTable as $value) {
  293. if ($tableNameIndex == $value) {
  294. for ($ii = $start; $ii <= $end; $ii++) {
  295. for ($i = 1; $i <= 4; $i++) {
  296. $tableSuffix[] = '_' . $ii . '_' . $i;
  297. }
  298. }
  299. break;
  300. }
  301. }
  302. //按照季度分表2
  303. foreach ($dateByTableTwo as $value) {
  304. if ($tableNameIndex == $value) {
  305. for ($ii = $start; $ii <= $end; $ii++) {
  306. for ($i = 1; $i <= 4; $i++) {
  307. $tableSuffix[] = '_' . substr($ii, -2) . $i;
  308. }
  309. }
  310. break;
  311. }
  312. }
  313. //按日期季度分表 拼接仓库id
  314. $isWarehouseId = false;
  315. foreach ($warehouseAndDateByTable as $value) {
  316. if ($tableNameIndex == $value) {
  317. $isWarehouseId = true;
  318. for ($ii = $start; $ii <= $end; $ii++) {
  319. for ($i = 1; $i <= 4; $i++) {
  320. $tableSuffix[] = '_' . substr($ii, -2) . $i;
  321. }
  322. }
  323. break;
  324. }
  325. }
  326. //按照仓库id分表
  327. foreach ($warehouseTable as $value) {
  328. if ($tableNameIndex == $value) {
  329. $isWarehouseId = true;
  330. break;
  331. }
  332. }
  333. if ($isWarehouseId && empty($stockDbName)) {
  334. return ResultWrapper::fail($tableNameIndex . '按照仓库分表, 请输入仓库表对应数据库名称', ErrorCode::$dberror);
  335. }
  336. $enterpriseFormat = array_column($enterpriseResult, null, 'id');
  337. if (!isset($enterpriseFormat[1])) {
  338. $enterprise1 = [
  339. 'id' => 1,
  340. ];
  341. array_unshift($enterpriseResult, $enterprise1);
  342. }
  343. $enterpriseData = [];
  344. $emptyTable = [];
  345. foreach ($enterpriseResult as $value) {
  346. $tableList = [];
  347. //拿出企业id
  348. $enterpriseId = $value['id'];
  349. //判断是否执行1企业
  350. if ($enterpriseId == 1) {
  351. if($oneEnterpriseTable == false){
  352. continue;
  353. }else{
  354. $tableList[] = $tableNameIndex . $enterpriseId;
  355. }
  356. }
  357. if ($isWarehouseId) {
  358. //查询仓库的商铺
  359. $sql = 'select id from `' . $stockDbName . '`.`qianniao_warehouse_' . $enterpriseId . '`';
  360. $dbResult = $objDGoods->query($sql);
  361. if ($dbResult === false) {
  362. return ResultWrapper::fail('查询仓库数据报错:'.$objDGoods->error(), ErrorCode::$dberror);
  363. }
  364. $warehouseResult = $dbResult;
  365. unset($dbResult);
  366. //循环仓库数据
  367. foreach ($warehouseResult as $warehouse) {
  368. $warehouseId = $warehouse['id'];
  369. //判断是否有日期后缀
  370. if (!empty($tableSuffix)) {
  371. foreach ($tableSuffix as $suffix) {
  372. //拼接表名
  373. $tableList[] = $tableNameIndex . $enterpriseId . '_' . $warehouseId . $suffix;
  374. }
  375. } else {
  376. $tableList[] = $tableNameIndex . $enterpriseId . '_' . $warehouseId;
  377. }
  378. }
  379. } else {
  380. //判断是否有日期后缀
  381. if (!empty($tableSuffix)) {
  382. foreach ($tableSuffix as $suffix) {
  383. //拼接表名
  384. $tableList[] = $tableNameIndex . $enterpriseId . $suffix;
  385. }
  386. } else {
  387. $tableList[] = $tableNameIndex . $enterpriseId;
  388. }
  389. }
  390. $tableList = array_unique($tableList);
  391. foreach ($tableList as $tableName) {
  392. $tableNum++;
  393. //查询表是否存在
  394. $sql = 'select count(TABLE_NAME) as isTable from information_schema.TABLES where TABLE_SCHEMA = "' . $dbName . '" and TABLE_NAME = "' . $tableName . '"';
  395. $dbResult = $objDGoods->query($sql);
  396. if ($dbResult === false) {
  397. return ResultWrapper::fail('查询表是否存在报错:'.$objDGoods->error(), ErrorCode::$dberror);
  398. }
  399. $isTable = 0;
  400. if (!empty($dbResult)) {
  401. if(isset($dbResult[0]['isTable']) && $dbResult[0]['isTable'] == 1){
  402. $isTable = 1;
  403. }
  404. }
  405. unset($dbResult);
  406. //判断表是否存在
  407. if ($isTable) {
  408. $existNum++;
  409. $existTable[] = $tableName;
  410. $enterpriseData[$enterpriseId][] = ['enterpriseId' => $enterpriseId, 'tableName' => $tableName];
  411. }else{
  412. $emptyTable[] = $tableName;
  413. }
  414. }
  415. }
  416. // self::prompt('', 0, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName);
  417. //执行语句
  418. foreach ($enterpriseData as $enterpriseId => $value) {
  419. foreach ($value as $enterprise) {
  420. $enterpriseId = $enterprise['enterpriseId'];
  421. $tableName = $enterprise['tableName'];
  422. //拼接修改sql语句
  423. $sql = "ALTER TABLE `" . $dbName . "`.`" . $tableName . "` " . $sqlIndex;
  424. // echo $sql.PHP_EOL;
  425. $dbResult = $objDGoods->query($sql);
  426. $true = true;
  427. if ($dbResult === false) {
  428. echo 'sql执行错误: '.$objDGoods->error().PHP_EOL;
  429. exit;
  430. $true = $dbResult;
  431. $runFalse++;
  432. $runFalseTableName[] = $tableName;
  433. self::prompt($objDGoods->error(), $enterpriseId, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName);
  434. }
  435. $runTrue++;
  436. $runTrueTableName[] = $tableName;
  437. var_dump('企业'.$enterpriseId.'成功执行一条'.$tableName.'表语句-'.$true);
  438. unset($dbResult);
  439. }
  440. }
  441. self::prompt('', 0, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName);
  442. }
  443. public function prompt($info = '', $enterpriseId = 0, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName, $surplusSql = '')
  444. {
  445. $title = '';
  446. if (!empty($info)) {
  447. $title = '执行到企业' . $enterpriseId . '时有报错:
  448. ' . $info . '
  449. ';
  450. }
  451. $title .= '一共' . count($enterpriseResult) . '个企业, 需要修改' . $tableNum . '个表, 存在的表有' . $existNum . '个, 修改成功的表' . $runTrue . '个, 成功的有:' . implode(',', $runTrueTableName);
  452. return ResultWrapper::success($title);
  453. }
  454. /**
  455. * 批量执行格式化客户名称首字母
  456. */
  457. public function initCustomerCondition()
  458. {
  459. $dbIndexName = 'www.qianniao.vip';
  460. $sql = 'select id from `' . $dbIndexName . '`.`qianniao_enterprise_1`';
  461. $dbResult = $this->objDInventory->query($sql);
  462. if($dbResult === false){
  463. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  464. }
  465. $enterpriseData = $dbResult;
  466. unset($dbResult);
  467. $objChineseCharacter = new ChineseCharacter();
  468. foreach($enterpriseData as $value){
  469. $sql = 'select * from `'.$dbIndexName.'`.qianniao_customer_'.$value['id'];
  470. $dbResult = $this->objDInventory->query($sql);
  471. if($dbResult === false){
  472. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  473. }
  474. foreach($dbResult as $v){
  475. $condition = $objChineseCharacter->getInitials(trim($v['name']));
  476. $sql = "update `".$dbIndexName."`.qianniao_customer_".$value['id']." set `condition` = '".$condition."' where id = ".$v['id'];
  477. $result = $this->objDInventory->query($sql);
  478. if($result === false){
  479. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  480. }
  481. }
  482. }
  483. return ResultWrapper::success('执行成功');
  484. }
  485. public function initCache()
  486. {
  487. $dbIndexName = 'www.qianniao.vip';
  488. $sql = 'select id from `' . $dbIndexName . '`.`qianniao_enterprise_1` where deleteStatus = 5';
  489. $dbResult = $this->objDInventory->query($sql);
  490. if($dbResult === false){
  491. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  492. }
  493. $enterpriseData = $dbResult;
  494. unset($dbResult);
  495. foreach($enterpriseData as $value){
  496. $objMRoleAcl = new MRoleAcl($value['id']);
  497. $modelResult = $objMRoleAcl->initCache();
  498. if(!$modelResult->isSuccess()){
  499. var_dump($modelResult->getData());
  500. }
  501. unset($objMRoleAcl);
  502. }
  503. return ResultWrapper::success('执行成功');
  504. }
  505. public function formatTableName($tableName, $dbName, $enterpriseId)
  506. {
  507. //按照用户人数分表 例:tableName_1_1
  508. $userByTables = [
  509. 'qianniao_order_',
  510. 'qianniao_order_coupon_',
  511. 'qianniao_order_goods_',
  512. 'qianniao_order_receive_',
  513. 'qianniao_goods_collect_',
  514. 'qianniao_supplier_order_details_',
  515. 'qianniao_account_detail_',
  516. 'qianniao_customer_balance_',
  517. 'qianniao_supplier_balance_',
  518. ];
  519. //按照仓库id分表
  520. $warehouseTable = [
  521. 'qianniao_inventory_batch_'
  522. ];
  523. //按照季度分表 例:tableName_1_2020_3
  524. $dateByTable = [
  525. 'qianniao_commission_order_',
  526. 'qianniao_received_account_',
  527. 'qianniao_paid_',
  528. 'qianniao_paid_account_',
  529. 'qianniao_customerPriceAdjustmentSheet_',
  530. 'qianniao_customer_balance_detail_',
  531. 'qianniao_pay_receipt_',
  532. 'qianniao_receive_receipt_',
  533. 'qianniao_received_',
  534. 'qianniao_supplier_balance_detail_',
  535. 'qianniao_customerTypePriceAdjustmentSheet_',
  536. 'qianniao_priceAdjustmentSheet_',
  537. ];
  538. //按照季度分表2 例:tableName_1_201
  539. $dateByTableTwo = [
  540. 'qianniao_inventory_locking_',
  541. ];
  542. //按照仓库id和季度分表 例:tableName_1_1_194
  543. $warehouseAndDateByTable = [
  544. 'qianniao_inventory_details_'
  545. ];
  546. //按人数分表 因为用户数据量小 所以统一后缀加_1
  547. $tableSuffix = [];
  548. foreach ($userByTables as $value) {
  549. if ($tableName == $value) {
  550. $tableSuffix[] = '_1';
  551. break;
  552. }
  553. }
  554. $start = 2020;
  555. $end = (int)date('Y', time());
  556. //按日期季度分表
  557. foreach ($dateByTable as $value) {
  558. if ($tableName == $value) {
  559. for ($ii = $start; $ii <= $end; $ii++) {
  560. for ($i = 1; $i <= 4; $i++) {
  561. $tableSuffix[] = '_' . $ii . '_' . $i;
  562. }
  563. }
  564. break;
  565. }
  566. }
  567. //按照季度分表2
  568. foreach ($dateByTableTwo as $value) {
  569. if ($tableName == $value) {
  570. for ($ii = $start; $ii <= $end; $ii++) {
  571. for ($i = 1; $i <= 4; $i++) {
  572. $tableSuffix[] = '_' . substr($ii, -2) . $i;
  573. }
  574. }
  575. break;
  576. }
  577. }
  578. //按日期季度分表 拼接仓库id
  579. $isWarehouseId = false;
  580. foreach ($warehouseAndDateByTable as $value) {
  581. if ($tableName == $value) {
  582. $isWarehouseId = true;
  583. for ($ii = $start; $ii <= $end; $ii++) {
  584. for ($i = 1; $i <= 4; $i++) {
  585. $tableSuffix[] = '_' . substr($ii, -2) . $i;
  586. }
  587. }
  588. break;
  589. }
  590. }
  591. //按照仓库id分表
  592. foreach ($warehouseTable as $value) {
  593. if ($tableName == $value) {
  594. $isWarehouseId = true;
  595. break;
  596. }
  597. }
  598. $stockDbName = 'stock.qianniao.vip';
  599. $objDGoods = new DGoods();
  600. if ($isWarehouseId) {
  601. //查询仓库的商铺
  602. $sql = 'select id from `' . $stockDbName . '`.`qianniao_warehouse_' . $enterpriseId . '`';
  603. $dbResult = $objDGoods->query($sql);
  604. $warehouseResult = $dbResult;
  605. unset($dbResult);
  606. //循环仓库数据
  607. foreach ($warehouseResult as $warehouse) {
  608. $warehouseId = $warehouse['id'];
  609. //判断是否有日期后缀
  610. if (!empty($tableSuffix)) {
  611. foreach ($tableSuffix as $suffix) {
  612. //拼接表名
  613. $tableList[] = $tableName . $enterpriseId . '_' . $warehouseId . $suffix;
  614. }
  615. } else {
  616. $tableList[] = $tableName . $enterpriseId . '_' . $warehouseId;
  617. }
  618. }
  619. } else {
  620. //判断是否有日期后缀
  621. if (!empty($tableSuffix)) {
  622. foreach ($tableSuffix as $suffix) {
  623. //拼接表名
  624. $tableList[] = $tableName . $enterpriseId . $suffix;
  625. }
  626. } else {
  627. $tableList[] = $tableName . $enterpriseId;
  628. }
  629. }
  630. if(empty($tableList)){
  631. return [$tableName];
  632. }
  633. $returnTable = [];
  634. foreach ($tableList as $table) {
  635. //查询表是否存在
  636. $sql = 'select count(TABLE_NAME) as isTable from information_schema.TABLES where TABLE_SCHEMA = "' . $dbName . '" and TABLE_NAME = "' . $table . '"';
  637. $dbResult = $objDGoods->query($sql);
  638. $isTable = 0;
  639. if (!empty($dbResult)) {
  640. if(isset($dbResult[0]['isTable']) && $dbResult[0]['isTable'] == 1){
  641. $isTable = 1;
  642. }
  643. }
  644. unset($dbResult);
  645. //判断表是否存在
  646. if ($isTable) {
  647. $returnTable[] = $table;
  648. }
  649. }
  650. return $returnTable;
  651. }
  652. public function removeEnterprise($enterpriseId)
  653. {
  654. //1.清空表
  655. $financeTables = [
  656. 'qianniao_account_detail_',
  657. 'qianniao_account_transfer_',
  658. 'qianniao_customer_balance_',
  659. 'qianniao_customer_balance_detail_',
  660. 'qianniao_customer_balance_detail_index_',
  661. 'qianniao_customer_balance_index_',
  662. 'qianniao_merchant_flow_',
  663. 'qianniao_paid_',
  664. 'qianniao_paid_account_',
  665. 'qianniao_paid_index_',
  666. 'qianniao_pay_receipt_',
  667. 'qianniao_pay_receipt_index_',
  668. 'qianniao_receive_receipt_',
  669. 'qianniao_receive_receipt_index_',
  670. 'qianniao_received_',
  671. 'qianniao_received_account_',
  672. 'qianniao_received_index_',
  673. 'qianniao_supplier_balance_',
  674. 'qianniao_supplier_balance_detail_',
  675. 'qianniao_supplier_balance_detail_index_',
  676. 'qianniao_supplier_balance_index_',
  677. ];
  678. $financeDbName = 'finance.qianniao.vip';
  679. $objDGoods = new DGoods();
  680. //因为分表规则不统一 规则太乱 清空表暂未启用
  681. // foreach($financeTables as $value){
  682. // $tables = self::formatTableName($value,$financeDbName,$enterpriseId);
  683. //// foreach($tables as $table){
  684. //// $sql = 'DELETE FROM ``.`'.$table.'`';
  685. //// $objDGoods->query($sql);
  686. //// }
  687. // }
  688. //修改表
  689. //customer表客户余额有2个字段需要清空
  690. //仓库表需要修改初始化状态
  691. foreach($financeTables as $value){
  692. $tables = self::formatTableName($value,$financeDbName,$enterpriseId);
  693. // foreach($tables as $table){
  694. // $sql = 'DELETE FROM ``.`'.$table.'`';
  695. // $objDGoods->query($sql);
  696. // }
  697. }
  698. //修改表
  699. //customer表客户余额有2个字段需要清空
  700. //2.清空es
  701. //订单es
  702. //退货单es
  703. //出库es
  704. //入库es
  705. //采购es
  706. //采购退货es
  707. //调拨es
  708. //盘点es
  709. //库存明细es
  710. $esIndexData = [
  711. // 'online_goods_search',
  712. // 'online_customer_search',
  713. // 'online_goods_basic_search',
  714. 'online_order_search',
  715. // 'online_shop_search',
  716. // 'online_login_log',
  717. // 'online_logs',
  718. 'online_inventory_in_search',
  719. // 'online_price_adjustment_sheet_search',
  720. // 'online_customer_price_adjustment_sheet_search',
  721. 'online_inventory_out_search',
  722. 'online_allocate_search',
  723. 'online_stocktaking_search',
  724. 'online_inventory_details_search',
  725. 'online_order_out_search',
  726. 'online_purchase_details_search',
  727. // 'online_coupon_search',
  728. // 'online_user_coupon_search',
  729. // 'online_activity_search',
  730. 'online_should_receive_receipt_search',
  731. 'online_should_pay_receipt_search',
  732. 'online_received_receipt_search',
  733. 'online_paid_receipt_search',
  734. 'online_inventory_batch_search',
  735. ];
  736. foreach($esIndexData as $value){
  737. $apiUrl = 'http://49.235.209.106:9200/'.$value.'/_delete_by_query';
  738. $curl_option = [
  739. CURLOPT_HTTPHEADER => [
  740. 'Content-Type: application/json',
  741. ],
  742. ];
  743. $postData = [
  744. 'query' => [
  745. 'bool' => [
  746. 'must' => [
  747. [
  748. 'term' => [
  749. 'enterpriseId' => $enterpriseId,
  750. ]
  751. ],
  752. [
  753. 'range' => [
  754. 'createTime' => [
  755. 'lte' => strtotime(date('Y-m-d'))
  756. ]
  757. ]
  758. ]
  759. ]
  760. ]
  761. ]
  762. ];
  763. $result = request($apiUrl, json_encode($postData), 10, false, $curl_option);
  764. if ($result['httpcode'] != 200) {
  765. return ResultWrapper::fail('删除es报错:'.$result['content'], ErrorCode::$paramError);
  766. }
  767. $content = json_decode($result['content'], true);
  768. var_dump($content['total']);
  769. }
  770. //3.清空缓存
  771. //出入库统计缓存
  772. $objCacheStock = new Stock($enterpriseId);
  773. $cacheResult = $objCacheStock->deleteCacheStatistics('InventoryIn');
  774. if(!$cacheResult && $cacheResult !== 0){
  775. return ResultWrapper::fail('删除缓存失败', ErrorCode::$paramError);
  776. }
  777. $cacheResult = $objCacheStock->deleteCacheStatistics('InventoryOut');
  778. if(!$cacheResult && $cacheResult !== 0){
  779. return ResultWrapper::fail('删除缓存失败', ErrorCode::$paramError);
  780. }
  781. return ResultWrapper::success('执行成功');
  782. }
  783. /**
  784. * 复制库存表
  785. */
  786. public function cpInventoryTable()
  787. {
  788. $masterDbName = 'www.qianniao.vip';
  789. $stockDbName = 'stock.qianniao.vip';
  790. /* $objDReceiptTemplate = new DReceiptTemplate();
  791. $sql = 'select * from `'.$masterDbName.'`.`qianniao_receipt_template` where receiptType in(1,5)';
  792. $dbResult = $this->objDInventory->query($sql);
  793. if($dbResult === false){
  794. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  795. }
  796. foreach($dbResult as $value){
  797. if($value['enterpriseId'] == 4){
  798. continue;
  799. }
  800. $fieldsData = json_decode($value['fieldsData'], true);
  801. $fieldsData['templateInfo']['basic'][] = [
  802. "alias" => "printing",
  803. "fields" => "打印标识",
  804. "isShow" => 5
  805. ];
  806. $update = [
  807. 'fieldsData' => json_encode($fieldsData),
  808. ];
  809. $result = $objDReceiptTemplate->update($update, ['id' => $value['id']]);
  810. if($result === false){
  811. return ResultWrapper::fail($objDReceiptTemplate->error(), ErrorCode::$dberror);
  812. }
  813. var_dump($value['id'].'修改成功');
  814. }
  815. V(123);*/
  816. //查询所有企业
  817. $sql = 'select * from `'.$masterDbName.'`.`qianniao_enterprise_1` where deleteStatus = 5';
  818. $dbResult = $this->objDInventory->query($sql);
  819. if($dbResult === false){
  820. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  821. }
  822. $enterprise = $dbResult;
  823. unset($dbResult);
  824. $objDLockingShop = new DLockingShop();
  825. foreach($enterprise as $value){
  826. $enterpriseId = $value['id'];
  827. // $sql = 'UPDATE `www.qianniao.vip`.`qianniao_order_'.$enterpriseId.'_1` SET `memberBalance` = 0.00';
  828. // $sql = 'update `www.qianniao.vip`.`qianniao_order_'.$enterpriseId.'_1` o,`www.qianniao.vip`.`qianniao_staff_'.$enterpriseId.'` s set o.salesManName = s.staffName where o.salesManId = s.id;';
  829. //$sql = 'update `'.$masterDbName.'`.`qianniao_order_'.$enterpriseId.'_1` set salesManName = "" where salesManId = 0';
  830. // $sql = 'UPDATE `devstock.qianniao.vip`.`qianniao_supplier_'.$enterpriseId.'` set money=(case when money>0 THEN 0-money when money<0 then abs(money) else money end)';
  831. // $sql = 'update `'.$masterDbName.'`.`qianniao_goods_'.$enterpriseId.'` set merchantId = 0 where merchantId is null';
  832. // $sql = 'UPDATE `finance.qianniao.vip`.`qianniao_supplier_balance_'.$enterpriseId.'_1` set openingBalance=(case when openingBalance>0 THEN 0-openingBalance when openingBalance<0 then abs(openingBalance) else openingBalance end) , interimBalance=(case when interimBalance>0 THEN 0-interimBalance when interimBalance<0 then abs(interimBalance) else interimBalance end), endingBalance=(case when endingBalance>0 THEN 0-endingBalance when endingBalance<0 then abs(endingBalance) else endingBalance end)';
  833. // $sql = 'UPDATE `stock.qianniao.vip`.`qianniao_supplier_'.$enterpriseId.'` set money=(case when money>0 THEN 0-money when money<0 then abs(money) else money end)';
  834. // $sql = 'update `'.$masterDbName.'`.`qianniao_goods_basic_'.$enterpriseId.'` set merchantId = 0 where merchantId is null';
  835. // $this->objDInventory->query($sql);
  836. // var_dump($enterpriseId.'--执行成功');
  837. $start = 2020;
  838. $end = (int)date('Y', time());
  839. //按日期季度分表
  840. for ($ii = $start; $ii <= $end; $ii++) {
  841. for ($i = 1; $i <= 4; $i++) {
  842. $tableSuffix[] = '_' . $ii . '_' . $i;
  843. }
  844. }
  845. foreach ($tableSuffix as $tableKey){
  846. // $tableName = 'qianniao_supplier_balance_detail_'.$enterpriseId .$tableKey;
  847. // $sql = 'UPDATE `finance.qianniao.vip`.`'.$tableName.'` set shouldPayBalance=(case when shouldPayBalance>0 THEN 0-shouldPayBalance when shouldPayBalance<0 then abs(shouldPayBalance) else shouldPayBalance end)';
  848. $tableName = 'qianniao_pay_receipt_'.$enterpriseId .$tableKey;
  849. $sql = 'update `finance.qianniao.vip`.`qianniao_pay_receipt_index_'.$enterpriseId.'` i,`finance.qianniao.vip`.`'.$tableName.'` r set r.id = i.id where i.payReceiptId = r.id and i.createTime = r.createTime';
  850. $this->objDInventory->query($sql);
  851. var_dump('企业'.$enterpriseId.', 的表: --'.$tableName.'--执行成功');
  852. }
  853. }
  854. V('执行成功');
  855. foreach($enterprise as $value){
  856. $enterpriseId = $value['id'];
  857. if($enterpriseId == 4){
  858. continue;
  859. }
  860. $inventoryTableName = 'qianniao_inventory_'.$enterpriseId;
  861. //查询每个企业的库存表是否存在
  862. $sql = 'select count(TABLE_NAME) as isTable from information_schema.TABLES where TABLE_SCHEMA = "'.$stockDbName.'" and TABLE_NAME = "'.$inventoryTableName.'"';
  863. $dbResult = $this->objDInventory->query($sql);
  864. if ($dbResult === false) {
  865. return ResultWrapper::fail('查询表是否存在报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  866. }
  867. $isTable = 0;
  868. if (!empty($dbResult)) {
  869. if(isset($dbResult[0]['isTable']) && $dbResult[0]['isTable'] == 1){
  870. $isTable = 1;
  871. }
  872. }
  873. unset($dbResult);
  874. //不存在退出
  875. if(!$isTable){
  876. continue;
  877. }
  878. //如果存在
  879. //重命名库存表为仓库库存表
  880. $inventoryWarehouseTableName = 'qianniao_inventory_warehouse_'.$enterpriseId;
  881. $sql = 'rename table `'.$stockDbName.'`.`'.$inventoryTableName.'` TO `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'`';
  882. $dbResult = $this->objDInventory->query($sql);
  883. if ($dbResult === false) {
  884. return ResultWrapper::fail('重命名报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  885. }
  886. unset($dbResult);
  887. //创建库存表
  888. $sql = "DROP TABLE IF EXISTS `".$stockDbName."`.`".$inventoryTableName."`";
  889. $dbResult = $this->objDInventory->query($sql);
  890. if ($dbResult === false) {
  891. return ResultWrapper::fail('创建库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  892. }
  893. unset($dbResult);
  894. $sql = "CREATE TABLE `".$stockDbName."`.`".$inventoryTableName."` (
  895. `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '自增id',
  896. `materielId` int(10) NOT NULL COMMENT '物料ID',
  897. `materielCode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '物料编码',
  898. `inventoryNum` decimal(20, 8) NOT NULL COMMENT '库存数',
  899. `lockInventory` decimal(20, 8) NOT NULL COMMENT '锁定库存数',
  900. `costPrice` decimal(15, 4) NULL DEFAULT NULL COMMENT '成本价',
  901. `skuId` int(10) NULL DEFAULT NULL COMMENT '单位id',
  902. `updateTime` int(10) NULL DEFAULT NULL COMMENT '修改时间',
  903. `createTime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  904. PRIMARY KEY (`id`) USING BTREE,
  905. INDEX `shopIdAndMaterielId`(`materielId`, `skuId`) USING BTREE COMMENT '组合索引'
  906. ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '库存汇总表' ROW_FORMAT = Dynamic;";
  907. $dbResult = $this->objDInventory->query($sql);
  908. if ($dbResult === false) {
  909. return ResultWrapper::fail('创建库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  910. }
  911. unset($dbResult);
  912. //查寻仓库库存表 按照skuId分组, 累加数量, 累加锁定
  913. $sql = 'select materielId,materielCode,skuId,sum(inventoryNum) as inventoryNum,sum(ifnull(lockInventory,0)) as lockInventory,createTime,updateTime from `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'` group by skuId';
  914. $dbResult = $this->objDInventory->query($sql);
  915. if ($dbResult === false) {
  916. return ResultWrapper::fail('查寻仓库库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  917. }
  918. $inventoryData = $dbResult;
  919. unset($dbResult);
  920. //把数据压进库存表
  921. $this->objDInventory->set_Table($inventoryTableName);
  922. if(!empty($inventoryData)){
  923. $dbResult = $this->objDInventory->insert($inventoryData, true);
  924. if($dbResult === false){
  925. return ResultWrapper::fail('把数据压进库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  926. }
  927. unset($dbResult);
  928. }
  929. $shopLockTableName = 'qianniao_locking_shop_'.$enterpriseId;
  930. //创建商铺锁定表
  931. $sql = "DROP TABLE IF EXISTS `".$stockDbName."`.`".$shopLockTableName."`";
  932. $dbResult = $this->objDInventory->query($sql);
  933. if($dbResult === false){
  934. return ResultWrapper::fail('创建商铺锁定表:'.$this->objDInventory->error(), ErrorCode::$dberror);
  935. }
  936. unset($dbResult);
  937. $sql = "CREATE TABLE `".$stockDbName."`.`".$shopLockTableName."` (
  938. `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '自增id',
  939. `shopId` int(10) NULL DEFAULT NULL COMMENT '商铺id',
  940. `materielId` int(10) NOT NULL COMMENT '物料ID',
  941. `materielCode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '物料编码',
  942. `lockInventory` decimal(20, 8) NOT NULL COMMENT '锁定库存数',
  943. `skuId` int(10) NULL DEFAULT NULL COMMENT '单位id',
  944. `updateTime` int(10) NULL DEFAULT NULL COMMENT '修改时间',
  945. `createTime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
  946. PRIMARY KEY (`id`) USING BTREE,
  947. INDEX `shopIdAndMaterielId`(`materielId`, `skuId`, `shopId`) USING BTREE COMMENT '组合索引'
  948. ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '商铺锁定表' ROW_FORMAT = Dynamic;";
  949. $dbResult = $this->objDInventory->query($sql);
  950. if($dbResult === false){
  951. return ResultWrapper::fail('创建商铺锁定表:'.$this->objDInventory->error(), ErrorCode::$dberror);
  952. }
  953. //查询每个企业有几个店铺
  954. $sql = 'select * from `'.$masterDbName.'`.`qianniao_shop_1` where enterpriseId = '.$enterpriseId.' and deleteStatus = 5';
  955. $dbResult = $this->objDInventory->query($sql);
  956. if($dbResult === false){
  957. return ResultWrapper::fail('查询商铺表:'.$this->objDInventory->error(), ErrorCode::$dberror);
  958. }
  959. $shopResult = $dbResult;
  960. unset($dbResult);
  961. $shopAsWarehouseId = [];
  962. foreach($shopResult as $shopValue){
  963. $shopAsWarehouseId[$shopValue['warehouseId']] = $shopValue['id'];
  964. }
  965. //查询仓库库存表 按照skuId,仓库id分组, 累加数量, 累加锁定, 均价成本
  966. $sql = 'select materielId,materielCode,skuId,warehouseId,sum(lockInventory) as lockInventory,createTime,updateTime from `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'` group by skuId,warehouseId';
  967. $dbResult = $this->objDInventory->query($sql);
  968. if ($dbResult === false) {
  969. return ResultWrapper::fail('查寻仓库库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  970. }
  971. $inventoryLockData = $dbResult;
  972. unset($dbResult);
  973. //把数据压进商铺锁定表
  974. $inventoryLockInsert = [];
  975. foreach($inventoryLockData as $lockValue){
  976. if(isset($shopAsWarehouseId[$lockValue['warehouseId']])){
  977. $inventoryLockInsert[] = [
  978. 'shopId' => $shopAsWarehouseId[$lockValue['warehouseId']],
  979. 'materielId' => $lockValue['materielId'],
  980. 'materielCode' => $lockValue['materielCode'],
  981. 'lockInventory' => empty($lockValue['lockInventory']) ? 0 : $lockValue['lockInventory'],
  982. 'skuId' => $lockValue['skuId'],
  983. 'updateTime' => time(),
  984. 'createTime' => $lockValue['createTime']
  985. ];
  986. }
  987. }
  988. $objDLockingShop->set_Table($shopLockTableName);
  989. if(!empty($inventoryLockInsert)){
  990. $dbResult = $objDLockingShop->insert($inventoryLockInsert, true);
  991. if($dbResult === false){
  992. return ResultWrapper::fail($objDLockingShop->error(), ErrorCode::$dberror);
  993. }
  994. unset($dbResult);
  995. }
  996. //删除仓库库存表锁定字段
  997. $sql = 'ALTER TABLE `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'` DROP COLUMN `lockInventory`';
  998. $dbResult = $this->objDInventory->query($sql);
  999. if($dbResult === false){
  1000. return ResultWrapper::fail('删除仓库库存表锁定字段报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  1001. }
  1002. $sql = "ALTER TABLE `".$stockDbName."`.`".$inventoryWarehouseTableName."` ADD COLUMN `sort` int(10) NULL DEFAULT 0 COMMENT '排序' AFTER `skuId`;";
  1003. $dbResult = $this->objDInventory->query($sql);
  1004. if($dbResult === false){
  1005. return ResultWrapper::fail('修改仓库库存表排序字段报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
  1006. }
  1007. //修改出库商品表total字段
  1008. $sql = 'update `'.$stockDbName.'`.`qianniao_inventory_out_details_'.$enterpriseId.'` set total = num where createTime < '.strtotime('2021-03-22 23:59:59');
  1009. $dbResult = $this->objDInventory->query($sql);
  1010. if($dbResult === false){
  1011. return ResultWrapper::fail('修改出库商品表total字段:'.$this->objDInventory->error(), ErrorCode::$dberror);
  1012. }
  1013. var_dump($enterpriseId.'修改成功');
  1014. }
  1015. return ResultWrapper::success('操作成功');
  1016. }
  1017. /**
  1018. * 更新真实销量
  1019. */
  1020. // public function updateGoods()
  1021. // {
  1022. // $masterDbName = 'dev.qiannao.vip';
  1023. // //查询所有企业
  1024. // $sql = 'select * from `'.$masterDbName.'`.`qianniao_enterprise_1` where deleteStatus = 5';
  1025. // $dbResult = $this->objDInventory->query($sql);
  1026. // if($dbResult === false){
  1027. // return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  1028. // }
  1029. // $enterprise = $dbResult;
  1030. // $objDGoods = new DGoods();
  1031. //
  1032. // foreach($enterprise as $value){
  1033. // $enterpriseId = $value['id'];
  1034. //
  1035. // //$sql = 'UPDATE `devstock.qianniao.vip`.`qianniao_supplier_'.$enterpriseId.'` set money=(case when money>0 THEN 0-money when money<0 then abs(money) else money end)';
  1036. // $sql = 'select * from `' . $masterDbName . '`.`qianniao_goods_' . $enterpriseId . '`' ;
  1037. //
  1038. // $goodsDbResult = $objDGoods->query($sql);
  1039. // foreach ($goodsDbResult as $goods){
  1040. // $salesNumArray = isset($goods['salesNum']) && !empty($goods['salesNum']) ? json_decode($goods['salesNum'],true) : [];
  1041. // $realSalesNum = 0;
  1042. // $realSalesNum = array_sum(array_column($salesNumArray,'salesNum'));
  1043. // var_dump('企业id'.$enterpriseId);
  1044. // var_dump('销售数量'.$realSalesNum);
  1045. // $updateSql = 'update `'.$masterDbName.'`.`qianniao_goods_' . $enterpriseId . '` set realSalesNum ='.$realSalesNum.' where id = '.$goods['id'];
  1046. // $objDGoods->query($updateSql);
  1047. // }
  1048. // }
  1049. // V('执行完成');
  1050. //
  1051. // }
  1052. public function updateOrderCost()
  1053. {
  1054. $masterDbName = 'www.huabeihudie.com';
  1055. $stockDbName = 'stock.huabeihudie.com';
  1056. $tableArray = [
  1057. 'qianniao_inventory_details_169_1_212',
  1058. 'qianniao_inventory_details_169_2_212',
  1059. 'qianniao_inventory_details_169_3_212',
  1060. ];
  1061. $i = 0;
  1062. foreach($tableArray as $tableName){
  1063. $sql = 'select * from `'.$stockDbName.'`.`'.$tableName.'` where createTime < 1617934818 and actionType = 4 and `source` = 5';
  1064. $dbResult = $this->objDInventory->query($sql);
  1065. if($dbResult === false){
  1066. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  1067. }
  1068. foreach($dbResult as $value){
  1069. $sql = 'update `'.$masterDbName.'`.`qianniao_order_goods_169_1` set outCostPrice = '.$value['averageCost'].' where orderId = '.$value['originId'].' and skuId = '.$value['skuId'];
  1070. $result = $this->objDInventory->query($sql);
  1071. if($result === false){
  1072. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  1073. }
  1074. $i++;
  1075. }
  1076. }
  1077. return ResultWrapper::success('共修复'.$i.'条订单商品数据');
  1078. }
  1079. /**
  1080. * 平账脚本
  1081. * ① 查出所有客户的money
  1082. * ② 根据money字段进行创建收款单平账
  1083. * ③ 创建完收款单后自动审核
  1084. */
  1085. public function balancingAccounts()
  1086. {
  1087. $objCustomer = new DCustomer();
  1088. $objAccount = new DAccount();
  1089. $objSupplier = new DSupplier();
  1090. $masterDbName = 'www.qianniao.vip';
  1091. $financeDbName = 'finance.qianniao.vip';
  1092. $stockDbName = 'stock.qianniao.vip';
  1093. //查询所有企业
  1094. $sql = 'select * from `'.$masterDbName.'`.`qianniao_enterprise_1` where id = 48 and deleteStatus = 5';
  1095. $dbResult = $this->objDInventory->query($sql);
  1096. if($dbResult === false){
  1097. return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
  1098. }
  1099. $enterprise = $dbResult;
  1100. // 收款
  1101. foreach ($enterprise as $enterpriseValue){
  1102. $enterpriseId = $enterpriseValue['id'];
  1103. $sql = 'select * from `' . $masterDbName . '`.`qianniao_customer_' . $enterpriseId . '` where money !=0 and money > 0' ;
  1104. $customerDbResult = $objCustomer->query($sql);
  1105. // 获取账户数据
  1106. $account[0] = [];
  1107. $accountSql = 'select * from `'.$financeDbName.'`.`qianniao_account` where deleteStatus = 5 and enableStatus = 5';
  1108. $accountDate = $objAccount->query($accountSql);
  1109. foreach ($customerDbResult as $customerKey => $customerValue){
  1110. //组装account
  1111. if($accountDate && !empty($accountDate)){
  1112. $account[0] = [
  1113. 'accountId' => $accountDate[0]['id'],
  1114. 'accountNumber'=>$accountDate[0]['accountNumber'],
  1115. 'accountName'=> $accountDate[0]['name'],
  1116. 'money'=> $customerValue['money'],
  1117. 'discountMoney'=> 0.00,
  1118. 'finalMoney' => $customerValue['money'],
  1119. 'payWay'=> '',
  1120. 'remark'=>'收款单收款'
  1121. ];
  1122. }else{
  1123. var_dump('企业'.$enterpriseId.'没有账户');
  1124. continue;
  1125. }
  1126. //组装收款单数据
  1127. $objMReceived = new MReceived($enterpriseId, $customerValue['userCenterId']);
  1128. //创建收款单
  1129. $received = [
  1130. 'currentAccountName' => $enterpriseValue['contact'],
  1131. 'financeType' => '销售收款',
  1132. 'financeTypeId' => 2,
  1133. 'shopId' => 0,
  1134. 'shopName' => '',
  1135. 'receiptTime' => time(),
  1136. 'operatorId' => $customerValue['userCenterId'],
  1137. 'accountList' => $account,
  1138. 'customerId' => $customerValue['id'],
  1139. 'customerName' => $customerValue['name'],
  1140. 'sourceNo' => '',
  1141. 'sourceNoMoney' => '',
  1142. 'createTime' => time(),
  1143. 'auditStatus' => StatusCode::$auditStatus['auditing'],
  1144. 'updateTime' => time(),
  1145. 'receiptOffsetData' => []
  1146. ];
  1147. $receivedResult = $objMReceived->addReceived($received);
  1148. if (!$receivedResult->isSuccess()) {
  1149. var_dump('新增收款单错误 data:'.$receivedResult->getData().'error'.$receivedResult->getErrorCode());
  1150. continue;
  1151. }
  1152. //审核收款单
  1153. $updateReceived = $objMReceived->updateReceivedStatus(['id'=>$receivedResult->getData(),'createTime'=>time()]);
  1154. if (!$updateReceived->isSuccess()) {
  1155. var_dump('审核收款单错误 data:'.$receivedResult->getData().'error'.$receivedResult->getErrorCode());
  1156. continue;
  1157. }
  1158. }
  1159. unset($accountSql);
  1160. unset($accountDate);
  1161. /* ----------------------------------------------------------付款-------------------------------------------------*/
  1162. // $sql = 'select * from `' . $stockDbName . '`.`qianniao_supplier_' . $enterpriseId . '` where money !=0 and money > 0' ;
  1163. // $supplierDbResult = $objSupplier->query($sql);
  1164. // // 获取账户数据
  1165. // $account[0] = [];
  1166. // $accountSql = 'select * from `'.$financeDbName.'`.`qianniao_account` where deleteStatus = 5 and enableStatus = 5';
  1167. // $accountDate = $objAccount->query($accountSql);
  1168. //
  1169. // foreach ($supplierDbResult as $supplierKey => $supplierValue){
  1170. // //组装account
  1171. // if($accountDate && !empty($accountDate)){
  1172. // $account[0] = [
  1173. // 'accountId' => $accountDate[0]['id'],
  1174. // 'accountNumber'=>$accountDate[0]['accountNumber'],
  1175. // 'accountName'=> $accountDate[0]['name'],
  1176. // 'money'=> $supplierValue['money'],
  1177. // 'discountMoney'=> 0.00,
  1178. // 'finalMoney' => $supplierValue['money'],
  1179. // 'payWay'=> '',
  1180. // 'remark'=>'付款单付款'
  1181. // ];
  1182. // }else{
  1183. // var_dump('企业'.$enterpriseId.'没有账户');
  1184. // continue;
  1185. // }
  1186. // //组装收款单数据
  1187. // $objMPaid = new MPaid($enterpriseId, $supplierValue['userCenterId']);
  1188. // //创建收款单
  1189. // $paid = [
  1190. // 'currentAccountName' => $enterpriseValue['contact'],
  1191. // 'supplierId' => $supplierValue['id'],
  1192. // 'supplierName' => $supplierValue['title'],
  1193. // 'sourceNo' => '',
  1194. // 'sourceNoMoney' => 0,
  1195. // 'financeType' => '采购预付',
  1196. // 'financeTypeId' => 4,
  1197. // 'shopId' => 0,
  1198. // 'shopName' => '',
  1199. // 'receiptTime' => time(),
  1200. // 'accountList' => $account,
  1201. // 'createTime' => time(),
  1202. // 'auditStatus' => StatusCode::$auditStatus['auditing'],
  1203. // 'updateTime' => time(),
  1204. // 'receiptOffsetData' => []
  1205. // ];
  1206. // $paidResult = $objMPaid->addPaid($paid);
  1207. // if (!$paidResult->isSuccess()) {
  1208. // var_dump('新增付款单错误 data:'.$paidResult->getData().'error'.$paidResult->getErrorCode());
  1209. // continue;
  1210. // }
  1211. // //审核收款单
  1212. // $updatePaid = $objMPaid->updatePaidStatus(['id'=>$paidResult->getData(),'createTime'=>time()]);
  1213. // if (!$updatePaid->isSuccess()) {
  1214. // var_dump('审核付款单错误 data:'.$updatePaid->getData().'error'.$updatePaid->getErrorCode());
  1215. // continue;
  1216. // }
  1217. // }
  1218. }
  1219. var_dump('收款执行完成');
  1220. V('付款执行完成');
  1221. }
  1222. /**
  1223. * 删除xhjc的商品属性
  1224. * 2077
  1225. */
  1226. public function delXhjcGoods()
  1227. {
  1228. $objDSku = new DSku();
  1229. $objDSku->setTable('qianniao_sku_63');
  1230. $objDGoodsBasic = new DGoodsBasic();
  1231. $objDGoodsBasic->setTable('qianniao_goods_basic_63');
  1232. $goodsBasicIds = [];
  1233. //去掉商户id 119 的数据
  1234. $goodsBasicDate = $objDGoodsBasic->query('select * from qianniao_goods_basic_63 where merchantId != 119');
  1235. if($goodsBasicDate === false){
  1236. return ResultWrapper::fail($objDGoodsBasic->error(), ErrorCode::$dberror);
  1237. }
  1238. foreach ($goodsBasicDate as $basicKey => $basicValue){
  1239. array_push($goodsBasicIds,$basicValue['id']);
  1240. }
  1241. $md5Key = [];
  1242. $dbResult = $objDSku->select(['specType'=>2,'goodsId' => $goodsBasicIds]);
  1243. if($dbResult === false){
  1244. return ResultWrapper::fail($objDSku->error(), ErrorCode::$dberror);
  1245. }
  1246. foreach ($dbResult as $key => $value){
  1247. if( in_array(md5($value['goodsId'].'+'.$value['unitId']),$md5Key) ){
  1248. echo md5($value['goodsId'].'+'.$value['unitId']).'存在'."\n";
  1249. // 删除该条商品
  1250. $objDSku->delete(['id'=>$value['id']]);
  1251. }else{
  1252. echo '需要修改的'."\n";
  1253. $update = $objDSku->update(['specType'=>1,'specData'=>'[]','specGroupHash'=>''],['id'=>$value['id']]);
  1254. if($update === false){
  1255. return ResultWrapper::fail($objDSku->error(), ErrorCode::$dberror);
  1256. }
  1257. }
  1258. array_push($md5Key,md5($value['goodsId'].'+'.$value['unitId'] ));
  1259. }
  1260. V($md5Key);
  1261. }
  1262. }