| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- package settle_account_main
- import (
- "dashoo.cn/micro_libary/request"
- "errors"
- "fmt"
- "github.com/gogf/gf/os/gtime"
- "github.com/gogf/gf/util/gconv"
- "lims_adapter/dao/account"
- "lims_adapter/model"
- accountModel "lims_adapter/model/account"
- "strconv"
- )
- // Service 会议室服务
- type Service struct {
- Dao *account.SettleAccountMainDao
- Tenant string
- }
- // NewSrv 服务初始化
- func NewService(tenant string) Service {
- return Service{Dao: account.NewSettleAccountMainDao(tenant), Tenant: tenant}
- }
- // List 会议室列表
- func (s Service) List(req model.ListReq) ([]accountModel.SettleAccountMain, int, error) {
- entityModel := s.Dao.M
- where := "1=1"
- if req.Entity != nil {
- entity := new(accountModel.SettleAccountMainReq)
- err := gconv.Struct(req.Entity, entity)
- if err != nil {
- return nil, 0, err
- }
- if entity.MainUserId != 0 {
- where += fmt.Sprintf(" AND MainUserId='%v'", entity.MainUserId)
- }
- if entity.MainUser != "" {
- where += fmt.Sprintf(" AND MainUser LIKE '%%%v%%'", entity.MainUser)
- }
- if entity.AttachUserId != 0 {
- where += fmt.Sprintf(" AND AttachUserId='%v'", entity.AttachUserId)
- }
- if entity.AttachUser != "" {
- where += fmt.Sprintf(" AND AttachUser LIKE '%%%v%%'", entity.AttachUser)
- }
- if entity.InstrumentId != 0 {
- where += fmt.Sprintf(" AND InstrumentId='%v'", entity.InstrumentId)
- }
- if entity.AppointUserId != 0 {
- where += fmt.Sprintf(" AND AppointUserId='%v'", entity.AppointUserId)
- }
- if entity.AppointUser != "" {
- where += fmt.Sprintf(" AND AppointUser LIKE '%%%v%%'", entity.AppointUser)
- }
- if entity.Status != "" {
- where += fmt.Sprintf(" AND Status='%v'", entity.Status)
- }
- if entity.SettleStatus != "" {
- where += fmt.Sprintf(" AND SettleStatus='%v'", entity.SettleStatus)
- }
- if entity.FeeType != "" {
- where += fmt.Sprintf(" AND FeeType='%v'", entity.FeeType)
- }
- if entity.AppointStartDate != "" && entity.AppointEndDate != "" {
- where += fmt.Sprintf(" AND AppointStartDate>'%v' AND AppointEndDate<'%v'", entity.AppointStartDate, entity.AppointEndDate)
- }
- }
- entityModel = entityModel.Where(where)
- total, err := entityModel.Count()
- if err != nil {
- return nil, 0, err
- }
- if total == 0 {
- return nil, 0, nil
- }
- res, err := entityModel.Page(req.Current, req.Size).Order("settle_account_main.CreateOn DESC").Fields("settle_account_main.*").FindAll()
- if err != nil {
- return nil, 0, err
- }
- if res.IsEmpty() {
- return nil, 0, nil
- }
- list := make([]accountModel.SettleAccountMain, 0)
- err = res.Structs(&list)
- if err != nil {
- return nil, 0, err
- }
- return list, total, nil
- }
- // 新增
- func (s Service) Add(req accountModel.AccountMainAddReq, user request.UserInfo) error {
- now := gtime.Now() // 获取当前时间
- // 更新必要信息
- req.Main.CreateUserId = int(user.Id)
- req.Main.CreateBy = user.RealName
- req.Main.CreateOn = now
- req.Main.UpdateUserId = int(user.Id)
- req.Main.UpdateBy = user.RealName
- req.Main.UpdateOn = now
- tx, err := s.Dao.DB.Begin()
- if err != nil {
- return err
- }
- result, err := tx.Insert("settle_account_main", req.Main)
- if err != nil {
- tx.Rollback()
- return err
- }
- id , _ := result.LastInsertId()
- for index := range req.Details { // 更新必要信息
- req.Details[index].CreateUserId = int(user.Id)
- req.Details[index].CreateBy = user.RealName
- req.Details[index].CreateOn = now
- req.Details[index].UpdateUserId = int(user.Id)
- req.Details[index].UpdateBy = user.RealName
- req.Details[index].UpdateOn = now
- req.Details[index].Pid = int(id)
- }
- _, err = tx.Insert("settle_account_detail", req.Details)
- if err != nil {
- tx.Rollback()
- return err
- }
- return tx.Commit()
- }
- // 确认
- func (s Service) Confirm(req accountModel.AccountMainConfirmReq, user request.UserInfo) error {
- if req.MainId == 0 {
- return errors.New("参数缺失")
- }
- _, err := s.Dao.M.Update(fmt.Sprintf("SettleStatus='1',SettleUserId='%v',SettleUser='%v',SettleDate='%v'", user.Id, user.RealName, gtime.Now()), fmt.Sprintf("Id='%v'", req.MainId))
- return err
- }
- // 预约取消
- func (s Service) Cancel(req accountModel.AccountMainCancelReq, user request.UserInfo) error {
- if req.AppointId == 0 {
- return errors.New("参数缺失")
- }
- var main accountModel.SettleAccountMain
- var details []accountModel.SettleAccountDetail
- var rules []Param
- now := gtime.Now()
- per := float64(0)
- one, err := s.Dao.M.Where(fmt.Sprintf("AppointId='%v'", req.AppointId)).FindOne()
- if err != nil {
- return err
- }
- err = one.Struct(&main)
- if err != nil {
- return err
- }
- if main.Id == 0 { // 该预约未生成账单
- return nil
- }
- all, err := s.Dao.DB.Model("settle_account_detail").Where(fmt.Sprintf("pid='%v'", main.Id)).FindAll()
- if err != nil {
- return err
- }
- err = all.Structs(&details)
- if err != nil {
- return err
- }
- all, err = s.Dao.DB.Model("base_param").Where("Name LIKE '违约计费规则%'").Order("Code ASC").FindAll()
- if err != nil {
- return err
- }
- err = all.Structs(&rules)
- if err != nil {
- return err
- }
- mins := now.Sub(main.AppointStartDate).Minutes()
- if mins > 0 { // 正向超时,计算违约收费
- r := ""
- for _, rule := range rules {
- value1, _ := strconv.ParseFloat(rule.Code, 64)
- if mins < value1 {
- per, _ := strconv.ParseFloat(rule.Value, 64)
- per /= 100
- r += rule.Code + "分钟内" + rule.Value + "%"
- }
- }
- main.TotalPrice *= per
- main.ActualMachineHour = 0
- for index := range details {
- details[index].PaymentAccount = 0 // 取消,计费为0
- }
- if main.TotalPrice > 0 {
- var detail accountModel.SettleAccountDetail
- detail.PaymentType = "1"
- detail.PaymentAccount = main.TotalPrice
- detail.Data1 = main.AppointStartDate.Format("Y-m-d H:m:s")
- detail.Data2 = main.AppointEndDate.Format("Y-m-d H:m:s")
- detail.Data3 = now.Format("Y-m-d H:m:s")
- detail.Data4 = r
- details = append(details, detail)
- }
- tx, err := s.Dao.DB.Begin()
- if err != nil {
- return err
- }
- _, err = tx.Save("settle_account_main", main)
- if err != nil {
- tx.Rollback()
- return err
- }
- _, err = tx.Save("settle_account_detail", details)
- if err != nil {
- tx.Rollback()
- return err
- }
- return tx.Commit()
- }
- return nil
- }
- 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"`
- }
|