package db

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

// 查询所有的模板
func GetAllPrinterModuleList(Db *xorm.Engine) ([]model.CommunityTeamStorePrinterModule, error) {
	var list []model.CommunityTeamStorePrinterModule
	err := Db.Desc("create_time").Find(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}
func GetAllPrinterModuleListByModuleId(Db *xorm.Engine, moduleIds []int) ([]model.CommunityTeamStorePrinterModule, error) {
	var list []model.CommunityTeamStorePrinterModule
	err := Db.In("id", moduleIds).Find(&list)
	if err != nil {
		return nil, err
	}
	return list, nil
}

func GetAllPrinterModuleListByModuleIdOne(Db *xorm.Engine, moduleId int) (*model.CommunityTeamStorePrinterModule, error) {
	var list model.CommunityTeamStorePrinterModule
	has, err := Db.Where("id=?", moduleId).Get(&list)
	if has == false || err != nil {
		return nil, err
	}
	return &list, nil
}

// 分页查询方案
func GetPrinterModuleList(Db *xorm.Engine, parameter *md.IndexRequest) ([]md.ModuleResList, error) {
	var list []model.CommunityTeamStorePrinterModule
	var webList []md.ModuleResList
	session := Db.Where("is_use= 1 ")
	//分页
	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
	}
	for _, v := range list {
		var tmp = md.ModuleResList{
			Id:      utils.IntToStr(v.Id),
			Name:    v.Name,
			Content: v.Content,
		}
		webList = append(webList, tmp)
	}
	return webList, nil
}
