package controllers import ( "fmt" "github.com/gin-gonic/gin" "github.com/shopspring/decimal" "gorm.io/gorm" "silkserver/constvar" "silkserver/controllers/request" "silkserver/extend/code" "silkserver/extend/util" "silkserver/middleware" "silkserver/models" "silkserver/pkg/structx" "silkserver/pkg/timex" "strconv" "time" ) type SalaryPlanController struct { } // SaveSalaryPlan // // @Tags 员工薪资/薪酬方案 // @Summary 保存薪酬方案 // @Produce application/json // @Param object body models.SalaryPlan true "参数" // @Param Authorization header string true "token" // @Success 200 {object} util.Response "成功" // @Router /api-jl/v1/salary/saveSalaryPlan [post] func (slf SalaryPlanController) SaveSalaryPlan(c *gin.Context) { var params models.SalaryPlan err := c.BindJSON(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } if params.ID > 0 { //更新 err = models.NewSalaryPlanSearch().Save(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "保存失败") return } } else { //新建 info := middleware.GetUserInfo(c) params.AddPeople = info.NickName params.CreateTime = timex.TimeToString2(time.Now()) err = models.NewSalaryPlanSearch().Create(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "保存失败") return } } util.ResponseFormat(c, code.Success, "保存成功") } // GetSalaryPlanList // // @Tags 员工薪资/薪酬方案 // @Summary 获取薪酬方案列表 // @Produce application/json // @Param object body request.GetSalaryPlanList true "参数" // @Param Authorization header string true "token" // @Success 200 {object} util.ResponseList{data=[]models.SalaryPlan} "成功" // @Router /api-jl/v1/salary/getSalaryPlanList [post] func (slf SalaryPlanController) GetSalaryPlanList(c *gin.Context) { var params request.GetSalaryPlanList err := c.BindJSON(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } find, total, err := models.NewSalaryPlanSearch().SetPage(params.Page, params.PageSize).SetPreload(true).Find() if err != nil { util.ResponseFormat(c, code.RequestParamError, "查找失败") return } util.ResponseFormatList(c, code.Success, find, total) } // DeleteSalaryPlanInfo // // @Tags 员工薪资/薪酬方案 // @Summary 删除薪酬方案 // @Produce application/json // @Param id path string true "id" // @Param Authorization header string true "token" // @Success 200 {object} util.Response "成功" // @Router /api-jl/v1/salary/deleteSalaryPlanInfo/{id} [delete] func (slf SalaryPlanController) DeleteSalaryPlanInfo(c *gin.Context) { id := c.Param("id") if id == "" { util.ResponseFormat(c, code.RequestParamError, "无效的id") return } atoi, _ := strconv.Atoi(id) if atoi == 0 { util.ResponseFormat(c, code.RequestParamError, "无效的id") return } err := models.NewSalaryPlanSearch().SetId(atoi).Delete() if err != nil { util.ResponseFormat(c, code.RequestParamError, "删除失败") return } util.ResponseFormat(c, code.Success, "删除成功") } // SaveSalaryType // // @Tags 员工薪资/薪酬方案 // @Summary 保存薪资类型 // @Produce application/json // @Param object body request.SalaryType true "参数" // @Param Authorization header string true "token" // @Success 200 {object} util.Response "成功" // @Router /api-jl/v1/salary/saveSalaryType [post] func (slf SalaryPlanController) SaveSalaryType(c *gin.Context) { var params request.SalaryType err := c.BindJSON(¶ms) if err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } if params.Type == 0 { util.ResponseFormat(c, code.RequestParamError, "类型不能为空") return } miniDicts, err := models.NewMiniDictSearch().SetType(params.Type).FindNotTotal() if err != nil { util.ResponseFormat(c, code.RequestParamError, err) return } dicts := params.Values add := make([]*models.MiniDict, 0) del := make([]uint, 0) update := make([]*models.MiniDict, 0) for _, mini := range miniDicts { flag := true for i, value := range dicts { var dict models.MiniDict dict.Name = value.Name dict.IsDefault = value.IsDefault dict.Type = params.Type if value.Id == 0 { add = append(add, &dict) flag = false if i < len(dicts)-1 { dicts = append(dicts[:i], dicts[i+1:]...) } else { dicts = dicts[:i] } break } else if value.Id == mini.ID { dict.ID = mini.ID update = append(update, &dict) flag = false if i < len(dicts)-1 { dicts = append(dicts[:i], dicts[i+1:]...) } else { dicts = dicts[:i] } break } } if flag { del = append(del, mini.ID) } } //新增的 for _, value := range dicts { var dict models.MiniDict dict.Name = value.Name dict.IsDefault = value.IsDefault dict.Type = params.Type add = append(add, &dict) } err = models.WithTransaction(func(db *gorm.DB) error { if len(del) > 0 { err = models.NewMiniDictSearch().SetOrm(db).SetIds(del).Delete() if err != nil { return err } } if len(update) > 0 { err = models.NewMiniDictSearch().SetOrm(db).SaveBatch(update) if err != nil { return err } } if len(add) > 0 { err = models.NewMiniDictSearch().SetOrm(db).CreateBatch(add) if err != nil { return err } } return nil }) if err != nil { util.ResponseFormat(c, code.RequestParamError, "保存失败") return } util.ResponseFormat(c, code.Success, "保存成功") } // GetSalaryTypeList // // @Tags 员工薪资/薪酬方案 // @Summary 获取薪资类型列表 // @Produce application/json // @Param number path string true "type" "参数" // @Param Authorization header string true "token" // @Success 200 {object} util.ResponseList{data=[]models.MiniDict} "成功" // @Router /api-jl/v1/salary/getSalaryTypeList/{type} [get] func (slf SalaryPlanController) GetSalaryTypeList(c *gin.Context) { tp := c.Param("type") if tp == "" { util.ResponseFormat(c, code.RequestParamError, "无效的类型") return } atoi, _ := strconv.Atoi(tp) if atoi == 0 { util.ResponseFormat(c, code.RequestParamError, "无效的类型") return } dicts, err := models.NewMiniDictSearch().SetType(constvar.MiniDictType(atoi)).FindNotTotal() if err != nil { util.ResponseFormat(c, code.RequestParamError, "查询失败") return } util.ResponseFormat(c, code.Success, dicts) } // GetPayrollProductionCarList // // @Tags 员工薪资/薪酬方案 // @Summary 获取车台每天的产量列表 // @Produce application/json // @Param Authorization header string true "token" // @Param object query request.PayrollProductionCar true "查询参数" // @Success 200 {object} util.ResponseList{data=[]models.PayrollProductionCar} "成功" // @Router /api-jl/v1/salary/getPayrollProductionCarList [get] func (slf SalaryPlanController) GetPayrollProductionCarList(c *gin.Context) { var params request.PayrollProductionCar if err := c.ShouldBindQuery(¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } carSearch := models.NewPayrollProductionCarSearch() if len(params.Cycle) == 0 && len(params.Monthly) == 0 { util.ResponseFormat(c, code.RequestParamError, "请检查查询周期,必须输入按天查询或按月查询。格式:Cycle(yyyy-MM-dd) 或 Monthly(yyyy-MM)") return } carSearch.SetPage(params.Page, params.PageSize) carSearch.SetWorkshopNumber(params.WorkshopNumber).SetGroupNumber(params.GroupNumber).SetCarNumber(params.CarNumber).SetMarketNumber(params.MarketNumber).SetSpec(params.Spec) list, total, err := carSearch.Find() if err != nil { util.ResponseFormat(c, code.RequestParamError, "查找失败") return } util.ResponseFormatList(c, code.Success, list, total) } // GetPayrollProductionGroupList // // @Tags 员工薪资/薪酬方案 // @Summary 获取小组每天的产量列表 // @Produce application/json // @Param Authorization header string true "token" // @Param object query request.PayrollProductionGroup true "查询参数" // @Success 200 {object} util.ResponseList{data=[]models.PayrollProductionGroup} "成功" // @Router /api-jl/v1/salary/getPayrollProductionGroupList [get] func (slf SalaryPlanController) GetPayrollProductionGroupList(c *gin.Context) { var params request.PayrollProductionGroup if err := c.ShouldBindQuery(¶ms); err != nil { util.ResponseFormat(c, code.RequestParamError, "参数解析失败,数据类型错误") return } //params = request.PayrollProductionGroup{ // Cycle: c.Query("cycle"), // Monthly: c.Query("monthly"), // WorkshopNumber: c.Query("workshopNumber"), //} //params.GroupNumber, _ = strconv.Atoi(c.Query("groupNumber")) //params.Page, _ = strconv.Atoi(c.Query("page")) //params.PageSize, _ = strconv.Atoi(c.Query("pageSize")) if len(params.Cycle) == 0 && len(params.Monthly) == 0 { util.ResponseFormat(c, code.RequestParamError, "请检查查询周期,必须输入按天查询或按月查询。格式:Cycle(yyyy-MM-dd) 或 Monthly(yyyy-MM)") return } list, total, err := models.NewPayrollProductionGroupSearch().SetPage(params.Page, params.PageSize). SetCycle(params.Cycle).SetMonthly(params.Monthly).SetWorkshopNumber(params.WorkshopNumber).SetGroupNumber(params.GroupNumber). Find() if err != nil { util.ResponseFormat(c, code.RequestParamError, "查找失败") return } util.ResponseFormatList(c, code.Success, list, total) } // GetPayrollSalaryPlanList // // @Tags 员工薪资/薪酬方案 // @Summary 获取人员每月的薪资列表 // @Produce application/json // @Param Authorization header string true "token" // @Param object query request.PayrollSalaryPlan true "查询参数" // @Success 200 {object} util.ResponseList{data=[]models.PayrollSalaryPlan} "成功" // @Router /api-jl/v1/salary/getPayrollSalaryPlanList [get] // // Deprecated: 此方法将在未来版本中删除 func (slf SalaryPlanController) GetPayrollSalaryPlanList(c *gin.Context) { var params request.PayrollSalaryPlan 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 } list, total, err := models.NewPayrollSalaryPlanSearch().SetPage(params.Page, params.PageSize). SetCycle(params.Cycle).SetWorkerID(params.WorkerID).SetKeyword(params.Keyword).SetWorkTypeID(uint(params.WorkTypeID)).SetWorkTypeCode(constvar.JobType(params.WorkTypeCode)). Find() if err != nil { util.ResponseFormat(c, code.RequestParamError, "查找失败") return } 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)") return } groupSearch := &models.ConstituteGroupSearch{PageNum: params.Page, PageSize: params.PageSize} groupSearch.Cycle = params.Cycle groupSearch.WorkerID = params.WorkerID groupSearch.WorkTypeCode = constvar.JobType(params.WorkTypeCode) groupList, err := models.NewPayrollConstituteSearch().ConstituteGroup(groupSearch) if err != nil { util.ResponseFormat(c, code.RequestParamError, "查找工资分类失败") return } groupMap := make(map[string][]*models.ConstituteGroup, 0) workerIds := make([]string, 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} } workerIds = append(workerIds, group.WorkerID) } // 员工信息 workers, total, err := models.NewWorkerSearch().SetOrder("id").SetPage(params.Page, params.PageSize).SetIds(workerIds).Find() 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). SetWorkerIDs(workerIds). 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 _, worker := range workers { result := make(map[string]interface{}) result["cycle"] = params.Cycle result["worker"] = worker amount := decimal.NewFromInt(0) constituteList := make([]map[string]interface{}, 0) if group, ok := groupMap[worker.ID]; ok { // 人员信息 for _, v := range group { temp := make(map[string]interface{}) temp["salaryPlanId"] = v.SalaryPlanId if _, ok := salaryPlanMap[v.SalaryPlanId]; ok { temp["salaryPlan"] = salaryPlanMap[v.SalaryPlanId] // 薪资方案 } key := fmt.Sprintf("%v%v%v", v.Cycle, v.WorkerID, v.SalaryPlanId) if payrollConstitute, ok := constituteMap[key]; ok { temp["amount"] = payrollConstitute.Amount amount = amount.Add(payrollConstitute.Amount) } constituteList = append(constituteList, temp) } } result["amount"] = amount // 应发工资 result["list"] = constituteList // 工资详情 result["remark"] = "备注信息" // 备注信息 list = append(list, result) } /* 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 // 工资详情 result["remark"] = "备注信息" // 备注信息 list = append(list, result) }*/ util.ResponseFormatList(c, code.Success, list, total) }