account.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. package account
  2. import (
  3. "dashoo.cn/micro_libary/request"
  4. "database/sql"
  5. "errors"
  6. "github.com/gogf/gf/frame/g"
  7. "github.com/gogf/gf/os/gtime"
  8. "github.com/gogf/gf/util/gconv"
  9. dao "lims_adapter/dao/account"
  10. "lims_adapter/model"
  11. account "lims_adapter/model/account"
  12. "strconv"
  13. )
  14. // Service 账号相关
  15. type Service struct {
  16. Dao *dao.SettleAccountMainDao
  17. Tenant string
  18. }
  19. type AccountService struct {
  20. Dao *dao.BaseAccountDao
  21. Tenant string
  22. }
  23. // NewSrv 服务初始化
  24. func NewSrv(tenant string) Service {
  25. return Service{Dao: dao.NewSettleAccountMainDao(tenant), Tenant: tenant}
  26. }
  27. func NewAccountSrv(tenant string) AccountService {
  28. return AccountService{Dao: dao.NewBaseAccountDao(tenant), Tenant: tenant}
  29. }
  30. // 结算明细分页
  31. func (s Service) SettleAccountList(req model.ListReq, info request.UserInfo) ([]account.SettleAccountMain, int, error) {
  32. entityModel := s.Dao.DB.Model("settle_account_main s").
  33. LeftJoin("appointment a", "s.AppointId = a.Id").
  34. LeftJoin("Instrument i").
  35. Fields("s.Id, s.BillId, s.AppointUser, s.MainUser, s.FeeType, s.TotalPrice, ").
  36. Where("MainUserId = ?", info.Id)
  37. if req.Entity != nil {
  38. entity := new(account.SettleAccountMainReq)
  39. gconv.Struct(req.Entity, entity)
  40. if strconv.Itoa(entity.AppointUserId) != "" {
  41. entityModel = entityModel.WhereLike(s.Dao.Columns.AppointUserId, "%"+strconv.Itoa(entity.AppointUserId)+"%")
  42. }
  43. if strconv.Itoa(entity.RelevanceId) != "" {
  44. entityModel = entityModel.WhereLike("RelevanceId", "%"+strconv.Itoa(entity.RelevanceId)+"%")
  45. }
  46. }
  47. total, err := entityModel.Count()
  48. if err != nil {
  49. return nil, 0, err
  50. }
  51. if total == 0 {
  52. return nil, 0, nil
  53. }
  54. return nil, 0, nil
  55. res, err := entityModel.Page(req.Current, req.Size).FindAll()
  56. if err != nil {
  57. return nil, 0, err
  58. }
  59. list := make([]account.SettleAccountMain, 0)
  60. res.Structs(&list)
  61. return list, total, nil
  62. }
  63. // 新增结算明细主表、明细子表
  64. func (s Service) AddCountMainDetail(mobAppoint account.AppointInfoReq) error {
  65. // 通过设备、用户查询优惠状态
  66. var result account.AppointInfo
  67. isDiscount := false
  68. err := s.Dao.DB.Model("base_equipment_qualification").Fields("Qualification as IsPreferential").
  69. Where("EquipmentId = " + strconv.Itoa(mobAppoint.Appointment.Instr.Id) + " and Qualification = 3 " +
  70. " and UserId = " + strconv.Itoa(mobAppoint.Appointment.User.Id)).Scan(&result.User)
  71. // 如果没有数据,则没有权限
  72. if err == sql.ErrNoRows {
  73. // TODO
  74. } else {
  75. if result.User.IsPreferential == "3" {
  76. isDiscount = true
  77. }
  78. }
  79. // 计算实际实验时长、费用,目前为一条明细子表数据,后期可能扩展为多条数据子表数据
  80. totalMinutes := 0.0
  81. //signInTime := mobAppoint.Appointment.Appoint.SignInTime
  82. //hour := mobAppoint.SignOutTime.Hour() - signInTime.Hour()
  83. //minute := mobAppoint.SignOutTime.Minute() - signInTime.Minute()
  84. //totalMinute += hour * 60 + minute
  85. totalMinutes += mobAppoint.Appointment.Appoint.RealityUseDuration * 60
  86. unitCount := float64(mobAppoint.Appointment.Instr.UnitCount)
  87. // 有优惠权,则按照优惠计费
  88. if isDiscount {
  89. unitCount = unitCount * float64(mobAppoint.Appointment.Instr.UnitCount) / 100
  90. }
  91. totalPrice := totalMinutes / 60 * unitCount
  92. // 生成结算明细主表
  93. now := gtime.Now()
  94. var mainEntity = g.Map{
  95. "AppointId": mobAppoint.Appointment.Appoint.Id,
  96. "AppointUserId": mobAppoint.Appointment.User.Id,
  97. "AppointUser": mobAppoint.Appointment.User.Realname,
  98. "SettleStatus": 1,
  99. "TotalPrice": totalPrice,
  100. "CreateUserId": mobAppoint.Appointment.User.Id,
  101. "CreateBy": mobAppoint.Appointment.User.Realname,
  102. "CreateOn": now,
  103. }
  104. id, err := s.Dao.DB.Model("settle_account_main").InsertAndGetId(mainEntity)
  105. if err != nil {
  106. return err
  107. } else {
  108. // 生成结算明细子表
  109. // TODO 判断正常支出或是违约
  110. var detailEntity = g.Map{
  111. "pid": id,
  112. "UnitPrice": unitCount,
  113. "Minutes": totalMinutes,
  114. "PaymentType": 1,
  115. "PaymentAccount": 1,
  116. "CreateUserId": mobAppoint.Appointment.User.Id,
  117. "CreateBy": mobAppoint.Appointment.User.Realname,
  118. "CreateOn": now,
  119. }
  120. _, err := s.Dao.DB.Model("settle_account_detail").Insert(detailEntity)
  121. if err != nil {
  122. return err
  123. }
  124. }
  125. return nil
  126. }
  127. // AddAccount 添加财务账号
  128. func (s Service) AddAccount(req *account.BaseAccount) error {
  129. count, err := s.Dao.DB.Model("base_account").Where("Account = ?", req.Account).Count()
  130. if err != nil {
  131. return err
  132. }
  133. if count > 0 {
  134. return errors.New("该账号已存在,请重新输入!")
  135. }
  136. _, err = s.Dao.DB.Model("base_account").Insert(req)
  137. if err != nil {
  138. return err
  139. }
  140. return nil
  141. }
  142. // AccountList 财务账号分页查询
  143. func (s Service) AccountList(req *account.AccountReq) (infos account.BaseAccountRsp, err error) {
  144. current := req.PageNun
  145. size := req.PageSize
  146. model := s.Dao.DB.Model("base_account a").
  147. LeftJoin("user_account_bind b", "a.Id = b.AccountId").
  148. LeftJoin("base_user c", "b.UserId = c.Id")
  149. if req.Account != "" {
  150. model = model.WhereLike("Account", req.Account)
  151. }
  152. if req.RealName != "" {
  153. model = model.WhereLike("RealName", req.RealName)
  154. }
  155. if req.AccountName != "" {
  156. model = model.Where("AccountName", req.AccountName)
  157. }
  158. infos.Total, err = model.Count()
  159. if err != nil {
  160. return infos, err
  161. }
  162. model.Fields("a.Id, a.Account,a.AccountName,A.Surplus,a.Available,a.Limit,a.Advance,c.Id UserId,c.RealName").
  163. Page(current, size).Scan(&infos.Records)
  164. return infos, nil
  165. }
  166. // DepositAccount 充值账户
  167. func (s AccountService) DepositAccount(req *account.BaseAccountResp) error {
  168. _, err := s.Dao.M.Update("Surplus = "+strconv.Itoa(req.Surplus), "Id = "+strconv.Itoa(req.Id))
  169. if err != nil {
  170. return err
  171. }
  172. return nil
  173. }