package business_trip

import (
	"github.com/beego/beego/v2/client/orm"
)

type BusinessTripCalendarAdmin struct {
	AdminId          int    `description:"研究员id"`
	RealName         string `description:"研究员名称"`
	BusinessTripList []*BusinessTripCalendar
}

type BusinessTripCalendar struct {
	BusinessApplyId int    `description:"出差申请id"`
	Status          string `description:"状态:'待审批','已审批','已驳回','已撤回','已过期'"`
	WeekDate        string `description:"开始日期"`
	City            string `description:"城市"`
	Week            string `description:"周"`
	WeekType        string `description:"当前周:current,下一周:next"`
	Reason          string `description:"出差事由"`
}

// GetResearcherV2 获取研究员列表（冻结的也要）
func GetBusinessTripCalendar(departmentId string) (list []*BusinessTripCalendarAdmin, err error) {
	o := orm.NewOrm()
	sql := ` SELECT a.admin_id,a.real_name 
			FROM admin AS a
			WHERE a.enabled=1
			AND a.department_id IN(` + departmentId + `)
			ORDER BY a.admin_id ASC `
	_, err = o.Raw(sql).QueryRows(&list)
	return
}

type BusinessTripCalendarGroup struct {
	GroupId      int    `description:"分组id"`
	GroupName    string `description:"分组名称"`
	DepartmentId string `json:"-" description:"部门id"`
	AdminList    []*BusinessTripCalendarAdmin
}

type BusinessTripCalendarResp struct {
	GroupList []*BusinessTripCalendarGroup
	BaseDate  string `description:"用于查询上下两周的开始日期"`
}

func GetBusinessTripList(adminId, startDate, endDate string) (list []*BusinessApplyView, err error) {
	o := orm.NewOrm()
	sql := `SELECT a.*
			FROM  business_apply AS a
			left join business_apply_peer b on a.business_apply_id =b.business_apply_id 
			WHERE a.status IN('待审批','已通过')
			AND ((a.arrive_date>=? AND a.arrive_date<=?) OR (a.return_date>=? AND a.return_date<=?)) `
	if adminId != "" {
		sql += ` AND (a.apply_admin_id IN  (` + adminId + `)  or b.peer_people_id IN  (` + adminId + `) ) `
		sql += ` ORDER BY a.apply_admin_id ASC,a.arrive_date ASC `
	} else {
		sql += ` ORDER BY a.apply_admin_id ASC,a.arrive_date ASC `
	}
	sql = `SELECT * FROM ( ` + sql + ` ) d GROUP BY business_apply_id `
	_, err = o.Raw(sql, startDate, endDate, startDate, endDate).QueryRows(&list)
	return
}
