package hdl

import (
	"applet/app/admin/lib/validate"
	"applet/app/admin/md"
	"applet/app/db"
	"applet/app/db/model"
	"applet/app/e"
	"applet/app/utils"
	"github.com/gin-gonic/gin"
	"time"
)

func MerchantList(c *gin.Context) {
	var req md.MerchantListReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		err = validate.HandleValidateErr(err)
		err1 := err.(e.E)
		e.OutErr(c, err1.Code, err1.Error())
		return
	}
	if req.Limit == 0 {
		req.Limit = 10
	}
	if req.Page == 0 {
		req.Page = 1
	}
	var m []*db.MerchantWithEnterpriseInfo
	engine := db.Db.Where("1=1")
	if req.EnterpriseId != 0 {
		engine = engine.And("merchant.enterprise_id =?", req.EnterpriseId)
	}
	if req.Name != "" {
		engine = engine.And("merchant.name like ?", "%"+req.Name+"%")
	}
	total, err := engine.Join("LEFT", "enterprise", "enterprise.id = merchant.enterprise_id").
		Limit(req.Limit, (req.Page-1)*req.Limit).FindAndCount(&m)
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err.Error())
		return
	}

	var resp []interface{}
	for _, v := range m {
		resp = append(resp, map[string]interface{}{
			"id":              v.Merchant.Id,
			"name":            v.Merchant.Name,
			"enterprise_id":   v.Merchant.EnterpriseId,
			"create_at":       v.Merchant.CreateAt,
			"update_at":       v.Merchant.UpdateAt,
			"enterprise_name": v.Enterprise.Name,
		})
	}
	e.OutSuc(c, map[string]interface{}{
		"list":  resp,
		"total": total,
	}, nil)
	return
}

func MerchantAdd(c *gin.Context) {
	var req md.MerchantAddReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		err = validate.HandleValidateErr(err)
		err1 := err.(e.E)
		e.OutErr(c, err1.Code, err1.Error())
		return
	}

	merchantDb := db.MerchantDb{}
	merchantDb.Set()
	now := time.Now()
	insertAffected, err := merchantDb.MerchantInsert(&model.Merchant{
		EnterpriseId: req.EnterpriseId,
		Name:         req.Name,
		CreateAt:     now.Format("2006-01-02 15:04:05"),
		UpdateAt:     now.Format("2006-01-02 15:04:05"),
	})
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err.Error())
		return
	}
	if insertAffected <= 0 {
		e.OutErr(c, e.ERR_DB_ORM, "新增数据失败")
		return
	}

	e.OutSuc(c, "success", nil)
	return
}

func MerchantUpdate(c *gin.Context) {
	var req md.MerchantUpdateReq
	err := c.ShouldBindJSON(&req)
	if err != nil {
		err = validate.HandleValidateErr(err)
		err1 := err.(e.E)
		e.OutErr(c, err1.Code, err1.Error())
		return
	}
	merchantDb := db.MerchantDb{}
	merchantDb.Set()
	merchant, err := merchantDb.GetMerchant(req.Id)
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err.Error())
		return
	}
	if merchant == nil {
		e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
		return
	}
	now := time.Now()
	merchant.EnterpriseId = req.EnterpriseId
	merchant.Name = req.Name
	merchant.UpdateAt = now.Format("2006-01-02 15:04:05")
	merchant.CreateAt = req.CreateAt
	_, err = merchantDb.MerchantUpdate(merchant, "name", "enterprise_id", "update_at", "create_at")
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err.Error())
		return
	}
	e.OutSuc(c, "success", nil)
	return
}

func MerchantDelete(c *gin.Context) {
	id := c.Param("merchant_id")
	merchantDb := db.MerchantDb{}
	merchantDb.Set()
	merchant, err := merchantDb.GetMerchant(utils.StrToInt(id))
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err.Error())
		return
	}
	if merchant == nil {
		e.OutErr(c, e.ERR_NO_DATA, "未查询到对应记录")
		return
	}

	_, err = merchantDb.MerchantDelete(id)
	if err != nil {
		e.OutErr(c, e.ERR_DB_ORM, err.Error())
		return
	}
	e.OutSuc(c, "success", nil)
	return
}