| | |
| | | package controllers |
| | | |
| | | import ( |
| | | "fmt" |
| | | "github.com/gin-gonic/gin" |
| | | "github.com/shopspring/decimal" |
| | | "gorm.io/gorm" |
| | | "silkserver/constvar" |
| | | "silkserver/controllers/request" |
| | |
| | | "silkserver/extend/util" |
| | | "silkserver/middleware" |
| | | "silkserver/models" |
| | | "silkserver/pkg/structx" |
| | | "silkserver/pkg/timex" |
| | | "strconv" |
| | | "time" |
| | |
| | | |
| | | util.ResponseFormatList(c, code.Success, list, total) |
| | | } |
| | | |
| | | // SavePayrollConstitute |
| | | // |
| | | // @Tags 员工薪资/薪酬方案 |
| | | // @Summary 薪酬数额调整 |
| | | // @Produce application/json |
| | | // @Param object body request.SavePayrollConstitute true "参数" |
| | | // @Param Authorization header string true "token" |
| | | // @Success 200 {object} util.Response "成功" |
| | | // @Router /api-jl/v1/salary/savePayrollConstitute [post] |
| | | func (slf SalaryPlanController) SavePayrollConstitute(c *gin.Context) { |
| | | var params request.SavePayrollConstitute |
| | | var constitute models.PayrollConstitute |
| | | err := c.BindJSON(¶ms) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | err = structx.AssignTo(params, &constitute) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "数据转换失败") |
| | | return |
| | | } |
| | | if len(constitute.Cycle) < 7 { |
| | | util.ResponseFormat(c, code.RequestParamError, "周期参数为空或格式错误") |
| | | } |
| | | if len(constitute.WorkerID) == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "员工ID参数为空") |
| | | } |
| | | if constitute.SalaryPlanId == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "薪资方案ID参数为空") |
| | | } |
| | | |
| | | info := middleware.GetUserInfo(c) |
| | | if info == nil || info.NickName == "" { |
| | | util.ResponseFormat(c, code.RequestParamError, "用户未登录") |
| | | } |
| | | constitute.CreatedBy = info.NickName |
| | | |
| | | if payrollConstitute, err := models.NewPayrollConstituteSearch().SetCycle(params.Cycle).SetWorkerID(params.WorkerID).SetSalaryPlanId(params.SalaryPlanId).First(); err != nil && payrollConstitute != nil { |
| | | constitute.ID = payrollConstitute.ID |
| | | constitute.CreatedAt = payrollConstitute.CreatedAt |
| | | } |
| | | |
| | | if constitute.ID > 0 { |
| | | err = models.NewPayrollConstituteSearch().Save(&constitute) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "保存失败") |
| | | return |
| | | } |
| | | } else { |
| | | err = models.NewPayrollConstituteSearch().Create(&constitute) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "保存失败") |
| | | return |
| | | } |
| | | } |
| | | |
| | | util.ResponseFormat(c, code.Success, "保存成功") |
| | | } |
| | | |
| | | // GetPayrollConstituteList |
| | | // |
| | | // @Tags 员工薪资/薪酬方案 |
| | | // @Summary 获取人员每月的薪资列表 |
| | | // @Produce application/json |
| | | // @Param Authorization header string true "token" |
| | | // @Param object query request.PayrollConstitute true "查询参数" |
| | | // @Success 200 {object} util.ResponseList{data=map[string]interface{}} "成功" |
| | | // @Router /api-jl/v1/salary/getPayrollConstituteList [get] |
| | | func (slf SalaryPlanController) GetPayrollConstituteList(c *gin.Context) { |
| | | |
| | | var params request.PayrollConstitute |
| | | if err := c.ShouldBindQuery(¶ms); err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") |
| | | return |
| | | } |
| | | if len(params.Cycle) == 0 { |
| | | util.ResponseFormat(c, code.RequestParamError, "请检查查询周期。格式:Cycle(yyyy-MM-dd)") |
| | | return |
| | | } |
| | | |
| | | groupList, err := models.NewPayrollConstituteSearch().ConstituteGroup(&models.ConstituteGroup{Cycle: params.Cycle}) |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找工资分类失败") |
| | | return |
| | | } |
| | | groupMap := make(map[string][]*models.ConstituteGroup, 0) |
| | | for _, group := range groupList { |
| | | if _, ok := groupMap[group.WorkerID]; ok { |
| | | groupMap[group.WorkerID] = append(groupMap[group.WorkerID], group) |
| | | } else { |
| | | groupMap[group.WorkerID] = []*models.ConstituteGroup{group} |
| | | } |
| | | } |
| | | |
| | | // 员工信息 |
| | | workers, err := models.NewWorkerSearch().FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找用户失败") |
| | | } |
| | | workerMap := make(map[string]*models.Worker) |
| | | for _, worker := range workers { |
| | | workerMap[worker.ID] = worker |
| | | } |
| | | |
| | | // 工资方案 |
| | | salaryPlans, err := models.NewSalaryPlanSearch().FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找薪资方案失败") |
| | | } |
| | | salaryPlanMap := make(map[uint]*models.SalaryPlan) |
| | | for _, plan := range salaryPlans { |
| | | salaryPlanMap[plan.ID] = plan |
| | | } |
| | | |
| | | // 工资单项 |
| | | constituteList, err := models.NewPayrollConstituteSearch().SetCycle(params.Cycle).SetWorkerID(params.WorkerID).SetWorkTypeID(uint(params.WorkTypeID)).SetWorkTypeCode(params.WorkTypeCode).FindNotTotal() |
| | | if err != nil { |
| | | util.ResponseFormat(c, code.RequestParamError, "查找工资单项失败") |
| | | return |
| | | } |
| | | constituteMap := make(map[string]*models.PayrollConstitute) |
| | | for _, v := range constituteList { |
| | | key := fmt.Sprintf("%v%v%v", v.Cycle, v.WorkerID, v.SalaryPlanId) |
| | | constituteMap[key] = v |
| | | } |
| | | |
| | | var list []map[string]interface{} |
| | | for workerId, group := range groupMap { |
| | | result := make(map[string]interface{}) |
| | | result["cycle"] = params.Cycle |
| | | if _, ok := workerMap[workerId]; ok { // 人员信息 |
| | | result["worker"] = workerMap[workerId] |
| | | } |
| | | amount := decimal.NewFromInt(0) |
| | | constituteList := make([]map[string]interface{}, 0) |
| | | for _, v := range group { |
| | | temp := make(map[string]interface{}) |
| | | if _, ok := salaryPlanMap[v.SalaryPlanId]; ok { |
| | | temp["salaryPlan"] = salaryPlanMap[v.SalaryPlanId] |
| | | } |
| | | |
| | | key := fmt.Sprintf("%v%v%v", v.Cycle, v.WorkerID, v.SalaryPlanId) |
| | | if _, ok := constituteMap[key]; ok { |
| | | temp["amount"] = constituteMap[key].Amount |
| | | amount = amount.Add(constituteMap[key].Amount) |
| | | } |
| | | constituteList = append(constituteList, temp) |
| | | } |
| | | result["amount"] = amount // 应发工资 |
| | | result["list"] = constituteList // 工资详情 |
| | | |
| | | list = append(list, result) |
| | | } |
| | | |
| | | util.ResponseFormat(c, code.Success, list) |
| | | |
| | | } |