198 Star 560 Fork 251

lock-li / opms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
leave.go 13.59 KB
一键复制 编辑 原始数据 按行查看 历史
lock 提交于 2017-03-30 17:44 . opms v1.3
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
package leaves
import (
"fmt"
"opms/controllers"
. "opms/models/leaves"
. "opms/models/messages"
. "opms/models/users"
"opms/utils"
"os"
"strconv"
"strings"
"time"
"github.com/astaxie/beego"
"github.com/astaxie/beego/utils/pagination"
)
type ManageApprovalController struct {
controllers.BaseController
}
func (this *ManageApprovalController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "approval-manage") {
//this.Abort("401")
}
this.TplName = "leaves/approval-index.tpl"
}
//我的
type ManageLeaveController struct {
controllers.BaseController
}
func (this *ManageLeaveController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-manage") {
this.Abort("401")
}
page, err := this.GetInt("p")
status := this.GetString("status")
ltype := this.GetString("type")
result := this.GetString("result")
if err != nil {
page = 1
}
offset, err1 := beego.AppConfig.Int("pageoffset")
if err1 != nil {
offset = 15
}
condArr := make(map[string]string)
condArr["status"] = status
condArr["type"] = ltype
condArr["result"] = result
condArr["userid"] = fmt.Sprintf("%d", this.BaseController.UserUserId)
countLeave := CountLeave(condArr)
paginator := pagination.SetPaginator(this.Ctx, offset, countLeave)
_, _, leaves := ListLeave(condArr, page, offset)
this.Data["paginator"] = paginator
this.Data["condArr"] = condArr
this.Data["leaves"] = leaves
this.Data["countLeave"] = countLeave
this.TplName = "leaves/index.tpl"
}
//审批
type ApprovalLeaveController struct {
controllers.BaseController
}
func (this *ApprovalLeaveController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-approval") {
this.Abort("401")
}
page, err := this.GetInt("p")
filter := this.GetString("filter")
if err != nil {
page = 1
}
offset, err1 := beego.AppConfig.Int("pageoffset")
if err1 != nil {
offset = 15
}
condArr := make(map[string]string)
condArr["filter"] = filter
if filter == "over" {
condArr["status"] = "1"
} else {
condArr["filter"] = "wait"
condArr["status"] = "0"
}
condArr["userid"] = fmt.Sprintf("%d", this.BaseController.UserUserId)
countLeave := CountLeaveApproval(condArr)
paginator := pagination.SetPaginator(this.Ctx, offset, countLeave)
_, _, leaves := ListLeaveApproval(condArr, page, offset)
this.Data["paginator"] = paginator
this.Data["condArr"] = condArr
this.Data["leaves"] = leaves
this.Data["countLeave"] = countLeave
this.TplName = "leaves/approval.tpl"
}
type ShowLeaveController struct {
controllers.BaseController
}
func (this *ShowLeaveController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-view") {
this.Abort("401")
}
idstr := this.Ctx.Input.Param(":id")
id, err := strconv.Atoi(idstr)
leave, err := GetLeave(int64(id))
if err != nil {
this.Abort("404")
}
this.Data["leave"] = leave
_, _, approvers := ListLeaveApproverProcess(leave.Id)
this.Data["approvers"] = approvers
if this.BaseController.UserUserId != leave.Userid {
//检测是否可以审批和是否已审批过
checkApproverid, checkStatus := CheckLeaveApprover(leave.Id, this.BaseController.UserUserId)
if 0 == checkApproverid {
this.Abort("401")
}
this.Data["checkStatus"] = checkStatus
this.Data["checkApproverid"] = checkApproverid
//检测审批顺序
var checkApproverCan = 1
for i, v := range approvers {
if v.Status == 2 {
checkApproverCan = 0
break
}
if v.Userid == this.BaseController.UserUserId {
if i != 0 {
if approvers[i-1].Status == 0 {
checkApproverCan = 0
break
}
}
}
}
this.Data["checkApproverCan"] = checkApproverCan
} else {
this.Data["checkStatus"] = 0
this.Data["checkApproverCan"] = 0
}
this.TplName = "leaves/detail.tpl"
}
func (this *ShowLeaveController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-approval") {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权设置"}
this.ServeJSON()
return
}
approverid, _ := this.GetInt64("id")
if approverid <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "参数出错"}
this.ServeJSON()
return
}
leaveid, _ := this.GetInt64("leaveid")
if leaveid <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "参数出错"}
this.ServeJSON()
return
}
status, _ := this.GetInt("status")
if status <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择状态"}
this.ServeJSON()
return
}
summary := this.GetString("summary")
var leave LeavesApprover
leave.Status = status
leave.Summary = summary
leave.Leaveid = leaveid
err := UpdateLeavesApprover(approverid, leave)
if err == nil {
//消息通知
lev, _ := GetLeave(leaveid)
var msg Messages
msg.Id = utils.SnowFlakeId()
msg.Userid = this.BaseController.UserUserId
msg.Touserid = lev.Userid
msg.Type = 3
msg.Subtype = 31
if status == 1 {
msg.Title = "同意"
} else if status == 2 {
msg.Title = "拒绝"
}
msg.Url = "/leave/approval/" + fmt.Sprintf("%d", leaveid)
AddMessages(msg)
this.Data["json"] = map[string]interface{}{"code": 1, "message": "审批成功"}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "审批失败"}
}
this.ServeJSON()
}
type AddLeaveController struct {
controllers.BaseController
}
func (this *AddLeaveController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-add") {
this.Abort("401")
}
var leave Leaves
this.Data["leave"] = leave
_, _, users := ListUserFind()
this.Data["users"] = users
this.TplName = "leaves/form.tpl"
}
func (this *AddLeaveController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-add") {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权设置"}
this.ServeJSON()
return
}
ltype, _ := this.GetInt("type")
if ltype <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择类型"}
this.ServeJSON()
return
}
startedstr := this.GetString("started")
startedtime := utils.GetDateParse(startedstr)
endedstr := this.GetString("ended")
endedtime := utils.GetDateParse(endedstr)
days, _ := this.GetFloat("days")
if days <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写请假天数"}
this.ServeJSON()
return
}
reason := this.GetString("reason")
if "" == reason {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写请假原因"}
this.ServeJSON()
return
}
approverids := strings.Trim(this.GetString("approverid"), ",")
if "" == approverids {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择审核批人"}
this.ServeJSON()
return
}
var filepath string
f, h, err := this.GetFile("picture")
if err == nil {
defer f.Close()
//生成上传路径
now := time.Now()
dir := "./static/uploadfile/" + strconv.Itoa(now.Year()) + "-" + strconv.Itoa(int(now.Month())) + "/" + strconv.Itoa(now.Day())
err1 := os.MkdirAll(dir, 0755)
if err1 != nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "目录权限不够"}
this.ServeJSON()
return
}
filename := h.Filename
if err != nil {
this.Data["json"] = map[string]interface{}{"code": 0, "message": err}
this.ServeJSON()
return
} else {
this.SaveToFile("picture", dir+"/"+filename)
filepath = strings.Replace(dir, ".", "", 1) + "/" + filename
}
}
var leave Leaves
leaveid := utils.SnowFlakeId()
leave.Id = leaveid
leave.Userid = this.BaseController.UserUserId
leave.Type = ltype
leave.Started = startedtime
leave.Ended = endedtime
leave.Days = days
leave.Reason = reason
leave.Picture = filepath
leave.Approverids = approverids
err = AddLeave(leave)
if err == nil {
//审批人入库
var leaveApp LeavesApprover
userids := strings.Split(approverids, ",")
for _, v := range userids {
userid, _ := strconv.Atoi(v)
id := utils.SnowFlakeId()
leaveApp.Id = id
leaveApp.Userid = int64(userid)
leaveApp.Leaveid = leaveid
AddLeavesApprover(leaveApp)
}
this.Data["json"] = map[string]interface{}{"code": 1, "message": "添加成功。请‘请假列表’中设置为正常,审批人才可以看到", "id": fmt.Sprintf("%d", leaveid)}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请假添加失败"}
}
this.ServeJSON()
}
type EditLeaveController struct {
controllers.BaseController
}
func (this *EditLeaveController) Get() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-edit") {
this.Abort("401")
}
idstr := this.Ctx.Input.Param(":id")
id, _ := strconv.Atoi(idstr)
leave, _ := GetLeave(int64(id))
if leave.Userid != this.BaseController.UserUserId {
this.Abort("401")
}
if leave.Status != 1 {
this.Abort("401")
}
this.Data["leave"] = leave
this.TplName = "leaves/form.tpl"
}
func (this *EditLeaveController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-edit") {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权设置"}
this.ServeJSON()
return
}
id, _ := this.GetInt64("id")
if id <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "参数出错"}
this.ServeJSON()
return
}
ltype, _ := this.GetInt("type")
if ltype <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请选择类型"}
this.ServeJSON()
return
}
startedstr := this.GetString("started")
startedtime := utils.GetDateParse(startedstr)
endedstr := this.GetString("ended")
endedtime := utils.GetDateParse(endedstr)
days, _ := this.GetFloat("days")
if days <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写请假天数"}
this.ServeJSON()
return
}
reason := this.GetString("reason")
if "" == reason {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请填写请假原因"}
this.ServeJSON()
return
}
var filepath string
f, h, err := this.GetFile("picture")
if err == nil {
defer f.Close()
//生成上传路径
now := time.Now()
dir := "./static/uploadfile/" + strconv.Itoa(now.Year()) + "-" + strconv.Itoa(int(now.Month())) + "/" + strconv.Itoa(now.Day())
err1 := os.MkdirAll(dir, 0755)
if err1 != nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "目录权限不够"}
this.ServeJSON()
return
}
filename := h.Filename
if err != nil {
this.Data["json"] = map[string]interface{}{"code": 0, "message": err}
this.ServeJSON()
return
} else {
this.SaveToFile("picture", dir+"/"+filename)
filepath = strings.Replace(dir, ".", "", 1) + "/" + filename
}
}
var leave Leaves
leave.Type = ltype
leave.Started = startedtime
leave.Ended = endedtime
leave.Days = days
leave.Reason = reason
leave.Picture = filepath
err = UpdateLeave(id, leave)
if err == nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "请假修改成功", "id": fmt.Sprintf("%d", id)}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请假修改失败"}
}
this.ServeJSON()
}
type AjaxLeaveDeleteController struct {
controllers.BaseController
}
func (this *AjaxLeaveDeleteController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-edit") {
this.Abort("401")
}
id, _ := this.GetInt64("id")
if id <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "参数出错"}
this.ServeJSON()
return
}
leave, _ := GetLeave(int64(id))
if leave.Userid != this.BaseController.UserUserId {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权操作"}
this.ServeJSON()
return
}
if leave.Status != 1 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请假状态修改成正常,不能再删除"}
this.ServeJSON()
return
}
err := DeleteLeave(id)
if err == nil {
this.Data["json"] = map[string]interface{}{"code": 1, "message": "删除成功"}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "删除失败"}
}
this.ServeJSON()
}
type AjaxLeaveStatusController struct {
controllers.BaseController
}
func (this *AjaxLeaveStatusController) Post() {
//权限检测
if !strings.Contains(this.GetSession("userPermission").(string), "leave-edit") {
this.Abort("401")
}
id, _ := this.GetInt64("id")
if id <= 0 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "参数出错"}
this.ServeJSON()
return
}
leave, _ := GetLeave(int64(id))
if leave.Userid != this.BaseController.UserUserId {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "无权操作"}
this.ServeJSON()
return
}
if leave.Status != 1 {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "请假状态修改成正常,不能再删除"}
this.ServeJSON()
return
}
err := ChangeLeaveStatus(id, 2)
if err == nil {
userids := strings.Split(leave.Approverids, ",")
for _, v := range userids {
//消息通知
userid, _ := strconv.Atoi(v)
var msg Messages
msg.Id = utils.SnowFlakeId()
msg.Userid = this.BaseController.UserUserId
msg.Touserid = int64(userid)
msg.Type = 4
msg.Subtype = 31
msg.Title = "去审批处理"
msg.Url = "/leave/approval/" + fmt.Sprintf("%d", leave.Id)
AddMessages(msg)
}
this.Data["json"] = map[string]interface{}{"code": 1, "message": "状态修改成功"}
} else {
this.Data["json"] = map[string]interface{}{"code": 0, "message": "状态修改失败"}
}
this.ServeJSON()
}
Go
1
https://gitee.com/lock-upme/opms.git
git@gitee.com:lock-upme/opms.git
lock-upme
opms
opms
558463a10cc0

搜索帮助