You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
416 lines
10 KiB
416 lines
10 KiB
package activebit
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"recook/internal/back"
|
|
"recook/internal/libs/bean"
|
|
"recook/internal/static_path"
|
|
"recook/internal/v2/logic/manage/activebit"
|
|
"recook/internal/v2/model/recommendedlist"
|
|
"recook/internal/v2/model/recook/goods"
|
|
"recook/internal/v2/model/rotation"
|
|
manage "recook/internal/v2/model/third"
|
|
"recook/tools"
|
|
"reflect"
|
|
"strconv"
|
|
"time"
|
|
|
|
mysql2 "git.oa00.com/go/mysql"
|
|
"github.com/360EntSecGroup-Skylar/excelize/v2"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type ControllerActive struct {
|
|
}
|
|
|
|
//Created 新建和修改轮播图
|
|
func (a ControllerActive) Created(c *gin.Context) {
|
|
var p activebit.CreatedReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
//新增
|
|
err := activebit.LogicActiveBit.Add(p)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "ok", nil)
|
|
}
|
|
|
|
type delReq struct {
|
|
Id uint `json:"id"`
|
|
}
|
|
|
|
//Del 删除
|
|
func (a ControllerActive) Del(c *gin.Context) {
|
|
var p delReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
var one rotation.RecookRotationModel
|
|
err := mysql2.Db.Table(one.TableName()).Where("id=?", p.Id).Update("is_active", 0).Error
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "ok", nil)
|
|
|
|
}
|
|
|
|
type ShowReq struct {
|
|
IsSale bool `json:"is_sale"`
|
|
}
|
|
|
|
func (a ControllerActive) ShowList(c *gin.Context) {
|
|
var p ShowReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
rest := activebit.LogicActiveBit.ShowList(p.IsSale)
|
|
back.Suc(c, "ok", rest)
|
|
|
|
}
|
|
|
|
//ActivityAdd 新增活动type
|
|
func (a ControllerActive) ActivityAdd(c *gin.Context) {
|
|
var p activebit.ActivityAddReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
err := activebit.LogicActiveBit.ActivityAdd(p)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "ok", nil)
|
|
|
|
}
|
|
|
|
type activityDelReq struct {
|
|
Id uint `json:"id"`
|
|
}
|
|
|
|
func (a ControllerActive) ActivityDel(c *gin.Context) {
|
|
var p activityDelReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
var dl rotation.RecookActivityTypeInfoModel
|
|
err := mysql2.Db.Table(dl.TableName()).Where("id=?", p.Id).Update("is_active", 0).Error
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "ok", nil)
|
|
|
|
}
|
|
|
|
type activityListReq struct {
|
|
IsType uint `json:"is_type"`
|
|
}
|
|
|
|
//ActivityList 展示list
|
|
func (a ControllerActive) ActivityList(c *gin.Context) {
|
|
var p activityListReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
rest, total := activebit.LogicActiveBit.ShowActivityList(p.IsType)
|
|
back.Suc(c, "ok", bean.ResultLists{
|
|
List: rest,
|
|
Total: int(total),
|
|
})
|
|
|
|
}
|
|
|
|
func (a ControllerActive) AddCountryId(c *gin.Context) {
|
|
|
|
var jt []manage.RecookThirdPartyJingtongDetailModel
|
|
mysql2.Db.Table((&manage.RecookThirdPartyJingtongDetailModel{}).TableName()).Find(&jt)
|
|
for _, v := range jt {
|
|
var contry1 manage.ThirdCountry
|
|
mysql2.Db.Table(contry1.TableName()).Where("id=?", v.CountryId).First(&contry1)
|
|
var num goods.RecookAbroadCountryModel
|
|
mysql2.Db.Table(num.TableName()).Where("name=?", contry1.CountryName).First(&num)
|
|
if num.ID == 0 {
|
|
continue
|
|
}
|
|
var gs = goods.RecookGoodsInfoModel{
|
|
Country: num.ID,
|
|
}
|
|
mysql2.Db.Table(gs.TableName()).Where("third_party_id=?", v.GoodsId).Updates(&gs)
|
|
}
|
|
|
|
}
|
|
|
|
func (a ControllerActive) AddRecommend(c *gin.Context) {
|
|
|
|
cf, err := c.FormFile("excel")
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
//save
|
|
url := filepath.Join(static_path.Dir.Excel, cf.Filename)
|
|
dst := filepath.Join(static_path.Dir.Root, url)
|
|
err = c.SaveUploadedFile(cf, dst)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
//open
|
|
body, err := excelize.OpenFile(dst)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
rows, err := body.GetRows(body.GetSheetName(body.GetActiveSheetIndex()))
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
result := make([]string, 0)
|
|
for _, row := range rows {
|
|
//该行 横坐标 0开始
|
|
result = append(result, row[0])
|
|
}
|
|
//存入 1高佣 2特惠
|
|
status, _ := c.GetPostForm("status")
|
|
|
|
isSale, _ := c.GetPostForm("is_sale")
|
|
is_sale, _ := strconv.Atoi(isSale)
|
|
err = activebit.LogicActiveBit.AddCommends(result, status, is_sale == 1)
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
back.Suc(c, "ok", nil)
|
|
}
|
|
|
|
type listReq struct {
|
|
Status uint `json:"status"`
|
|
IsSale bool `json:"is_sale"`
|
|
}
|
|
|
|
func (a ControllerActive) List(c *gin.Context) {
|
|
var p listReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if p.Status != 1 && p.Status != 2 && p.Status != 10 {
|
|
back.Fail(c, "传入参数有误")
|
|
return
|
|
}
|
|
var list []recommendedlist.RecookCommendList
|
|
mysql2.Db.Table((&recommendedlist.RecookCommendList{}).TableName()).
|
|
Where("is_sale=?", p.IsSale).Where("status=?", p.Status).Find(&list)
|
|
back.Suc(c, "", list)
|
|
|
|
}
|
|
|
|
type delRecommendReq struct {
|
|
List []uint `json:"list"`
|
|
}
|
|
|
|
func (a ControllerActive) DelList(c *gin.Context) {
|
|
var p delRecommendReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
var one recommendedlist.RecookCommendList
|
|
for _, v := range p.List {
|
|
err := mysql2.Db.Table(one.TableName()).Where("id=?", v).Delete(&one).Error
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
}
|
|
back.Suc(c, "", nil)
|
|
}
|
|
|
|
type data struct {
|
|
GoodsId uint `json:"goods_id" xlsx:"商品ID"`
|
|
GoodsName string `json:"goods_name" xlsx:"商品名称"`
|
|
CommissionList string `json:"commission_list" xlsx:"佣金率"`
|
|
DiscountPriceList string `json:"discount_price_list" xlsx:"劵后价"`
|
|
GysName string `json:"gys_name" xlsx:"供应商名称"`
|
|
}
|
|
type up struct {
|
|
Name string
|
|
Url string
|
|
}
|
|
|
|
func (a ControllerActive) UpExcel(c *gin.Context) {
|
|
var p listReq
|
|
if err := tools.Params(&p, c); err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
if p.Status != 1 && p.Status != 2 && p.Status != 10 {
|
|
back.Fail(c, "传入参数有误")
|
|
return
|
|
}
|
|
//获取需要导出的数据
|
|
var list []data
|
|
mysql2.Db.Table((&recommendedlist.RecookCommendList{}).TableName()).Where("status=?", p.Status).Find(&list)
|
|
// 创建一个工作表
|
|
f := WriteXlsx("sheet1", list)
|
|
// 根据指定路径保存文件
|
|
name := time.Now().Format("20060102-150405") + tools.GenerateGoodsHashSign() + ".xlsx"
|
|
err := f.SaveAs(filepath.Join(static_path.Dir.Root, static_path.Dir.Temp, name))
|
|
if err != nil {
|
|
back.Fail(c, err.Error())
|
|
return
|
|
}
|
|
var fileRest = up{
|
|
Name: name,
|
|
Url: filepath.Join(static_path.Dir.Static, static_path.Dir.Temp, name),
|
|
}
|
|
|
|
back.Suc(c, "", fileRest)
|
|
|
|
}
|
|
func WriteXlsx(sheet string, records interface{}) *excelize.File {
|
|
xlsx := excelize.NewFile() // new file
|
|
index := 0
|
|
if sheet != "Sheet1" {
|
|
xlsx.SetSheetName("Sheet1", sheet)
|
|
index = xlsx.GetSheetIndex(sheet)
|
|
} else {
|
|
index = xlsx.NewSheet(sheet) // new sheet
|
|
}
|
|
|
|
xlsx.SetActiveSheet(index) // set active (default) sheet
|
|
firstCharacter := 65 // start from 'A' line
|
|
t := reflect.TypeOf(records)
|
|
|
|
if t.Kind() != reflect.Slice {
|
|
return xlsx
|
|
}
|
|
|
|
s := reflect.ValueOf(records)
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
elem := s.Index(i).Interface()
|
|
elemType := reflect.TypeOf(elem)
|
|
elemValue := reflect.ValueOf(elem)
|
|
temp := 0
|
|
for j := 0; j < elemType.NumField(); j++ {
|
|
field := elemType.Field(j)
|
|
tag := field.Tag.Get("xlsx")
|
|
name := tag
|
|
if tag == "" {
|
|
temp = temp + 1
|
|
continue
|
|
}
|
|
column := string(firstCharacter + j - temp)
|
|
|
|
// 设置表头
|
|
if i == 0 {
|
|
xlsx.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+1), name)
|
|
}
|
|
// 设置内容
|
|
xlsx.SetCellValue(sheet, fmt.Sprintf("%s%d", column, i+2), elemValue.Field(j).Interface())
|
|
}
|
|
}
|
|
return xlsx
|
|
}
|
|
|
|
type WriteList struct {
|
|
SheetName string
|
|
Records interface{}
|
|
}
|
|
|
|
func WriteXlsxList(list []WriteList) (writeRes, error) {
|
|
xlsx := excelize.NewFile() // new file
|
|
index := 0
|
|
for i1, model := range list {
|
|
fmt.Println(model.SheetName)
|
|
if model.SheetName != fmt.Sprintf("Sheet%d", i1+1) {
|
|
if i1 == 0 {
|
|
xlsx.SetSheetName(fmt.Sprintf("Sheet%d", i1+1), model.SheetName)
|
|
index = xlsx.GetSheetIndex(model.SheetName)
|
|
} else {
|
|
index = xlsx.NewSheet(fmt.Sprintf("Sheet%d", i1+1))
|
|
xlsx.SetSheetName(fmt.Sprintf("Sheet%d", i1+1), model.SheetName)
|
|
}
|
|
} else {
|
|
index = xlsx.NewSheet(fmt.Sprintf("Sheet%d", i1+1)) // new sheet
|
|
}
|
|
//index = xlsx.NewSheet(model.SheetName) // new sheet
|
|
xlsx.SetActiveSheet(index) // set active (default) sheet
|
|
firstCharacter := 65 // start from 'A' line
|
|
t := reflect.TypeOf(model.Records)
|
|
|
|
if t.Kind() != reflect.Slice {
|
|
return writeRes{}, errors.New("系统问题")
|
|
}
|
|
|
|
s := reflect.ValueOf(model.Records)
|
|
|
|
for i := 0; i < s.Len(); i++ {
|
|
elem := s.Index(i).Interface()
|
|
elemType := reflect.TypeOf(elem)
|
|
elemValue := reflect.ValueOf(elem)
|
|
temp := 0
|
|
for j := 0; j < elemType.NumField(); j++ {
|
|
field := elemType.Field(j)
|
|
tag := field.Tag.Get("xlsx")
|
|
name := tag
|
|
if tag == "" {
|
|
temp = temp + 1
|
|
continue
|
|
}
|
|
column := ""
|
|
if j-temp >= 26 {
|
|
//目前取52列内 其余的以后在考虑
|
|
fmt.Println(j - temp)
|
|
cn1 := (j - temp + 1) % 26
|
|
column = fmt.Sprintf("%s%s", string(int32(65)), string(int32(firstCharacter+cn1-1)))
|
|
fmt.Println(column)
|
|
} else {
|
|
column = string(firstCharacter + j - temp)
|
|
}
|
|
|
|
fmt.Println(column)
|
|
|
|
// 设置表头
|
|
if i == 0 {
|
|
xlsx.SetCellValue(model.SheetName, fmt.Sprintf("%s%d", column, i+1), name)
|
|
}
|
|
// 设置内容
|
|
xlsx.SetCellValue(model.SheetName, fmt.Sprintf("%s%d", column, i+2), elemValue.Field(j).Interface())
|
|
}
|
|
}
|
|
}
|
|
name := time.Now().Format("20060102-150405") + tools.GenerateGoodsHashSign() + ".xlsx"
|
|
dst := filepath.Join(static_path.Dir.Root, static_path.Dir.Temp, name)
|
|
dstNew := filepath.Join(static_path.Dir.Temp, name)
|
|
err := xlsx.SaveAs(dst)
|
|
|
|
result := writeRes{
|
|
name, dstNew,
|
|
}
|
|
if err != nil {
|
|
return writeRes{}, err
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
type writeRes struct {
|
|
Name string `json:"name"`
|
|
Dst string `json:"dst"`
|
|
}
|