package business_trip

import (
	"github.com/beego/beego/v2/client/orm"
	"github.com/rdlucklib/rdluck_tools/paging"
	"time"
)

type BusinessApply struct {
	BusinessApplyId int       `orm:"column(business_apply_id);pk;auto" description:"ID"`
	ApplyAdminId    int       `description:"申请人id"`
	ApplyRealName   string    `description:"申请人姓名"`
	ArriveDate      string    `description:"到达日期"`
	ReturnDate      string    `description:"返程日期"`
	Province        string    `description:"目的地省"`
	City            string    `description:"目的地市"`
	Reason          string    `description:"出差事由"`
	Transportation  string    `description:"交通工具"`
	PeerPeopleId    string    `description:"同行人id"`
	PeerPeopleName  string    `description:"同行人"`
	Status          string    `description:"状态:'待审批','已审批','已驳回','已撤回','已过期'"`
	ApproveId       int       `description:"审批人id"`
	ApproveName     string    `description:"审批人姓名"`
	RefuseReason    string    `description:"拒绝理由"`
	RefuseTime      time.Time `description:"拒绝时间"`
	ApproveTime     time.Time `description:"审批时间"`
	CloseReason     string    `description:"关闭理由"`
	CloseTime       time.Time `description:"关闭时间"`
	CreateTime      time.Time `description:"创建时间"`
	ModifyTime      time.Time `description:"修改时间"`
}

// 添加出差申请
func AddBusinessApply(item *BusinessApply) (applyId int64, err error) {
	o := orm.NewOrm()
	applyId, err = o.Insert(item)
	if err != nil {
		return
	}
	return
}

type BusinessApplyReq struct {
	ArriveDate     string `description:"到达日期"`
	ReturnDate     string `description:"返程日期"`
	Province       string `description:"目的地省"`
	City           string `description:"目的地市"`
	Reason         string `description:"出差事由"`
	Transportation string `description:"交通工具"`
	PeerPeopleId   string `description:"同行人id"`
	PeerPeopleName string `description:"同行人"`
}

type BusinessApplyView struct {
	BusinessApplyId int    `description:"出差申请id"`
	ApplyAdminId    int    `description:"申请人id"`
	ApplyRealName   string `description:"申请人姓名"`
	ArriveDate      string `description:"到达日期"`
	ReturnDate      string `description:"返程日期"`
	Province        string `description:"目的地省"`
	City            string `description:"目的地市"`
	Reason          string `description:"出差事由"`
	Transportation  string `description:"交通工具"`
	PeerPeopleId    string `description:"同行人id"`
	PeerPeopleName  string `description:"同行人"`
	Status          string `description:"状态:'待审批','已审批','已驳回','已撤回','已过期'"`
	ApproveId       int    `description:"审批人id"`
	ApproveName     string `description:"审批人姓名"`
	RefuseReason    string `description:"拒绝理由"`
	RefuseTime      string `description:"拒绝时间"`
	ApproveTime     string `description:"审批时间"`
	CreateTime      string `description:"创建时间"`
	ModifyTime      string `description:"修改时间"`
	IsClose         bool   `description:"true,可关闭,false,不可关闭"`
	CloseReason     string `description:"关闭理由"`
}

func GetBusinessApplyListCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM  business_apply AS a WHERE 1=1  `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func GetBusinessApplyList(condition string, pars []interface{}, startSize, pageSize int) (list []*BusinessApplyView, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM business_apply AS a WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	sql += ` ORDER BY a.create_time DESC LIMIT ?,? `
	_, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
	return
}

type BusinessApplyListResp struct {
	Paging *paging.PagingItem
	List   []*BusinessApplyView
}

type BusinessApplyDeleteReq struct {
	BusinessApplyId int `description:"出差申请id"`
}

func GetBusinessApplyById(businessApplyId int) (item *BusinessApplyView, err error) {
	o := orm.NewOrm()
	sql := `SELECT * FROM business_apply WHERE business_apply_id=? `
	err = o.Raw(sql, businessApplyId).QueryRow(&item)
	return
}

// 删除
func DeleteBusinessApply(businessApplyId int) (err error) {
	o := orm.NewOrm()
	sql := ` DELETE FROM business_apply WHERE business_apply_id=? `
	_, err = o.Raw(sql, businessApplyId).Exec()
	if err != nil {
		return err
	}
	return err
}

type BusinessApplyBackReq struct {
	BusinessApplyId int `description:"出差申请id"`
}

// 更新
func UpdateBusinessApply(where, updateParams map[string]interface{}) error {
	o := orm.NewOrm()
	ptrStructOrTableName := "business_apply"
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range where {
		qs = qs.Filter(expr, exprV)
	}
	_, err := qs.Update(updateParams)
	return err
}

// 更新
func UpdateBusinessApplyPeer(where, updateParams map[string]interface{}) error {
	o := orm.NewOrm()
	ptrStructOrTableName := "business_apply_peer"
	qs := o.QueryTable(ptrStructOrTableName)
	for expr, exprV := range where {
		qs = qs.Filter(expr, exprV)
	}
	_, err := qs.Update(updateParams)
	return err
}

type BusinessApplyEditReq struct {
	BusinessApplyId int    `description:"出差申请id"`
	ArriveDate      string `description:"到达日期"`
	ReturnDate      string `description:"返程日期"`
	Province        string `description:"目的地省"`
	City            string `description:"目的地市"`
	Reason          string `description:"出差事由"`
	Transportation  string `description:"交通工具"`
	PeerPeopleId    string `description:"同行人id"`
	PeerPeopleName  string `description:"同行人"`
}

func CheckBusinessApplyDateCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM  business_apply AS a
		WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func CheckBusinessApplyDate(startDateTime, endDateTime, status string, applyId, businessApplyId int) (calendarCount int, err error) {
	var condition string
	var pars []interface{}

	if businessApplyId > 0 {
		condition += " AND business_apply_id <> ? "
		pars = append(pars, businessApplyId)
	}

	condition += " AND apply_admin_id = ? "
	pars = append(pars, applyId)

	condition += " AND status IN (" + status + ") "

	condition += ` AND ((? >= arrive_date AND ? <= return_date) OR (? >= arrive_date AND ? <= return_date))`
	pars = append(pars, startDateTime)
	pars = append(pars, startDateTime)
	pars = append(pars, endDateTime)
	pars = append(pars, endDateTime)
	calendarCount, err = CheckBusinessApplyDateCount(condition, pars)
	return
}

func CheckBusinessApplyPeerDateCount(condition string, pars []interface{}) (count int, err error) {
	o := orm.NewOrm()
	sql := `SELECT COUNT(1) AS count FROM  business_apply_peer AS a
		WHERE 1=1 `
	if condition != "" {
		sql += condition
	}
	err = o.Raw(sql, pars).QueryRow(&count)
	return
}

func CheckBusinessApplyPeerDate(startDateTime, endDateTime, status string, applyId, businessApplyId int) (calendarCount int, err error) {
	var condition string
	var pars []interface{}

	if businessApplyId > 0 {
		condition += " AND business_apply_id <> ? "
		pars = append(pars, businessApplyId)
	}

	condition += " AND peer_people_id = ? "
	pars = append(pars, applyId)

	condition += " AND status IN (" + status + ") "

	condition += ` AND ((? >= arrive_date AND ? <= return_date) OR (? >= arrive_date AND ? <= return_date))`
	pars = append(pars, startDateTime)
	pars = append(pars, startDateTime)
	pars = append(pars, endDateTime)
	pars = append(pars, endDateTime)
	calendarCount, err = CheckBusinessApplyPeerDateCount(condition, pars)
	return
}

type BusinessApplyCloseReq struct {
	BusinessApplyId int    `description:"出差申请id"`
	CloseReason     string `description:"关闭原因"`
}
