<?php

/**
 * @link      http://www.u-bo.com
 * @copyright 南京友博网络科技有限公司 
 * @license   http://www.u-bo.com/license/
 */

namespace home\modules\statement\models;

use Yii;
use home\modules\customer\models\Customer;

/**
 * This is the model class for table "qc_statement_link".
 *
 * 进账单关联
 *
 * @property integer $statement_id  (进账单ID)
 * @property string $link_type  (关联类型)
 * @property string $link_id  (关联信息ID)
 * @property integer $create_time  (关联时间)
 * @property integer $user_id  (提交人)
 * @property string $ip  (IP)
 *
 * @author HuLimin http://blog.u-bo.com
 * @since 0.1
 */
class StatementLink extends \yii\db\ActiveRecord
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%statement_link}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['statement_id'], 'required'],
            [['statement_id', 'create_time', 'user_id'], 'integer'],
            [['link_type', 'link_id'], 'string', 'max' => 100],
            [['create_time'], 'default', 'value' => time()],
            [['ip'], 'string', 'max' => 15]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'statement_id' => '进账单ID',
            'link_type' => '关联类型',
            'link_id' => '关联信息ID',
            'create_time' => '关联时间',
            'user_id' => '提交人',
            'ip' => 'IP',
        ];
    }

    /**
     * 前置操作
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $this->user_id = Yii::$app->user->identity->user_id;
            $this->ip = Yii::$app->getRequest()->getUserIP();
            return true;
        }
    }

    public function beforeDelete()
    {
        if(parent::beforeDelete()){
            $linkContracts = StatementLinkContract::find()->where(['statement_id' => $this->statement_id])->all();
            $result = true;
            if(!empty($linkContracts)){
                $transaction = \Yii::$app->db->beginTransaction();
                foreach ($linkContracts as $key => $contract) {
                    $statementLinkContract = StatementLinkContract::findOne($contract->link_contract_id);
                    if(!$statementLinkContract->delete()){
                        $result = false;
                    }
                }
                if($result){
                    $transaction->commit();
                    return true;
                } else {
                    $transaction->rollback();
                    return false;
                }
            }
            return true;
        }
    }

    /**
     * 获取关联信息
     * 
     * @param integer $statementId 进账单ID
     * @param string $linkType 关联类型
     * @param string $linkId 关联主键
     * @param string $field 查询字段
     * @param boolean $column 
     */
    public static function getLinkInfo($statementId = 0, $linkType = null, $linkId = null, $field = "*", $column = false)
    {
        if(empty($statementId) && empty($linkType) && empty($linkId)){
            return;
        }
        $info = [];
        $info = static::find()->select($field)->asArray();
        if(!empty($statementId)){
            $info = $info->andWhere(['statement_id' => $statementId]);
        }
        if(!empty($linkType)){
            $info = $info->andWhere(['link_type' => $linkType]);
        }
        if(!empty($linkId)){
            $info = $info->andWhere(['link_id' => $linkId]);
        }
        if($column){
            $info = $info->column();
        } else {
            $info = $info->all();
        }
        return $info;
    }

    public static function getStatementIdByCustomer($customerName)
    {
        $customerNo = Customer::find()
                ->where(['like', 'name', $customerName])
                ->select('customer_no')->asArray()
                ->column();
        $statementIds = static::find()
                ->where(['link_type' => 'customer', 'link_id' => $customerNo])
                ->select('statement_id')
                ->asArray()
                ->column();
        return $statementIds;
    }

}
