package service

import (
	"fmt"
	"github.com/robfig/cron"
	"kpt.xdmy/apiserver/model"
	"kpt.xdmy/apiserver/service/cownum_sqlserver"
	"time"
)

var s *Service

func SapJobInit() {

	duetimecst, _ := time.ParseInLocation("15:04:05", "00:01:00", time.Local)
	duetimecst1, _ := time.ParseInLocation("15:04:05", "00:00:00", time.Local)
	spec := fmt.Sprintf("@every %v", duetimecst.Sub(duetimecst1))
	c := cron.New()
	c.AddFunc(spec, func() {
		//AutoSapOrderJob()
		//AutoSrmOrderJob()
		//AutoSapReturnOrderJob()
		//AutoSrmReturnOrderJob()
	})
	c.Start()

	//pastureList := make([]*model.Pasture, 0)
	//s.d.DB.Table("pasture").Find(&pastureList)
	//var t time.Time
	//t = time.Now()
	//i := -320
	//for {
	//	i++
	//for _, pasture := range pastureList {
	//s.SapAssetSalvage(t, "1024", 346)
	//}
	//	if t.AddDate(0, 0, i).Format("2006-01-02") == "2023-08-24" {
	//		break
	//	}
	//}
	c1 := cron.New()
	c1.AddFunc("0 40 23 * * ?", func() {
		fmt.Println(time.Now())
		//更新设备维修和保养费用
		var t time.Time
		t = time.Now()
		var code string
		pastureList := make([]*model.Pasture, 0)
		err := s.d.DB.Table("pasture").Find(&pastureList)
		fmt.Println(err)
		for _, pasture := range pastureList {
			s.SapSupplier(t, pasture.CompanyCode, pasture.ID)
			s.SapAsset(t, code, pasture.CompanyCode)
		}
		s.SapMaterial(t, code)
		s.d.DB.Exec(`call createEqstatus()`)
	})

	//保养计划
	duetimecst, _ = time.ParseInLocation("15:04:05", "01:00:00", time.Local)
	duetimecst1, _ = time.ParseInLocation("15:04:05", "00:00:00", time.Local)
	spec1 := fmt.Sprintf("@every %v", duetimecst.Sub(duetimecst1))
	c1.AddFunc(spec1, func() {
		pastureList := make([]*model.Pasture, 0)
		s.d.DB.Table("pasture").Where("vendor = 1").Find(&pastureList)
		for _, pasture := range pastureList {
			fmt.Println(pasture.ID)
			s.d.DB.Exec(`call createRUcode(?)`, pasture.ID)
		}
	})

	duetimecst, _ = time.ParseInLocation("15:04:05", "00:05:00", time.Local)
	duetimecst1, _ = time.ParseInLocation("15:04:05", "00:00:00", time.Local)
	spec2 := fmt.Sprintf("@every %v", duetimecst.Sub(duetimecst1))
	c1.AddFunc(spec2, func() {
		s.d.DB.Exec(`call wxmsg()`)
	})

	c1.AddFunc("0 50 01 * * ?", func() {
		//更新设备维修和保养费用
		pastureList := make([]*model.Pasture, 0)
		s.d.DB.Table("pasture").Where("vendor = 1 ").Where("id != 18 ").Find(&pastureList)
		now := time.Now()
		start := now.AddDate(-1, 0, 0).Format("2006-01-02")
		end := now.Format("2006-01-02")
		for _, pasture := range pastureList {
			updateEqYearMaintainCostAndYearUpkeepCost(pasture.ID, start, end)
		}
	})

	c1.AddFunc("0 20 18 * * ?", func() {
		cownum_sqlserver.SyncCownum(s.sqlserver, s.d, time.Now().AddDate(0, 0, -3).Format("2006-01-02"))
		cownum_sqlserver.SyncCownum(s.sqlserver, s.d, time.Now().AddDate(0, 0, -2).Format("2006-01-02"))
		cownum_sqlserver.SyncCownum(s.sqlserver, s.d, time.Now().AddDate(0, 0, -1).Format("2006-01-02"))
		pastureList := make([]*model.Pasture, 0)
		s.d.DB.Table("pasture").Where("vendor = 1 ").Where("factory_code  != '' ").Where("factory_code  is not null ").Find(&pastureList)
		for _, item := range pastureList {
			AutoFeeQuery(item.FactoryCode, item.ID)
		}
	})

	c1.AddFunc("0 10 12 * * ?", func() {
		AutoContractToASP()
	})

	c1.AddFunc("0 20 23 * * ?", func() {
		AutoContractToASP()
	})
	//AutoContractToASP()
	//c1.Start()
}

func jobMaterial() {
	t := time.Now()
	var code string
	s.SapMaterial(t, code)
}
func jobTransfer() {
	autoTransfer()
}