1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360 |
- <?php
- /**
- * 公共Model
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/11/18
- * Time: 09:22
- */
- namespace JinDouYun\Model\Common;
- use JinDouYun\Cache\Stock;
- use JinDouYun\Dao\Customer\DCustomer;
- use JinDouYun\Dao\Finance\DAccount;
- use JinDouYun\Dao\Finance\DReceiptRequisition;
- use JinDouYun\Dao\Goods\DGoods;
- use JinDouYun\Dao\GoodsManage\DGoodsBasic;
- use JinDouYun\Dao\GoodsManage\DSku;
- use JinDouYun\Dao\Purchase\DSupplier;
- use JinDouYun\Dao\Stock\DLockingShop;
- use JinDouYun\Dao\System\DReceiptTemplate;
- use JinDouYun\Model\Customer\MCustomer;
- use JinDouYun\Model\Department\MRoleAcl;
- use JinDouYun\Model\Finance\MPaid;
- use JinDouYun\Model\Finance\MReceived;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Stock\DInventory;
- use JinDouYun\Dao\Stock\DInventoryBatch;
- use JinDouYun\Model\MBaseModel;
- use Util\Common\ChineseCharacter;
- class MSql extends MBaseModel
- {
- private $objDInventory;
- public function __construct()
- {
- parent::__construct();
- $this->objDInventory = new DInventory();
- }
- public function batchRestoreSql()
- {
- $objDSku = new DSku();
- $objDSku->setTable('qianniao_sku_105');
- $sql = 'select * from `www.qianniao.vip`.`qianniao_sku_105` where deleteStatus = 5';
- $dbResult = $this->objDInventory->query($sql);
- $data = [];
- foreach($dbResult as $value){
- $data[$value['goodsId']][] = $value;
- }
- // V($data);
- foreach($data as $value){
- if($value[0]['isNew'] == 5){
- continue;
- }
- if(count($value) == 1){
- $value = $value[0];
- if($value['unitId'] == 1){
- $update = [
- 'isNew' => 5,
- 'unitId' => 3,
- 'unitName' => '袋'
- ];
- }else{
- $update = [
- 'isNew' => 5,
- ];
- }
- $objDSku->update($update,['id' => $value['id']]);
- }else{
- foreach($value as $key => $v){
- $array = $key == 0 ? $value[1] : $value[0];
- if($v['isMaster'] == 5){
- $update = [
- 'isMaster' => 4,
- 'conversion' => $array['conversion'],
- 'isNew' => 5,
- ];
- }else{
- $update = [
- 'isMaster' => 5,
- 'conversion' => 0,
- 'isNew' => 5,
- ];
- }
- $objDSku->update($update,['id' => $v['id']]);
- }
- }
- }
- exit;
- $whereSql = ' and skuId = 94';
- $whereSql = '';
- //查询所有库存数据
- $sql = 'select * from qianniao_inventory_details_56_1_203 where actionType = 5'.$whereSql;
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $inData = $dbResult;
- unset($dbResult);
- $sql = 'select * from qianniao_inventory_details_56_1_203 where actionType = 4'.$whereSql;
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $outData = $dbResult;
- unset($dbResult);
- $outFormat = [];
- foreach($outData as $value){
- $batch = json_decode($value['batch'], true)[0];
- $outFormat[$value['skuId']][$batch['batch']] = $value;
- }
- $addBatchData = [];
- //生成批次数据
- foreach($inData as $value){
- $batch = json_decode($value['batch'], true)[0];
- $sql = 'select inventoryNum from qianniao_inventory_56 where skuId = '.$value['skuId'];
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $inventoryData = $dbResult[0];
- unset($dbResult);
- if(isset($outFormat[$value['skuId']])){
- if((int)$outFormat[$value['skuId']][$batch['batch']]['inventoryChangeNum'] == (int)$inventoryData['inventoryNum']){
- $addBatchData[] = [
- 'originId' => $value['originId'],
- 'originNo' => $value['originNo'],
- 'warehouseId' => $value['warehouseId'],
- 'materielId' => $value['materielId'],
- 'materielCode' => $value['materielCode'],
- 'sourceNo' => $value['sourceNo'],
- 'skuId' => $value['skuId'],
- 'batchNo' => $batch['batch'],
- 'num' => $outFormat[$value['skuId']][$batch['batch']]['inventoryChangeNum'],
- 'averageCost' => $value['averageCost'],
- 'batchCost' => $batch['batchCost'],
- // 'productionData' => "",//生产日期
- // 'shelfLife' => "",//保质期
- 'batchStatus' => StatusCode::$standard,
- 'createTime' => $value['createTime'],
- 'updateTime' => $value['updateTime'],
- ];
- }
- }else{
- $addBatchData[] = [
- 'originId' => $value['originId'],
- 'originNo' => $value['originNo'],
- 'warehouseId' => $value['warehouseId'],
- 'materielId' => $value['materielId'],
- 'materielCode' => $value['materielCode'],
- 'sourceNo' => $value['sourceNo'],
- 'skuId' => $value['skuId'],
- 'batchNo' => $batch['batch'],
- 'num' => $value['inventoryChangeNum'],
- 'averageCost' => $value['averageCost'],
- 'batchCost' => $batch['batchCost'],
- // 'productionData' => "",//生产日期
- // 'shelfLife' => "",//保质期
- 'batchStatus' => StatusCode::$standard,
- 'createTime' => $value['createTime'],
- 'updateTime' => $value['updateTime'],
- ];
- }
- }
- $objDInventoryBatch = new DInventoryBatch();
- $objDInventoryBatch->setTable('qianniao_inventory_batch_56_1');
- $dbResult = $objDInventoryBatch->insert($addBatchData, true);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * sql执行
- * 该方法只执行需要拼接企业id的表 不要拼接企业id的表请自行手动执行
- * @param string $sqlParent
- */
- public function batchSql($sqlParent = '')
- {
- var_dump($sqlParent);
- //执行语句
- $sqlIndex = substr($sqlParent, stripos($sqlParent, "` ") + 2);
- //执行语句表所属数据库
- $dbName = substr($sqlParent, stripos($sqlParent, ' `') + 2, stripos($sqlParent, '`.' ) - stripos($sqlParent, ' `') - 2);
- //企业表所属数据库
- $dbIndexName = 'www.qianniao.vip';
- //库存数据库名
- $stockDbName = 'stock.qianniao.vip';
- //修改config
- //表名
- $sqlTable = substr($sqlParent, stripos($sqlParent, '.`') + 2, stripos($sqlParent, '` ' ) - stripos($sqlParent, '.`') - 2);
- $strNumIndex = 0;
- for($a = 1; $a <= 9; $a++){
- $strNum = stripos($sqlTable, (string)$a);
- if($strNum){
- if(!$strNumIndex || $strNum < $strNumIndex){
- $strNumIndex = $strNum;
- }
- }
- }
- if(!$strNumIndex){
- return ResultWrapper::fail('表名错误', ErrorCode::$dberror);
- }
- $tableNameIndex = substr($sqlTable, 0, $strNumIndex);
- //是否执行企业1表
- $oneEnterpriseTable = true;
- //从某个企业执行
- $startEnterpriseId = 0;
- //按照用户人数分表 例:tableName_1_1
- $userByTables = [
- 'qianniao_order_',
- 'qianniao_order_coupon_',
- 'qianniao_order_goods_',
- 'qianniao_order_receive_',
- 'qianniao_goods_collect_',
- 'qianniao_supplier_order_details_',
- 'qianniao_account_detail_',
- 'qianniao_customer_balance_',
- 'qianniao_supplier_balance_',
- ];
- //按照仓库id分表
- $warehouseTable = [
- 'qianniao_inventory_batch_'
- ];
- //按照季度分表 例:tableName_1_2020_3
- $dateByTable = [
- 'qianniao_commission_order_',
- 'qianniao_received_account_',
- 'qianniao_paid_',
- 'qianniao_paid_account_',
- 'qianniao_customerPriceAdjustmentSheet_',
- 'qianniao_customer_balance_detail_',
- 'qianniao_pay_receipt_',
- 'qianniao_receive_receipt_',
- 'qianniao_received_',
- 'qianniao_supplier_balance_detail_',
- 'qianniao_customerTypePriceAdjustmentSheet_',
- 'qianniao_priceAdjustmentSheet_',
- 'qianniao_refund_',
- ];
- //按照季度分表2 例:tableName_1_201
- $dateByTableTwo = [
- 'qianniao_inventory_locking_',
- ];
- //按照仓库id和季度分表 例:tableName_1_1_194
- $warehouseAndDateByTable = [
- 'qianniao_inventory_details_'
- ];
- $runTrue = 0;
- $runFalse = 0;
- $runTrueTableName = [];
- $runFalseTableName = [];
- $tableNum = 0;
- $existNum = 0;
- $existTable = [];
- if (empty($sqlIndex)) {
- return ResultWrapper::fail('请输入sql语句', ErrorCode::$dberror);
- }
- if (empty($dbIndexName)) {
- return ResultWrapper::fail('请输入数据库名', ErrorCode::$dberror);
- }
- if (empty($tableNameIndex)) {
- return ResultWrapper::fail('请输入表名', ErrorCode::$dberror);
- }
- //查询所有企业
- $whereSql = '';
- if ($startEnterpriseId) {
- $whereSql = ' where id >= ' . $startEnterpriseId;
- }
- $sql = 'select id from `' . $dbIndexName . '`.`qianniao_enterprise_1`' . $whereSql;
- $objDGoods = new DGoods();
- $dbResult = $objDGoods->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('查询企业时sql报错:'.$objDGoods->error(),ErrorCode::$dberror);
- }
- $enterpriseResult = $dbResult;
- unset($dbResult);
- unset($sql);
- if (empty($enterpriseResult)) {
- return ResultWrapper::fail('企业数据为空',ErrorCode::$dberror);
- }
- //按人数分表 因为用户数据量小 所以统一后缀加_1
- $tableSuffix = [];
- foreach ($userByTables as $value) {
- if ($tableNameIndex == $value) {
- $tableSuffix[] = '_1';
- break;
- }
- }
- $start = 2020;
- $end = (int)date('Y', time());
- //按日期季度分表
- foreach ($dateByTable as $value) {
- if ($tableNameIndex == $value) {
- for ($ii = $start; $ii <= $end; $ii++) {
- for ($i = 1; $i <= 4; $i++) {
- $tableSuffix[] = '_' . $ii . '_' . $i;
- }
- }
- break;
- }
- }
- //按照季度分表2
- foreach ($dateByTableTwo as $value) {
- if ($tableNameIndex == $value) {
- for ($ii = $start; $ii <= $end; $ii++) {
- for ($i = 1; $i <= 4; $i++) {
- $tableSuffix[] = '_' . substr($ii, -2) . $i;
- }
- }
- break;
- }
- }
- //按日期季度分表 拼接仓库id
- $isWarehouseId = false;
- foreach ($warehouseAndDateByTable as $value) {
- if ($tableNameIndex == $value) {
- $isWarehouseId = true;
- for ($ii = $start; $ii <= $end; $ii++) {
- for ($i = 1; $i <= 4; $i++) {
- $tableSuffix[] = '_' . substr($ii, -2) . $i;
- }
- }
- break;
- }
- }
- //按照仓库id分表
- foreach ($warehouseTable as $value) {
- if ($tableNameIndex == $value) {
- $isWarehouseId = true;
- break;
- }
- }
- if ($isWarehouseId && empty($stockDbName)) {
- return ResultWrapper::fail($tableNameIndex . '按照仓库分表, 请输入仓库表对应数据库名称', ErrorCode::$dberror);
- }
- $enterpriseFormat = array_column($enterpriseResult, null, 'id');
- if (!isset($enterpriseFormat[1])) {
- $enterprise1 = [
- 'id' => 1,
- ];
- array_unshift($enterpriseResult, $enterprise1);
- }
- $enterpriseData = [];
- $emptyTable = [];
- foreach ($enterpriseResult as $value) {
- $tableList = [];
- //拿出企业id
- $enterpriseId = $value['id'];
- //判断是否执行1企业
- if ($enterpriseId == 1) {
- if($oneEnterpriseTable == false){
- continue;
- }else{
- $tableList[] = $tableNameIndex . $enterpriseId;
- }
- }
- if ($isWarehouseId) {
- //查询仓库的商铺
- $sql = 'select id from `' . $stockDbName . '`.`qianniao_warehouse_' . $enterpriseId . '`';
- $dbResult = $objDGoods->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('查询仓库数据报错:'.$objDGoods->error(), ErrorCode::$dberror);
- }
- $warehouseResult = $dbResult;
- unset($dbResult);
- //循环仓库数据
- foreach ($warehouseResult as $warehouse) {
- $warehouseId = $warehouse['id'];
- //判断是否有日期后缀
- if (!empty($tableSuffix)) {
- foreach ($tableSuffix as $suffix) {
- //拼接表名
- $tableList[] = $tableNameIndex . $enterpriseId . '_' . $warehouseId . $suffix;
- }
- } else {
- $tableList[] = $tableNameIndex . $enterpriseId . '_' . $warehouseId;
- }
- }
- } else {
- //判断是否有日期后缀
- if (!empty($tableSuffix)) {
- foreach ($tableSuffix as $suffix) {
- //拼接表名
- $tableList[] = $tableNameIndex . $enterpriseId . $suffix;
- }
- } else {
- $tableList[] = $tableNameIndex . $enterpriseId;
- }
- }
- $tableList = array_unique($tableList);
- foreach ($tableList as $tableName) {
- $tableNum++;
- //查询表是否存在
- $sql = 'select count(TABLE_NAME) as isTable from information_schema.TABLES where TABLE_SCHEMA = "' . $dbName . '" and TABLE_NAME = "' . $tableName . '"';
- $dbResult = $objDGoods->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('查询表是否存在报错:'.$objDGoods->error(), ErrorCode::$dberror);
- }
- $isTable = 0;
- if (!empty($dbResult)) {
- if(isset($dbResult[0]['isTable']) && $dbResult[0]['isTable'] == 1){
- $isTable = 1;
- }
- }
- unset($dbResult);
- //判断表是否存在
- if ($isTable) {
- $existNum++;
- $existTable[] = $tableName;
- $enterpriseData[$enterpriseId][] = ['enterpriseId' => $enterpriseId, 'tableName' => $tableName];
- }else{
- $emptyTable[] = $tableName;
- }
- }
- }
- // self::prompt('', 0, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName);
- //执行语句
- foreach ($enterpriseData as $enterpriseId => $value) {
- foreach ($value as $enterprise) {
- $enterpriseId = $enterprise['enterpriseId'];
- $tableName = $enterprise['tableName'];
- //拼接修改sql语句
- $sql = "ALTER TABLE `" . $dbName . "`.`" . $tableName . "` " . $sqlIndex;
- // echo $sql.PHP_EOL;
- $dbResult = $objDGoods->query($sql);
- $true = true;
- if ($dbResult === false) {
- echo 'sql执行错误: '.$objDGoods->error().PHP_EOL;
- exit;
-
- $true = $dbResult;
- $runFalse++;
- $runFalseTableName[] = $tableName;
- self::prompt($objDGoods->error(), $enterpriseId, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName);
- }
- $runTrue++;
- $runTrueTableName[] = $tableName;
- var_dump('企业'.$enterpriseId.'成功执行一条'.$tableName.'表语句-'.$true);
- unset($dbResult);
- }
- }
- self::prompt('', 0, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName);
- }
- public function prompt($info = '', $enterpriseId = 0, $enterpriseResult, $tableNum, $existNum, $runTrue, $runTrueTableName, $surplusSql = '')
- {
- $title = '';
- if (!empty($info)) {
- $title = '执行到企业' . $enterpriseId . '时有报错:
- ' . $info . '
- ';
- }
- $title .= '一共' . count($enterpriseResult) . '个企业, 需要修改' . $tableNum . '个表, 存在的表有' . $existNum . '个, 修改成功的表' . $runTrue . '个, 成功的有:' . implode(',', $runTrueTableName);
- return ResultWrapper::success($title);
- }
- /**
- * 批量执行格式化客户名称首字母
- */
- public function initCustomerCondition()
- {
- $dbIndexName = 'www.qianniao.vip';
- $sql = 'select id from `' . $dbIndexName . '`.`qianniao_enterprise_1`';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $enterpriseData = $dbResult;
- unset($dbResult);
- $objChineseCharacter = new ChineseCharacter();
- foreach($enterpriseData as $value){
- $sql = 'select * from `'.$dbIndexName.'`.qianniao_customer_'.$value['id'];
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- foreach($dbResult as $v){
- $condition = $objChineseCharacter->getInitials(trim($v['name']));
- $sql = "update `".$dbIndexName."`.qianniao_customer_".$value['id']." set `condition` = '".$condition."' where id = ".$v['id'];
- $result = $this->objDInventory->query($sql);
- if($result === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- }
- }
- return ResultWrapper::success('执行成功');
- }
- public function initCache()
- {
- $dbIndexName = 'www.qianniao.vip';
- $sql = 'select id from `' . $dbIndexName . '`.`qianniao_enterprise_1` where deleteStatus = 5';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $enterpriseData = $dbResult;
- unset($dbResult);
- foreach($enterpriseData as $value){
- $objMRoleAcl = new MRoleAcl($value['id']);
- $modelResult = $objMRoleAcl->initCache();
- if(!$modelResult->isSuccess()){
- var_dump($modelResult->getData());
- }
- unset($objMRoleAcl);
- }
- return ResultWrapper::success('执行成功');
- }
- public function formatTableName($tableName, $dbName, $enterpriseId)
- {
- //按照用户人数分表 例:tableName_1_1
- $userByTables = [
- 'qianniao_order_',
- 'qianniao_order_coupon_',
- 'qianniao_order_goods_',
- 'qianniao_order_receive_',
- 'qianniao_goods_collect_',
- 'qianniao_supplier_order_details_',
- 'qianniao_account_detail_',
- 'qianniao_customer_balance_',
- 'qianniao_supplier_balance_',
- ];
- //按照仓库id分表
- $warehouseTable = [
- 'qianniao_inventory_batch_'
- ];
- //按照季度分表 例:tableName_1_2020_3
- $dateByTable = [
- 'qianniao_commission_order_',
- 'qianniao_received_account_',
- 'qianniao_paid_',
- 'qianniao_paid_account_',
- 'qianniao_customerPriceAdjustmentSheet_',
- 'qianniao_customer_balance_detail_',
- 'qianniao_pay_receipt_',
- 'qianniao_receive_receipt_',
- 'qianniao_received_',
- 'qianniao_supplier_balance_detail_',
- 'qianniao_customerTypePriceAdjustmentSheet_',
- 'qianniao_priceAdjustmentSheet_',
- ];
- //按照季度分表2 例:tableName_1_201
- $dateByTableTwo = [
- 'qianniao_inventory_locking_',
- ];
- //按照仓库id和季度分表 例:tableName_1_1_194
- $warehouseAndDateByTable = [
- 'qianniao_inventory_details_'
- ];
- //按人数分表 因为用户数据量小 所以统一后缀加_1
- $tableSuffix = [];
- foreach ($userByTables as $value) {
- if ($tableName == $value) {
- $tableSuffix[] = '_1';
- break;
- }
- }
- $start = 2020;
- $end = (int)date('Y', time());
- //按日期季度分表
- foreach ($dateByTable as $value) {
- if ($tableName == $value) {
- for ($ii = $start; $ii <= $end; $ii++) {
- for ($i = 1; $i <= 4; $i++) {
- $tableSuffix[] = '_' . $ii . '_' . $i;
- }
- }
- break;
- }
- }
- //按照季度分表2
- foreach ($dateByTableTwo as $value) {
- if ($tableName == $value) {
- for ($ii = $start; $ii <= $end; $ii++) {
- for ($i = 1; $i <= 4; $i++) {
- $tableSuffix[] = '_' . substr($ii, -2) . $i;
- }
- }
- break;
- }
- }
- //按日期季度分表 拼接仓库id
- $isWarehouseId = false;
- foreach ($warehouseAndDateByTable as $value) {
- if ($tableName == $value) {
- $isWarehouseId = true;
- for ($ii = $start; $ii <= $end; $ii++) {
- for ($i = 1; $i <= 4; $i++) {
- $tableSuffix[] = '_' . substr($ii, -2) . $i;
- }
- }
- break;
- }
- }
- //按照仓库id分表
- foreach ($warehouseTable as $value) {
- if ($tableName == $value) {
- $isWarehouseId = true;
- break;
- }
- }
- $stockDbName = 'stock.qianniao.vip';
- $objDGoods = new DGoods();
- if ($isWarehouseId) {
- //查询仓库的商铺
- $sql = 'select id from `' . $stockDbName . '`.`qianniao_warehouse_' . $enterpriseId . '`';
- $dbResult = $objDGoods->query($sql);
- $warehouseResult = $dbResult;
- unset($dbResult);
- //循环仓库数据
- foreach ($warehouseResult as $warehouse) {
- $warehouseId = $warehouse['id'];
- //判断是否有日期后缀
- if (!empty($tableSuffix)) {
- foreach ($tableSuffix as $suffix) {
- //拼接表名
- $tableList[] = $tableName . $enterpriseId . '_' . $warehouseId . $suffix;
- }
- } else {
- $tableList[] = $tableName . $enterpriseId . '_' . $warehouseId;
- }
- }
- } else {
- //判断是否有日期后缀
- if (!empty($tableSuffix)) {
- foreach ($tableSuffix as $suffix) {
- //拼接表名
- $tableList[] = $tableName . $enterpriseId . $suffix;
- }
- } else {
- $tableList[] = $tableName . $enterpriseId;
- }
- }
- if(empty($tableList)){
- return [$tableName];
- }
- $returnTable = [];
- foreach ($tableList as $table) {
- //查询表是否存在
- $sql = 'select count(TABLE_NAME) as isTable from information_schema.TABLES where TABLE_SCHEMA = "' . $dbName . '" and TABLE_NAME = "' . $table . '"';
- $dbResult = $objDGoods->query($sql);
- $isTable = 0;
- if (!empty($dbResult)) {
- if(isset($dbResult[0]['isTable']) && $dbResult[0]['isTable'] == 1){
- $isTable = 1;
- }
- }
- unset($dbResult);
- //判断表是否存在
- if ($isTable) {
- $returnTable[] = $table;
- }
- }
- return $returnTable;
- }
- public function removeEnterprise($enterpriseId)
- {
- //1.清空表
- $financeTables = [
- 'qianniao_account_detail_',
- 'qianniao_account_transfer_',
- 'qianniao_customer_balance_',
- 'qianniao_customer_balance_detail_',
- 'qianniao_customer_balance_detail_index_',
- 'qianniao_customer_balance_index_',
- 'qianniao_merchant_flow_',
- 'qianniao_paid_',
- 'qianniao_paid_account_',
- 'qianniao_paid_index_',
- 'qianniao_pay_receipt_',
- 'qianniao_pay_receipt_index_',
- 'qianniao_receive_receipt_',
- 'qianniao_receive_receipt_index_',
- 'qianniao_received_',
- 'qianniao_received_account_',
- 'qianniao_received_index_',
- 'qianniao_supplier_balance_',
- 'qianniao_supplier_balance_detail_',
- 'qianniao_supplier_balance_detail_index_',
- 'qianniao_supplier_balance_index_',
- ];
- $financeDbName = 'finance.qianniao.vip';
- $objDGoods = new DGoods();
- //因为分表规则不统一 规则太乱 清空表暂未启用
- // foreach($financeTables as $value){
- // $tables = self::formatTableName($value,$financeDbName,$enterpriseId);
- //// foreach($tables as $table){
- //// $sql = 'DELETE FROM ``.`'.$table.'`';
- //// $objDGoods->query($sql);
- //// }
- // }
- //修改表
- //customer表客户余额有2个字段需要清空
- //仓库表需要修改初始化状态
- foreach($financeTables as $value){
- $tables = self::formatTableName($value,$financeDbName,$enterpriseId);
- // foreach($tables as $table){
- // $sql = 'DELETE FROM ``.`'.$table.'`';
- // $objDGoods->query($sql);
- // }
- }
- //修改表
- //customer表客户余额有2个字段需要清空
- //2.清空es
- //订单es
- //退货单es
- //出库es
- //入库es
- //采购es
- //采购退货es
- //调拨es
- //盘点es
- //库存明细es
- $esIndexData = [
- // 'online_goods_search',
- // 'online_customer_search',
- // 'online_goods_basic_search',
- 'online_order_search',
- // 'online_shop_search',
- // 'online_login_log',
- // 'online_logs',
- 'online_inventory_in_search',
- // 'online_price_adjustment_sheet_search',
- // 'online_customer_price_adjustment_sheet_search',
- 'online_inventory_out_search',
- 'online_allocate_search',
- 'online_stocktaking_search',
- 'online_inventory_details_search',
- 'online_order_out_search',
- 'online_purchase_details_search',
- // 'online_coupon_search',
- // 'online_user_coupon_search',
- // 'online_activity_search',
- 'online_should_receive_receipt_search',
- 'online_should_pay_receipt_search',
- 'online_received_receipt_search',
- 'online_paid_receipt_search',
- 'online_inventory_batch_search',
- ];
- foreach($esIndexData as $value){
- $apiUrl = 'http://49.235.209.106:9200/'.$value.'/_delete_by_query';
- $curl_option = [
- CURLOPT_HTTPHEADER => [
- 'Content-Type: application/json',
- ],
- ];
- $postData = [
- 'query' => [
- 'bool' => [
- 'must' => [
- [
- 'term' => [
- 'enterpriseId' => $enterpriseId,
- ]
- ],
- [
- 'range' => [
- 'createTime' => [
- 'lte' => strtotime(date('Y-m-d'))
- ]
- ]
- ]
- ]
- ]
- ]
- ];
- $result = request($apiUrl, json_encode($postData), 10, false, $curl_option);
- if ($result['httpcode'] != 200) {
- return ResultWrapper::fail('删除es报错:'.$result['content'], ErrorCode::$paramError);
- }
- $content = json_decode($result['content'], true);
- var_dump($content['total']);
- }
- //3.清空缓存
- //出入库统计缓存
- $objCacheStock = new Stock($enterpriseId);
- $cacheResult = $objCacheStock->deleteCacheStatistics('InventoryIn');
- if(!$cacheResult && $cacheResult !== 0){
- return ResultWrapper::fail('删除缓存失败', ErrorCode::$paramError);
- }
- $cacheResult = $objCacheStock->deleteCacheStatistics('InventoryOut');
- if(!$cacheResult && $cacheResult !== 0){
- return ResultWrapper::fail('删除缓存失败', ErrorCode::$paramError);
- }
- return ResultWrapper::success('执行成功');
- }
- /**
- * 复制库存表
- */
- public function cpInventoryTable()
- {
- $masterDbName = 'www.qianniao.vip';
- $stockDbName = 'stock.qianniao.vip';
- /* $objDReceiptTemplate = new DReceiptTemplate();
- $sql = 'select * from `'.$masterDbName.'`.`qianniao_receipt_template` where receiptType in(1,5)';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- foreach($dbResult as $value){
- if($value['enterpriseId'] == 4){
- continue;
- }
- $fieldsData = json_decode($value['fieldsData'], true);
- $fieldsData['templateInfo']['basic'][] = [
- "alias" => "printing",
- "fields" => "打印标识",
- "isShow" => 5
- ];
- $update = [
- 'fieldsData' => json_encode($fieldsData),
- ];
- $result = $objDReceiptTemplate->update($update, ['id' => $value['id']]);
- if($result === false){
- return ResultWrapper::fail($objDReceiptTemplate->error(), ErrorCode::$dberror);
- }
- var_dump($value['id'].'修改成功');
- }
- V(123);*/
- //查询所有企业
- $sql = 'select * from `'.$masterDbName.'`.`qianniao_enterprise_1` where deleteStatus = 5';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $enterprise = $dbResult;
- unset($dbResult);
- $objDLockingShop = new DLockingShop();
- foreach($enterprise as $value){
- $enterpriseId = $value['id'];
- // $sql = 'UPDATE `www.qianniao.vip`.`qianniao_order_'.$enterpriseId.'_1` SET `memberBalance` = 0.00';
- // $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;';
- //$sql = 'update `'.$masterDbName.'`.`qianniao_order_'.$enterpriseId.'_1` set salesManName = "" where salesManId = 0';
- // $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)';
- // $sql = 'update `'.$masterDbName.'`.`qianniao_goods_'.$enterpriseId.'` set merchantId = 0 where merchantId is null';
- // $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)';
- // $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)';
- // $sql = 'update `'.$masterDbName.'`.`qianniao_goods_basic_'.$enterpriseId.'` set merchantId = 0 where merchantId is null';
- // $this->objDInventory->query($sql);
- // var_dump($enterpriseId.'--执行成功');
- $start = 2020;
- $end = (int)date('Y', time());
- //按日期季度分表
- for ($ii = $start; $ii <= $end; $ii++) {
- for ($i = 1; $i <= 4; $i++) {
- $tableSuffix[] = '_' . $ii . '_' . $i;
- }
- }
- foreach ($tableSuffix as $tableKey){
- // $tableName = 'qianniao_supplier_balance_detail_'.$enterpriseId .$tableKey;
- // $sql = 'UPDATE `finance.qianniao.vip`.`'.$tableName.'` set shouldPayBalance=(case when shouldPayBalance>0 THEN 0-shouldPayBalance when shouldPayBalance<0 then abs(shouldPayBalance) else shouldPayBalance end)';
- $tableName = 'qianniao_pay_receipt_'.$enterpriseId .$tableKey;
- $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';
- $this->objDInventory->query($sql);
- var_dump('企业'.$enterpriseId.', 的表: --'.$tableName.'--执行成功');
- }
- }
- V('执行成功');
- foreach($enterprise as $value){
- $enterpriseId = $value['id'];
- if($enterpriseId == 4){
- continue;
- }
- $inventoryTableName = 'qianniao_inventory_'.$enterpriseId;
- //查询每个企业的库存表是否存在
- $sql = 'select count(TABLE_NAME) as isTable from information_schema.TABLES where TABLE_SCHEMA = "'.$stockDbName.'" and TABLE_NAME = "'.$inventoryTableName.'"';
- $dbResult = $this->objDInventory->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('查询表是否存在报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- $isTable = 0;
- if (!empty($dbResult)) {
- if(isset($dbResult[0]['isTable']) && $dbResult[0]['isTable'] == 1){
- $isTable = 1;
- }
- }
- unset($dbResult);
- //不存在退出
- if(!$isTable){
- continue;
- }
- //如果存在
- //重命名库存表为仓库库存表
- $inventoryWarehouseTableName = 'qianniao_inventory_warehouse_'.$enterpriseId;
- $sql = 'rename table `'.$stockDbName.'`.`'.$inventoryTableName.'` TO `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'`';
- $dbResult = $this->objDInventory->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('重命名报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- //创建库存表
- $sql = "DROP TABLE IF EXISTS `".$stockDbName."`.`".$inventoryTableName."`";
- $dbResult = $this->objDInventory->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('创建库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- $sql = "CREATE TABLE `".$stockDbName."`.`".$inventoryTableName."` (
- `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '自增id',
- `materielId` int(10) NOT NULL COMMENT '物料ID',
- `materielCode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '物料编码',
- `inventoryNum` decimal(20, 8) NOT NULL COMMENT '库存数',
- `lockInventory` decimal(20, 8) NOT NULL COMMENT '锁定库存数',
- `costPrice` decimal(15, 4) NULL DEFAULT NULL COMMENT '成本价',
- `skuId` int(10) NULL DEFAULT NULL COMMENT '单位id',
- `updateTime` int(10) NULL DEFAULT NULL COMMENT '修改时间',
- `createTime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
- PRIMARY KEY (`id`) USING BTREE,
- INDEX `shopIdAndMaterielId`(`materielId`, `skuId`) USING BTREE COMMENT '组合索引'
- ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '库存汇总表' ROW_FORMAT = Dynamic;";
- $dbResult = $this->objDInventory->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('创建库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- //查寻仓库库存表 按照skuId分组, 累加数量, 累加锁定
- $sql = 'select materielId,materielCode,skuId,sum(inventoryNum) as inventoryNum,sum(ifnull(lockInventory,0)) as lockInventory,createTime,updateTime from `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'` group by skuId';
- $dbResult = $this->objDInventory->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('查寻仓库库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- $inventoryData = $dbResult;
- unset($dbResult);
- //把数据压进库存表
- $this->objDInventory->set_Table($inventoryTableName);
- if(!empty($inventoryData)){
- $dbResult = $this->objDInventory->insert($inventoryData, true);
- if($dbResult === false){
- return ResultWrapper::fail('把数据压进库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- }
- $shopLockTableName = 'qianniao_locking_shop_'.$enterpriseId;
- //创建商铺锁定表
- $sql = "DROP TABLE IF EXISTS `".$stockDbName."`.`".$shopLockTableName."`";
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail('创建商铺锁定表:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- $sql = "CREATE TABLE `".$stockDbName."`.`".$shopLockTableName."` (
- `id` int(10) NOT NULL AUTO_INCREMENT COMMENT '自增id',
- `shopId` int(10) NULL DEFAULT NULL COMMENT '商铺id',
- `materielId` int(10) NOT NULL COMMENT '物料ID',
- `materielCode` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '物料编码',
- `lockInventory` decimal(20, 8) NOT NULL COMMENT '锁定库存数',
- `skuId` int(10) NULL DEFAULT NULL COMMENT '单位id',
- `updateTime` int(10) NULL DEFAULT NULL COMMENT '修改时间',
- `createTime` int(10) NULL DEFAULT NULL COMMENT '创建时间',
- PRIMARY KEY (`id`) USING BTREE,
- INDEX `shopIdAndMaterielId`(`materielId`, `skuId`, `shopId`) USING BTREE COMMENT '组合索引'
- ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci COMMENT = '商铺锁定表' ROW_FORMAT = Dynamic;";
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail('创建商铺锁定表:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- //查询每个企业有几个店铺
- $sql = 'select * from `'.$masterDbName.'`.`qianniao_shop_1` where enterpriseId = '.$enterpriseId.' and deleteStatus = 5';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail('查询商铺表:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- $shopResult = $dbResult;
- unset($dbResult);
- $shopAsWarehouseId = [];
- foreach($shopResult as $shopValue){
- $shopAsWarehouseId[$shopValue['warehouseId']] = $shopValue['id'];
- }
- //查询仓库库存表 按照skuId,仓库id分组, 累加数量, 累加锁定, 均价成本
- $sql = 'select materielId,materielCode,skuId,warehouseId,sum(lockInventory) as lockInventory,createTime,updateTime from `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'` group by skuId,warehouseId';
- $dbResult = $this->objDInventory->query($sql);
- if ($dbResult === false) {
- return ResultWrapper::fail('查寻仓库库存表报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- $inventoryLockData = $dbResult;
- unset($dbResult);
- //把数据压进商铺锁定表
- $inventoryLockInsert = [];
- foreach($inventoryLockData as $lockValue){
- if(isset($shopAsWarehouseId[$lockValue['warehouseId']])){
- $inventoryLockInsert[] = [
- 'shopId' => $shopAsWarehouseId[$lockValue['warehouseId']],
- 'materielId' => $lockValue['materielId'],
- 'materielCode' => $lockValue['materielCode'],
- 'lockInventory' => empty($lockValue['lockInventory']) ? 0 : $lockValue['lockInventory'],
- 'skuId' => $lockValue['skuId'],
- 'updateTime' => time(),
- 'createTime' => $lockValue['createTime']
- ];
- }
- }
- $objDLockingShop->set_Table($shopLockTableName);
- if(!empty($inventoryLockInsert)){
- $dbResult = $objDLockingShop->insert($inventoryLockInsert, true);
- if($dbResult === false){
- return ResultWrapper::fail($objDLockingShop->error(), ErrorCode::$dberror);
- }
- unset($dbResult);
- }
- //删除仓库库存表锁定字段
- $sql = 'ALTER TABLE `'.$stockDbName.'`.`'.$inventoryWarehouseTableName.'` DROP COLUMN `lockInventory`';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail('删除仓库库存表锁定字段报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- $sql = "ALTER TABLE `".$stockDbName."`.`".$inventoryWarehouseTableName."` ADD COLUMN `sort` int(10) NULL DEFAULT 0 COMMENT '排序' AFTER `skuId`;";
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail('修改仓库库存表排序字段报错:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- //修改出库商品表total字段
- $sql = 'update `'.$stockDbName.'`.`qianniao_inventory_out_details_'.$enterpriseId.'` set total = num where createTime < '.strtotime('2021-03-22 23:59:59');
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail('修改出库商品表total字段:'.$this->objDInventory->error(), ErrorCode::$dberror);
- }
- var_dump($enterpriseId.'修改成功');
- }
- return ResultWrapper::success('操作成功');
- }
- /**
- * 更新真实销量
- */
- // public function updateGoods()
- // {
- // $masterDbName = 'dev.qiannao.vip';
- // //查询所有企业
- // $sql = 'select * from `'.$masterDbName.'`.`qianniao_enterprise_1` where deleteStatus = 5';
- // $dbResult = $this->objDInventory->query($sql);
- // if($dbResult === false){
- // return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- // }
- // $enterprise = $dbResult;
- // $objDGoods = new DGoods();
- //
- // foreach($enterprise as $value){
- // $enterpriseId = $value['id'];
- //
- // //$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)';
- // $sql = 'select * from `' . $masterDbName . '`.`qianniao_goods_' . $enterpriseId . '`' ;
- //
- // $goodsDbResult = $objDGoods->query($sql);
- // foreach ($goodsDbResult as $goods){
- // $salesNumArray = isset($goods['salesNum']) && !empty($goods['salesNum']) ? json_decode($goods['salesNum'],true) : [];
- // $realSalesNum = 0;
- // $realSalesNum = array_sum(array_column($salesNumArray,'salesNum'));
- // var_dump('企业id'.$enterpriseId);
- // var_dump('销售数量'.$realSalesNum);
- // $updateSql = 'update `'.$masterDbName.'`.`qianniao_goods_' . $enterpriseId . '` set realSalesNum ='.$realSalesNum.' where id = '.$goods['id'];
- // $objDGoods->query($updateSql);
- // }
- // }
- // V('执行完成');
- //
- // }
- public function updateOrderCost()
- {
- $masterDbName = 'www.huabeihudie.com';
- $stockDbName = 'stock.huabeihudie.com';
- $tableArray = [
- 'qianniao_inventory_details_169_1_212',
- 'qianniao_inventory_details_169_2_212',
- 'qianniao_inventory_details_169_3_212',
- ];
- $i = 0;
- foreach($tableArray as $tableName){
- $sql = 'select * from `'.$stockDbName.'`.`'.$tableName.'` where createTime < 1617934818 and actionType = 4 and `source` = 5';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- foreach($dbResult as $value){
- $sql = 'update `'.$masterDbName.'`.`qianniao_order_goods_169_1` set outCostPrice = '.$value['averageCost'].' where orderId = '.$value['originId'].' and skuId = '.$value['skuId'];
- $result = $this->objDInventory->query($sql);
- if($result === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $i++;
- }
- }
- return ResultWrapper::success('共修复'.$i.'条订单商品数据');
- }
- /**
- * 平账脚本
- * ① 查出所有客户的money
- * ② 根据money字段进行创建收款单平账
- * ③ 创建完收款单后自动审核
- */
- public function balancingAccounts()
- {
- $objCustomer = new DCustomer();
- $objAccount = new DAccount();
- $objSupplier = new DSupplier();
- $masterDbName = 'www.qianniao.vip';
- $financeDbName = 'finance.qianniao.vip';
- $stockDbName = 'stock.qianniao.vip';
- //查询所有企业
- $sql = 'select * from `'.$masterDbName.'`.`qianniao_enterprise_1` where id = 48 and deleteStatus = 5';
- $dbResult = $this->objDInventory->query($sql);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDInventory->error(), ErrorCode::$dberror);
- }
- $enterprise = $dbResult;
- // 收款
- foreach ($enterprise as $enterpriseValue){
- $enterpriseId = $enterpriseValue['id'];
- $sql = 'select * from `' . $masterDbName . '`.`qianniao_customer_' . $enterpriseId . '` where money !=0 and money > 0' ;
- $customerDbResult = $objCustomer->query($sql);
- // 获取账户数据
- $account[0] = [];
- $accountSql = 'select * from `'.$financeDbName.'`.`qianniao_account` where deleteStatus = 5 and enableStatus = 5';
- $accountDate = $objAccount->query($accountSql);
- foreach ($customerDbResult as $customerKey => $customerValue){
- //组装account
- if($accountDate && !empty($accountDate)){
- $account[0] = [
- 'accountId' => $accountDate[0]['id'],
- 'accountNumber'=>$accountDate[0]['accountNumber'],
- 'accountName'=> $accountDate[0]['name'],
- 'money'=> $customerValue['money'],
- 'discountMoney'=> 0.00,
- 'finalMoney' => $customerValue['money'],
- 'payWay'=> '',
- 'remark'=>'收款单收款'
- ];
- }else{
- var_dump('企业'.$enterpriseId.'没有账户');
- continue;
- }
- //组装收款单数据
- $objMReceived = new MReceived($enterpriseId, $customerValue['userCenterId']);
- //创建收款单
- $received = [
- 'currentAccountName' => $enterpriseValue['contact'],
- 'financeType' => '销售收款',
- 'financeTypeId' => 2,
- 'shopId' => 0,
- 'shopName' => '',
- 'receiptTime' => time(),
- 'operatorId' => $customerValue['userCenterId'],
- 'accountList' => $account,
- 'customerId' => $customerValue['id'],
- 'customerName' => $customerValue['name'],
- 'sourceNo' => '',
- 'sourceNoMoney' => '',
- 'createTime' => time(),
- 'auditStatus' => StatusCode::$auditStatus['auditing'],
- 'updateTime' => time(),
- 'receiptOffsetData' => []
- ];
- $receivedResult = $objMReceived->addReceived($received);
- if (!$receivedResult->isSuccess()) {
- var_dump('新增收款单错误 data:'.$receivedResult->getData().'error'.$receivedResult->getErrorCode());
- continue;
- }
- //审核收款单
- $updateReceived = $objMReceived->updateReceivedStatus(['id'=>$receivedResult->getData(),'createTime'=>time()]);
- if (!$updateReceived->isSuccess()) {
- var_dump('审核收款单错误 data:'.$receivedResult->getData().'error'.$receivedResult->getErrorCode());
- continue;
- }
- }
- unset($accountSql);
- unset($accountDate);
- /* ----------------------------------------------------------付款-------------------------------------------------*/
- // $sql = 'select * from `' . $stockDbName . '`.`qianniao_supplier_' . $enterpriseId . '` where money !=0 and money > 0' ;
- // $supplierDbResult = $objSupplier->query($sql);
- // // 获取账户数据
- // $account[0] = [];
- // $accountSql = 'select * from `'.$financeDbName.'`.`qianniao_account` where deleteStatus = 5 and enableStatus = 5';
- // $accountDate = $objAccount->query($accountSql);
- //
- // foreach ($supplierDbResult as $supplierKey => $supplierValue){
- // //组装account
- // if($accountDate && !empty($accountDate)){
- // $account[0] = [
- // 'accountId' => $accountDate[0]['id'],
- // 'accountNumber'=>$accountDate[0]['accountNumber'],
- // 'accountName'=> $accountDate[0]['name'],
- // 'money'=> $supplierValue['money'],
- // 'discountMoney'=> 0.00,
- // 'finalMoney' => $supplierValue['money'],
- // 'payWay'=> '',
- // 'remark'=>'付款单付款'
- // ];
- // }else{
- // var_dump('企业'.$enterpriseId.'没有账户');
- // continue;
- // }
- // //组装收款单数据
- // $objMPaid = new MPaid($enterpriseId, $supplierValue['userCenterId']);
- // //创建收款单
- // $paid = [
- // 'currentAccountName' => $enterpriseValue['contact'],
- // 'supplierId' => $supplierValue['id'],
- // 'supplierName' => $supplierValue['title'],
- // 'sourceNo' => '',
- // 'sourceNoMoney' => 0,
- // 'financeType' => '采购预付',
- // 'financeTypeId' => 4,
- // 'shopId' => 0,
- // 'shopName' => '',
- // 'receiptTime' => time(),
- // 'accountList' => $account,
- // 'createTime' => time(),
- // 'auditStatus' => StatusCode::$auditStatus['auditing'],
- // 'updateTime' => time(),
- // 'receiptOffsetData' => []
- // ];
- // $paidResult = $objMPaid->addPaid($paid);
- // if (!$paidResult->isSuccess()) {
- // var_dump('新增付款单错误 data:'.$paidResult->getData().'error'.$paidResult->getErrorCode());
- // continue;
- // }
- // //审核收款单
- // $updatePaid = $objMPaid->updatePaidStatus(['id'=>$paidResult->getData(),'createTime'=>time()]);
- // if (!$updatePaid->isSuccess()) {
- // var_dump('审核付款单错误 data:'.$updatePaid->getData().'error'.$updatePaid->getErrorCode());
- // continue;
- // }
- // }
- }
- var_dump('收款执行完成');
- V('付款执行完成');
- }
- /**
- * 删除xhjc的商品属性
- * 2077
- */
- public function delXhjcGoods()
- {
- $objDSku = new DSku();
- $objDSku->setTable('qianniao_sku_63');
- $objDGoodsBasic = new DGoodsBasic();
- $objDGoodsBasic->setTable('qianniao_goods_basic_63');
- $goodsBasicIds = [];
- //去掉商户id 119 的数据
- $goodsBasicDate = $objDGoodsBasic->query('select * from qianniao_goods_basic_63 where merchantId != 119');
- if($goodsBasicDate === false){
- return ResultWrapper::fail($objDGoodsBasic->error(), ErrorCode::$dberror);
- }
- foreach ($goodsBasicDate as $basicKey => $basicValue){
- array_push($goodsBasicIds,$basicValue['id']);
- }
- $md5Key = [];
- $dbResult = $objDSku->select(['specType'=>2,'goodsId' => $goodsBasicIds]);
- if($dbResult === false){
- return ResultWrapper::fail($objDSku->error(), ErrorCode::$dberror);
- }
- foreach ($dbResult as $key => $value){
- if( in_array(md5($value['goodsId'].'+'.$value['unitId']),$md5Key) ){
- echo md5($value['goodsId'].'+'.$value['unitId']).'存在'."\n";
- // 删除该条商品
- $objDSku->delete(['id'=>$value['id']]);
- }else{
- echo '需要修改的'."\n";
- $update = $objDSku->update(['specType'=>1,'specData'=>'[]','specGroupHash'=>''],['id'=>$value['id']]);
- if($update === false){
- return ResultWrapper::fail($objDSku->error(), ErrorCode::$dberror);
- }
- }
- array_push($md5Key,md5($value['goodsId'].'+'.$value['unitId'] ));
- }
- V($md5Key);
- }
- }
|