account.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. contractBreach := 1.0
  66. paymentType := 1
  67. // TODO 如果是已取消预约,则计算违约费用
  68. if mobAppoint.Appointment.Appoint.Status == 4 {
  69. contractBreachEntity := model.ContractBreach{}
  70. // 计算取消时间与实验开始时间差,选择相应违约规则
  71. startTime := mobAppoint.Appointment.Appoint.StartTime
  72. cancelTime := mobAppoint.Appointment.Appoint.UpdateAt
  73. totalMinute := (startTime.Hour()-cancelTime.Hour())*60 + (startTime.Minute() - cancelTime.Minute())
  74. if err := s.Dao.DB.Model("contract_breach").
  75. Where("MinPoint <= ? and MaxPoint >= ?", totalMinute, totalMinute).Scan(&contractBreachEntity); err != nil {
  76. return err
  77. }
  78. contractBreach = contractBreachEntity.Persent
  79. paymentType = 2
  80. }
  81. // 通过设备、用户查询优惠状态
  82. var result account.AppointInfo
  83. isDiscount := false
  84. err := s.Dao.DB.Model("base_equipment_qualification").Fields("Qualification as IsPreferential").
  85. Where("EquipmentId = " + strconv.Itoa(mobAppoint.Appointment.Instr.Id) + " and Qualification = 3 " +
  86. " and UserId = " + strconv.Itoa(mobAppoint.Appointment.User.Id)).Scan(&result.User)
  87. // 如果没有数据,则没有权限
  88. if err == sql.ErrNoRows {
  89. // TODO
  90. } else {
  91. if result.User.IsPreferential == "3" {
  92. isDiscount = true
  93. }
  94. }
  95. // 计算实际实验时长、费用,目前为一条明细子表数据,后期可能扩展为多条数据子表数据
  96. totalMinutes := 0.0
  97. //signInTime := mobAppoint.Appointment.Appoint.SignInTime
  98. //hour := mobAppoint.SignOutTime.Hour() - signInTime.Hour()
  99. //minute := mobAppoint.SignOutTime.Minute() - signInTime.Minute()
  100. //totalMinute += hour * 60 + minute
  101. totalMinutes += mobAppoint.Appointment.Appoint.RealityUseDuration * 60
  102. unitCount := float64(mobAppoint.Appointment.Instr.UnitCount)
  103. // 有优惠权,则按照优惠计费
  104. if isDiscount {
  105. unitCount = unitCount * float64(mobAppoint.Appointment.Instr.UnitCount) / 100
  106. }
  107. totalPrice := totalMinutes / 60 * unitCount
  108. // 生成结算明细主表
  109. now := gtime.Now()
  110. var mainEntity = g.Map{
  111. "AppointId": mobAppoint.Appointment.Appoint.Id,
  112. "AppointUserId": mobAppoint.Appointment.User.Id,
  113. "AppointUser": mobAppoint.Appointment.User.Realname,
  114. "SettleStatus": 1,
  115. "TotalPrice": totalPrice * contractBreach,
  116. "CreateUserId": mobAppoint.Appointment.User.Id,
  117. "CreateBy": mobAppoint.Appointment.User.Realname,
  118. "CreateOn": now,
  119. }
  120. id, err := s.Dao.DB.Model("settle_account_main").InsertAndGetId(mainEntity)
  121. if err != nil {
  122. return err
  123. } else {
  124. // 生成结算明细子表
  125. // TODO 判断正常支出或是违约
  126. var detailEntity = g.Map{
  127. "pid": id,
  128. "UnitPrice": unitCount,
  129. "Minutes": totalMinutes,
  130. "PaymentType": paymentType,
  131. "PaymentAccount": totalPrice * contractBreach,
  132. "CreateUserId": mobAppoint.Appointment.User.Id,
  133. "CreateBy": mobAppoint.Appointment.User.Realname,
  134. "CreateOn": now,
  135. }
  136. _, err := s.Dao.DB.Model("settle_account_detail").Insert(detailEntity)
  137. if err != nil {
  138. return err
  139. }
  140. }
  141. return nil
  142. }
  143. // AddAccount 添加财务账号
  144. func (s Service) AddAccount(req *account.BaseAccount) error {
  145. count, err := s.Dao.DB.Model("base_account").Where("Account = ?", req.Account).Count()
  146. if err != nil {
  147. return err
  148. }
  149. if count > 0 {
  150. return errors.New("该账号已存在,请重新输入!")
  151. }
  152. _, err = s.Dao.DB.Model("base_account").Insert(req)
  153. if err != nil {
  154. return err
  155. }
  156. return nil
  157. }
  158. // AccountList 财务账号分页查询
  159. func (s Service) AccountList(req *account.AccountReq) (infos account.BaseAccountRsp, err error) {
  160. current := req.PageNun
  161. size := req.PageSize
  162. model := s.Dao.DB.Model("base_account a").
  163. LeftJoin("user_account_bind b", "a.Id = b.AccountId").
  164. LeftJoin("base_user c", "b.UserId = c.Id")
  165. if req.Account != "" {
  166. model = model.WhereLike("Account", req.Account)
  167. }
  168. if req.RealName != "" {
  169. model = model.WhereLike("RealName", req.RealName)
  170. }
  171. if req.AccountName != "" {
  172. model = model.Where("AccountName", req.AccountName)
  173. }
  174. infos.Total, err = model.Count()
  175. if err != nil {
  176. return infos, err
  177. }
  178. model.Fields("a.Id, a.Account,a.AccountName,A.Surplus,a.Available,a.Limit,a.Advance,c.Id UserId,c.RealName").
  179. Page(current, size).Scan(&infos.Records)
  180. return infos, nil
  181. }
  182. // DepositAccount 充值账户
  183. func (s AccountService) DepositAccount(req *account.BaseAccountResp) error {
  184. _, err := s.Dao.M.Update("Surplus = "+strconv.Itoa(req.Surplus), "Id = "+strconv.Itoa(req.Id))
  185. if err != nil {
  186. return err
  187. }
  188. return nil
  189. }