Browse Source

feature(结算): 自动生成账单定时器实现;自动确认未出账单接口实现

likai 4 years ago
parent
commit
b0f3eb5c25

+ 4 - 0
model/account/settle_account_bill.go

@@ -22,4 +22,8 @@ type AccountBillSettleReq struct {
 	BillId  int `json:"billId"`
 	BillId  int `json:"billId"`
 	AccountId  int `json:"accountId"`
 	AccountId  int `json:"accountId"`
 	Amount  float64 `json:"amount"`
 	Amount  float64 `json:"amount"`
+}
+
+type AccountBillConfirmReq struct {
+	BillId  int `json:"billId"`
 }
 }

+ 222 - 2
service/settle_account_bill/settle_account_bill.go

@@ -2,6 +2,7 @@ package settle_account_bill
 
 
 import (
 import (
 	"dashoo.cn/micro_libary/request"
 	"dashoo.cn/micro_libary/request"
+	"database/sql"
 	"errors"
 	"errors"
 	"fmt"
 	"fmt"
 	"github.com/gogf/gf/os/gtime"
 	"github.com/gogf/gf/os/gtime"
@@ -9,6 +10,7 @@ import (
 	"lims_adapter/dao/account"
 	"lims_adapter/dao/account"
 	"lims_adapter/model"
 	"lims_adapter/model"
 	accountModel "lims_adapter/model/account"
 	accountModel "lims_adapter/model/account"
+	"strconv"
 )
 )
 
 
 // Service 会议室服务
 // Service 会议室服务
@@ -92,9 +94,227 @@ func (s Service) Settle(req accountModel.AccountBillSettleReq, user request.User
 	return tx.Commit()
 	return tx.Commit()
 }
 }
 
 
