selectservice.go 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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, OilCorporateInfoName, 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.EffectEndTime,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, f.FullName, g.CheckUnitName, b.Remark, `
  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 Base_Organize f on f.Id = b.ThirdAudit `
  47. sql += `left join OilCorporateInfo g on g.CommercialNo = a.CommercialNo `
  48. sql += `left join ` + OilInfoChangeName + ` c on c.SupplierId = a.Id `
  49. sql += liftjoon
  50. sql += `left join ` + OilSupplierFileName + ` e on e.SupplierId = a.Id `
  51. sql += `where ` + where
  52. sql += ` group by a.Id,b.Id,g.CheckUnitName ` + having
  53. if asc {
  54. sql += ` order by ` + orderby + ` ASC `
  55. } else {
  56. sql += ` order by ` + orderby + ` DESC `
  57. }
  58. sql += ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  59. sqlCount := "SELECT FOUND_ROWS() as total"
  60. var totalResult TotalResult
  61. session := s.DBE.NewSession()
  62. session.Begin()
  63. session.SQL(sql).Find(entitiesPtr)
  64. session.SQL(sqlCount).Get(&totalResult)
  65. session.Commit()
  66. total = totalResult.Total
  67. return total
  68. }
  69. func (s *SelectService) GetUp( supplierCertTableName string, pageIndex, itemsPerPage int64, orderby string, asc bool, entitiesPtr interface{}, where string) (total int64) {
  70. var resultsSlice []map[string][]byte
  71. var sql string
  72. sql = ` select (select FullName from Base_Organize where Id = a.CommitComId) FullName,max(a.CommitComId) CommitComId,a.SupplierTypeName,`
  73. 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 `
  74. sql += ` from ` + supplierCertTableName + ` a `
  75. sql += ` where a.CommitComId is not null and a.CommitComId!="" and a.CommitComId!=0 ` + where
  76. sql += ` group by FullName,SupplierTypeName `
  77. if asc {
  78. sql += ` order by ` + orderby + ` ASC `
  79. } else {
  80. sql += ` order by ` + orderby + ` DESC `
  81. }
  82. //获取总记录数
  83. sqlCount := `select count(*) from (` + sql+ `) a `
  84. sql += ` limit ` + utils.ToStr((pageIndex-1)*itemsPerPage) + "," + utils.ToStr(itemsPerPage)
  85. s.DBE.SQL(sql).Find(entitiesPtr)
  86. resultsSlice, _ = s.DBE.Query(sqlCount)
  87. if len(resultsSlice) > 0 {
  88. results := resultsSlice[0]
  89. for _, value := range results {
  90. total, _ = strconv.ParseInt(string(value), 10, 64)
  91. break
  92. }
  93. }
  94. return total
  95. }
  96. func (s *SelectService) GetTotalInMonth( orderby string, asc bool, entitiesTotal interface{}, where string) (total int64) {
  97. var resultsSlice []map[string][]byte
  98. endtime, _ := strconv.Atoi(where)
  99. if endtime == 0 {
  100. endtime = 5000
  101. }else {
  102. endtime = endtime+1}
  103. timedone := strconv.Itoa(endtime)
  104. var sql string
  105. sql = `(SELECT AnnualAudit.Month,AnnualAudit,Store,Append,InfoChange,CatalogSelf,CatalogWin,CatalogTrade,CatalogComp,CatalogSpe FROM(
  106. SELECT b.Month,a.AnnualAudit from (select extract(month from ApplyTime) time ,COUNT(*) AnnualAudit
  107. from OilAnnualAudit
  108. WHERE ApplyTime >= '` + where +`' AND ApplyTime < '` + timedone +`' AND Status = '11'
  109. group by extract(month from ApplyTime)) a
  110. RIGHT JOIN OilCountMonth b ON b.Month = a.time) AnnualAudit
  111. LEFT JOIN (SELECT b.Month,a.Store from (select extract(month from StorageOn) time ,COUNT(*) Store
  112. from OilSupplierCert
  113. WHERE StorageOn >= '` + where +`' AND StorageOn < '` + timedone +`' AND InFlag = '1'
  114. group by extract(month from StorageOn)) a
  115. RIGHT JOIN OilCountMonth b ON b.Month = a.time) Store ON AnnualAudit.Month = Store.Month
  116. LEFT JOIN (SELECT b.Month,a.Append from (select extract(month from ApplyDate) time ,COUNT(*) Append
  117. from OilSupplierCertAppend
  118. WHERE ApplyDate >= '` + where +`' AND ApplyDate < '` + timedone +`' AND Status = '11'
  119. group by extract(month from ApplyDate)) a
  120. RIGHT JOIN OilCountMonth b ON b.Month = a.time) Append ON AnnualAudit.Month = Append.Month
  121. LEFT JOIN (SELECT b.Month,a.InfoChange from (select extract(month from CreateOn) time ,COUNT(*) InfoChange
  122. from OilInfoChange
  123. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND Status = '11'
  124. group by extract(month from CreateOn)) a
  125. RIGHT JOIN OilCountMonth b ON b.Month = a.time) InfoChange ON AnnualAudit.Month = InfoChange.Month
  126. LEFT JOIN (SELECT b.Month,a.CatalogSelf from (select extract(month from CreateOn) time ,COUNT(*) CatalogSelf
  127. from OilCatalog
  128. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=1
  129. group by extract(month from CreateOn)) a
  130. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogSelf ON AnnualAudit.Month = CatalogSelf.Month
  131. LEFT JOIN (SELECT b.Month,a.CatalogWin from (select extract(month from CreateOn) time ,COUNT(*) CatalogWin
  132. from OilCatalog
  133. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=2
  134. group by extract(month from CreateOn)) a
  135. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogWin ON AnnualAudit.Month = CatalogWin.Month
  136. LEFT JOIN (SELECT b.Month,a.CatalogTrade from (select extract(month from CreateOn) time ,COUNT(*) CatalogTrade
  137. from OilCatalog
  138. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=3
  139. group by extract(month from CreateOn)) a
  140. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogTrade ON AnnualAudit.Month = CatalogTrade.Month
  141. LEFT JOIN (SELECT b.Month,a.CatalogComp from (select extract(month from CreateOn) time ,COUNT(*) CatalogComp
  142. from OilCatalog
  143. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=4
  144. group by extract(month from CreateOn)) a
  145. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogComp ON AnnualAudit.Month = CatalogComp.Month
  146. LEFT JOIN (SELECT b.Month,a.CatalogSpe from (select extract(month from CreateOn) time ,COUNT(*) CatalogSpe
  147. from OilCatalog
  148. WHERE CreateOn >= '` + where +`' AND CreateOn < '` + timedone +`' AND CatalogType=5
  149. group by extract(month from CreateOn)) a
  150. RIGHT JOIN OilCountMonth b ON b.Month = a.time) CatalogSpe ON AnnualAudit.Month = CatalogSpe.Month ORDER BY AnnualAudit.Month) `
  151. //if asc {
  152. // sql += ` order by ` + orderby + ` ASC `
  153. //} else {
  154. // sql += ` order by ` + orderby + ` DESC `
  155. //}
  156. s.DBE.SQL(sql).Find(entitiesTotal)
  157. if len(resultsSlice) > 0 {
  158. results := resultsSlice[0]
  159. for _, value := range results {
  160. total, _ = strconv.ParseInt(string(value), 10, 64)
  161. break
  162. }
  163. }
  164. return total
  165. }
  166. //获取公司信息和准入范围
  167. func (s *SelectService) GetInfoByCompId(where string,entitiesPtr interface{}) (total int64) {
  168. var sql string
  169. sql = `SELECT a.* from OilSupplier a `+ where
  170. var totalResult TotalResult
  171. session := s.DBE.NewSession()
  172. session.Begin()
  173. session.SQL(sql).Get(entitiesPtr)
  174. //session.SQL(sqlCount).Get(&totalResult)
  175. session.Commit()
  176. total = totalResult.Total
  177. return total
  178. }
  179. func (s *SelectService) Getoilsuppliercertsub( tableName, where string, entitiesPtr interface{}) error {
  180. var sql string
  181. sql = `SELECT a.* from `+ tableName +` a `+ where
  182. err := s.DBE.SQL(sql).Find(entitiesPtr)
  183. return err
  184. }