<?php
/**
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/11/14
 * Time: 15:45
 */

namespace JinDouYun\Controller\Cron;

use JinDouYun\Cache\FinanceCache;

use JinDouYun\Model\Finance\MReceive;
use JinDouYun\Model\Finance\MReceived;
use JinDouYun\Model\Finance\MPay;

use JinDouYun\Dao\Finance\DReceivedReceiptCheckQueue;

class FinanceCron
{

    private $objFinanceCache;
    private $objMReceive;
    private $objMReceived;
    private $objDReceivedReceiptCheckQueue;

    public function __construct()
    {
        $this->objFinanceCache = new FinanceCache();
        $this->objDReceivedReceiptCheckQueue = new DReceivedReceiptCheckQueue('finance');
    }

    // 自动审核应付单
    public function autoAuditPayReceipt()
    {
        $result = $this->objFinanceCache->getExistPayEnterprise();
        if(!$result){
            echo '没有企业下有应付单需要去审核'.PHP_EOL;
            return false;
        }

        // 每个企业去单独处理
        foreach ($result as $key => $value){
            // 查询指定企业下所有未审核的应付单
            $objMPay  =new MPay($value, 0);
            $payReceiptData = $objMPay->getAllAuditingPay();
            if(!$payReceiptData->isSuccess()){
                echo $payReceiptData->getData().PHP_EOL;
                continue;
            }
            $payReceiptData = $payReceiptData->getData();

            foreach ($payReceiptData as $k => $v){
                $params = [
                    'createTime' => $v['createTime'],
                    'id'         => $v['payReceiptId'],
                ];
                $updateResult = $objMPay->updatePayStatus($params);
                if(!$updateResult->isSuccess()){
                    echo '应付审核失败：原因是---'.$updateResult->getData().PHP_EOL;
                    continue;
                }
            }

            // 处理完一个企业删除一个企业
            $this->objFinanceCache->delExistPayEnterprise($value);
            unset($params);
        }

    }

    //自动审核销售出库应收单
    public function ReviewReceiveReceipt()
    {
        $result = $this->objFinanceCache->getAllSalesOutReceive();
        if (!$result) {
            echo "没有需要审核的应收单据". PHP_EOL;;
            return false;
        }

        foreach ($result as $key => $value) {
            $this->objMReceive = new MReceive($key, false);
            foreach ($value as $params) {
                $dbResult = $this->objMReceive->updateReceiveStatus($params);
                if ($dbResult->isSuccess()) {
                    $this->objFinanceCache->delSalesOutReceive($key, $params['id']);
                    echo "应收单审核成功 -- id: ". $params['id'] . ', 所属表: '. date('Y', $params['createTime']) . '_' . ceil(date('m', $params['createTime']) / 3);
                } else {
                    echo "应收单审核失败，失败原因：" . $dbResult->getData();
                }
            }
        }
    }

    //自动审核收款单队列中的数据，并删除记录
    public function AutoCheckReceivedReceipt() {
        $dbResult = $this->objDReceivedReceiptCheckQueue->select();

        if($dbResult === false){
            echo "收款单审核错误，原因：" . $this->objDReceivedReceiptCheckQueue->error();die;
        }

        if(empty($dbResult)) {
            echo "暂无需要审核的收款单". PHP_EOL;;die;
        }

        foreach ($dbResult as $record) {
            $this->objMReceived = new MReceived($record['enterpriseId'], false);
            $params = [
                'no'=>$record['receivedNo'],
                'createTime'=>$record['createTime'],
            ];
            $updateResult = $this->objMReceived->updateReceivedStatus($params);
            if($updateResult->isSuccess() == false) {
                echo "企业".$record['enterpriseId']."下的收款单".$record['receivedNo']."审核失败001，原因：" . $updateResult->getData();
                if($updateResult->getData()=="当前收款单已经审核过了"){
                    echo "正在删除失效队列记录";
                    $result = $this->objDReceivedReceiptCheckQueue->delete($record['id']);
                    if($result === false){
                    echo "队列记录删除失败，原因:".$this->objDReceivedReceiptCheckQueue->error();
                    } else {
                        echo "队列记录删除成功";
                    }
                }
            } else {
                //审核成功后删除队列数据
                echo "企业".$record['enterpriseId']."下的收款单".$record['receivedNo']."审核成功，正在删除队列记录". PHP_EOL;;
                $result = $this->objDReceivedReceiptCheckQueue->delete($record['id']);
                if($result === false){
                    echo "队列记录删除失败，原因:".$this->objDReceivedReceiptCheckQueue->error();
                } else {
                    echo "队列记录删除成功";
                }
            }
            echo PHP_EOL;
        }
    }
}