package db

import (
	"applet/app/db/model"
	"applet/app/md"
	"applet/app/utils"
	"applet/app/utils/logx"
	"time"
	"xorm.io/xorm"
)

func GetPrinterIndexAll(Db *xorm.Engine, storeId string) ([]model.CommunityTeamStorePrinter, error) {
	var list []model.CommunityTeamStorePrinter
	session := Db.Where("store_id= ? ", storeId)
	//排序
	err := session.Desc("create_time").Find(&list)
	if err != nil {
		return nil, err
	}
	return list, err
}
func GetPrinterIndexById(Db *xorm.Engine, id, storeId string) (*model.CommunityTeamStorePrinter, error) {
	var list model.CommunityTeamStorePrinter
	session := Db.Where("id=? and store_id=?", id, storeId)
	//排序
	has, err := session.Desc("create_time").Get(&list)
	if has == false || err != nil {
		return nil, err
	}
	return &list, err
}

func GetPrinterIndexByBluetooth(Db *xorm.Engine, storeId string) (*model.CommunityTeamStorePrinter, error) {
	var list model.CommunityTeamStorePrinter
	session := Db.Where("type=? and store_id=?", "bluetooth", storeId)
	//排序
	has, err := session.Desc("create_time").Get(&list)
	if has == false || err != nil {
		return nil, err
	}
	return &list, err
}

// 分页查询方案
func GetPrinterIndexList(Db *xorm.Engine, parameter *md.IndexRequest) ([]md.IndexResList, error) {
	var list []model.CommunityTeamStorePrinter
	var webList []md.IndexResList

	session := Db.Where("store_id= ? ", parameter.StoreId)
	//分页
	if utils.StrToInt(parameter.P) > 0 && utils.StrToInt(parameter.PageSize) > 0 {
		session = session.Limit(utils.StrToInt(parameter.PageSize), (utils.StrToInt(parameter.P)-1)*utils.StrToInt(parameter.PageSize))
	}
	//排序
	err := session.Desc("create_time").Find(&list)
	if err != nil {
		return nil, err
	}
	var typeList = map[string]string{
		"ordinary":  "普通打印机",
		"cloud":     "云打印机",
		"bluetooth": "蓝牙打印机",
	}
	var locationTypeList = map[string]string{
		"reception": "前台",
		"kitchen":   "后厨",
	}
	for _, v := range list {
		var tmp = md.IndexResList{
			Id:                 utils.IntToStr(v.Id),
			Name:               v.Name,
			IsUse:              utils.IntToStr(v.IsUse),
			StoreId:            utils.IntToStr(v.StoreId),
			SnNum:              v.SnNum,
			IdentificationCode: v.IdentificationCode,
			ModuleId:           utils.IntToStr(v.ModuleId),
			Type:               v.Type,
			TypeStr:            typeList[v.Type],
			LocationType:       v.LocationType,
			LocationTypeStr:    locationTypeList[v.LocationType],
		}
		webList = append(webList, tmp)
	}
	return webList, nil
}

func indexGetCount(Db *xorm.Engine, parameter *md.IndexRequest) int {
	var ord model.CommunityTeamStorePrinter
	session := Db.Where("store_id= ? ", parameter.StoreId)
	count, err := session.Count(&ord)
	if err != nil {
		return 0
	}
	return int(count)
}
func IndexExists(Db *xorm.Engine, parameter *md.IndexSaveRequest) int {
	var ord model.CommunityTeamStorePrinter
	session := Db.Where("sn_num= ? ", parameter.SnNum)
	count, err := session.Count(&ord)
	if err != nil {
		return 0
	}
	return int(count)
}
func IndexById(Db *xorm.Engine, parameter *md.IndexSaveRequest) (*model.CommunityTeamStorePrinter, error) {
	var ord model.CommunityTeamStorePrinter
	session := Db.Where("id= ? ", parameter.Id)
	_, err := session.Get(&ord)
	return &ord, err
}

// 写入数据
func IndexInsert(eg *xorm.Engine, m *model.CommunityTeamStorePrinter) (int64, error) {
	if m.Id > 0 { //编辑
		id, err := IndexUpdate(eg, m, "type,is_use,sn_num,identification_code,name,update_time,location_type")
		if id == 0 || err != nil {
			return 0, logx.Warn("cannot insert data :", err)
		}
		return id, nil
	}
	m.CreateTime = time.Now()
	//写入
	id, err := eg.InsertOne(m)
	if id == 0 || err != nil {
		return 0, logx.Warn("cannot insert data :", err)
	}
	return id, nil
}

// 修改数据
func IndexUpdate(eg *xorm.Engine, m *model.CommunityTeamStorePrinter, cols ...string) (int64, error) {
	update, err := eg.ID(m.Id).Cols(cols...).Update(m)
	if err != nil || update == 0 {
		return int64(m.Id), err
	}
	return int64(m.Id), nil

}

// 分页查询打印明细
func GetPrinterRecordList(Db *xorm.Engine, parameter *md.RecordRequest) ([]md.RecordResList, error) {
	var list []model.CommunityTeamStorePrinterDetail
	var webList []md.RecordResList
	session := Db.Where("store_id >0 and store_id=?", parameter.StoreId)
	if utils.StrToInt(parameter.PrinterId) > 0 {
		session.And("printer_id=? ", parameter.PrinterId)
	}
	if parameter.StartTime != "" {
		session.And("create_time>=? ", parameter.StartTime)
	}
	if parameter.EndTime != "" {
		session.And("create_time<=? ", parameter.EndTime)
	}
	//分页
	if utils.StrToInt(parameter.P) > 0 && utils.StrToInt(parameter.PageSize) > 0 {
		session = session.Limit(utils.StrToInt(parameter.PageSize), (utils.StrToInt(parameter.P)-1)*utils.StrToInt(parameter.PageSize))
	}
	//排序
	if parameter.Sort == "printer_time_desc" {
		session.Desc("create_time")
	} else if parameter.Sort == "printer_time_asc" {
		session.Asc("create_time")
	} else {
		session.Desc("id")
	}
	//排序
	err := session.Find(&list)
	if err != nil {
		return nil, err
	}
	for _, v := range list {

		var tmp = md.RecordResList{
			Id:           utils.IntToStr(v.Id),
			OrdId:        v.OrdId,
			OrdType:      utils.IntToStr(v.OrdType),
			PrintContent: v.PrintContent,
			State:        utils.IntToStr(v.State),
			PrintTime:    v.PrintTime.Format("2006-01-02 15:04:05"),
			CreateTime:   v.CreateTime.Format("2006-01-02 15:04:05"),
		}
		if tmp.PrintTime == "0001-01-01 00:00:00" {
			tmp.PrintTime = ""
		}
		webList = append(webList, tmp)
	}
	return webList, nil
}
