mall.go 3.39 KB
package models

import (
	"database/sql"
	"errors"
	//"fmt"

	//"icesimba.mall/utils"

	db "client/icesimba.mall/database"
)

type Mall struct {
	Id     string
	Code   string
	Desc   string
	Name   string
	Icon   string
	GameId string
}

type MallOut struct {
	Id   string `json:"id"`
	Desc string `json:"desc"`
	Name string `json:"name"`
	Icon string `json:"icon"`
}

type Malls struct {
	Mallout []MallOut `json:"malls"`
}

type mallDb struct {
	MallId   sql.NullString
	MallDesc sql.NullString
	MallName sql.NullString
	MallIcon sql.NullString
}

func (in *Mall) QueryMallsByGameId() (out Malls, err error) {
	if in.GameId == "" {
		err = errors.New("参数错误")
		return
	}
	sqlQu := "select code,description,name,icon from ice_mall where gameid=?"
	rows, err := db.SqlDB.Query(sqlQu, in.GameId)
	if err != nil {
		err = nil
		return
	}
	for rows.Next() {
		malldb := mallDb{}
		mall := MallOut{}
		err = rows.Scan(
			&malldb.MallId,
			&malldb.MallDesc,
			&malldb.MallName,
			&malldb.MallIcon,
		)
		if err != nil {
			continue
		}
		if malldb.MallId.Valid {
			mall.Id = malldb.MallId.String
		}
		if malldb.MallDesc.Valid {
			mall.Desc = malldb.MallDesc.String
		}
		if malldb.MallName.Valid {
			mall.Name = malldb.MallName.String
		}
		if malldb.MallIcon.Valid {
			mall.Icon = malldb.MallIcon.String
		}
		out.Mallout = append(out.Mallout, mall)
	}
	return
}

func (in *Mall) QueryMallByMallCode() (code int, out Mall, err error) {
	if in.Code == "" {
		code = 400
		err = errors.New("参数错误")
		return
	}
	sqlQu := "select code,description,name,icon from ice_mall where code=? and gameid=?"
	row := db.SqlDB.QueryRow(sqlQu, in.Code, in.GameId)
	malldb := mallDb{}
	err = row.Scan(
		&malldb.MallId,
		&malldb.MallDesc,
		&malldb.MallName,
		&malldb.MallIcon,
	)
	if err != nil {
		code = 400
		err = errors.New("数据不存在")
		return
	}
	if malldb.MallId.Valid {
		out.Id = malldb.MallId.String
	}
	if malldb.MallDesc.Valid {
		out.Desc = malldb.MallDesc.String
	}
	if malldb.MallName.Valid {
		out.Name = malldb.MallName.String
	}
	if malldb.MallIcon.Valid {
		out.Icon = malldb.MallIcon.String
	}
	out.GameId = in.GameId
	code = 200
	return
}

/*
	创建或者更新设置商城
	@param gameid 游戏Id
	@param code 游戏定义的商城code
*/
//func (in *Mall) InsertOrUpdateMall() (code int, err error) {
//	if in.GameId == "" || in.Code == "" {
//		code = 400
//		err = errors.New("参数错误")
//		return
//	}
//	_, _, err = in.QueryMallByMallCode()
//	if err != nil {
//		code, err = in.CreateNewMall()
//	} else {
//		code, err = in.UpdateMall()
//	}
//	return
//}

//func (in *Mall) CreateNewMall() (code int, err error) {
//	id := utils.GetUuid()
//	sqlIn := "insert into ice_mall(id, code, desc, name, icon, gameid) values(?, ?, ?, ?, ?, ?)"
//	_, err = db.SqlDB.Exec(
//		sqlIn,
//		id,
//		in.Code,
//		in.Desc,
//		in.Name,
//		in.Icon,
//		in.GameId,
//	)
//	if err != nil {
//		err = errors.New("创建商城失败")
//		code = 400
//		fmt.Println(err)
//		return
//	}
//	code = 200
//	return
//}

//func (in *Mall) UpdateMall() (code int, err error) {
//	sqlUp := "update ice_mall set desc=?, name=?, icon=? where code=? and gameid=? "
//	stmt, err := db.SqlDB.Prepare(sqlUp)
//	defer stmt.Close()
//	_, err = stmt.Exec(
//		in.Desc,
//		in.Name,
//		in.Icon,
//		in.Code,
//		in.GameId,
//	)
//	if err != nil {
//		code = 400
//		err = errors.New("更新商城数据失败")
//		return
//	}
//	code = 200
//	return
//}