<?php
defined('IN_WEB') or die('Include Error!');
/**
 * 商品相关
 * Created by PhpStorm.
 * User: lqx
 * Date: 2017/12/15 0015
 * Time: 15:11
 */
class Paymentgoods
{
        private $goodsTable;

        public function __construct()
        {
            $this->goodsTable = otable::goods();
        }

        /**
         * 获取商品列表
         * @param $param
         */
        public function getGoodsList($param)
        {
            $gpid = oo::functions()->escape($param['gpid']);
            $gname = oo::functions()->escape($param['gname']);
            $page = oo::functions()->uint($param['page']) ?? 0;
            $limit = oo::functions()->uint($param['limit']);
            $limit = empty($limit) ? 10 : $limit;
            $where = "WHERE 1=1";
            if(!empty($gpid)) {
                $where .= " AND gpid like '%{$gpid}%' ";
            }
            if(!empty($gname)) {
                $where .= " AND gname like '%{$gname}%' ";
            }

            $sql = " SELECT * FROM {$this->goodsTable} {$where} ORDER BY gsort DESC, gid DESC LIMIT {$page}, {$limit} ";
            $list = oo::commonOprDb('common')->getAll($sql, 1);

            $sqlTotal = " SELECT count(1) as total FROM {$this->goodsTable} {$where}";
            $listTotal = oo::commonOprDb('common')->getOne($sqlTotal, 1);

            $data['list'] = $list;
            $data['total'] = $listTotal['total'];
            return json_encode($data);
        }

    /**
     * 通过商品ID查找商品信息
     * @param $param
     */
        public function getGoodsByID($param)
        {
            $gid = oo::functions()->uint($param['gid']);//通过gid查找
            if(empty($gid)) {
                return json_encode(['code'=>-1]);
            }
            $sql = " SELECT * FROM {$this->goodsTable} WHERE gid = {$gid}";
            $ret = oo::commonOprDb('common')->getOne($sql, 1);
            return json_encode(['code'=>0, 'data'=>$ret]);
        }


    /**
     * 商品添加
     * @param $param
     */
        public function addGoods($param)
        {
            $data['gimg'] = oo::functions()->escape($param['gimg']);
            $data['gpid'] = oo::functions()->escape($param['gpid']);
            $data['gprice'] = floatval($param['gprice']);
            $data['gpriceThb'] = oo::functions()->uint($param['gpriceThb']);
            $data['gname'] = oo::functions()->escape($param['gname']);
            $data['gdesc'] = oo::functions()->escape($param['gdesc']);
            $data['gbaseReward'] = oo::functions()->escape($param['gbaseReward']);
            $data['gtype'] = oo::functions()->uint($param['gtype']);
            $data['gaddPro'] = floatval($param['gaddPro']);
            $data['gactivityReward'] =  oo::functions()->escape($param['gactivityReward']);
            $data['gdiscount'] = floatval($param['gdiscount']);
            $data['gchannel']= oo::functions()->uint($param['gchannel']);
            $data['gsort'] =  oo::functions()->uint($param['gsort']);
            $data['gchannel_sort'] = oo::functions()->uint($param['gchannel_sort']);
            $data['gstatus'] = oo::functions()->uint($param['gstatus']);
            $data['gattribute'] = oo::functions()->uint($param['gattribute']);
            $data['ggameid'] = oo::functions()->uint($param['ggameid']);
            $data['gplatform'] = oo::functions()->uint($param['gplatform']);

            $goodsTable = otable::goods();
            $kgoods = implode(',', array_keys($data));
            $valueGoods = implode("','", $data);
            $valueGoods = "'".$valueGoods."'";
            //如果goodsList存在，则先删除
            $goodsKey = okeys::goodsList();
            $goodsList = oo::commonOprRedis('common')->get($goodsKey);
            if(!empty($goodsList)) {
                oo::commonOprRedis('common')->delete($goodsKey);
            }
            try{
                $sql = " INSERT INTO {$goodsTable} ({$kgoods}) VALUES ($valueGoods)";
                $num = oo::commonOprDb('common')->query($sql);
                return json_encode(['code'=>0]);
            }catch (Exception $e) {
                oo::logs()->debug3("addGoods:".$e->getMessage(), 'PaymentGoods.php');
                return json_encode(['code'=>-1]);
            }
        }

    /**
     * 商品修改
     * @param $param
     */
        public function editGoods($param)
        {
            $gid = oo::functions()->uint($param['gid']);
            if(empty($gid)) {
                return json_encode(['code'=>-1]);
            }
            $data['gimg'] = oo::functions()->escape($param['gimg']);
            $data['gpid'] = oo::functions()->escape($param['gpid']);
            $data['gprice'] = floatval($param['gprice']);
            $data['gpriceThb'] = oo::functions()->uint($param['gpriceThb']);
            $data['gname'] = oo::functions()->escape($param['gname']);
            $data['gdesc'] = oo::functions()->escape($param['gdesc']);
            $data['gbaseReward'] = oo::functions()->escape($param['gbaseReward']);
            $data['gtype'] = oo::functions()->uint($param['gtype']);
            $data['gaddPro'] = floatval($param['gaddPro']);
            $data['gactivityReward'] =  oo::functions()->escape($param['gactivityReward']);
            $data['gdiscount'] = floatval($param['gdiscount']);
            $data['gchannel']= oo::functions()->uint($param['gchannel']);
            $data['gsort'] =  oo::functions()->uint($param['gsort']);
            $data['gchannel_sort'] = oo::functions()->uint($param['gchannel_sort']);
            $data['gstatus'] = oo::functions()->uint($param['gstatus']);
            $data['gattribute'] = oo::functions()->uint($param['gattribute']);
            $data['ggameid'] = oo::functions()->uint($param['ggameid']);
            $data['gplatform'] = oo::functions()->uint($param['gplatform']);

            $goodsTable = otable::goods();
            $setStr = '';
            foreach ($data as $k=>$v) {
                $setStr .= $k ."= '".$v."',";
            }
            $setStr = rtrim($setStr, ',');

            //如果goodsList存在，则先删除
            $goodsKey = okeys::goodsList();
            $goodsList = oo::commonOprRedis('common')->get($goodsKey);
            if(!empty($goodsList)) {
                oo::commonOprRedis('common')->delete($goodsKey);
            }
            try{
                $sql = " UPDATE {$goodsTable} SET $setStr WHERE gid = {$gid} LIMIT 1 ";
                $num = oo::commonOprDb('common')->query($sql);
                return json_encode(['code'=>0]);
            }catch (Exception $e) {
                oo::logs()->debug3("editGoods:".$e->getMessage(), 'PaymentGoods.php');
                return json_encode(['code'=>-1]);
            }
        }
}
