<?php
/**
 * 商铺管理模块
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/10/31
 * Time: 15:02
 */

namespace JinDouYun\Model\Customer;

use Exception;

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


use JinDouYun\Model\MBaseModel;
use Monolog\Handler\IFTTTHandler;


class MCustomerCard extends MBaseModel
{

    private $obj;
    private $cutTable = 250000;
    private $enterpriseId;
    private $userCenterId;
    private $DCardNum;

    public function __construct($enterpriseId, $userCenterId = '')
    {
        $this->enterpriseId = $enterpriseId;
        $this->userCenterId = $userCenterId;
        parent::__construct($this->enterpriseId, $this->userCenterId);
        $this->obj = new DCustomerCard('default');
        $this->DCardNum = new DCustomerCardNum('default');

    }

    /**
     * 列表
     * @param $where
     * @return mixed
     */
    public function list($where)
    {
        $limit = $where['limit'];
        unset($where['limit']);
        $offset = $where['offset'];
        unset($where['offset']);


        $join = 'Left Join qianniao_customer_'.$this->enterpriseId.' AS b on a.customer_id = b.id';
        $join .= ' Left Join qianniao_shop_card AS c on a.card_id = c.id';
        $wherein[] = ['a.enterprise_id' ,'=' ,$this->enterpriseId];
        $dbResult = $this->obj->select($wherein, 'a.*,b.name as user_name,c.name', 'id DESC', $limit, $offset, array(), true, false,$join);
        $count = $this->obj->count($wherein);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }

        $return = [
            'data' => $dbResult,
            'total' => $count,
        ];

        if($return === false){
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($return);
        }
    }


    /**
     * 详情
     * @param $where
     * @return mixed
     */
    public function details($where)
    {
        $limit = $where['limit'];
        unset($where['limit']);
        $offset = $where['offset'];
        unset($where['offset']);


        $join = ' Left Join qianniao_shop_project AS b on a.project_id = b.id';
        $wherein[] = ['a.customer_card' ,'=' ,$where['id']];
        $dbResult = $this->DCardNum->select($wherein, 'a.*,b.name', 'id DESC', $limit, $offset, array(), true, false,$join);

        $count = $this->DCardNum->count($wherein);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }

        $return = [
            'data' => $dbResult,
            'total' => $count,
        ];

        if($return === false){
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }else{
            return ResultWrapper::success($return);
        }

    }

    /**
     * Doc: (des="添加")
     * User: XMing
     * Date: 2020/7/15
     * Time: 10:42 上午
     * @throws Exception
     * @param array $params
     * @return ResultWrapper
     */
    public function insert(array $params)
    {
        $res = $this->obj->insert($params);
        if ($res){
            return ResultWrapper::success('添加成功');
        }else{
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }

    }

    public function update($params, $id)
    {
        $data = $this->obj->get($id);
        if (!$data){
            return ResultWrapper::fail('数据不存在', ErrorCode::$dberror);
        }

        $dbResult = $this->obj->update($params,$id);
        if($dbResult === false){
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }
        return ResultWrapper::success('修改成功');

    }


    /**
     * 格式数据详情
     * @param $data
     * @return mixed
     */
    public function formatInfo($data)
    {
        return $data;
    }


    public function delete($id)
    {
        $res = $this->obj->delete(['id' => $id]);
        if ($res){
            return ResultWrapper::success('删除成功');
        }
        return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
    }

}