package oa_logic import ( "bytes" "context" "errors" "fmt" "github.com/360EntSecGroup-Skylar/excelize" "github.com/fonchain_enterprise/fonchain-main/api/employee" "github.com/fonchain_enterprise/fonchain-main/api/oa" "github.com/fonchain_enterprise/fonchain-main/pkg/e" "github.com/fonchain_enterprise/fonchain-main/pkg/model/oa_model" "github.com/fonchain_enterprise/fonchain-main/pkg/service" "github.com/fonchain_enterprise/fonchain-main/pkg/service/oa_new/common" "github.com/fonchain_enterprise/fonchain-main/pkg/service/oa_new/model" "github.com/fonchain_enterprise/fonchain-main/pkg/utils/excel" "github.com/fonchain_enterprise/fonchain-main/pkg/utils/holiday" "io" "os" "sort" "strconv" "time" ) func makeDateList(holidaysMap map[string]*holiday.RespData) (dateList []string) { dateList = make([]string, 0) sortDateList := make([]string, 0) // 获取日期列表 for d, _ := range holidaysMap { sortDateList = append(sortDateList, d) } sort.Strings(sortDateList) for i := 0; i < len(sortDateList); i++ { switch holidaysMap[sortDateList[i]].Type { case oa_model.HolidayDay: if holidaysMap[sortDateList[i]].WeekDay == 6 { dateList = append(dateList, "六") } else if holidaysMap[sortDateList[i]].WeekDay == 7 { dateList = append(dateList, "日") } case oa_model.FestivalHoliday: dateList = append(dateList, holidaysMap[sortDateList[i]].TypeDes) case oa_model.WorkDay: dateList = append(dateList, strconv.Itoa(int(holidaysMap[sortDateList[i]].Day))) continue } } return } func ExportActionRecord(dates []string, holidaysMap map[string]*holiday.RespData, accountInfos []*employee.FindEmployeeFileListInfo, fileName string) (url string, err error) { // 获取日期列表 tableDate := makeDateList(holidaysMap) if len(tableDate) == 0 { return "", errors.New(e.ErrQueryMultiData) } // 写表头 xlsx := excelize.NewFile() sheet := "Sheet1" bottomRightCell := excel.GetColumnIndex(len(dates) + 6) A1 := fmt.Sprintf("打卡时间表 统计日期:%s 至 %s", dates[0], dates[len(dates)-1]) A1Style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"left","vertical":"center"},"font":{"bold":true,"size":24,"family":"新宋体","color":"#008080"},"fill":{"type":"pattern","color":["#ccfcfc"],"pattern":1},"border":[{"type":"left","color":"000000","style":1},{"type":"top","color":"000000","style":1},{"type":"right","color":"000000","style":1},{"type":"bottom","color":"000000","style":1}]}`) A2 := fmt.Sprintf("报表生成时间: %s", common.ConvertCurrentTimeWithoutSecond()) A2Style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"left","vertical":"center"},"font":{"bold":false,"size":14,"family":"新宋体","color":"#008080"},"fill":{"type":"pattern","color":["#ccfcfc"],"pattern":1},"border":[{"type":"left","color":"000000","style":1},{"type":"top","color":"000000","style":1},{"type":"right","color":"000000","style":1},{"type":"bottom","color":"000000","style":1}]}`) A3 := []string{ "姓名", "考勤组", "部门", "工号", "职位", } A3 = append(A3, tableDate...) A3Style, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center","vertical":"center"},"font":{"bold":true,"size":12,"family":"新宋体"},"fill":{"type":"pattern","color":["#FFFFCC"],"pattern":1},"border":[{"type":"left","color":"000000","style":1},{"type":"top","color":"000000","style":1},{"type":"right","color":"000000","style":1},{"type":"bottom","color":"000000","style":1}]}`) xlsx.SetCellStyle(sheet, "A1", fmt.Sprintf("%s%d", bottomRightCell, 1), A1Style) xlsx.MergeCell(sheet, "A1", fmt.Sprintf("%s%d", bottomRightCell, 1)) xlsx.SetRowHeight(sheet, 1, 51.25) xlsx.SetCellValue(sheet, "A1", A1) xlsx.SetCellStyle(sheet, "A2", fmt.Sprintf("%s%d", bottomRightCell, 2), A2Style) xlsx.MergeCell(sheet, "A2", fmt.Sprintf("%s%d", bottomRightCell, 2)) xlsx.SetRowHeight(sheet, 2, 25.75) xlsx.SetCellValue(sheet, "A2", A2) xlsx.SetCellStyle(sheet, "A3", fmt.Sprintf("%s%d", bottomRightCell, 3), A3Style) xlsx.SetSheetRow(sheet, "A3", &A3) // 居中 换行 centerWrapStyle, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"center","vertical":"center","wrap_text":true},"font":{"bold":false,"size":12,"family":"黑体"},"border":[{"type":"left","color":"000000","style":1},{"type":"top","color":"000000","style":1},{"type":"right","color":"000000","style":1},{"type":"bottom","color":"000000","style":1}]}`) // 靠左 换行 leftWrapStyle, _ := xlsx.NewStyle(`{"alignment":{"horizontal":"left","vertical":"center","wrap_text":true},"font":{"bold":false,"size":12,"family":"黑体"},"border":[{"type":"left","color":"000000","style":1},{"type":"top","color":"000000","style":1},{"type":"right","color":"000000","style":1},{"type":"bottom","color":"000000","style":1}]}`) index := 3 // 获取员工考勤信息 for i := 0; i < len(accountInfos); i++ { index++ employeeInfo := accountInfos[i] fmt.Println("==================================== employeeInfo ==================================== ") fmt.Println("employeeInfo ", fmt.Sprintf(" v: %+v", employeeInfo)) fmt.Println("==================================== employeeInfo ==================================== ") // 获取员工档案信息 中的 部门 岗位信息 employeeBasicInfo := MakeDefaultOaMonthByDatesSingleEmployee(employeeInfo) xlsx.SetCellStyle(sheet, fmt.Sprintf("A%d", index), fmt.Sprintf("%s%d", bottomRightCell, index), centerWrapStyle) // 姓名 xlsx.SetCellValue(sheet, fmt.Sprintf("A%d", index), employeeInfo.Name) // 考勤组 xlsx.SetCellValue(sheet, fmt.Sprintf("B%d", index), employeeInfo.CurrentAttendanceSection) // 部门 xlsx.SetCellValue(sheet, fmt.Sprintf("C%d", index), employeeBasicInfo.DepartmentName) // 工号 xlsx.SetCellValue(sheet, fmt.Sprintf("D%d", index), employeeInfo.JobNumber) // 职位 xlsx.SetCellValue(sheet, fmt.Sprintf("E%d", index), employeeBasicInfo.PositionName) workingTime, workingTimeErr := QueryWorkingTimeFormal(employeeInfo.CurrentAttendanceSectionUuid) if workingTimeErr != nil { fmt.Println("QueryWorkingTimeFormal ", fmt.Sprintf(" v: %+v", workingTimeErr.Error())) continue } allRecordReq := &model.CollectionDetailReq{ StaffUID: employeeInfo.UserId, Dates: dates, } collectionDetailRes, allRecordeErr := AllRecord(allRecordReq, workingTime) if allRecordeErr != nil { return "", allRecordeErr } valueList := make([]string, 0) for j := 0; j < len(dates); j++ { value := "" for k := 0; k < len(collectionDetailRes.WorkDay); k++ { if dates[j] != collectionDetailRes.WorkDay[k].WorkDate { continue } workDay := collectionDetailRes.WorkDay[k] for l := 0; l < len(workDay.Records); l++ { value += workDay.Records[l].ActionTime switch workDay.Records[l].ActionType { case model.ActionTypeLate: value += " 迟到" case model.ActionTypeBefore: value += " 早退" case model.ActionTypeOutWorkLate: value += " 外勤迟到" case model.ActionTypeOutWorkBefore: value += " 外勤早退" case model.ActionTypeOutWork: value += " 外勤" } value += "\n" } } valueList = append(valueList, value) } xlsx.SetCellStyle(sheet, fmt.Sprintf("F%d", index), fmt.Sprintf("%s%d", bottomRightCell, index), leftWrapStyle) xlsx.SetSheetRow(sheet, fmt.Sprintf("F%d", index), &valueList) } // 将文件写入内存 var buffer bytes.Buffer if err = xlsx.Write(&buffer); err != nil { fmt.Println("xlsx.Write ", fmt.Sprintf(" v: %+v", err.Error())) return "", errors.New(e.ErrorWriteExcel) } url, err = saveFile(fileName, common.ActionTimeDir, &buffer) return url, err } func saveFile(fileName, fileDir string, buffer *bytes.Buffer) (url string, err error) { content := bytes.NewReader(buffer.Bytes()) localFile, err := os.Create("./data/" + fileName) fileDir = fileDir + "/" + fmt.Sprintf("%d", time.Now().Unix()) + "_" + fileName if err != nil { fmt.Println("saveFile Create ", fmt.Sprintf(" v: %+v", err.Error())) return "", errors.New(e.ErrorCreateLocalFile) } defer localFile.Close() // 将内容写入本地文件 _, err = io.Copy(localFile, content) if err != nil { return "", errors.New(e.ErrorWriteLocalFile) } inputUrl, err := service.PutBosWithName(localFile.Name(), true, fileDir) if err != nil { fmt.Println("PutBosWithName ", fmt.Sprintf(" v: %+v", err.Error())) return "", errors.New(e.ErrorPutOos) } return inputUrl, nil } func UpdateExportRecordExportFailed(exportRecordReq *oa.ExportRecordReq) error { exportRecordReq.Status = 3 _, err := service.GrpcOAImpl.UpdateExportRecord(context.Background(), exportRecordReq) return err } func UpdateExportRecordExportSuccess(exportRecordReq *oa.ExportRecordReq) error { exportRecordReq.Status = 1 _, err := service.GrpcOAImpl.UpdateExportRecord(context.Background(), exportRecordReq) return err } func AllEmployee() (*employee.FindEmployeeFileListResponse, error) { accountReq := new(employee.FindEmployeeFileListRequest) accountReq.WorkingConditionId = 80 accountReq.Page = 1 accountReq.PageSize = 999999 accountRes, err := service.GrpcEmployeeProvider.FindEmployeeFileList(context.Background(), accountReq) if err != nil { return nil, errors.New(e.ErrorEmployFindUser) } return accountRes, nil }