| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- package rentComputer
- import (
- "context"
- "errors"
- "github.com/xuri/excelize/v2"
- "go.uber.org/zap"
- "gorm.io/gorm"
- "log-server/global"
- "log-server/model/rentComputer"
- "log-server/model/rentComputer/request"
- "log-server/model/rentComputer/response"
- "math"
- "os"
- "strconv"
- "time"
- )
- type ServiceRentComputer struct {
- }
- func (s *ServiceRentComputer) RentComputerList(ctx context.Context, api rentComputer.RentComputer, info request.PageInfo, order string, desc bool) (interface{}, int64, error) {
- db := global.GVA_DB.Model(&rentComputer.RentComputer{})
- db = db.Select("rent_computer.id,rent_computer.update_time,rent_computer.create_time,rent_computer.pc_num,rent_computer.set_meal_id,rent_computer.director_name," +
- "rent_computer.pc_name,rent_computer.shop_id,rent_computer.rent_start,rent_computer.rent_duration,rent_computer.is_off_shelf,rent_computer.rent_end," +
- "rent_computer.remark,rent_computer.todesk_id,rent_computer.todesk_password,rent_computer.sunflower_id,rent_computer.sunflower_password,rent_computer.rent_price_used,rent_computer.is_expire," +
- "s.name as shop_name, r.name as set_meal_name,r.rent_price,r.price_type,r.rent_price_day")
- db = db.Joins("left join rent_computer_shop s on s.id = rent_computer.shop_id")
- db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
- if api.PcNum != "" {
- db = db.Where("rent_computer.pc_num = ?", api.PcNum)
- }
- if api.ShopId != 0 {
- db = db.Where("rent_computer.shop_id = ?", api.ShopId)
- }
- if api.SetMealId != 0 {
- db = db.Where("rent_computer.set_meal_id = ?", api.SetMealId)
- }
- if api.DirectorName != "ALL" {
- if api.DirectorName == "" {
- db = db.Where("rent_computer.director_name IS NULL")
- } else {
- db = db.Where("rent_computer.director_name = ?", api.DirectorName)
- }
- }
- //
- //global.GVA_LOG.Info(strconv.Itoa(int(api.IsOffShelf)))
- //global.GVA_LOG.Info(strconv.Itoa(int(api.IsExpire)))
- //if api.IsOffShelf != -1 {
- // db = db.Where("rent_computer.is_off_shelf = ?", api.IsOffShelf)
- //}
- if api.IsExpire != -1 {
- db = db.Where("rent_computer.is_expire = ?", api.IsExpire)
- }
- var total int64
- err := db.Count(&total).Error
- if err != nil {
- return nil, 0, err
- }
- limit := info.PageSize
- offset := info.PageSize * (info.Page - 1)
- //var statisticsLogs []*log.LogComputer
- var statisticscomputers []*response.ComputerStatisticsReply2
- db = db.Limit(limit).Offset(offset)
- if order != "" {
- var OrderStr string
- // 设置有效排序key 防止sql注入
- // 感谢 Tom4t0 提交漏洞信息
- orderMap := make(map[string]bool, 3)
- orderMap["pc_num"] = true
- //orderMap["game_id"] = true
- //orderMap["operator"] = true
- if orderMap[order] {
- if desc {
- OrderStr = order + " desc"
- } else {
- OrderStr = order
- }
- } else { // didn't matched any order key in `orderMap`
- global.GVA_LOG.Error("获取失败!", zap.Error(err))
- return statisticscomputers, total, err
- }
- err = db.Order(OrderStr).Find(&statisticscomputers).Error
- } else {
- err = db.Order("pc_num").Find(&statisticscomputers).Error
- }
- if err != nil {
- return nil, 0, err
- }
- var statisticsLogsComputer []*response.ComputerStatisticsReply2
- for _, statisticsLog := range statisticscomputers {
- statisticscomputer := new(response.ComputerStatisticsReply2)
- statisticscomputer.Id = statisticsLog.Id
- statisticscomputer.UpdateTime = statisticsLog.UpdateTime
- statisticscomputer.CreateTime = statisticsLog.CreateTime
- statisticscomputer.PcNum = statisticsLog.PcNum
- statisticscomputer.PcName = statisticsLog.PcName
- statisticscomputer.ShopId = statisticsLog.ShopId
- statisticscomputer.RentStart = statisticsLog.RentStart
- statisticscomputer.RentDuration = statisticsLog.RentDuration
- statisticscomputer.RentEnd = statisticsLog.RentEnd
- statisticscomputer.Remark = statisticsLog.Remark
- statisticscomputer.TodeskId = statisticsLog.TodeskId
- statisticscomputer.TodeskPassword = statisticsLog.TodeskPassword
- statisticscomputer.SunflowerId = statisticsLog.SunflowerId
- statisticscomputer.SunflowerPassword = statisticsLog.SunflowerPassword
- statisticscomputer.RentPrice = statisticsLog.RentPrice
- statisticscomputer.RentPriceDay = statisticsLog.RentPriceDay
- statisticscomputer.RentPriceUsed = statisticsLog.RentPriceUsed
- statisticscomputer.IsExpire = statisticsLog.IsExpire
- statisticscomputer.SetMealId = statisticsLog.SetMealId
- statisticscomputer.PriceType = statisticsLog.PriceType
- statisticscomputer.IsOffShelf = statisticsLog.IsOffShelf
- statisticscomputer.ShopName = statisticsLog.ShopName
- statisticscomputer.SetMealName = statisticsLog.SetMealName
- statisticscomputer.DirectorName = statisticsLog.DirectorName
- statisticsLogsComputer = append(statisticsLogsComputer, statisticscomputer)
- }
- return statisticsLogsComputer, total, err
- }
- func (s *ServiceRentComputer) GetRentComputerNum(ctx context.Context, api rentComputer.RentComputer) int64 {
- var total int64
- db := global.GVA_DB.Model(&rentComputer.RentComputer{})
- db = db.Distinct("id")
- //db = db.Where("is_off_shelf = 0 and is_expire != 1")
- if api.PcNum != "" {
- db = db.Where("rent_computer.pc_num = ?", api.PcNum)
- }
- if api.ShopId != 0 {
- db = db.Where("rent_computer.shop_id = ?", api.ShopId)
- }
- if api.SetMealId != 0 {
- db = db.Where("rent_computer.set_meal_id = ?", api.SetMealId)
- }
- if api.DirectorName != "ALL" {
- if api.DirectorName == "" {
- db = db.Where("rent_computer.director_name IS NULL")
- } else {
- db = db.Where("rent_computer.director_name = ?", api.DirectorName)
- }
- }
- if api.IsExpire != -1 {
- db = db.Where("rent_computer.is_expire = ?", api.IsExpire)
- }
- _ = db.Count(&total).Error
- return total
- }
- func (s *ServiceRentComputer) AddRentComputer(requestCoding request.ComputerRequest) (err error) {
- if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ?", requestCoding.PcNum, requestCoding.ShopId).First(&rentComputer.RentComputer{}).Error, gorm.ErrRecordNotFound) {
- return errors.New("租机编号已存在")
- }
- computer := new(rentComputer.RentComputer)
- //computer.Id = requestCoding.Id
- computer.PcNum = requestCoding.PcNum
- computer.PcName = requestCoding.PcName
- computer.ShopId = requestCoding.ShopId
- computer.RentStart = requestCoding.RentStart
- computer.RentDuration = requestCoding.RentDuration
- computer.RentEnd = requestCoding.RentEnd
- computer.Remark = requestCoding.Remark
- computer.TodeskId = requestCoding.TodeskId
- computer.TodeskPassword = requestCoding.TodeskPassword
- computer.SunflowerId = requestCoding.SunflowerId
- computer.SunflowerPassword = requestCoding.SunflowerPassword
- computer.RentPriceUsed = requestCoding.RentPriceUsed
- computer.IsExpire = requestCoding.IsExpire
- computer.SetMealId = requestCoding.SetMealId
- computer.IsOffShelf = requestCoding.IsOffShelf
- computer.DirectorName = requestCoding.DirectorName
- computer.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- computer.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
- //key := codeListCacheKey
- //_ = s.cache.DelBatheHsCache(context.Background(), key)
- return global.GVA_DB.Create(&computer).Error
- }
- func (s *ServiceRentComputer) GetRentComputerById(id int) (computerShop response.ComputerStatisticsReply2, err error) {
- db := global.GVA_DB.Model(&rentComputer.RentComputer{})
- var api response.ComputerStatisticsReply2
- db = db.Select("rent_computer.id,rent_computer.update_time,rent_computer.create_time,rent_computer.pc_num,rent_computer.set_meal_id,rent_computer.director_name," +
- "rent_computer.pc_name,rent_computer.shop_id,rent_computer.rent_start,rent_computer.rent_duration,rent_computer.is_off_shelf,rent_computer.rent_end," +
- "rent_computer.remark,rent_computer.todesk_id,rent_computer.todesk_password,rent_computer.sunflower_id,rent_computer.sunflower_password,rent_computer.rent_price_used,rent_computer.is_expire," +
- "s.name as shop_name, r.name as set_meal_name,r.rent_price,r.price_type,r.rent_price_day")
- db = db.Joins("left join rent_computer_shop s on s.id = rent_computer.shop_id")
- db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
- //db = db.Joins("left join responsible_person p on p.name = rent_computer.director_name")
- err = db.Where("rent_computer.id = ?", id).Order(id).Limit(1).Find(&api).Error
- if err != nil {
- return
- }
- computerShop.Id = api.Id
- computerShop.PcNum = api.PcNum
- computerShop.PcName = api.PcName
- computerShop.RentStart = api.RentStart
- computerShop.RentDuration = api.RentDuration
- computerShop.RentEnd = api.RentEnd
- computerShop.TodeskId = api.TodeskId
- computerShop.TodeskPassword = api.TodeskPassword
- computerShop.SunflowerId = api.SunflowerId
- computerShop.SunflowerPassword = api.SunflowerPassword
- computerShop.RentPrice = api.RentPrice
- computerShop.RentPriceDay = api.RentPriceDay
- computerShop.RentPriceUsed = api.RentPriceUsed
- computerShop.IsExpire = api.IsExpire
- computerShop.SetMealId = api.SetMealId
- computerShop.PriceType = api.PriceType
- computerShop.IsOffShelf = api.IsOffShelf
- //global.GVA_LOG.Info(strconv.Itoa(api.IsOffShelf))
- //global.GVA_LOG.Info(strconv.Itoa(computerShop.IsOffShelf))
- computerShop.ShopName = api.ShopName
- computerShop.SetMealName = api.SetMealName
- computerShop.Remark = api.Remark
- computerShop.ShopId = api.ShopId
- computerShop.DirectorName = api.DirectorName
- computerShop.UpdateTime = api.UpdateTime
- computerShop.CreateTime = api.CreateTime
- return
- }
- func (s *ServiceRentComputer) EditRentComputer(computer rentComputer.RentComputer) (err error) {
- err = global.GVA_DB.Save(computer).Error
- return err
- }
- func (s *ServiceRentComputer) DeleteRentComputerByIds(ids request.IdsReq) (err error) {
- err = global.GVA_DB.Delete(&[]rentComputer.RentComputer{}, "id in ?", ids.Ids).Error
- return err
- }
- func (s *ServiceRentComputer) DeleteRentComputerById(api rentComputer.RentComputer) (err error) {
- var entity rentComputer.RentComputer
- //global.GVA_LOG.Info(strconv.Itoa(int(api.Id)))
- err = global.GVA_DB.Where("id = ?", api.Id).First(&entity).Error // 根据id查询api记录
- if errors.Is(err, gorm.ErrRecordNotFound) { // api记录不存在
- return err
- }
- return global.GVA_DB.Delete(&entity).Error
- }
- // RenewRentComputer 租机续费
- func (s *ServiceRentComputer) RenewRentComputer(api request.RenewRentComputerRequest) (err error) {
- var info rentComputer.RentComputer
- if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", api.PcId).First(&info).Error, gorm.ErrRecordNotFound) {
- return err
- }
- //global.GVA_LOG.Info(info.PcNum)
- remark := time.Now().Format("2006-01-02 15:04:05") + ",租机编号" + api.PcNum + "续费" + strconv.Itoa(api.Day) + "天,续费至" + api.RentRenew + ";\n"
- global.GVA_LOG.Info(remark)
- api.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
- nowUnix := time.Now().Unix()
- rentRenew, _ := time.ParseInLocation("2006-01-02 15:04:05", api.RentRenew, time.Local)
- if rentRenew.Unix() < nowUnix {
- api.IsExpire = 1
- } else if rentRenew.Unix()-nowUnix <= 24*60*60 {
- api.IsExpire = 2
- } else {
- api.IsExpire = 0
- }
- //global.GVA_LOG.Info(strconv.Itoa(api.IsExpire))
- api.Remark = info.Remark + remark
- var updateInfo rentComputer.RentComputer
- updateInfo.UpdateTime = api.UpdateTime
- updateInfo.Id = api.PcId
- updateInfo.IsExpire = api.IsExpire
- updateInfo.RentDuration = info.RentDuration + api.Day
- updateInfo.RentEnd = api.RentRenew
- updateInfo.Remark = api.Remark
- err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", api.PcId).Updates(updateInfo).Error
- return err
- }
- // RentingOutRentComputer 租机退租
- func (s *ServiceRentComputer) RentingOutRentComputer(api request.RentingOutRentComputerRequest) (errMessage string, err error, okMessage string) {
- var info rentComputer.RentComputer
- if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ? ", api.PcId).First(&info).Error, gorm.ErrRecordNotFound) {
- return "待退租的租机编号不存在", err, ""
- }
- nowUnix := time.Now().Unix()
- now := time.Now().Format("2006-01-02 15:04:05")
- const oneDayUnix = 24 * 60 * 60
- rentEnd1, _ := time.ParseInLocation("2006-01-02 15:04:05", info.RentEnd, time.Local)
- rentEnd2, _ := time.ParseInLocation("2006-01-02 15:04:05", api.RentEnd, time.Local)
- if rentEnd1.Unix() < nowUnix {
- return "待退租的租机已到期", err, ""
- }
- surplusDay := math.Ceil(float64((rentEnd1.Unix() - rentEnd2.Unix()) / oneDayUnix))
- remark := now + ",租机编号" + info.PcNum + "退租,剩余" + strconv.Itoa(int(surplusDay)) + "天,"
- for i, v := range api.AddList {
- //global.GVA_LOG.Info(v.PcNum)
- //global.GVA_LOG.Info(strconv.Itoa(v.AddDay))
- var infoTemp rentComputer.RentComputer
- if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("pc_num = ? and shop_id = ?", v.PcNum, info.ShopId).First(&infoTemp).Error, gorm.ErrRecordNotFound) {
- return "增加时间的租机编号不存在", err, ""
- }
- if i < len(api.AddList) {
- remark += "编号" + v.PcNum + "加" + strconv.Itoa(v.AddDay) + "天,"
- } else {
- remark += "编号" + v.PcNum + "加" + strconv.Itoa(v.AddDay) + "天;\n"
- }
- infoTemp.UpdateTime = now
- infoTemp.RentDuration = infoTemp.RentDuration + v.AddDay
- rentEndTemp, _ := time.ParseInLocation("2006-01-02 15:04:05", infoTemp.RentEnd, time.Local)
- infoTemp.RentEnd = rentEndTemp.AddDate(0, 0, v.AddDay).Format("2006-01-02 15:04:05")
- err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", infoTemp.Id).Updates(infoTemp).Error
- if err != nil {
- return "增加时间的租机信息修改失败!", err, ""
- }
- }
- //修改退租机器信息
- info.RentEnd = api.RentEnd
- info.RentDuration = info.RentDuration - int(surplusDay)
- info.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
- info.Remark = info.Remark + remark
- err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", info.Id).Updates(info).Error
- if err != nil {
- return "退租机器信息修改失败!", err, ""
- }
- return "", err, remark
- }
- // ReplaceNumRentComputer 租机换编号
- func (s *ServiceRentComputer) ReplaceNumRentComputer(api request.ReplaceNumRentComputerRequest) (errMessage string, err error, okMessage string) {
- var infoOld rentComputer.RentComputer
- if errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("pc_num = ? and shop_id = ?", api.PcNumOld, api.ShopId).First(&infoOld).Error, gorm.ErrRecordNotFound) {
- return "待更换的租机编号不存在", err, ""
- }
- nowUnix := time.Now().Unix()
- const oneDayUnix = 24 * 60 * 60
- rentEndOld, _ := time.ParseInLocation("2006-01-02 15:04:05", infoOld.RentEnd, time.Local)
- if rentEndOld.Unix() < nowUnix {
- return "待更换的租机已到期", err, ""
- }
- var infoNew rentComputer.RentComputer
- if !errors.Is(global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("pc_num = ? and shop_id = ?", api.PcNumNew, api.ShopId).First(&infoNew).Error, gorm.ErrRecordNotFound) {
- return "要更换的租机编号已存在,不允许更换", err, ""
- }
- remark := time.Now().Format("2006-01-02 15:04:05") + ",租机编号" + api.PcNumOld + "更换为租机编号" + api.PcNumNew + ";\n"
- okMessage = remark
- rentDurationNew := math.Ceil(float64((rentEndOld.Unix() - nowUnix) / oneDayUnix))
- infoNew.PcNum = api.PcNumNew
- infoNew.PcName = api.PcNumNew
- infoNew.ShopId = api.ShopId
- infoNew.RentStart = time.Now().Format("2006-01-02 15:04:05")
- infoNew.RentDuration = int(rentDurationNew)
- infoNew.RentEnd = infoOld.RentEnd
- infoNew.Remark = remark
- infoNew.TodeskId = ""
- infoNew.TodeskPassword = ""
- infoNew.SunflowerId = ""
- infoNew.SunflowerPassword = ""
- infoNew.RentPriceUsed = 0
- infoNew.IsExpire = 0
- infoNew.SetMealId = infoOld.SetMealId
- infoNew.IsOffShelf = 0
- infoNew.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- infoNew.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
- // 修改旧编号信息
- if infoOld.Remark != "" {
- remark = infoOld.Remark + ";" + remark
- }
- rentStartOld, _ := time.ParseInLocation("2006-01-02 15:04:05", infoOld.RentStart, time.Local)
- rentDurationOld := math.Ceil(float64((rentEndOld.Unix() - rentStartOld.Unix()) / oneDayUnix))
- infoOld.RentDuration = int(rentDurationOld)
- infoOld.RentEnd = time.Now().Format("2006-01-02 15:04:05")
- infoOld.Remark = remark
- infoOld.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
- err = global.GVA_DB.Model(&rentComputer.RentComputer{}).Where("id = ?", infoOld.Id).Updates(infoOld).Error
- if err != nil {
- return "旧编号租机修改失败!", err, ""
- }
- // 插入新编号至数据库
- err = global.GVA_DB.Create(&infoNew).Error
- if err != nil {
- return "新编号租机创建失败!", err, ""
- }
- return "", err, okMessage
- }
- func (exa *ServiceRentComputer) ParseExcel2InfoList(filePath string) (err error) {
- skipHeader := true
- fixedHeader := []string{"租机编码", "供应商", "所属套餐", "套餐类型", "起租日", "到期日", "todesk号", "todesk密码", "向日葵号", "向日葵密码"}
- file, err := excelize.OpenFile(filePath)
- if err != nil {
- return err
- }
- var insertComputers []*rentComputer.RentComputer
- //var updateComputers []*rentComputer.RentComputer
- rows, err := file.Rows("Sheet1")
- if err != nil {
- return err
- }
- //查询数据库里的套餐与供应商=====================================
- var setMeals []*response.SetMealStatisticsReply1
- db := global.GVA_DB.Model(&rentComputer.RentSetMeal{})
- db = db.Select("rent_set_meal.id, rent_set_meal.name," +
- "rent_set_meal.price_type,rent_set_meal.shop_id,s.name as shop_name")
- db = db.Joins("left join rent_computer_shop s on s.id = rent_set_meal.shop_id")
- err = db.Order("id").Find(&setMeals).Error
- if err != nil {
- return err
- }
- // =========================================================
- index := 0
- for rows.Next() {
- index++
- row, err := rows.Columns()
- if err != nil {
- return err
- }
- if skipHeader {
- if exa.compareStrSlice(row, fixedHeader) {
- skipHeader = false
- continue
- } else {
- return errors.New("Excel格式错误")
- }
- }
- if len(row) == 0 {
- //global.GVA_LOG.Info("数组为空")
- continue
- }
- c := new(rentComputer.RentComputer)
- c.PcNum = row[0]
- c.PcName = row[0]
- flagShopId := false
- flagSetMealName := false
- for _, setMeal := range setMeals {
- if row[1] == setMeal.ShopName {
- flagShopId = true
- c.ShopId = setMeal.ShopId
- //break
- }
- priceType := 0
- if row[3] == "天卡" {
- priceType = 0
- } else if row[3] == "周卡" {
- priceType = 1
- } else {
- priceType = 2
- }
- if row[2] == setMeal.Name && priceType == setMeal.PriceType {
- flagSetMealName = true
- c.SetMealId = setMeal.Id
- break
- }
- }
- if flagShopId == false || flagSetMealName == false {
- return errors.New("供应商错误")
- }
- // 时间统一设置为日期+12:00:00
- rentStart, _ := time.ParseInLocation("2006-01-02 15:04:05", row[4]+" 12:00:00", time.Local)
- rentEnd, _ := time.ParseInLocation("2006-01-02 15:04:05", row[5]+" 12:00:00", time.Local)
- //global.GVA_LOG.Info(strconv.FormatInt(rentStart.Unix(), 10))
- //global.GVA_LOG.Info(strconv.FormatInt(rentEnd.Unix(), 10))
- c.RentStart = row[4] + " 00:00:00"
- c.RentEnd = row[5] + " 00:00:00"
- c.RentDuration = int(math.Abs(float64(SubDays(rentStart, rentEnd)))) + 1
- c.IsExpire = 0
- c.IsOffShelf = 0
- // 获取工作表中指定单元格的值
- todeskId, _ := file.GetCellValue("Sheet1", "G"+strconv.Itoa(index))
- todeskPassword, _ := file.GetCellValue("Sheet1", "H"+strconv.Itoa(index))
- sunflowerId, _ := file.GetCellValue("Sheet1", "I"+strconv.Itoa(index))
- sunflowerPassword, _ := file.GetCellValue("Sheet1", "J"+strconv.Itoa(index))
- c.TodeskId = todeskId //todesk账号密码
- c.TodeskPassword = todeskPassword //todesk账号密码
- c.SunflowerId = sunflowerId //向日葵账号密码
- c.SunflowerPassword = sunflowerPassword //向日葵账号密码
- c.CreateTime = time.Now().Format("2006-01-02 15:04:05")
- c.UpdateTime = time.Now().Format("2006-01-02 15:04:05")
- // 租机编号已存在,存入更新数组中
- if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ?", c.PcNum, c.ShopId).First(&rentComputer.RentComputer{}).Error, gorm.ErrRecordNotFound) {
- // 租机编号已存在,存入更新数组中
- //updateComputers = append(updateComputers, c)
- continue
- } else {
- insertComputers = append(insertComputers, c)
- }
- }
- defer func() {
- global.GVA_LOG.Info("defer 删除文件")
- err = os.RemoveAll(filePath)
- if err != nil {
- return
- }
- }()
- //if updateComputers != nil {
- // err = global.GVA_DB.Updates(updateComputers).Error
- // if err != nil {
- // return
- // }
- //}
- if insertComputers != nil {
- err = global.GVA_DB.Create(insertComputers).Error
- if err != nil {
- return
- }
- }
- //var cp log.Computer
- //err = cp.DelAllOnlinePcCodeCache()
- return
- }
- func (exa *ServiceRentComputer) compareStrSlice(a, b []string) bool {
- if len(a) != len(b) {
- return false
- }
- if (b == nil) != (a == nil) {
- return false
- }
- for key, value := range a {
- if value != b[key] {
- return false
- }
- }
- return true
- }
- // CheckIsExpire 定时检查是否有电脑到期,修改租机状态
- func (s *ServiceRentComputer) CheckIsExpire() {
- global.GVA_LOG.Info("检查是否有电脑到期开始执行:" + time.Now().Format("2006-01-02 15:04:05"))
- db := global.GVA_DB.Model(&rentComputer.RentComputer{})
- //SELECT pc_num,rent_start,rent_end,is_expire FROM shuyou_rent_computer
- //db = db.Where("is_off_shelf = 0")
- var computers []*rentComputer.RentComputer
- err := db.Order("id").Find(&computers).Error
- if err != nil {
- global.GVA_LOG.Error("检查是否有电脑到期执行失败:" + time.Now().Format("2006-01-02 15:04:05"))
- return
- }
- const overtime = 24 * 60 * 60
- nowUnix := time.Now().Unix()
- //global.GVA_LOG.Info(strconv.FormatInt(nowUnix, 10))
- //global.GVA_LOG.Info("---------------------------------------------------------------------------------")
- computer := new(rentComputer.RentComputer)
- for _, c := range computers {
- endUnix, _ := time.ParseInLocation("2006-01-02 15:04:05", c.RentEnd, time.Local)
- //global.GVA_LOG.Info(strconv.FormatInt(endUnix.Unix(), 10))
- if endUnix.Unix() < nowUnix {
- computer.IsExpire = 1
- } else if endUnix.Unix()-nowUnix <= overtime {
- computer.IsExpire = 2
- } else {
- computer.IsExpire = 0
- }
- if c.IsExpire == computer.IsExpire {
- continue
- }
- computer.Id = c.Id
- computer.UpdateTime = c.UpdateTime
- computer.CreateTime = c.CreateTime
- computer.PcNum = c.PcNum
- computer.PcName = c.PcName
- computer.ShopId = c.ShopId
- computer.RentStart = c.RentStart
- computer.RentDuration = c.RentDuration
- computer.RentEnd = c.RentEnd
- computer.Remark = c.Remark
- computer.TodeskId = c.TodeskId
- computer.TodeskPassword = c.TodeskPassword
- computer.SunflowerId = c.SunflowerId
- computer.SunflowerPassword = c.SunflowerPassword
- computer.RentPriceUsed = c.RentPriceUsed
- computer.SetMealId = c.SetMealId
- computer.IsOffShelf = c.IsOffShelf
- err = global.GVA_DB.Save(computer).Error
- }
- global.GVA_LOG.Info("检查是否有电脑到期执行成功:" + time.Now().Format("2006-01-02 15:04:05"))
- return
- }
- // CreateRentComputerLedger 定时生成今日租机台账表(财务)
- func (s *ServiceRentComputer) CreateRentComputerLedger() {
- global.GVA_LOG.Info("定时生成今日租机台账表开始执行:" + time.Now().Format("2006-01-02 15:04:05"))
- //查询当前所有在租电脑
- db := global.GVA_DB.Model(&rentComputer.RentComputer{})
- var computers []*rentComputer.RentComputerSetMeal
- db = db.Select("rent_computer.pc_num, rent_computer.pc_name,rent_computer.shop_id, rent_computer.rent_start, rent_computer.rent_duration, rent_computer.rent_end, rent_computer.is_expire, " +
- "r.rent_price,r.rent_price_day,rent_computer.set_meal_id")
- db = db.Joins("left join rent_set_meal r on r.id = rent_computer.set_meal_id")
- db = db.Where("rent_computer.is_expire != 1")
- err := db.Order("rent_computer.id").Find(&computers).Error
- if err != nil {
- global.GVA_LOG.Error("定时生成今日租机台账表执行失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
- return
- }
- //查询昨日租机台账表中的rent_price_used_then参数
- var computersYest []*rentComputer.RentComputerLedger
- dbYest := global.GVA_DB.Model(&rentComputer.RentComputerLedger{})
- dbYest = dbYest.Select("rent_price_used_then").Order("id")
- err = dbYest.Find(&computersYest).Error
- if err != nil {
- global.GVA_LOG.Error("定时生成今日租机台账表执行失败,查询昨日数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
- return
- }
- nowTime := time.Now().Format("2006-01-02 15:04:05")
- nowDate := time.Now().Format("2006-01-02")
- var insertInfos []rentComputer.RentComputerLedger
- for _, computer := range computers {
- var info rentComputer.RentComputerLedger
- info.UpdateTime = nowTime
- info.CreateTime = nowTime
- info.NewDate = nowDate
- info.PcNum = computer.PcNum
- info.PcName = computer.PcName
- info.ShopId = computer.ShopId
- info.RentStartThen = computer.RentStart
- info.RentDurationThen = computer.RentDuration
- info.RentEndThen = computer.RentEnd
- info.RentPriceThen = computer.RentPrice
- info.RentPriceDayThen = computer.RentPriceDay
- info.IsExpire = computer.IsExpire
- info.SetMealId = computer.SetMealId
- info.Remark = computer.Remark
- flag := false
- for _, yest := range computersYest {
- if info.PcNum == yest.PcNum {
- flag = true
- info.RentPriceUsedThen = yest.RentPriceUsedThen + computer.RentPriceDay
- break
- }
- }
- if flag == false {
- info.RentPriceUsedThen = computer.RentPriceDay
- }
- if !errors.Is(global.GVA_DB.Where("pc_num = ? and shop_id = ? and new_date = ?", computer.PcNum, computer.ShopId, nowDate).First(&rentComputer.RentComputerLedger{}).Error, gorm.ErrRecordNotFound) {
- // 已存在,更新
- err = global.GVA_DB.Model(&rentComputer.RentComputerLedger{}).Where("pc_num = ? and shop_id = ? and new_date = ?", computer.PcNum, computer.ShopId, nowDate).Updates(info).Error
- if err != nil {
- global.GVA_LOG.Error("定时生成今日租机台账表执行失败,更新数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
- return
- }
- } else {
- insertInfos = append(insertInfos, info)
- }
- }
- if insertInfos != nil {
- err = global.GVA_DB.Create(&insertInfos).Error
- if err != nil {
- global.GVA_LOG.Error("定时生成今日租机台账表执行失败,插入数据失败:"+time.Now().Format("2006-01-02 15:04:05"), zap.Error(err))
- return
- }
- }
- global.GVA_LOG.Info("定时生成今日租机台账表执行成功:" + time.Now().Format("2006-01-02 15:04:05"))
- return
- }
- // SubDays 计算日期相差多少天
- // 返回值day>0, t1晚于t2; day<0, t1早于t2
- func SubDays(t1, t2 time.Time) (day int) {
- swap := false
- if t1.Unix() < t2.Unix() {
- t_ := t1
- t1 = t2
- t2 = t_
- swap = true
- }
- //global.GVA_LOG.Info(strconv.Itoa(int(t1.Unix())))
- //global.GVA_LOG.Info(strconv.Itoa(int(t2.Unix())))
- day = int(t1.Sub(t2).Hours() / 24)
- // 计算在被24整除外的时间是否存在跨自然日
- if int(t1.Sub(t2).Milliseconds())%86400000 > int(86400000-t2.Unix()%86400000) {
- day += 1
- }
- if swap {
- day = -day
- }
- //global.GVA_LOG.Info(strconv.Itoa(day))
- return
- }
|