<?php
/**
 * 消耗品
 * Created by PhpStorm.
 * User: wxj
 * Date: 2019/10/31
 * Time: 15:02
 */

namespace JinDouYun\Model\Stock;

use Exception;


use JinDouYun\Dao\Department\DStaff;
use JinDouYun\Dao\Stock\DConsume;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ResultWrapper;

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


class MConsume extends MBaseModel
{

    private $obj;
    private $cutTable = 250000;
    private $enterpriseId;
    private $userCenterId;
    private $DCardBind;
    private $DCard;

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

    }

    /**
     * 列表
     * @param $where
     * @return mixed
     */
    public function list($where)
    {
        $limit = $where['limit'];
        unset($where['limit']);
        $offset = $where['offset'];
        unset($where['offset']);
        $wherein['a.enterpriseId'] = $this->enterpriseId;

        if (isset($where['shopId'])) {
            $wherein['a.shopId'] = $where['shopId'];
        }

        if (isset($where['status'])) {
            $wherein['a.status'] = $where['status'];
        }

        if (isset($where['mobile'])) {
            $wherein['u.mobile'] = $where['mobile'];
        }

        if (isset($where['write_mobile'])) {
            $wherein['c.mobile'] = $where['write_mobile'];
        }

        if (isset($where['name'])){
            $wherein[] = ['a.name', 'like', '%'.$where['name'].'%'];
        }


        if (isset($where['start_time']))
        {
            $wherein[] = ['a.createTime', '>=', strtotime($where['start_time'])];
        }

        if (isset($where['end_time']))
        {
            $wherein[] = ['a.createTime', '<=', strtotime($where['end_time'])];
        }


        $join = 'Left Join qianniao_shop_1 as b on a.shopId = b.id';
        $join .= ' Left Join qianniao_user_center as u on a.userCenterId = u.id';
        $join .= ' Left Join qianniao_user_center as c on a.write = c.id';
//        $join .= ' Left Join qianniao_user_bind_enterprise as d on d.userCenterId = u.id';
//        $join .= ' Left Join qianniao_user_bind_enterprise as e on e.userCenterId = c.id';

        $dbResult = $this->obj->select($wherein, 'a.*,b.name as shop_name,u.mobile,c.mobile as write_mobile', 'id DESC', $limit, $offset,  array(), true, false,$join);
        $count = $this->obj->count($wherein, array(), $join);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }
        $staff = new DStaff('default');
        $staff->setTable('qianniao_staff_'.$this->enterpriseId);
        foreach ($dbResult as &$item)
        {
            $item['write_off_person'] = '';
            $item['transferor'] = '';
            if ($item['userCenterId']){
                $staffs = $staff->get(['userCenterId' => $item['userCenterId']]);
                if ($staffs){
                    $item['transferor'] = $staffs['staffName'];
                }else{
                    $item['transferor'] = '总账号';
                }


            }
            if ($item['write']){
                $staffs = $staff->get(['userCenterId' => $item['write']]);
                if ($staffs){
                    $item['write_off_person'] = $staffs['staffName'];
                }else {
                    $item['write_off_person'] = '总账号';
                }
            }
        }


        $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)
    {
        if (empty($where)) {
            return ResultWrapper::success($where);
        }
        $dbResult = $this->obj->get($where);
        if ($dbResult === false) {
            return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
        }

        return ResultWrapper::success(self::formatInfo($dbResult));

    }

    /**
     * Doc: (des="添加")
     * User: XMing
     * Date: 2020/7/15
     * Time: 10:42 上午
     * @param array $params
     * @return ResultWrapper
     * @throws Exception
     */
    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($where)
    {
        $res = $this->obj->delete($where);
        if ($res) {
            return ResultWrapper::success('删除成功');
        }
        return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);
    }

}