package dao

import (
	"server/global"
)

type ProjectWorkingHours struct {
	global.GVA_MODEL
	ProjectCode string  `json:"projectCode" form:"projectCode" gorm:"comment:项目编号"`
	People      int     `json:"people" form:"people" gorm:"comment:报工人"`
	SysUser     SysUser `json:"constructor" form:"constructor" gorm:"foreignKey:People;references:id;"`
	Days        float64 `json:"days" form:"days" gorm:"type:double(5,1);not null;comment:天数"`
	PeopleTime  string  `json:"peopleTime" form:"peopleTime" gorm:"comment:报工时间"`
}

func (ProjectWorkingHours) TableName() string {
	return "project_working_hours"
}

// TODO:工时查询

// QueryWorkingHoursList 查询工时详情列表
func QueryWorkingHoursList(limit, offset, people int, code, time, yearTime string) (projectWorkingHours []ProjectWorkingHours, total int64, err error) {
	// 创建db
	db := global.GVA_DB.Model(&ProjectWorkingHours{})
	// 如果有条件搜索 下方会自动创建搜索语句
	if code != "" {
		db = db.Where("project_code like ?", "%"+code+"%")
	}
	if people != 0 {
		db = db.Where("people = ?", people)
	}
	if time != "" {
		db = db.Where("DATE_FORMAT(people_time, '%Y-%m') = ?", time)
	}
	if yearTime != "" {
		db = db.Where("DATE_FORMAT(people_time, '%Y') = ?", yearTime)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Order("id desc").Limit(limit).Offset(offset).Preload("SysUser").Find(&projectWorkingHours).Error
	return projectWorkingHours, total, err
}

func QueryWorkingHoursSum(limit, offset, people int, code, time, yearTime string) (list interface{}, total int64, err error) {
	type Result struct {
		People   uint
		Name     string
		SumDay   float64
		PriceSum float64
	}

	var results []Result
	db := global.GVA_DB.Model(&ProjectWorkingHours{}).Select("people,sys_users.`nick_name` as name,SUM(days) as sum_day,(SUM(days) * one_price) as price_sum").
		Joins("JOIN sys_users ON project_working_hours.people = sys_users.id")

	// 如果有条件搜索 下方会自动创建搜索语句
	if code != "" {
		db = db.Where("project_code like ?", "%"+code+"%")
	}
	if people != 0 {
		db = db.Where("people = ?", people)
	}
	if time != "" {
		db = db.Where("DATE_FORMAT(people_time, '%Y-%m') = ?", time)
	}
	if yearTime != "" {
		db = db.Where("DATE_FORMAT(people_time, '%Y') = ?", yearTime)
	}
	err = db.Group("people").Limit(limit).Offset(offset).Preload("SysUser").Count(&total).Scan(&results).Error
	return results, total, err
}

// QueryWorkingHours 查询工时
func QueryWorkingHours(code string) (workingHours []ProjectWorkingHours, err error) {
	err = global.GVA_DB.Model(&ProjectWorkingHours{}).Where("project_code = ?", code).Preload("SysUser").Find(&workingHours).Error
	return workingHours, err
}

// QueryWorkingHoursByCode 查询单个工时 按id
func QueryWorkingHoursByCode(code string, people int) (workingHours ProjectWorkingHours, err error) {
	err = global.GVA_DB.Model(&ProjectWorkingHours{}).Where("code = ? and people = ?", code, people).Preload("SysUser").First(&workingHours).Error
	return workingHours, err
}

// TODO:工时新增

// CreateWorkingHours 创建工时
func (pwh ProjectWorkingHours) CreateWorkingHours() error {
	return global.GVA_DB.Create(&pwh).Error
}

// TODO:工时修改

// UpdateWorkingHours 修改工时
func (pwh ProjectWorkingHours) UpdateWorkingHours() error {
	return global.GVA_DB.Model(&ProjectWorkingHours{}).Where("id = ?", pwh.ID).Updates(&pwh).Error
}

// TODO:工时删除

// DeleteWorkingHours 删除工时
func (pwh ProjectWorkingHours) DeleteWorkingHours() error {
	return global.GVA_DB.Model(&ProjectWorkingHours{}).Unscoped().Delete(&pwh).Error
}
