package cms

import (
	"git.rosy.net.cn/baseapi/utils"
	"git.rosy.net.cn/jx-callback/business/jxutils/jxcontext"
	"git.rosy.net.cn/jx-callback/business/jxutils/netprinter"
	"git.rosy.net.cn/jx-callback/business/model"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"git.rosy.net.cn/jx-callback/business/partner"
)

type StoreManager struct {
}

var (
	FixedStoreManager *StoreManager
)

func init() {
	FixedStoreManager = &StoreManager{}
	partner.InitStoreManager(FixedStoreManager)
}

func (s *StoreManager) OnStoreStatusChanged(vendorStoreID string, vendorID int, storeStatus int) (err error) {
	db := dao.GetDB()
	storeDetail, err := dao.GetStoreDetailByVendorStoreID(db, vendorStoreID, vendorID, "")
	if err == nil {
		if storeDetail.IsSync == model.NO || storeDetail.Status == model.StoreStatusDisabled || storeDetail.Status == model.StoreStatusHaveRest {
			return err
		}
		var storeKV, storeMapKV map[string]interface{}
		if storeStatus == model.StoreStatusOpened {
			if storeDetail.Status != model.StoreStatusOpened {
				storeKV = map[string]interface{}{
					"Status": model.StoreStatusOpened,
				}
			}
			if storeDetail.VendorStatus != model.StoreStatusOpened {
				storeMapKV = map[string]interface{}{
					"Status": model.StoreStatusOpened,
				}
			}
		} else {
			if storeDetail.Status == model.StoreStatusOpened {
				if storeDetail.VendorStatus != storeStatus {
					storeMapKV = map[string]interface{}{
						"Status": storeStatus,
					}
				}
			} else if storeDetail.Status <= storeStatus {
				if storeDetail.VendorStatus != storeStatus {
					storeMapKV = map[string]interface{}{
						"Status": storeStatus,
					}
				}
			}
		}
		if err == nil && (storeKV != nil || storeMapKV != nil) {
			txDB, _ := dao.Begin(db)
			defer func() {
				if r := recover(); r != nil || err != nil {
					dao.Rollback(db, txDB)
					if r != nil {
						panic(r)
					}
				}
			}()
			if storeKV != nil {
				store := &model.Store{}
				store.ID = storeDetail.Store.ID
				if err = utils.CallFuncLogError(func() error {
					_, err = dao.UpdateEntityLogically(db, store, storeKV, model.AdminName, nil)
					return err
				}, "OnStoreStatusChanged Update Store venvendorStoreID:%s", vendorStoreID); err != nil {
					return err
				}
			}
			if storeMapKV != nil {
				if err = utils.CallFuncLogError(func() error {
					_, err = dao.UpdateEntityLogically(db, &model.StoreMap{}, storeMapKV, model.AdminName, map[string]interface{}{
						model.FieldStoreID:   storeDetail.Store.ID,
						model.FieldVendorID:  vendorID,
						model.FieldDeletedAt: utils.DefaultTimeValue,
					})
					return err
				}, "OnStoreStatusChanged Update StoreMap venvendorStoreID:%s", vendorStoreID); err != nil {
					return err
				}
			}
			if storeStatus != model.StoreStatusOpened {
				// 因为storeStatus != model.StoreStatusOpened不会修改京西门店的状态，所以直接用storeDetail.Status是合适的
				if err = utils.CallFuncLogError(func() error {
					return dao.FormalizeStoreStatus(db, storeDetail.Store.ID, storeDetail.Status)
				}, "OnStoreStatusChanged FormalizeStoreStatus venvendorStoreID:%s", vendorStoreID); err != nil {
					return err
				}
			}
			dao.Commit(db, txDB)
		}

		// 打印门第离线消息
		if storeStatus < model.StoreStatusOpened && storeDetail.PrinterVendorID == model.VendorIDJxprint {
			netprinter.PrintStoreStatus(jxcontext.AdminCtx, storeDetail, vendorID, 0)
		}
	}
	return err
}

func (s *StoreManager) OnCourierStoreStatusChanged(ctx *jxcontext.Context, vendorStoreID string, vendorID int, auditStatus int, message string) (err error) {
	if vendorStoreID != "" {
		db := dao.GetDB()
		_, err2 := dao.GetStoreDetail2(db, 0, vendorStoreID, vendorID)
		if err = err2; err == nil {
			status := model.StoreStatusOpened
			if auditStatus != model.StoreAuditStatusOnline && auditStatus != model.StoreAuditStatusUpdated {
				status = model.StoreStatusDisabled
			}
			comment := ""
			if auditStatus == model.StoreAuditStatusRejected {
				comment = message
			}
			_, err = dao.UpdateEntityLogically(db, &model.StoreCourierMap{}, map[string]interface{}{
				model.FieldStatus: status,
				"AuditStatus":     auditStatus,
				"Comment":         comment,
			}, ctx.GetUserName(), map[string]interface{}{
				model.FieldVendorStoreID: vendorStoreID,
				model.FieldVendorID:      vendorID,
			})
		} else if dao.IsNoRowsError(err) {
			err = nil
		}
	}
	return err
}
