settle_account_bill.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. package settle_account_bill
  2. import (
  3. "dashoo.cn/micro_libary/request"
  4. "database/sql"
  5. "errors"
  6. "fmt"
  7. "github.com/gogf/gf/os/gtime"
  8. "github.com/gogf/gf/util/gconv"
  9. "lims_adapter/dao/account"
  10. "lims_adapter/model"
  11. accountModel "lims_adapter/model/account"
  12. "strconv"
  13. "strings"
  14. )
  15. // Service 会议室服务
  16. type Service struct {
  17. Dao *account.SettleAccountBillDao
  18. Tenant string
  19. }
  20. // NewSrv 服务初始化
  21. func NewService(tenant string) Service {
  22. return Service{Dao: account.NewSettleAccountBillDao(tenant), Tenant: tenant}
  23. }
  24. // List 会议室列表
  25. func (s Service) List(req model.ListReq) ([]accountModel.SettleAccountBill, int, error) {
  26. entityModel := s.Dao.M
  27. where := "1=1"
  28. if req.Entity != nil {
  29. entity := new(accountModel.SettleAccountBillReq)
  30. err := gconv.Struct(req.Entity, entity)
  31. if err != nil {
  32. return nil, 0, err
  33. }
  34. if entity.MainUserId != 0 {
  35. where += fmt.Sprintf(" AND MainUserId='%v'", entity.MainUserId)
  36. }
  37. if entity.Status != "" {
  38. where += fmt.Sprintf(" AND Status='%v'", entity.Status)
  39. }
  40. if entity.SettleDate != "" {
  41. timelist := strings.Split(entity.SettleDate,",")
  42. if len(timelist) == 2 {
  43. where += fmt.Sprintf(" AND SettleDate>'%v' AND SettleDate<'%v'", timelist[0],timelist[1])
  44. }
  45. }
  46. if entity.SettleUser != "" {
  47. where += fmt.Sprintf(" AND SettleUser LIKE '%%%v%%'", entity.SettleUser)
  48. }
  49. if entity.MainUser != "" {
  50. where += fmt.Sprintf(" AND MainUser LIKE '%%%v%%'", entity.MainUser)
  51. }
  52. if entity.StartDate != "" && entity.EndDate != "" {
  53. where += fmt.Sprintf(" AND StartDate>'%v' AND EndDate<'%v'", entity.StartDate, entity.EndDate)
  54. }
  55. }
  56. entityModel = entityModel.Where(where)
  57. total, err := entityModel.Count()
  58. if err != nil {
  59. return nil, 0, err
  60. }
  61. if total == 0 {
  62. return nil, 0, nil
  63. }
  64. res, err := entityModel.Page(req.Current, req.Size).Order("settle_account_bill.CreateOn DESC").Fields("settle_account_bill.*").FindAll()
  65. if err != nil {
  66. return nil, 0, err
  67. }
  68. if res.IsEmpty() {
  69. return nil, 0, nil
  70. }
  71. list := make([]accountModel.SettleAccountBill, 0)
  72. err = res.Structs(&list)
  73. if err != nil {
  74. return nil, 0, err
  75. }
  76. return list, total, nil
  77. }
  78. // 结算
  79. func (s Service) Settle(req accountModel.AccountBillSettleReq, user request.UserInfo) error {
  80. if req.BillId == 0 || req.Amount == 0 {
  81. return errors.New("参数缺失")
  82. }
  83. tx, err := s.Dao.DB.Begin()
  84. if err != nil {
  85. return err
  86. }
  87. _, err = tx.Update("settle_account_bill", fmt.Sprintf("Status='2',SettleUserId='%v',SettleUser='%v',SettleDate='%v'", user.Id, user.RealName, gtime.Now()), fmt.Sprintf("Id='%v'", req.BillId))
  88. if err != nil {
  89. tx.Rollback()
  90. return err
  91. }
  92. _, err = tx.Update("settle_account_main", fmt.Sprintf("AccountStatus='1',SettleUserId='%v',SettleUser='%v',SettleDate='%v'", user.Id, user.RealName, gtime.Now()), fmt.Sprintf("BillId='%v'", req.BillId))
  93. if err != nil {
  94. tx.Rollback()
  95. return err
  96. }
  97. _, err = tx.Update("base_account", fmt.Sprintf("Surplus=Surplus-%v", req.Amount), fmt.Sprintf("Id='%v'", req.Amount))
  98. if err != nil {
  99. tx.Rollback()
  100. return err
  101. }
  102. return tx.Commit()
  103. }
  104. // 确认
  105. func (s Service) Confirm(req accountModel.AccountBillConfirmReq, user request.UserInfo) error {
  106. if req.BillId == 0 {
  107. return errors.New("参数缺失")
  108. }
  109. _, 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))
  110. return err
  111. }
  112. // 自动生成账单
  113. func (s Service) GenerateBill() error {
  114. var rules []Param
  115. var rule1 Param
  116. var rule2 Param
  117. var rule3 Param
  118. var mains []accountModel.SettleAccountMain
  119. //var details []accountModel.SettleAccountDetail
  120. var accounts []accountModel.BaseAccount
  121. var auto AutoConfirmRecord
  122. mainMap := make(map[int][]accountModel.SettleAccountMain, 0)
  123. //detailMap := make(map[int][]accountModel.SettleAccountDetail, 0)
  124. accountMap := make(map[int]accountModel.BaseAccount, 0)
  125. now := gtime.Now()
  126. all, err := s.Dao.DB.Model("base_param").Where("Name='已出账单生成日期' OR Name='账单明细自动确认天数' OR Name='付款截止日期参数'").FindAll()
  127. if err != nil {
  128. return err
  129. }
  130. err = all.Structs(&rules)
  131. if err != nil {
  132. if err == sql.ErrNoRows {
  133. return errors.New("参数未配置")
  134. }
  135. return err
  136. }
  137. for _, item := range rules {
  138. if item.Name == "已出账单生成日期" {
  139. rule1 = item
  140. continue
  141. }
  142. if item.Name == "账单明细自动确认天数" {
  143. rule2 = item
  144. continue
  145. }
  146. if item.Name == "付款截止日期参数" {
  147. rule3 = item
  148. continue
  149. }
  150. }
  151. if rule1.Id == 0 || rule2.Id == 0 || rule3.Id == 0 {
  152. return errors.New("参数未配置")
  153. }
  154. all, err = s.Dao.DB.Model("base_account").FindAll()
  155. if err != nil {
  156. return err
  157. }
  158. err = all.Structs(&accounts)
  159. if err != nil {
  160. return err
  161. }
  162. for _, item := range accounts {
  163. if accountMap[item.MainUserId].Id == 0 {
  164. accountMap[item.MainUserId] = item
  165. } else if accountMap[item.MainUserId].Advance > item.Advance {
  166. accountMap[item.MainUserId] = item
  167. }
  168. }
  169. pDay, _ := strconv.Atoi(rule1.Value)
  170. diffDay, _ := strconv.Atoi(rule2.Value)
  171. endDay, _ := strconv.Atoi(rule3.Value)
  172. if now.Day() != pDay {
  173. return nil
  174. }
  175. endDate := now.AddDate(0, 0, -1).Format("Y-m-d 23:59:59")
  176. startDate := now.AddDate(0, -1, 0).Format("Y-m-d 00:00:00")
  177. all, err = s.Dao.DB.Model("settle_account_main").Where(fmt.Sprintf("CreateOn>='%v' AND CreateOn<='%v'", startDate, endDate)).FindAll()
  178. if err != nil {
  179. return err
  180. }
  181. err = all.Structs(&mains)
  182. if err != nil {
  183. if err == sql.ErrNoRows {
  184. return nil
  185. }
  186. return err
  187. }
  188. //all, err = s.Dao.DB.Model("settle_account_detail").Where(fmt.Sprintf("CreateOn>='%v' AND CreateOn<='%v'", startDate, endDate)).FindAll()
  189. //if err != nil {
  190. // return err
  191. //}
  192. //err = all.Structs(&details)
  193. //if err != nil {
  194. // return err
  195. //}
  196. for _, item := range mains { // 统计结算明细主表
  197. if mainMap[item.MainUserId] == nil {
  198. mainMap[item.MainUserId] = make([]accountModel.SettleAccountMain, 0)
  199. }
  200. mainMap[item.MainUserId] = append(mainMap[item.MainUserId], item)
  201. }
  202. //for _, item := range details { // 统计结算明细子表
  203. // if detailMap[item.Pid] == nil {
  204. // detailMap[item.Pid] = make([]accountModel.SettleAccountDetail, 0)
  205. // }
  206. // detailMap[item.Pid] = append(detailMap[item.Pid], item)
  207. //}
  208. tx, err := s.Dao.DB.Begin()
  209. if err != nil {
  210. return err
  211. }
  212. auto.BillIds = "-1"
  213. auto.AutoSettleDate = now.AddDate(0, 0, diffDay)
  214. for key, value := range mainMap {
  215. var bill accountModel.SettleAccountBill
  216. bill.PaymentDueDate = now.AddDate(0, 0, endDay)
  217. bill.StartDate = gtime.NewFromStr(startDate)
  218. bill.EndDate = gtime.NewFromStr(endDate)
  219. bill.MainUserId = key
  220. bill.MainUser = value[0].MainUser
  221. bill.AccountId = accountMap[bill.MainUserId].Id
  222. ids := "-1"
  223. for _, item := range value {
  224. ids += fmt.Sprintf(",%v", item.Id)
  225. bill.TotalCount += item.TotalPrice
  226. }
  227. r, err := tx.Save("settle_account_bill", bill)
  228. if err != nil {
  229. tx.Rollback()
  230. return err
  231. }
  232. id, _ := r.LastInsertId()
  233. auto.BillIds += fmt.Sprintf(",%v", id)
  234. _, err = tx.Update("settle_account_main", fmt.Sprintf("BillId='%v',Status='1'", id), fmt.Sprintf("Id IN (%v)", ids))
  235. if err != nil {
  236. tx.Rollback()
  237. return err
  238. }
  239. }
  240. _, err = tx.Save("auto_confirm_record", auto) // 自动确认辅助表
  241. if err != nil {
  242. tx.Rollback()
  243. return err
  244. }
  245. return tx.Commit()
  246. }
  247. // 自动确认
  248. func (s Service) AutoConfirm() error {
  249. now := gtime.Now()
  250. fmt.Println(now.Format("Y-m-d H:m:s"))
  251. endDate := now.Format("Y-m-d 23:59:59")
  252. startDate := now.Format("Y-m-d 00:00:00")
  253. var autos []AutoConfirmRecord
  254. all, err := s.Dao.DB.Model("auto_confirm_record").Where(fmt.Sprintf("AutoSettleDate>='%v' AND AutoSettleDate<='%v'", startDate, endDate)).FindAll()
  255. if err != nil {
  256. return err
  257. }
  258. err = all.Structs(&autos)
  259. if err != nil {
  260. if err == sql.ErrNoRows {
  261. return nil
  262. }
  263. return err
  264. }
  265. if len(autos) == 0 {
  266. return nil
  267. }
  268. tx, err := s.Dao.DB.Begin()
  269. if err != nil {
  270. return err
  271. }
  272. ids := "-1"
  273. for _, item := range autos {
  274. ids += fmt.Sprintf(",%v", item.Id)
  275. _, 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))
  276. if err != nil {
  277. tx.Rollback()
  278. return err
  279. }
  280. }
  281. _, err = tx.Delete("auto_confirm_record", fmt.Sprintf("Id IN (%v)", ids))
  282. if err != nil {
  283. tx.Rollback()
  284. return err
  285. }
  286. return tx.Commit()
  287. }
  288. type Param struct {
  289. Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
  290. Code string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
  291. Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
  292. Value string `protobuf:"bytes,4,opt,name=value,proto3" json:"value,omitempty"`
  293. Sys string `protobuf:"bytes,5,opt,name=sys,proto3" json:"sys"`
  294. SortCode int32 `protobuf:"varint,6,opt,name=sort_code,json=sortCode,proto3" json:"sort_code"`
  295. Description string `protobuf:"bytes,7,opt,name=description,proto3" json:"description,omitempty"`
  296. CreateOn string `protobuf:"bytes,8,opt,name=create_on,json=createOn,proto3" json:"create_on,omitempty"`
  297. CreateUserId int32 `protobuf:"varint,9,opt,name=create_user_id,json=createUserId,proto3" json:"create_user_id,omitempty"`
  298. CreateBy string `protobuf:"bytes,10,opt,name=create_by,json=createBy,proto3" json:"create_by,omitempty"`
  299. ModifiedOn string `protobuf:"bytes,11,opt,name=modified_on,json=modifiedOn,proto3" json:"modified_on,omitempty"`
  300. ModifiedUserId int32 `protobuf:"varint,12,opt,name=modified_user_id,json=modifiedUserId,proto3" json:"modified_user_id,omitempty"`
  301. ModifiedBy string `protobuf:"bytes,13,opt,name=modified_by,json=modifiedBy,proto3" json:"modified_by,omitempty"`
  302. }
  303. // auto_confirm_record
  304. type AutoConfirmRecord struct {
  305. Id int `orm:"Id,primary" json:"id"` // 主键
  306. BillIds string `orm:"BillIds" json:"bill_ids"` // 账单Ids
  307. AutoSettleDate *gtime.Time `orm:"AutoSettleDate" json:"auto_settle_date"` // 自动确认时间
  308. }