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

namespace JinDouYun\Model\Shop;

use Exception;

use JinDouYun\Cache\SystemCache;
use JinDouYun\Controller\Common\Logger;
use JinDouYun\Dao\Department\DStaff;
use JinDouYun\Dao\GoodsManage\DGoodsBasic;
use JinDouYun\Dao\Shop\DShopProject;
use JinDouYun\Dao\UserCenter\DUserCenter;
use JinDouYun\Model\Common\Location;
use JinDouYun\Model\System\MPaymentSetting;
use Mall\Framework\Core\ErrorCode;
use Mall\Framework\Core\StatusCode;
use Mall\Framework\Core\ResultWrapper;

use JinDouYun\Dao\Shop\DShop;
use JinDouYun\Dao\Shop\DEnterpriseBindShop;

use Jindouyun\Cache\EnterpriseCache;
use JinDouYun\Cache\RoleAclCache;
use JinDouYun\Cache\ShopCache;
use JinDouYun\Cache\SysAreaChinaCache;
use JinDouYun\Cache\GoodsBasicRelevant;

use JinDouYun\Model\ShopPartner\MShopPartner;
use JinDouYun\Model\Stock\MWarehouse;
use JinDouYun\Model\SysAreaChina\MSysAreaChina;
use JinDouYun\Model\Customer\MCustomer;
use JinDouYun\Model\Enterprise\MEnterprise;
use JinDouYun\Model\Department\MStaff;
use JinDouYun\Model\MBaseModel;
use Monolog\Handler\IFTTTHandler;
use Util\WeiXin\Pay;


class MShopProject extends MBaseModel
{

    private $objDShopProject;
    private $cutTable = 250000;
    private $enterpriseId;
    private $userCenterId;
    private $objMShopPartner;
    private $objMStaff;

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

    }

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

        $file = ' a.* ';
        $sql = 'SELECT'.$file.'FROM qianniao_shop_project AS a 
                WHERE enterprise_id = '. $this->enterpriseId;

        if(isset($where['name'])) {
            $sql .= ' AND a.name LIKE "%' . $where['name'] . '%"';
        }

        if(isset($where['is_show'])) {
            $sql .= ' AND a.is_show = '.$where['is_show'];
        }

        if(isset($where['shop_id'])) {
            $sql .= ' AND a.shop_id = '.$where['shop_id'];
        }

        $total = count((array) $this->objDShopProject->query($sql));

        $sql .= ' ORDER BY createTime DESC LIMiT '.$offset.','.$limit;

        $dbResult = $this->objDShopProject->query($sql);

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

        $return = [
            'data' => $dbResult,
            'total' => ($total) ? intval($total) : 0,
        ];

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

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

    }

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

    }

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

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

    }


    function select($where = null, $fields = '*', $order = null, $limit = null, $offset = null, $data = array(), $multiple = true, $isExport = false)
    {
        $data = $this->objDShopProject->select($where, $fields, $order, $limit, $offset, $data, $multiple, $isExport);
        if ($data){
            return $data;
        }
        return ResultWrapper::fail($this->obj->error(), ErrorCode::$dberror);

    }


    /**
     * 格式数据详情
     * @param $data
     * @return mixed
     */
    public function formatInfo($data)
    {
        $data['slider_image'] = json_decode($data['slider_image']);
        $data['result'] = html_entity_decode($data['result']);
        return $data;
    }


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


    public function get($where)
    {
        $data = $this->objDShopProject->get($where);

        return $data;
    }

}