<?php
/**
 * 客户标签管理模块
 * Created by PhpStorm.
 * User: kyl
 * Date: 2021/03/05
 */

namespace JinDouYun\Model\Customer;

use JinDouYun\Dao\Customer\DCustomer;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Model\MBaseModel;

use JinDouYun\Dao\Customer\DCustomerTagLib;

class MCustomerTagLib extends MBaseModel
{
    
    private $objDCustomerTagLib;
    private $objDCustomer;
    private $enterpriseId;
    private $userCenterId;
    
    
    public function __construct($enterpriseId, $userCenterId)
    {
        $this->enterpriseId = $enterpriseId;
        $this->userCenterId = $userCenterId;
        parent::__construct($this->enterpriseId, $this->userCenterId);
        
        $this->objDCustomerTagLib = new DCustomerTagLib('default');
        $this->objDCustomer = new DCustomer('default');
        $this->objDCustomer->setTable('qianniao_customer_'.$enterpriseId);
        
    }
    
    /**
     * 添加客户标签
     * @param $params
     * @return ResultWrapper
     * @throws Exception
     */
    public function addCustomerTagLib($params)
    {
        $dbResult = $this->objDCustomerTagLib->get(['name' => $params['name']]);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
        }
        if(!empty($dbResult)){
            return ResultWrapper::fail('该标签组已存在', ErrorCode::$paramError);
        }
        unset($dbResult);
    
        $tags = $params['tags'];
        unset($params['tags']);
        
        $beginTransactionStatus = $this->objDCustomerTagLib->beginTransaction();
        $customerTagLibId = $this->objDCustomerTagLib->insert($params);
        if($customerTagLibId === false){
            return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
        }
    
        $tmpCustomerTags = [];
        foreach ($tags as $key => $value){
            $tmpCustomerTags[] = [
                'enterpriseId' => $this->enterpriseId,
                'name' => $value,
                'pid'  => $customerTagLibId,
                'createTime' => time(),
                'updateTime' => time(),
            ];
        }
        $dbResult = $this->objDCustomerTagLib->insert($tmpCustomerTags, true);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
        }
        if($beginTransactionStatus){
            $this->objDCustomerTagLib->commit();
        }
        return ResultWrapper::success($customerTagLibId);
    }
    
    /**
     * 获取指定客户标签
     */
    public function getCustomerTagLibInfo($customerTagLibId)
    {
        $where = 'id = '.$customerTagLibId .' or pid = '.$customerTagLibId.' and deleteStatus = '.StatusCode::$standard;
        $dbResult = $this->objDCustomerTagLib->select($where,'*');
        
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success( arr2tree($dbResult) );
        }
    }
    
    /**
     * 编辑客户标签
     *
     * @param int|array $params 修改客户标签的数据
     *
     * @return ResultWrapper
     */
    public function editCustomerTagLib($params)
    {
        // 区分是修改标签组还是新增标签
        if( isset($params['update']) && !empty($params['update']) ){
            foreach ($params['update'] as $key=>$value){
                $id = $value['id'];
                unset($value['id']);
                $params['update'][$key]['updateTime'] = time();
                $dbResult = $this->objDCustomerTagLib->update($value, $id);
            }
        }
        
        if( isset($params['add']) && !empty($params['add'])){
            foreach ($params['add'] as $key => $value){
                $params['add'][$key]['enterpriseId'] = $this->enterpriseId;
                $params['add'][$key]['updateTime']  = time();
            }
            $dbResult = $this->objDCustomerTagLib->insert($params['add'], true);
        }
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    /**
     * 删除客户标签
     *
     * @param array $params  要删除的客户标签
     *
     * @return ResultWrapper
     */
    public function delCustomerTagLib($params)
    {
        // 区分是删除标签组还是删除标签值
        if( isset($params['pid']) && !empty($params['pid']) ){
            $where = 'id = '.$params['id'] .' or pid = '.$params['id'].' and deleteStatus = '.StatusCode::$standard;
        }else{
            $where = 'id = '.$params['id'] .' and deleteStatus = '.StatusCode::$standard;
        }
        
        $dbResult = $this->objDCustomerTagLib->update( ['deleteStatus'=>StatusCode::$delete], $where );
        
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
    
    /**
     * 获取所有客户标签数据
     *
     * @param array $selectParams 过滤条件
     *
     * @return ResultWrapper
     */
    public function getAllCustomerTagLib()
    {
        $selectParams['deleteStatus'] = StatusCode::$standard;
        $dbResult = $this->objDCustomerTagLib->select('', '*', 'createTime desc');
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCustomerTagLib->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success($dbResult?arr2tree($dbResult):[]);
    }
    
    /**
     * 编辑客户指定标签
     *
     * @param int|array $params 修改客户指定标签的数据
     *
     * @return ResultWrapper
     */
    public function updateCustomerTagLibById($updateCustomerLibData)
    {
        $customerid = $updateCustomerLibData['id'];
        unset($updateCustomerLibData['id']);
        $updateCustomerLibData['taglib'] = json_encode($updateCustomerLibData['taglib']);
     
        $dbResult = $this->objDCustomer->update($updateCustomerLibData,$customerid);
        if($dbResult === false){
            return ResultWrapper::fail($this->objDCustomer->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($dbResult);
        }
    }
}