package models import ( "fmt" "github.com/go-xorm/xorm" "lapp_-wy/utils" "strings" "time" ) type Chargeallocationtab struct { Cid int `json:"cid" xorm:"not null pk INT(4)"` Chargeallocationnr int `json:"chargeallocationnr" xorm:"not null pk INT(4)"` Chargenr int `json:"chargenr" xorm:"INT(4)"` Allocateyear int `json:"allocateyear" xorm:"INT(4)"` Allocatemonth int `json:"allocatemonth" xorm:"INT(2)"` Allocateexpense float64 `json:"allocateexpense" xorm:"DECIMAL(10,2)"` Createtime string `json:"createtime" xorm:"VARCHAR(14)"` Lastmodifytime string `json:"lastmodifytime" xorm:"VARCHAR(20)"` Lastmodifyby string `json:"lastmodifyby" xorm:"VARCHAR(20)"` } func (t *Chargeallocationtab) TableName() string { return "chargeallocationtab" } /*** *分摊算法:递归 *逻辑:把跨年的费用分摊到每年, *@param:begdate 开始时间 *@param:enddate 结束时间 *@param:money 分摊金额 *@param:chargenr 费用单号 ***********/ func ChargeDiGui(session *xorm.Session, cid int, chargenr int, begdate string, enddate string, money float64) bool { //计算当前开始时间的最后一天 begtime, _ := utils.TimeParseyyyyMMdd(begdate) lastYearDay := utils.TimeReturnLastYear(begtime) //比较日期,退出条件 t1, _ := utils.TimeParseyyyyMMdd(lastYearDay) t2, _ := utils.TimeParseyyyyMMdd(enddate) ok := t1.After(t2) if ok { //生成流水号 Cnr := new(Snrtab) Cnr.Cid = cid orderId, err := Cnr.GetNextSnr("Yearnr") if err != nil { return false } //直接添加费用记录 info := new(Chargeallocationtab) info.Cid = cid info.Chargenr = chargenr info.Createtime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss") info.Lastmodifytime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss") info.Allocateexpense = money info.Allocateyear = t1.Year() info.Chargeallocationnr = utils.ValueToInt(orderId, 0) _, err = session.Table("chargeallocationtab").Insert(info) if err != nil { return false } return true } //计算分摊比例金额,添加记录,并进入下一次递归循环 allday := utils.TimeSub(begtime, t2) //总天数 nowday := utils.TimeSub(begtime, t1) //当前年份总天数 rate := utils.ValueToFloat(nowday, 0.0) / utils.ValueToFloat(allday, 0.0) temMoney := money * rate //当前金额 tem := fmt.Sprintf("%0.0f", temMoney) nowMoney := utils.ValueToFloat(tem, 0.0) remainMoney := money - nowMoney //剩余金额 //生成流水号 Cnr := new(Snrtab) Cnr.Cid = cid orderId, err := Cnr.GetNextSnr("Yearnr") if err != nil { return false } info := new(Chargeallocationtab) info.Cid = cid info.Chargenr = chargenr info.Createtime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss") info.Lastmodifytime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss") info.Allocateexpense = nowMoney info.Allocateyear = t1.Year() info.Chargeallocationnr = utils.ValueToInt(orderId, 0) _, err = session.Table("chargeallocationtab").Insert(info) if err != nil { return false } //递归循环 begYear := t1.AddDate(0, 0, 1) begYearDay := utils.TimeFormat(begYear, "yyyy-MM-dd") res := ChargeDiGui(session, cid, chargenr, begYearDay, enddate, remainMoney) if !res { return false } return true } /*** *逻辑:把跨年的费用分摊到每年每月, *@param:begdate 开始时间 *@param:enddate 结束时间 *@param:money 分摊金额,保留四位小数 *@param:chargenr 费用单号 ***********/ func ChargeShareTheMonth(session *xorm.Session, cid int, chargenr int, begdate string, enddate string, money float64) bool { //计算出一共有多少个年月的数组 fmt.Println("begdate:", begdate, "enddate:", enddate) months := utils.SelectMonth(begdate, enddate) //计算出总数 lenth := len(months) if lenth == 0 { fmt.Println("month here error:") return false } monthAmt := money / float64(lenth) for _, v := range months { //分割年月 yam := strings.Split(v, "/") year := yam[0] month := yam[1] //生成流水号 Cnr := new(Snrtab) Cnr.Cid = cid orderId, err := Cnr.GetNextSnr("Yearnr") if err != nil { fmt.Println("here error:", err) return false } //直接添加费用记录 info := new(Chargeallocationtab) info.Cid = cid info.Chargenr = chargenr info.Createtime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss") info.Lastmodifytime = utils.TimeFormat(time.Now(), "yyyyMMddHHmmss") info.Allocateexpense = monthAmt info.Allocateyear = utils.ValueToInt(year, 0) info.Allocatemonth = utils.ValueToInt(month, 0) info.Chargeallocationnr = utils.ValueToInt(orderId, 0) _, err = session.Table("chargeallocationtab").Insert(info) if err != nil { continue } } return true }