-// 结算
+// 确认
+func (s Service) Confirm(req accountModel.AccountBillConfirmReq, user request.UserInfo) error {
+	if req.BillId == 0 {
+		return errors.New("参数缺失")
+	}
+
+	_, err := s.Dao.Update(fmt.Sprintf("Status='1',VerificationUserId='%v',VerificationUser='%v',VerificationDate='%v'", user.Id, user.RealName, gtime.Now()), fmt.Sprintf("Id='%v'", req.BillId))
+	return err
+}
+
+// 自动生成账单
 func (s Service) GenerateBill() error {
 func (s Service) GenerateBill() error {
+	var rules []Param
+	var rule1 Param
+	var rule2 Param
+	var rule3 Param
+	var mains []accountModel.SettleAccountMain
+	//var details []accountModel.SettleAccountDetail
+	var accounts []accountModel.BaseAccount
+	var auto AutoConfirmRecord
+	mainMap := make(map[int][]accountModel.SettleAccountMain, 0)
+	//detailMap := make(map[int][]accountModel.SettleAccountDetail, 0)
+	accountMap := make(map[int]accountModel.BaseAccount, 0)
+	now := gtime.Now()
 
 
+	all, err := s.Dao.DB.Model("base_param").Where("Name='已出账单生成日期' OR Name='账单明细自动确认天数' OR Name='付款截止日期参数'").FindAll()
+	if err != nil {
+		return err
+	}
+	err = all.Structs(&rules)
+	if err != nil {
+		if err == sql.ErrNoRows {
+			return errors.New("参数未配置")
+		}
+		return err
+	}
+	for _, item := range rules {
+		if item.Name == "已出账单生成日期" {
+			rule1 = item
+			continue
+		}
+		if item.Name == "账单明细自动确认天数" {
+			rule2 = item
+			continue
+		}
+		if item.Name == "付款截止日期参数" {
+			rule3 = item
+			continue
+		}
+	}
+	if rule1.Id == 0 || rule2.Id == 0 || rule3.Id == 0 {
+		return errors.New("参数未配置")
+	}
+
+	all, err = s.Dao.DB.Model("base_account").FindAll()
+	if err != nil {
+		return err
+	}
+	err = all.Structs(&accounts)
+	if err != nil {
+		return err
+	}
+	for _, item := range accounts {
+		if accountMap[item.MainUserId].Id == 0 {
+			accountMap[item.MainUserId] = item
+		} else if accountMap[item.MainUserId].Advance > item.Advance {
+			accountMap[item.MainUserId] = item
+		}
+	}
+
+	pDay, _ := strconv.Atoi(rule1.Value)
+	diffDay, _ := strconv.Atoi(rule2.Value)
+	endDay, _ := strconv.Atoi(rule3.Value)
+
+	if now.Day() != pDay {
+		return nil
+	}
+	endDate := now.AddDate(0, 0, -1).Format("Y-m-d 23:59:59")
+	startDate := now.AddDate(0, -1, 0).Format("Y-m-d 00:00:00")
+
+	all, err = s.Dao.DB.Model("settle_account_main").Where(fmt.Sprintf("CreateOn>='%v' AND CreateOn<='%v'", startDate, endDate)).FindAll()
+	if err != nil {
+		return err
+	}
+	err = all.Structs(&mains)
+	if err != nil {
+		if err == sql.ErrNoRows {
+			return nil
+		}
+		return err
+	}
+
+	//all, err = s.Dao.DB.Model("settle_account_detail").Where(fmt.Sprintf("CreateOn>='%v' AND CreateOn<='%v'", startDate, endDate)).FindAll()
+	//if err != nil {
+	//	return err
+	//}
+	//err = all.Structs(&details)
+	//if err != nil {
+	//	return err
+	//}
+
+	for _, item := range mains { // 统计结算明细主表
+		if mainMap[item.MainUserId] == nil {
+			mainMap[item.MainUserId] = make([]accountModel.SettleAccountMain, 0)
+		}
+		mainMap[item.MainUserId] = append(mainMap[item.MainUserId], item)
+	}
+
+	//for _, item := range details { // 统计结算明细子表
+	//	if detailMap[item.Pid] == nil {
+	//		detailMap[item.Pid] = make([]accountModel.SettleAccountDetail, 0)
+	//	}
+	//	detailMap[item.Pid] = append(detailMap[item.Pid], item)
+	//}
+
+	tx, err := s.Dao.DB.Begin()
+	if err != nil {
+		return err
+	}
+	auto.BillIds = "-1"
+	auto.AutoSettleDate = now.AddDate(0, 0, diffDay)
+	for key, value := range mainMap {
+		var bill accountModel.SettleAccountBill
+		bill.PaymentDueDate = now.AddDate(0, 0, endDay)
+		bill.StartDate = gtime.NewFromStr(startDate)
+		bill.EndDate = gtime.NewFromStr(endDate)
+		bill.MainUserId = key
+		bill.MainUser = value[0].MainUser
+		bill.AccountId = accountMap[bill.MainUserId].Id
+		ids := "-1"
+		for _, item := range value {
+			ids += fmt.Sprintf(",%v", item.Id)
+			bill.TotalCount += item.TotalPrice
+		}
+		r, err := tx.Save("settle_account_bill", bill)
+		if err != nil {
+			tx.Rollback()
+			return err
+		}
+		id, _ := r.LastInsertId()
+		auto.BillIds += fmt.Sprintf(",%v", id)
+		_, err = tx.Update("settle_account_main", fmt.Sprintf("BillId='%v',Status='1'", id), fmt.Sprintf("Id IN (%v)", ids))
+		if err != nil {
+			tx.Rollback()
+			return err
+		}
+	}
+
+	_, err = tx.Save("auto_confirm_record", auto) // 自动确认辅助表
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	return tx.Commit()
+}
+
+// 自动确认
+func (s Service) AutoConfirm() error {
+	now := gtime.Now()
+	fmt.Println(now.Format("Y-m-d H:m:s"))
+	endDate := now.Format("Y-m-d 23:59:59")
+	startDate := now.Format("Y-m-d 00:00:00")
+	var autos []AutoConfirmRecord
+
+	all, err := s.Dao.DB.Model("auto_confirm_record").Where(fmt.Sprintf("AutoSettleDate>='%v' AND AutoSettleDate<='%v'", startDate, endDate)).FindAll()
+	if err != nil {
+		return err
+	}
+	err = all.Structs(&autos)
+	if err != nil {
+		if err == sql.ErrNoRows {
+			return nil
+		}
+		return err
+	}
+	if len(autos) == 0 {
+		return nil
+	}
+	tx, err := s.Dao.DB.Begin()
+	if err != nil {
+		return err
+	}
+
+	ids := "-1"
+	for _, item := range autos {
+		ids += fmt.Sprintf(",%v", item.Id)
+		_, err = tx.Update("settle_account_main", fmt.Sprintf("SettleUserId=0,SettleUser='系统自动确认',SettleDate='%v',SettleStatus='1'", now.Format("Y-m-d H:m:s")), fmt.Sprintf("BillId IN (%v) AND (SettleUserId IS NULL OR SettleUser='')", item.BillIds))
+		if err != nil {
+			tx.Rollback()
+			return err
+		}
+	}
+	_, err = tx.Delete("auto_confirm_record", fmt.Sprintf("Id IN (%v)", ids))
+	if err != nil {
+		tx.Rollback()
+		return err
+	}
+
+	return tx.Commit()
+}
+
+type Param struct {
+	Id             int32  `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
+	Code           string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
+	Name           string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
+	Value          string `protobuf:"bytes,4,opt,name=value,proto3" json:"value,omitempty"`
+	Sys            string `protobuf:"bytes,5,opt,name=sys,proto3" json:"sys"`
+	SortCode       int32  `protobuf:"varint,6,opt,name=sort_code,json=sortCode,proto3" json:"sort_code"`
+	Description    string `protobuf:"bytes,7,opt,name=description,proto3" json:"description,omitempty"`
+	CreateOn       string `protobuf:"bytes,8,opt,name=create_on,json=createOn,proto3" json:"create_on,omitempty"`
+	CreateUserId   int32  `protobuf:"varint,9,opt,name=create_user_id,json=createUserId,proto3" json:"create_user_id,omitempty"`
+	CreateBy       string `protobuf:"bytes,10,opt,name=create_by,json=createBy,proto3" json:"create_by,omitempty"`
+	ModifiedOn     string `protobuf:"bytes,11,opt,name=modified_on,json=modifiedOn,proto3" json:"modified_on,omitempty"`
+	ModifiedUserId int32  `protobuf:"varint,12,opt,name=modified_user_id,json=modifiedUserId,proto3" json:"modified_user_id,omitempty"`
+	ModifiedBy     string `protobuf:"bytes,13,opt,name=modified_by,json=modifiedBy,proto3" json:"modified_by,omitempty"`
+}
 
 
-	return nil
+// auto_confirm_record
+type AutoConfirmRecord struct {
+	Id             int         `orm:"Id,primary"     json:"id"`               // 主键
+	BillIds        string      `orm:"BillIds"        json:"bill_ids"`         // 账单Ids
+	AutoSettleDate *gtime.Time `orm:"AutoSettleDate" json:"auto_settle_date"` // 自动确认时间
 }
 }

+ 3 - 0
service/timers/timers.go

@@ -17,5 +17,8 @@ func (t Timer) Run() {
 		if err := localService.GenerateBill(); err != nil {
 		if err := localService.GenerateBill(); err != nil {
 			glog.Error(err.Error())
 			glog.Error(err.Error())
 		}
 		}
+		if err := localService.AutoConfirm(); err != nil {
+			glog.Error(err.Error())
+		}
 	}
 	}
 }
 }