selectservice.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package selectbusiness
  2. import (
  3. . "dashoo.cn/backend/api/mydb"
  4. "dashoo.cn/utils"
  5. "github.com/go-xorm/xorm"
  6. "strconv"
  7. )
  8. type TotalResult struct {
  9. Total int64 `xorm:"INT(11) 'total'"`
  10. }
  11. type SelectService struct {
  12. MyServiceBase
  13. }
  14. func GetSelectService(xormEngine *xorm.Engine) *SelectService {
  15. s := new(SelectService)
  16. s.DBE = xormEngine
  17. return s
  18. }
  19. func (s *SelectService) GetMyPagingEntitiesWithOrderBytbl(supplierTableName, supplierCertTableName, OilInfoChangeName, OilSupplierCertSubName,
  20. OilSupplierFileName string, pageIndex, itemsPerPage int64, orderby string, asc bool, entitiesPtr interface{}, where,having, liftjoon string) (total int64) {
  21. //获取总记录数
  22. /*sqlCount := `select count(*) from (`
  23. sqlCount += `select a.Id,a.SupplierName,b.AccessCardNo,max(c.OldSupplierName) OldSupplierName,b.SupplierTypeCode,`
  24. sqlCount += `a.LegalPerson,a.RegCapital,a.Mobile,b.AuditDate,b.ApplyTime,b.InFlag,a.ContactName,a.CommercialNo, `
  25. sqlCount += `a.DepositBank,a.HseTraining,a.CompanyType,a.SetupTime,a.Address,a.Province,a.City,a.Street, `
  26. sqlCount += `a.LinkAddress,a.LinkProvince,a.LinkCity,a.LinkStreet,a.BusinessScope, `
  27. sqlCount += `group_concat(distinct d.Name) CerSubName,group_concat(distinct d.NeedFileType) NeedFileType `
  28. sqlCount += `from ` + supplierTableName + ` a `
  29. sqlCount += `left join ` + supplierCertTableName + ` b on b.SupplierId = a.Id `
  30. sqlCount += `left join ` + OilInfoChangeName + ` c on c.SupplierId = a.Id `
  31. sqlCount += `left join ` + OilSupplierCert2FileName + ` d on d.SupplierId = b.Id `
  32. sqlCount += `where ` + where
  33. sqlCount += `group by a.Id,b.Id `+having+`) f`*/
  34. var sql string
  35. sql = `select SQL_CALC_FOUND_ROWS a.Id,a.SupplierName,b.AccessCardNo,max(c.OldSupplierName) OldSupplierName,b.SupplierTypeCode,`
  36. sql += `a.LegalPerson,a.RegCapital,a.Mobile,b.AuditDate,b.ApplyTime,b.InFlag,a.ContactName,a.CommercialNo, `
  37. sql += `a.DepositBank,a.HseTraining,a.CompanyType,a.SetupTime,a.Address,a.Province,a.City,a.Street, `
  38. sql += `a.LinkAddress,a.LinkProvince,a.LinkCity,a.LinkStreet,a.BusinessScope, `
  39. sql += `b.InStyle, b.Id CertId,`
  40. sql += `group_concat(distinct e.NeedFileType) NeedFileType `
  41. if liftjoon != "" {
  42. sql += `,group_concat(distinct d.Name) CerSubName `
  43. }
  44. sql += `from ` + supplierTableName + ` a `
  45. sql += `left join ` + supplierCertTableName + ` b on b.SupplierId = a.Id `
  46. sql += `left join ` + OilInfoChangeName + ` c on c.SupplierId = a.Id `
  47. sql += liftjoon
  48. sql += `left join ` + OilSupplierFileName + ` e on e.SupplierId = a.Id `
  49. sql += `where ` + where
  50. sql += ` group by a.Id,b.Id ` + having
  51. if asc {
  52. sql += ` order by ` + orderby + ` ASC `
  53. } else {
  54. sql += ` order by ` + orderby + ` DESC `
  55. }
  56. sql += ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  57. sqlCount := "SELECT FOUND_ROWS() as total"
  58. var totalResult TotalResult
  59. session := s.DBE.NewSession()
  60. session.Begin()
  61. session.SQL(sql).Find(entitiesPtr)
  62. session.SQL(sqlCount).Get(&totalResult)
  63. session.Commit()
  64. total = totalResult.Total
  65. return total
  66. }
  67. func (s *SelectService) GetUp( supplierCertTableName string, pageIndex, itemsPerPage int64, orderby string, asc bool, entitiesPtr interface{}, where string) (total int64) {
  68. var resultsSlice []map[string][]byte
  69. var sql string
  70. sql = ` select (select FullName from Base_Organize where Id = a.CommitComId) FullName,max(a.CommitComId) CommitComId,a.SupplierTypeName,`
  71. sql +=` count(a.Status>=6 or null ) HeGe,count(a.Status=-5 or null ) BuHeGe,count(a.Status=-5 or null ) + count(a.Status>=6 or null ) ZongShu,GROUP_CONCAT(a.BackReason SEPARATOR ';') Reason `
  72. sql += ` from ` + supplierCertTableName + ` a `
  73. sql += ` where a.CommitComId is not null and a.CommitComId!="" and a.CommitComId!=0 ` + where
  74. sql += ` group by FullName,SupplierTypeName `
  75. if asc {
  76. sql += ` order by ` + orderby + ` ASC `
  77. } else {
  78. sql += ` order by ` + orderby + ` DESC `
  79. }
  80. //获取总记录数
  81. sqlCount := `select count(*) from (` + sql+ `) a `
  82. sql += ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  83. s.DBE.SQL(sql).Find(entitiesPtr)
  84. resultsSlice, _ = s.DBE.Query(sqlCount)
  85. if len(resultsSlice) > 0 {
  86. results := resultsSlice[0]
  87. for _, value := range results {
  88. total, _ = strconv.ParseInt(string(value), 10, 64)
  89. break
  90. }
  91. }
  92. return total
  93. }
  94. func (s *SelectService) GetTotalInMonth( orderby string, asc bool, entitiesTotal interface{}, where string) (total int64) {
  95. var resultsSlice []map[string][]byte
  96. endtime, _ := strconv.Atoi(where)
  97. if endtime == 0 {
  98. endtime = 5000
  99. }else {
  100. endtime = endtime+1}
  101. timedone := strconv.Itoa(endtime)
  102. var sql string
  103. sql = `(SELECT AnnualAudit.Month,AnnualAudit,Store,Append,InfoChange,CatalogSelf,CatalogWin,CatalogTrade,CatalogComp,CatalogSpe FROM(
  104. SELECT b.Month,a.AnnualAudit from (select extract(month from ApplyTime) time ,COUNT(*) AnnualAudit
  105. from OilAnnualAudit
  106. WHERE ApplyTime >= '` + where +`' AND ApplyTime < '` + timedone +`' AND Status = '11'
  107. group by extract(month from ApplyTime)) a
  108. RIGHT JOIN OilCountMonth b ON b.Month = a.time) AnnualAudit
  109. LEFT JOIN (SELECT b.Month,a.Store from (select extract(month from ApplyTime) time ,COUNT(*) Store
  110. from OilSupplierCert
  111. WHERE ApplyTime >= '` + where +`' AND ApplyTime < '` + timedone +`' AND InFlag = '1'
  112. group by extract(month from ApplyTime)) a
  113. RIGHT JOIN OilCountMonth b ON b.Month = a.time) Store ON AnnualAudit.Month = Store.Month
  114. LEFT JOIN (SELECT b.Month,a.Append from (select extract(month from ApplyDate) time ,COUNT(*) Append
  115. from OilSupplierCertAppend
  116. WHERE ApplyDate >= '` + where +`' AND ApplyDate < '` + timedone +`' AND Status = '11'
  117. group by extract(month from ApplyDate)) a
  118. RIGHT JOIN OilCountMonth b ON b.Month = a.time) Append ON AnnualAudit.Month = Append.Month
  119. LEFT JOIN (SELECT b.Month,a.InfoChange from (select extract(month from CreateOn) time ,COUNT(*) InfoChange
  120. from OilInfoChange
  121. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND Status = '11'
  122. group by extract(month from CreateOn)) a
  123. RIGHT JOIN OilCountMonth b ON b.Month = a.time) InfoChange ON AnnualAudit.Month = InfoChange.Month
  124. LEFT JOIN (SELECT b.Month,a.CatalogSelf from (select extract(month from CreateOn) time ,COUNT(*) CatalogSelf
  125. from OilCatalog
  126. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=1
  127. group by extract(month from CreateOn)) a
  128. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogSelf ON AnnualAudit.Month = CatalogSelf.Month
  129. LEFT JOIN (SELECT b.Month,a.CatalogWin from (select extract(month from CreateOn) time ,COUNT(*) CatalogWin
  130. from OilCatalog
  131. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=2
  132. group by extract(month from CreateOn)) a
  133. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogWin ON AnnualAudit.Month = CatalogWin.Month
  134. LEFT JOIN (SELECT b.Month,a.CatalogTrade from (select extract(month from CreateOn) time ,COUNT(*) CatalogTrade
  135. from OilCatalog
  136. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=3
  137. group by extract(month from CreateOn)) a
  138. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogTrade ON AnnualAudit.Month = CatalogTrade.Month
  139. LEFT JOIN (SELECT b.Month,a.CatalogComp from (select extract(month from CreateOn) time ,COUNT(*) CatalogComp
  140. from OilCatalog
  141. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=4
  142. group by extract(month from CreateOn)) a
  143. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogComp ON AnnualAudit.Month = CatalogComp.Month
  144. LEFT JOIN (SELECT b.Month,a.CatalogSpe from (select extract(month from CreateOn) time ,COUNT(*) CatalogSpe
  145. from OilCatalog
  146. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=5
  147. group by extract(month from CreateOn)) a
  148. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogSpe ON AnnualAudit.Month = CatalogSpe.Month ORDER BY AnnualAudit.Month) `
  149. //if asc {
  150. // sql += ` order by ` + orderby + ` ASC `
  151. //} else {
  152. // sql += ` order by ` + orderby + ` DESC `
  153. //}
  154. s.DBE.SQL(sql).Find(entitiesTotal)
  155. if len(resultsSlice) > 0 {
  156. results := resultsSlice[0]
  157. for _, value := range results {
  158. total, _ = strconv.ParseInt(string(value), 10, 64)
  159. break
  160. }
  161. }
  162. return total
  163. }