2
3

selectservice.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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, OilSupplierCert2FileName string, pageIndex, itemsPerPage int64, orderby string, asc bool, entitiesPtr interface{}, where,having string) (total int64) {
  20. //获取总记录数
  21. /*sqlCount := `select count(*) from (`
  22. sqlCount += `select a.Id,a.SupplierName,b.AccessCardNo,max(c.OldSupplierName) OldSupplierName,b.SupplierTypeCode,`
  23. sqlCount += `a.LegalPerson,a.RegCapital,a.Mobile,b.AuditDate,b.ApplyTime,b.InFlag,a.ContactName,a.CommercialNo, `
  24. sqlCount += `a.DepositBank,a.HseTraining,a.CompanyType,a.SetupTime,a.Address,a.Province,a.City,a.Street, `
  25. sqlCount += `a.LinkAddress,a.LinkProvince,a.LinkCity,a.LinkStreet,a.BusinessScope, `
  26. sqlCount += `group_concat(distinct d.Name) CerSubName,group_concat(distinct d.NeedFileType) NeedFileType `
  27. sqlCount += `from ` + supplierTableName + ` a `
  28. sqlCount += `left join ` + supplierCertTableName + ` b on b.SupplierId = a.Id `
  29. sqlCount += `left join ` + OilInfoChangeName + ` c on c.SupplierId = a.Id `
  30. sqlCount += `left join ` + OilSupplierCert2FileName + ` d on d.SupplierId = b.Id `
  31. sqlCount += `where ` + where
  32. sqlCount += `group by a.Id,b.Id `+having+`) f`*/
  33. var sql string
  34. sql = `select SQL_CALC_FOUND_ROWS a.Id,a.SupplierName,b.AccessCardNo,max(c.OldSupplierName) OldSupplierName,b.SupplierTypeCode,`
  35. sql += `a.LegalPerson,a.RegCapital,a.Mobile,b.AuditDate,b.ApplyTime,b.InFlag,a.ContactName,a.CommercialNo, `
  36. sql += `a.DepositBank,a.HseTraining,a.CompanyType,a.SetupTime,a.Address,a.Province,a.City,a.Street, `
  37. sql += `a.LinkAddress,a.LinkProvince,a.LinkCity,a.LinkStreet,a.BusinessScope, `
  38. sql += `b.InStyle,`
  39. sql += `group_concat(distinct d.Name) CerSubName,group_concat(distinct d.NeedFileType) NeedFileType `
  40. sql += `from ` + supplierTableName + ` a `
  41. sql += `left join ` + supplierCertTableName + ` b on b.SupplierId = a.Id `
  42. sql += `left join ` + OilInfoChangeName + ` c on c.SupplierId = a.Id `
  43. sql += `left join ` + OilSupplierCert2FileName + ` d on d.SupplierId = b.Id `
  44. sql += `where ` + where
  45. sql += ` group by a.Id,b.Id ` + having
  46. if asc {
  47. sql += ` order by ` + orderby + ` ASC `
  48. } else {
  49. sql += ` order by ` + orderby + ` DESC `
  50. }
  51. sql += ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  52. sqlCount := "SELECT FOUND_ROWS() as total"
  53. var totalResult TotalResult
  54. session := s.DBE.NewSession()
  55. session.Begin()
  56. session.SQL(sql).Find(entitiesPtr)
  57. session.SQL(sqlCount).Get(&totalResult)
  58. session.Commit()
  59. total = totalResult.Total
  60. return total
  61. }
  62. func (s *SelectService) GetUp( supplierCertTableName string, pageIndex, itemsPerPage int64, orderby string, asc bool, entitiesPtr interface{}, where string) (total int64) {
  63. var resultsSlice []map[string][]byte
  64. var sql string
  65. sql = ` select (select FullName from Base_Organize where Id = a.CommitComId) FullName,max(a.CommitComId) CommitComId,a.SupplierTypeName,`
  66. 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 `
  67. sql += ` from ` + supplierCertTableName + ` a `
  68. sql += ` where a.CommitComId is not null and a.CommitComId!="" and a.CommitComId!=0 ` + where
  69. sql += ` group by FullName,SupplierTypeName `
  70. if asc {
  71. sql += ` order by ` + orderby + ` ASC `
  72. } else {
  73. sql += ` order by ` + orderby + ` DESC `
  74. }
  75. //获取总记录数
  76. sqlCount := `select count(*) from (` + sql+ `) a `
  77. sql += ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  78. s.DBE.SQL(sql).Find(entitiesPtr)
  79. resultsSlice, _ = s.DBE.Query(sqlCount)
  80. if len(resultsSlice) > 0 {
  81. results := resultsSlice[0]
  82. for _, value := range results {
  83. total, _ = strconv.ParseInt(string(value), 10, 64)
  84. break
  85. }
  86. }
  87. return total
  88. }