package services

import (
	"eta/eta_api/models"
	"eta/eta_api/utils"
	"time"
)

// SwitchHzUserEnabledByMobile 根据手机号启用/禁用弘则研究下的联系人
func SwitchHzUserEnabledByMobile(opEnabled int, mobile string) (err error) {
	if mobile == "" {
		return
	}
	userInfo, err := models.GetWxUserByMobile(mobile)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	// 存在相应联系人
	if userInfo != nil {
		// 禁用-联系人为弘则研究下的则设置为潜在用户
		updateCols := make([]string, 0)
		if opEnabled == 0 && userInfo.CompanyId == 16 {
			userInfo.CompanyId = 1
			userInfo.LastUpdatedTime = time.Now()
			updateCols = append(updateCols, "CompanyId", "LastUpdatedTime")
			_ = userInfo.Update(updateCols)
		}
		// 启用-联系人为潜在用户则设置为弘则研究下
		if opEnabled == 1 && userInfo.CompanyId == 1 {
			userInfo.CompanyId = 16
			userInfo.LastUpdatedTime = time.Now()
			updateCols = append(updateCols, "CompanyId", "LastUpdatedTime")
			_ = userInfo.Update(updateCols)
		}
	}

	return nil
}

// DeleteHzUserByMobile 根据手机号删除弘则研究下的联系人
func DeleteHzUserByMobile(mobile string) (err error) {
	if utils.BusinessCode != utils.BusinessCodeRelease && utils.BusinessCode != utils.BusinessCodeSandbox {
		return
	}
	if mobile == "" {
		return
	}
	companyId := 16
	userInfo, err := models.GetWxUserByCompanyIdAndMobile(companyId, mobile)
	if err != nil && err.Error() != utils.ErrNoRow() {
		return
	}
	if userInfo != nil {
		userId := int(userInfo.UserId)
		if err = models.DeleteWxUserAndRecordByUserId(userId); err != nil {
			return
		}
		// 删除所有的标识，并真正删除数据
		_ = models.DeleteUserSellerRelationByProductId(userId, 1)
		_ = models.DeleteUserSellerRelationByProductId(userId, 2)
	}

	return nil
}
