package logic

import (
	"app.yhyue.com/moapp/jybase/common"
	"context"
	"fmt"
	IC "jyBXSubscribe/rpc/init"
	"jyBXSubscribe/rpc/model"
	"jyBXSubscribe/rpc/util"
	"log"
	"strings"
	"time"

	"jyBXSubscribe/rpc/internal/svc"
	"jyBXSubscribe/rpc/type/bxsubscribe"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetViewStatusLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetViewStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetViewStatusLogic {
	return &GetViewStatusLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 查看状态
func (l *GetViewStatusLogic) GetViewStatus(in *bxsubscribe.GetViewStatusReq) (*bxsubscribe.ViewStatusResp, error) {
	// todo: add your logic here and delete this line
	user := model.EntInfo(common.IntAll(in.EntId), common.IntAll(in.EntUserId))
	var (
		users  *[]*model.User
		ss     []string
		finsql string
	)
	in.InfoId = util.DecodeId(in.InfoId)
	log.Println("查看状态参数", in)
	res := new(bxsubscribe.ViewStatusResp)
	finsql = `SELECT userid,isvisit,visittime,date,source FROM push.pushentniche where  infoid =?  order By date asc`
	if user.Role_admin_department {
		users = model.GetDisUsers(common.IntAll(in.EntId), user.Dept.Id)
		if users != nil && len(*users) > 0 {
			//获取所有部门和子部门员工
			for _, v := range *users {
				ss = append(ss, common.InterfaceToStr(v.Id))
			}
		} else {
			log.Println("部门管理员下为获取到员工", in.EntUserId)
			return &bxsubscribe.ViewStatusResp{}, nil
		}
	} else if user.Role_admin_system {
		//获取企业所有用户
		users = model.GetEntUsers(common.IntAll(in.EntId))
	}
	if len(ss) > 0 {
		finsql = fmt.Sprintf(`SELECT userid,isvisit,visittime,date,source FROM push.pushentniche where  infoid =?  %s order By date asc`, fmt.Sprintf(`and userid in (%s)`, strings.Join(ss, ",")))
	}
	data := IC.BaseServiceMysql.SelectBySql(finsql, in.InfoId)
	userMap := make(map[int]*model.User)
	for _, u := range *users {
		if u != nil {
			userMap[u.Id] = u
		}
	}
	if data != nil && len(*data) > 0 {
		//d := make(map[string]int)
		for _, v := range *data {
			userid := common.IntAll(v["userid"])
			if u, ok := userMap[userid]; ok && u != nil {
				var da bxsubscribe.UserStatus
				da.Name = u.Name
				da.Isvisit = common.Int64All(v["isvisit"])
				visittime := common.InterfaceToStr(v["visittime"])
				date := common.Int64All(v["date"])
				if date > 0 {
					da.Date = time.Unix(date, 0).Format("2006-01-02 15:04:05")
				}
				da.Visittime = visittime
				da.Id = common.Int64All(userid)
				da.Phone = u.Phone
				da.Source = common.Int64All(v["source"])
				res.Items = append(res.Items, &da)
				//if _, ok1 := d[common.InterfaceToStr(userid)]; !ok1 {
				//	res.Items = append(res.Items, &da)
				//	d[common.InterfaceToStr(userid)] = len(res.Items) - 1
				//}
			}
		}
	}
	log.Println("查看输出结果参数", res)
	//Reverse(&res.Items)
	return res, nil
}

// 数组倒序函数
func Reverse(arr *[]*bxsubscribe.UserStatus) {
	var temp *bxsubscribe.UserStatus
	length := len(*arr)
	for i := 0; i < length/2; i++ {
		temp = (*arr)[i]
		(*arr)[i] = (*arr)[length-1-i]
		(*arr)[length-1-i] = temp
	}
}
