package cms

import (
	"encoding/json"
	"git.rosy.net.cn/jx-callback/business/model/dao"
	"strconv"
)

// 更新门店负责人信息
func UpdateStoreOperatorConfig() {
	db := dao.GetDB()
	store, err := dao.GetStoreList(db, nil, nil, []int{-1, 0, 1}, nil, nil, "")
	if err != nil {
		return
	}
	userData := make(map[string][][]interface{}, 0)
	jx := make(map[string]string, 0)
	jd := make(map[string]string, 0)
	mt := make(map[string]string, 0)
	eb := make(map[string]string, 0)
	for _, v := range store {
		storeDetail, err := dao.GetStoreDetail(dao.GetDB(), v.ID, 0, "")
		if err != nil {
			return
		}
		if storeDetail.MarketManPhone != "" { // 平台负责人jx
			jx[storeDetail.MarketManName] = storeDetail.MarketManPhone
		}
		if storeDetail.OperatorPhone != "" { // 京东负责人电话
			jd[storeDetail.OperatorName] = storeDetail.OperatorPhone
		}
		if storeDetail.OperatorPhone2 != "" { // 美团负责人电话
			mt[storeDetail.OperatorName2] = storeDetail.OperatorPhone2
		}
		if storeDetail.OperatorPhone3 != "" { // 饿了么负责人电话
			eb[storeDetail.OperatorName3] = storeDetail.OperatorPhone3
		}
	}
	userData["jx"] = Map2Slice(jx)
	userData["jd"] = Map2Slice(jd)
	userData["mt"] = Map2Slice(mt)
	userData["eb"] = Map2Slice(eb)
	// 写入配置
	byteData, err := json.Marshal(userData)
	if err != nil {
		return
	}
	if err := dao.UpdateOperatorConfig(string(byteData), "Sys", "FZR"); err != nil {
		return
	}
	return
}

func Map2Slice(param map[string]string) [][]interface{} {
	if len(param) <= 0 {
		return nil
	}

	result := make([][]interface{}, 0, 0)
	for k, v := range param {
		phone, _ := strconv.Atoi(v)
		result = append(result, []interface{}{k, phone})
	}
	return result
}
