statistic_report.go 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. package models
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hz_crm_api/models/company"
  5. )
  6. // 获取即将过期的客户数量(根据客户来展示)
  7. func GetWillExpireCompanyListCount(condition string, pars []interface{}) (count int, err error) {
  8. o := orm.NewOrm()
  9. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name FROM company_contract a
  10. JOIN company b ON a.company_id = b.company_id
  11. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1`
  12. if condition != "" {
  13. sql += condition
  14. }
  15. sql = "SELECT COUNT(1) AS count FROM ( SELECT * from (" + sql + " order by a.end_date desc ) d GROUP BY d.company_id,d.product_id ) f ;"
  16. err = o.Raw(sql, pars).QueryRow(&count)
  17. return
  18. }
  19. func GetWillExpireCompanyListCountV2(condition string, pars []interface{}) (count int, err error) {
  20. o := orm.NewOrm()
  21. sql := `SELECT a.company_id,a.product_id,a.seller_id,a.seller_name,b.region_type FROM company_product a
  22. JOIN company b ON a.company_id = b.company_id WHERE a.status = "正式"`
  23. if condition != "" {
  24. sql += condition
  25. }
  26. sql = "SELECT COUNT(1) AS count FROM ( SELECT * from (" + sql + " order by a.end_date desc ) d GROUP BY d.company_id ) f ;"
  27. err = o.Raw(sql, pars).QueryRow(&count)
  28. return
  29. }
  30. // 待签约用户列表
  31. type WillExpireCompanyList struct {
  32. CompanyContractId int `description:"合同id"`
  33. ContractType string `description:"合同类型"`
  34. CompanyId int `description:"企业客户id"`
  35. CompanyName string `description:"企业客户名称"`
  36. ProductId int `description:"产品id"`
  37. ProductName string `description:"产品名称"`
  38. CompanyProductId int `description:"客户购买产品授权id"`
  39. ContractCode string `description:"合同编码"`
  40. StartDate string `description:"合同开始日期"`
  41. EndDate string `description:"合同结束日期"`
  42. Money float64 `description:"合同金额"`
  43. PayMethod string `description:"付款方式"`
  44. PayChannel string `description:"付款渠道"`
  45. ImgUrl string `description:"合同图片"`
  46. CreateTime string `description:"合同创建时间"`
  47. ModifyTime string `description:"合同修改时间"`
  48. Status string `description:"合同审批状态,0:待审批,1:已审批;默认:1"`
  49. RegionType string `description:"企业客户所属区域;可选范围:国内,海外"`
  50. SellerId int `description:"归属销售id"`
  51. SellerName string `description:"归属销售名称"`
  52. ExpireDay string `description:"剩余可用天数"`
  53. PermissionList []*company.CompanyReportPermissionAndName `description:"产品权限"`
  54. }
  55. // 获取即将过期的客户数量(根据合同来展示)
  56. func GetWillExpireContactListCount(condition string, pars []interface{}) (count int, err error) {
  57. o := orm.NewOrm()
  58. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name FROM company_contract a
  59. JOIN company b ON a.company_id = b.company_id
  60. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1`
  61. if condition != "" {
  62. sql += condition
  63. }
  64. sql = "SELECT COUNT(1) AS count FROM (" + sql + " order by a.end_date desc ) d ;"
  65. err = o.Raw(sql, pars).QueryRow(&count)
  66. return
  67. }
  68. func GetWillExpireContactListCountV2(condition string, pars []interface{}) (count int, err error) {
  69. o := orm.NewOrm()
  70. sql := `SELECT a.company_id,a.product_id,a.seller_id,a.seller_name,b.region_type FROM company_product a
  71. JOIN company b ON a.company_id = b.company_id WHERE a.status = "正式"`
  72. if condition != "" {
  73. sql += condition
  74. }
  75. sql = "SELECT COUNT(1) AS count FROM (" + sql + " GROUP BY a.company_id,product_id ) f ;"
  76. err = o.Raw(sql, pars).QueryRow(&count)
  77. return
  78. }
  79. // 获取即将过期的客户列表(根据合同来展示)
  80. func GetWillExpireCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*WillExpireCompanyList, err error) {
  81. o := orm.NewOrm()
  82. //o.Using("rddp")
  83. //产品权限
  84. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  85. JOIN company b ON a.company_id = b.company_id
  86. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  87. if condition != "" {
  88. sql += condition
  89. }
  90. sql = sql + " order by a.end_date asc,a.company_id desc LIMIT ?,? "
  91. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  92. return
  93. }
  94. func GetWillExpireCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*WillExpireCompanyList, err error) {
  95. o := orm.NewOrm()
  96. //o.Using("rddp")
  97. //产品权限
  98. sql := `SELECT a.company_id,a.product_id,a.seller_id,a.seller_name,a.product_name,a.start_date,a.end_date,b.region_type,b.company_name FROM company_product a
  99. JOIN company b ON a.company_id = b.company_id WHERE a.status = "正式"`
  100. if condition != "" {
  101. sql += condition
  102. }
  103. sql = sql + " order by a.end_date asc,a.company_id desc LIMIT ?,? "
  104. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  105. return
  106. }
  107. // GetContractStatListCount 获取合同统计数据列表数量(根据客户来展示)
  108. func GetContractStatListCount(condition string, pars []interface{}) (count, countContract int, money float64, err error) {
  109. o := orm.NewOrm()
  110. sql := `SELECT a.company_contract_id,a.company_id,a.product_id,b.region_type,c.seller_id,c.seller_name,sum(a.money) money,count(1) count_contract FROM company_contract a
  111. JOIN company b ON a.company_id = b.company_id
  112. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1`
  113. if condition != "" {
  114. sql += condition
  115. }
  116. sql = "SELECT COUNT(1) AS count,sum(money) money,sum(count_contract) count_contract FROM (" + sql + " group by a.company_id order by a.end_date desc ) d "
  117. err = o.Raw(sql, pars).QueryRow(&count, &money, &countContract)
  118. return
  119. }
  120. // ContractStatList 合同数据报表结构体
  121. type ContractStatList struct {
  122. ContractId int `description:"合同id"`
  123. CompanyContractId int `description:"客户使用合同id"`
  124. CompanyId int `description:"企业客户id"`
  125. CompanyName string `description:"企业客户名称"`
  126. ProductId int `description:"产品id"`
  127. ProductName string `description:"产品名称"`
  128. Count int `description:"有效合同数"`
  129. SumMoney float64 `description:"有效合同总金额"`
  130. Status string `description:"合同审批状态,0:待审批,1:已审批;默认:1"`
  131. RegionType string `description:"企业客户所属区域;可选范围:国内,海外"`
  132. SellerId int `description:"归属销售id"`
  133. SellerName string `description:"归属销售名称"`
  134. Source string `description:"合同来源,枚举值:上传附件、系统合同"`
  135. CreateTime string `description:"新增合同时间"`
  136. }
  137. // GetContractStatList 获取合同统计数据列表(根据客户来展示)
  138. func GetContractStatList(condition string, pars []interface{}, startSize, pageSize int) (items []*ContractStatList, err error) {
  139. o := orm.NewOrm()
  140. //o.Using("rddp")
  141. //产品权限
  142. sql := `SELECT a.company_contract_id,a.company_id,a.product_id,a.product_name,a.end_date,b.region_type,c.seller_id,c.seller_name,b.company_name,count(1) count,sum(money) sum_money FROM company_contract a
  143. JOIN company b ON a.company_id = b.company_id
  144. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  145. if condition != "" {
  146. sql += condition
  147. }
  148. sql += " GROUP BY company_id order by end_date asc,a.company_id desc LIMIT ?,? "
  149. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  150. return
  151. }
  152. // GetContractStatListCountBySource 根据合同来源获取合同统计数据列表数量(根据合同来展示)
  153. func GetContractStatListCountBySource(source, condition, condition2 string, pars1, pars2 []interface{}) (count int, err error) {
  154. o := orm.NewOrm()
  155. sql1 := `SELECT a.company_contract_id FROM company_contract a
  156. JOIN company b ON a.company_id = b.company_id
  157. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.source="上传附件" `
  158. if condition != "" {
  159. sql1 += condition
  160. }
  161. sql2 := `SELECT a.contract_id,a.seller_id,a.seller_name,a.company_name,a.product_id FROM contract a join admin b on a.seller_id=b.admin_id WHERE a.is_delete = 0 and a.contract_business_type="业务合同" and a.status="已签回" `
  162. if condition2 != "" {
  163. sql2 += condition2
  164. }
  165. sql := ``
  166. pars := make([]interface{}, 0)
  167. switch source {
  168. case "上传附件":
  169. sql = sql1
  170. pars = pars1
  171. case "系统合同":
  172. sql = sql2
  173. pars = pars2
  174. default:
  175. sql = `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f `
  176. pars = append(pars, pars1, pars2)
  177. }
  178. sql = `select count(1) count from (` + sql + `) g`
  179. //
  180. //sql := `SELECT a.company_contract_id FROM company_contract a
  181. // JOIN company b ON a.company_id = b.company_id
  182. // JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  183. //
  184. //if condition != "" {
  185. // sql += condition
  186. //}
  187. //sql = "SELECT COUNT(1) AS count FROM (" + sql + " order by a.end_date desc ) d "
  188. err = o.Raw(sql, pars).QueryRow(&count)
  189. return
  190. }
  191. // GetContractStatListBySource 获取合同统计数据列表(根据客户来展示)
  192. func GetContractStatListBySource(source, condition, condition2 string, pars1, pars2 []interface{}, startSize, pageSize int) (items []*ContractStatList, err error) {
  193. o := orm.NewOrm()
  194. //o.Using("rddp")
  195. //产品权限
  196. //sql := `SELECT a.company_contract_id,a.company_id,a.product_id,a.product_name,a.end_date,a.source,a.create_time,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  197. // JOIN company b ON a.company_id = b.company_id
  198. // JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  199. //
  200. //if condition != "" {
  201. // sql += condition
  202. //}
  203. //sql += " order by end_date asc,a.company_id desc LIMIT ?,? "
  204. sql1 := `SELECT 0 contract_id,a.company_contract_id,a.company_id,a.product_id,a.product_name,a.end_date,a.source,a.create_time,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  205. JOIN company b ON a.company_id = b.company_id
  206. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.source="上传附件" `
  207. if condition != "" {
  208. sql1 += condition
  209. }
  210. sql2 := `SELECT a.contract_id,0 company_contract_id,0 company_id,a.product_id,"" product_name,a.end_date,"系统合同" source,a.check_back_file_time create_time,"国内" region_type,a.seller_id,a.seller_name,a.company_name FROM contract a join admin b on a.seller_id=b.admin_id WHERE a.is_delete = 0 and a.contract_business_type="业务合同" and a.status="已签回" `
  211. if condition2 != "" {
  212. sql2 += condition2
  213. }
  214. sql := ``
  215. pars := make([]interface{}, 0)
  216. switch source {
  217. case "上传附件":
  218. sql = sql1
  219. pars = pars1
  220. case "系统合同":
  221. sql = sql2
  222. pars = pars2
  223. default:
  224. sql = `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f `
  225. pars = append(pars, pars1, pars2)
  226. }
  227. //sql += " order by end_date asc,company_name desc LIMIT ?,? "
  228. sql += " order by create_time desc,company_name desc LIMIT ?,? "
  229. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  230. return
  231. }
  232. // 企业客户的合同数据报表
  233. type CompanyContractStatList struct {
  234. CompanyContractId int `description:"客户合同id"`
  235. ContractType string `description:"合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  236. ProductId int `description:"产品id"`
  237. ProductName string `description:"产品名称"`
  238. CompanyId int `description:"客户id"`
  239. ContractCode string `description:"合同编码"`
  240. StartDate string `description:"合同开始时间"`
  241. EndDate string `description:"合同结束时间"`
  242. Money float64 `description:"合同金额"`
  243. PayMethod string `description:"支付方式"`
  244. PayChannel string `description:"支付渠道"`
  245. PermissionList []CompanyContractPermission `description:"合同中的权限列表"`
  246. }
  247. // 客户合同产品权限数据结构
  248. type CompanyContractPermission struct {
  249. ClassifyName string `description:"权限分类名称"`
  250. PermissionList []*company.CompanyContractPermissionName
  251. }
  252. // 企业客户的获取合同统计数据列表(根据合同来展示)
  253. func GetCompanyContractStatList(condition string, pars []interface{}) (items []*CompanyContractStatList, err error) {
  254. o := orm.NewOrm()
  255. //o.Using("rddp")
  256. //产品权限
  257. sql := `SELECT * FROM company_contract a WHERE a.status = 1 `
  258. if condition != "" {
  259. sql += condition
  260. }
  261. sql += "order by end_date asc,company_id desc"
  262. _, err = o.Raw(sql, pars).QueryRows(&items)
  263. return
  264. }
  265. // 收入统计报表合计数据(根据合同来展示)
  266. func GetIncomeListCount(condition string, pars []interface{}) (count int, money float64, err error) {
  267. o := orm.NewOrm()
  268. /*sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name FROM company_contract a
  269. LEFT JOIN company b ON a.company_id = b.company_id
  270. LEFT JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1`
  271. if condition != "" {
  272. sql += condition
  273. }
  274. sql = "SELECT COUNT(1) AS count FROM (" + sql + " order by a.end_date desc ) d GROUP BY d.company_id,d.product_id; "*/
  275. sql := `SELECT COUNT(1) AS count,SUM(money) money FROM company_contract a
  276. JOIN company b ON a.company_id = b.company_id
  277. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1`
  278. if condition != "" {
  279. sql += condition
  280. }
  281. err = o.Raw(sql, pars).QueryRow(&count, &money)
  282. return
  283. }
  284. // 收入统计报表列表数据结构
  285. type IncomeList struct {
  286. CompanyContractId int `description:"合同id"`
  287. ContractType string `description:"合同类型"`
  288. CompanyId int `description:"企业客户id"`
  289. CompanyName string `description:"企业客户名称"`
  290. ProductId int `description:"产品id"`
  291. ProductName string `description:"产品名称"`
  292. CompanyProductId int `description:"客户购买产品授权id"`
  293. ContractCode string `description:"合同编码"`
  294. StartDate string `description:"合同开始日期"`
  295. EndDate string `description:"合同结束日期"`
  296. Money float64 `description:"合同金额"`
  297. PayMethod string `description:"付款方式"`
  298. PayChannel string `description:"付款渠道"`
  299. ImgUrl string `description:"合同图片"`
  300. CreateTime string `description:"合同创建时间"`
  301. ModifyTime string `description:"合同修改时间"`
  302. Status string `description:"合同审批状态,0:待审批,1:已审批;默认:1"`
  303. RegionType string `description:"企业客户所属区域;可选范围:国内,海外"`
  304. SellerId int `description:"归属销售id"`
  305. SellerName string `description:"归属销售名称"`
  306. ExpireDay string `description:"剩余可用天数"`
  307. PermissionList []CompanyContractPermission `description:"客户的产品权限列表"`
  308. }
  309. // 客户合同产品权限数据结构
  310. type CompanyReportPermission struct {
  311. ClassifyName string `description:"权限分类名称"`
  312. PermissionList []*company.CompanyContractPermissionName
  313. }
  314. // 获取收入统计报表列表数据(根据合同来展示)
  315. func GetIncomeList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncomeList, err error) {
  316. o := orm.NewOrm()
  317. //o.Using("rddp")
  318. //产品权限
  319. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  320. JOIN company b ON a.company_id = b.company_id
  321. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  322. if condition != "" {
  323. sql += condition
  324. }
  325. sql += " order by a.start_date desc,a.company_id desc LIMIT ?,? "
  326. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  327. return
  328. }
  329. // 增量客户统计报表列表数据结构
  330. type IncrementalList struct {
  331. CompanyContractId int `description:"合同id"`
  332. ContractType string `description:"合同类型"`
  333. CompanyId int `description:"企业客户id"`
  334. CompanyName string `description:"企业客户名称"`
  335. ProductId int `description:"产品id"`
  336. ProductName string `description:"产品名称"`
  337. ProductStatus string `description:"产品名称"`
  338. CompanyProductId int `description:"客户购买产品授权id"`
  339. ContractCode string `description:"合同编码"`
  340. StartDate string `description:"合同开始日期"`
  341. EndDate string `description:"合同结束日期"`
  342. Money float64 `description:"合同金额"`
  343. PayMethod string `description:"付款方式"`
  344. PayChannel string `description:"付款渠道"`
  345. ImgUrl string `description:"合同图片"`
  346. CreateTime string `description:"合同创建时间"`
  347. ModifyTime string `description:"合同修改时间"`
  348. Status string `description:"合同审批状态,0:待审批,1:已审批;默认:1"`
  349. RegionType string `description:"企业客户所属区域;可选范围:国内,海外"`
  350. SellerId int `description:"归属销售id"`
  351. SellerName string `description:"归属销售名称"`
  352. SellerNameLast string `description:"合同到期之前最后所属归属销售名称"`
  353. ShareSeller string `description:"合同到期之前最后所属共享销售员"`
  354. ShareSellerLast string `description:"共享销售员"`
  355. ExpireDay string `description:"剩余可用天数"`
  356. PermissionList []*company.CompanyReportPermission `description:"产品权限"`
  357. Count int `json:"-" description:"合同数"`
  358. RenewalReason string `description:"未续约说明"`
  359. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  360. PackageDifference string `description:"和上一份合同的区别"`
  361. AscribeContent string `description:"归因标签说明"`
  362. IsShowNoRenewedNote bool `description:"是否展示未续约备注按钮"`
  363. Content string `description:"归因内容说明"`
  364. PermissionName string `description:"权限名"`
  365. PermissionNameExport string `description:"权限名导出时使用"`
  366. PermissionNameStatus string `description:"权限状态"`
  367. CompanyProductStatus string `description:"客户状态"`
  368. //CompanyContractIdGroup string `description:"表company_contract合并的 company_contract_id"`
  369. IsUserMaker int `description:"近四周之内是否包含决策人互动过 ,0否,1是"`
  370. SellerNameInit string `description:"权益初始化销售"`
  371. SellerIdInit int `description:"权益初始化销售ID"`
  372. SellerIdLast int `description:"合同到期之前最后所属销售ID"`
  373. ShareSellerInit string `description:"共享销售员"`
  374. SysRealName string `description:"操作者名称"`
  375. SysUserId string `description:"操作者Id"`
  376. Operation string `description:"操作"`
  377. WxUserId int `description:"用户ID,开通个人权限使用"`
  378. WxUserName string `description:"用户姓名,针对某份合同仅对单个用户使用的时候的场景"`
  379. }
  380. // GetIncrementalNewCompanyCount 获取增量客户报表列表统计数据(根据合同来展示)
  381. func GetIncrementalNewCompanyCount(condition string, pars []interface{}) (total int, err error) {
  382. o := orm.NewOrm()
  383. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  384. JOIN company b ON a.company_id = b.company_id
  385. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  386. if condition != "" {
  387. sql += condition
  388. }
  389. //sql += " order by a.start_date desc "
  390. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  391. err = o.Raw(sql, pars).QueryRow(&total)
  392. return
  393. }
  394. // GetIncrementalNewCompanyProductCount 获取增量客户产品报表列表统计数据(根据合同来展示)
  395. func GetIncrementalNewCompanyProductCount(condition string, pars []interface{}) (total int, err error) {
  396. o := orm.NewOrm()
  397. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  398. JOIN company b ON a.company_id = b.company_id
  399. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  400. if condition != "" {
  401. sql += condition
  402. }
  403. //sql += " order by a.start_date desc "
  404. sql = `select count(1) count from (` + sql + ` group by company_id,product_id) f`
  405. err = o.Raw(sql, pars).QueryRow(&total)
  406. return
  407. }
  408. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  409. func GetIncrementalNewCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  410. o := orm.NewOrm()
  411. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  412. JOIN company b ON a.company_id = b.company_id
  413. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  414. if condition != "" {
  415. sql += condition
  416. }
  417. sql += " order by a.start_date desc "
  418. sql = `select *,count(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  419. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  420. return
  421. }
  422. // GetIncrementalNewCompanyCountV2 获取增量客户报表列表统计数据(根据合同来展示)
  423. func GetIncrementalNewCompanyCountV2(condition string, pars []interface{}) (total int, err error) {
  424. o := orm.NewOrm()
  425. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  426. JOIN company b ON a.company_id = b.company_id
  427. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=1 `
  428. if condition != "" {
  429. sql1 += condition
  430. }
  431. sql1 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=1 and end_date>?) `
  432. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  433. JOIN company b ON a.company_id = b.company_id
  434. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=2 `
  435. if condition != "" {
  436. sql2 += condition
  437. }
  438. sql2 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=2 and end_date>?) `
  439. sql := sql1 + ` union (` + sql2 + `) `
  440. sql = `select * from (` + sql + `) f group by company_id`
  441. //sql += " order by a.start_date desc "
  442. sql = `select count(1) count from (` + sql + `) g`
  443. pars = append(pars, pars...)
  444. err = o.Raw(sql, pars).QueryRow(&total)
  445. return
  446. }
  447. // GetIncrementalNewCompanyProductCountV2 获取增量客户产品报表列表统计数据(根据合同来展示)
  448. func GetIncrementalNewCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  449. o := orm.NewOrm()
  450. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  451. JOIN company b ON a.company_id = b.company_id
  452. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=1 `
  453. if condition != "" {
  454. sql1 += condition
  455. }
  456. sql1 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=1 and end_date > ?) `
  457. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  458. JOIN company b ON a.company_id = b.company_id
  459. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=2 `
  460. if condition != "" {
  461. sql2 += condition
  462. }
  463. sql2 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=2 and end_date > ?) `
  464. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f group by company_id,product_id`
  465. //sql := sql1 + ` union (` + sql2 + `) `
  466. //sql += " order by a.start_date desc "
  467. sql = `select count(1) count from (` + sql + `) g`
  468. pars = append(pars, pars...)
  469. err = o.Raw(sql, pars).QueryRow(&total)
  470. return
  471. }
  472. // GetIncrementalNewCompanyListV2 获取增量客户报表列表数据(根据合同来展示)
  473. func GetIncrementalNewCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  474. o := orm.NewOrm()
  475. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  476. JOIN company b ON a.company_id = b.company_id
  477. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=1 `
  478. if condition != "" {
  479. sql1 += condition
  480. }
  481. sql1 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=1 and end_date > ?) `
  482. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  483. JOIN company b ON a.company_id = b.company_id
  484. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=2 `
  485. if condition != "" {
  486. sql2 += condition
  487. }
  488. sql2 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=2 and end_date > ?) `
  489. //sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  490. // JOIN company b ON a.company_id = b.company_id
  491. // JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  492. //
  493. //if condition != "" {
  494. // sql += condition
  495. //}
  496. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  497. sql += " "
  498. sql = `select *,count(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  499. pars = append(pars, pars...)
  500. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  501. return
  502. }
  503. // 获取今日存量客户报表列表统计数据(根据合同来展示)
  504. func GetTodayStackCompanyProductCount(condition string, pars []interface{}) (total int, err error) {
  505. o := orm.NewOrm()
  506. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  507. JOIN company b ON a.company_id = b.company_id
  508. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  509. if condition != "" {
  510. sql += condition
  511. }
  512. //sql += " order by a.start_date desc "
  513. sql = `select count(1) count from (` + sql + ` ) f`
  514. err = o.Raw(sql, pars).QueryRow(&total)
  515. return
  516. }
  517. // GetTodayStackCompanyList 获取当天存量客户报表列表数据(根据合同来展示)
  518. func GetTodayStackCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  519. o := orm.NewOrm()
  520. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  521. JOIN company b ON a.company_id = b.company_id
  522. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  523. if condition != "" {
  524. sql += condition
  525. }
  526. sql += " order by a.start_date desc "
  527. sql = `select * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  528. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  529. return
  530. }
  531. // GetTodayStackCompanyProductCountV2 获取今日存量客户报表列表统计数据(续约客户数,根据合同来展示)
  532. func GetTodayStackCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  533. o := orm.NewOrm()
  534. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  535. JOIN company b ON a.company_id = b.company_id
  536. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=1 `
  537. if condition != "" {
  538. sql1 += condition
  539. }
  540. sql1 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=1 and end_date>?) `
  541. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  542. JOIN company b ON a.company_id = b.company_id
  543. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=2 `
  544. if condition != "" {
  545. sql2 += condition
  546. }
  547. sql2 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=2 and end_date>?) `
  548. sql := sql1 + ` union (` + sql2 + `) `
  549. sql = `select * from (` + sql + `) f group by company_id`
  550. //sql += " order by a.start_date desc "
  551. sql = `select count(1) count from (` + sql + `) g`
  552. pars = append(pars, pars...)
  553. err = o.Raw(sql, pars).QueryRow(&total)
  554. return
  555. }
  556. // GetTodayStackCompanyListV2 获取当天存量客户报表列表数据(续约客户,根据合同来展示)
  557. func GetTodayStackCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  558. o := orm.NewOrm()
  559. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  560. JOIN company b ON a.company_id = b.company_id
  561. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=1 `
  562. if condition != "" {
  563. sql1 += condition
  564. }
  565. sql1 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=1 and end_date > ?) `
  566. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  567. JOIN company b ON a.company_id = b.company_id
  568. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 and a.product_id=2 `
  569. if condition != "" {
  570. sql2 += condition
  571. }
  572. sql2 += ` and a.company_id not in (select company_id from company_contract where contract_type="新签合同" and status=1 and product_id=2 and end_date > ?) `
  573. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  574. sql = `select * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  575. pars = append(pars, pars...)
  576. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  577. return
  578. }
  579. // 获取其他增量客户报表列表数据(根据合同来展示)
  580. func GetOtherIncrementalNewCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  581. o := orm.NewOrm()
  582. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  583. JOIN company b ON a.company_id = b.company_id
  584. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1
  585. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  586. if condition != "" {
  587. sql += condition
  588. }
  589. sql += " order by a.start_date desc "
  590. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  591. _, err = o.Raw(sql, pars).QueryRows(&items)
  592. return
  593. }
  594. // 获取存量客户未续约报表列表统计数据(根据合同来展示)
  595. func GetIncrementalNotRenewalCompanyTotalCount(condition string, pars []interface{}) (total int, err error) {
  596. o := orm.NewOrm()
  597. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  598. JOIN company b ON a.company_id = b.company_id
  599. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  600. if condition != "" {
  601. sql += condition
  602. }
  603. //sql += " order by a.start_date desc "
  604. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  605. err = o.Raw(sql, pars).QueryRow(&total)
  606. return
  607. }
  608. // 获取存量客户未续约报表列表数据(根据合同来展示)
  609. func GetIncrementalNotRenewalCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  610. o := orm.NewOrm()
  611. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  612. JOIN company b ON a.company_id = b.company_id
  613. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  614. if condition != "" {
  615. sql += condition
  616. }
  617. sql += " order by a.start_date desc "
  618. sql = `select *,count(*) count from (` + sql + `) b group by company_id order by end_date asc,company_id desc limit ?,?`
  619. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  620. return
  621. }
  622. // 获取存量客户未续约报表列表数据(根据合同来展示)
  623. func GetOtherIncrementalNotRenewalCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  624. o := orm.NewOrm()
  625. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  626. JOIN company b ON a.company_id = b.company_id
  627. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1
  628. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  629. if condition != "" {
  630. sql += condition
  631. }
  632. sql += " order by a.start_date desc "
  633. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  634. _, err = o.Raw(sql, pars).QueryRows(&items)
  635. return
  636. }
  637. // 获取试用客户报表列表统计数据(根据新增客户时间来展示)
  638. func GetIncrementalCompanyCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  639. o := orm.NewOrm()
  640. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  641. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  642. RIGHT JOIN company b ON a.company_id = b.company_id
  643. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  644. if condition != "" {
  645. sql += condition
  646. }
  647. //sql += " order by a.start_date desc "
  648. sql = `select count(1) count from (` + sql + ` group by company_id ) f `
  649. err = o.Raw(sql, pars).QueryRow(&total)
  650. return
  651. }
  652. // 获取试用客户报表列表统计数据(根据新增客户时间来展示)
  653. func GetIncrementalCompanyCountByOperationRecordRai(condition string, pars []interface{}) (total int, err error) {
  654. o := orm.NewOrm()
  655. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  656. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  657. RIGHT JOIN company b ON a.company_id = b.company_id
  658. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  659. if condition != "" {
  660. sql += condition
  661. }
  662. //sql += " order by a.start_date desc "
  663. sql = `select count(1) count from (` + sql + ` group by a.id ) f `
  664. err = o.Raw(sql, pars).QueryRow(&total)
  665. return
  666. }
  667. // 获取试用客户报表列表统计数据(根据新增客户和对应的产品时间来展示)
  668. func GetIncrementalCompanyProductCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  669. o := orm.NewOrm()
  670. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  671. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  672. RIGHT JOIN company b ON a.company_id = b.company_id
  673. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  674. if condition != "" {
  675. sql += condition
  676. }
  677. //sql += " order by a.start_date desc "
  678. sql = `select count(1) count from (` + sql + ` group by company_id,product_id ) f `
  679. err = o.Raw(sql, pars).QueryRow(&total)
  680. return
  681. }
  682. // 获取试用客户报表列表数据(根据新增客户时间来展示)(2022年02月09日14:47:45废弃)
  683. func GetIncrementalCompanyListByOperationRecordOld(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  684. o := orm.NewOrm()
  685. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  686. a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  687. RIGHT JOIN company b ON a.company_id = b.company_id
  688. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  689. if condition != "" {
  690. sql += condition
  691. }
  692. //sql += " order by a.start_date desc "
  693. sql = `select * from (` + sql + ` order by create_time asc) f group by company_id,product_id order by create_time desc,company_id desc limit ?,? `
  694. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  695. return
  696. }
  697. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  698. func GetIncrementalCompanyListByOperationRecord(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  699. o := orm.NewOrm()
  700. // sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  701. //a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  702. // RIGHT JOIN company b ON a.company_id = b.company_id
  703. // JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  704. //查询出最大id
  705. sql1 := `SELECT max(id) id FROM company_operation_record a
  706. RIGHT JOIN company b ON a.company_id = b.company_id
  707. JOIN company_product c ON b.company_id = c.company_id
  708. AND a.product_id = c.product_id
  709. WHERE 1 = 1 `
  710. if condition != "" {
  711. sql1 += condition
  712. }
  713. sql1 += ` GROUP BY a.company_id, a.product_id `
  714. //查询真正的数据
  715. sql := `SELECT a.id, a.company_id, b.company_name, c.seller_id, c.seller_name, c.seller_name_init, a.product_id, a.product_name, a.create_time, b.region_type, c.renewal_reason, c.renewal_todo, c.status , a.sys_real_name, a.operation, c.share_seller FROM company_operation_record a
  716. RIGHT JOIN company b ON a.company_id = b.company_id
  717. JOIN company_product c ON b.company_id = c.company_id
  718. AND a.product_id = c.product_id
  719. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  720. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  721. return
  722. }
  723. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  724. func GetIncrementalCompanyListByOperationRecordRai(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  725. o := orm.NewOrm()
  726. //查询出最大id
  727. sql1 := `SELECT max(id) id FROM company_operation_record a
  728. RIGHT JOIN company b ON a.company_id = b.company_id
  729. JOIN company_product c ON b.company_id = c.company_id
  730. AND a.product_id = c.product_id
  731. WHERE 1 = 1 `
  732. if condition != "" {
  733. sql1 += condition
  734. }
  735. sql1 += ` GROUP BY a.id `
  736. //查询真正的数据
  737. sql := `SELECT a.id, a.company_id, b.company_name, c.seller_id, c.seller_name, c.seller_name_init, a.product_id, a.product_name, a.create_time, b.region_type, c.renewal_reason, c.renewal_todo, c.status , a.sys_real_name, a.operation ,a.sys_user_id FROM company_operation_record a
  738. RIGHT JOIN company b ON a.company_id = b.company_id
  739. JOIN company_product c ON b.company_id = c.company_id
  740. AND a.product_id = c.product_id
  741. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  742. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  743. return
  744. }
  745. // GetIncrementalCompanyListByOperationRecordLast 获取试用客户报表列表数据(根据新增客户时间来展示)
  746. func GetIncrementalCompanyListByOperationRecordLast(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  747. o := orm.NewOrm()
  748. // sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  749. //a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  750. // RIGHT JOIN company b ON a.company_id = b.company_id
  751. // JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  752. //查询出最大id
  753. sql1 := `SELECT max(id) id FROM company_operation_record a
  754. RIGHT JOIN company b ON a.company_id = b.company_id
  755. JOIN company_product c ON b.company_id = c.company_id
  756. AND a.product_id = c.product_id
  757. WHERE 1 = 1 `
  758. if condition != "" {
  759. sql1 += condition
  760. }
  761. sql1 += ` GROUP BY a.company_id, a.product_id `
  762. //查询真正的数据
  763. sql := `SELECT a.id, a.company_id, b.company_name, c.seller_id_last AS seller_id, c.seller_name_last AS seller_name_init, c.share_seller_last AS share_seller, a.product_id, a.product_name, a.create_time, b.region_type, c.renewal_reason, c.renewal_todo, c.status FROM company_operation_record a
  764. RIGHT JOIN company b ON a.company_id = b.company_id
  765. JOIN company_product c ON b.company_id = c.company_id
  766. AND a.product_id = c.product_id
  767. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  768. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  769. return
  770. }
  771. // 根据操作记录获取其他增量客户报表列表数据(根据新增客户时间来展示)
  772. func GetOtherIncrementalCompanyListByOperationRecord(companyIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  773. o := orm.NewOrm()
  774. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  775. a.product_id,a.product_name,a.create_time,b.region_type FROM company_operation_record a
  776. RIGHT JOIN company b ON a.company_id = b.company_id
  777. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 AND a.company_id in (` + companyIds + `) `
  778. if condition != "" {
  779. sql += condition
  780. }
  781. //sql += " order by a.start_date desc "
  782. sql = `select * from (` + sql + ` order by create_time asc) f group by company_id`
  783. _, err = o.Raw(sql, pars).QueryRows(&items)
  784. return
  785. }
  786. // 获取当前客户报表列表统计数据(正式/试用)
  787. func GetCurrCompanyCount(condition string, pars []interface{}) (count int, err error) {
  788. o := orm.NewOrm()
  789. sql := `SELECT b.company_id from company a
  790. join company_product b on a.company_id=b.company_id WHERE 1 = 1 `
  791. if condition != "" {
  792. sql += condition
  793. }
  794. sql = `SELECT count(*) count from (` + sql + ` group by company_id) d`
  795. err = o.Raw(sql, pars).QueryRow(&count)
  796. return
  797. }
  798. // GetMoreRenewalReason 获取更多未续约说明列表
  799. func GetMoreRenewalReason(CompanyId, ProductId string) (items []*company.CompanyRenewalReason, err error) {
  800. o := orm.NewOrm()
  801. sql := "SELECT * FROM company_renewal_reason WHERE company_id=? AND product_id=? ORDER BY create_time DESC"
  802. _, err = o.Raw(sql, CompanyId, ProductId).QueryRows(&items)
  803. return
  804. }
  805. // GetMoreRenewalReason 获取更多未续约说明列表15.9.1
  806. func GetMoreRenewalReasoninit() (items []*company.CompanyRenewalReason, err error) {
  807. o := orm.NewOrm()
  808. sql := "SELECT * FROM company_renewal_reason WHERE product_id=2 ORDER BY create_time DESC"
  809. _, err = o.Raw(sql).QueryRows(&items)
  810. return
  811. }
  812. // GetLastContractMoney 获取上一份权益合同的金额
  813. func GetLastContractMoney(CompanyIds string) (items []*IncrementalList, err error) {
  814. o := orm.NewOrm()
  815. sql := `SELECT * FROM company_contract WHERE company_id IN (` + CompanyIds + `) AND product_id=2 AND status=1 ORDER BY create_time DESC `
  816. _, err = o.Raw(sql).QueryRows(&items)
  817. return
  818. }
  819. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  820. func GetIncrementalCompanyMergeList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  821. o := orm.NewOrm()
  822. sql := `SELECT a.*,b.region_type,c.seller_id,a.seller_name_init as seller_name ,a.share_seller_init as share_seller ,b.company_name,c.renewal_reason FROM company_contract a
  823. JOIN company b ON a.company_id = b.company_id
  824. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  825. if condition != "" {
  826. sql += condition
  827. }
  828. sql += ` group by a.company_contract_id order by a.start_date desc,a.company_id desc limit ?,?`
  829. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  830. return
  831. }
  832. // 增量客户统计报表列表数据结构
  833. type CompanyContractGroupList struct {
  834. CompanyId int `description:"企业客户id"`
  835. CompanyCount int `description:"合同数"`
  836. }
  837. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  838. func GetCompanyContractGroupList(condition string, pars []interface{}) (items []*CompanyContractGroupList, err error) {
  839. o := orm.NewOrm()
  840. sql := `SELECT
  841. a.company_id,
  842. COUNT( a.company_id ) company_count
  843. FROM
  844. company_contract AS a
  845. INNER JOIN company_product AS b ON a.company_id = b.company_id
  846. WHERE b.product_id = 2 `
  847. if condition != "" {
  848. sql += condition
  849. }
  850. sql += ` GROUP BY a.company_id `
  851. _, err = o.Raw(sql, pars).QueryRows(&items)
  852. return
  853. }
  854. // GetIncrementalNewCompanyList 获取到期合同列表数据(根据合同来展示)
  855. func GetIncrementalCompanyMergeListEnd(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  856. o := orm.NewOrm()
  857. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason,c.status AS product_status FROM company_contract a
  858. JOIN company b ON a.company_id = b.company_id
  859. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  860. if condition != "" {
  861. sql += condition
  862. }
  863. sql += ` group by a.company_contract_id order by a.end_date desc,a.company_id desc limit ?,?`
  864. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  865. return
  866. }
  867. // GetIncrementalCompanyListByOperationRecordMerge 未续约合同
  868. func GetIncrementalCompanyListByOperationRecordMerge(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  869. o := orm.NewOrm()
  870. //查询真正的数据
  871. sql := `SELECT a.company_contract_id,a.contract_type ,a.company_product_id ,a.contract_code ,a.pay_method ,a.pay_channel ,a.package_difference ,a.company_id, a.start_date, a.end_date, a.money, b.company_name, a.seller_id_last as seller_id, a.seller_name_last as seller_name, a.share_seller_last as share_seller, a.product_id, a.product_name, a.create_time, b.region_type, c.renewal_reason, c.renewal_todo, c.status , a.wx_user_id , a.wx_user_name FROM company_contract a
  872. RIGHT JOIN company b ON a.company_id = b.company_id
  873. JOIN company_product c ON b.company_id = c.company_id
  874. AND a.product_id = c.product_id where 1=1 `
  875. //where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  876. // _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  877. if condition != "" {
  878. sql += condition
  879. }
  880. sql += ` group by a.company_contract_id order by end_date desc,company_id desc limit ?,?`
  881. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  882. return
  883. }
  884. func GetIncrementalCompanyListByOperationRecordMerge879() (items []*IncrementalList, err error) {
  885. o := orm.NewOrm()
  886. //查询真正的数据
  887. sql := `SELECT
  888. a.company_contract_id,
  889. a.contract_type,
  890. a.company_product_id,
  891. a.contract_code,
  892. a.pay_method,
  893. a.pay_channel,
  894. a.package_difference,
  895. a.company_id,
  896. a.start_date,
  897. a.end_date,
  898. a.money,
  899. b.company_name,
  900. c.seller_id,
  901. c.seller_name,
  902. a.product_id,
  903. a.product_name,
  904. a.create_time,
  905. b.region_type,
  906. c.renewal_reason,
  907. c.renewal_todo,
  908. c.STATUS
  909. FROM
  910. company_contract a
  911. RIGHT JOIN company b ON a.company_id = b.company_id
  912. JOIN company_product c ON b.company_id = c.company_id
  913. AND a.product_id = c.product_id
  914. WHERE
  915. 1 = 1
  916. AND c.product_id = 2
  917. AND a.STATUS = 1
  918. AND a.end_date >= '2017-09-05'
  919. AND a.end_date <= '2022-12-31'
  920. AND c.STATUS NOT IN ( "永续", "正式", "关闭" )
  921. AND a.company_contract_id NOT IN (SELECT company_contract_id FROM company_contract_no_renewed_ascribe )
  922. GROUP BY
  923. a.company_contract_id
  924. ORDER BY
  925. end_date DESC,
  926. company_id DESC limit 10
  927. `
  928. _, err = o.Raw(sql).QueryRows(&items)
  929. return
  930. }
  931. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  932. func GetIncrementalCompanyPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  933. o := orm.NewOrm()
  934. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason, c.status as company_product_status, d.permission_name as permission_name_export FROM company_contract a
  935. JOIN company b ON a.company_id = b.company_id
  936. JOIN company_product c ON a.company_id = c.company_id
  937. JOIN company_contract_permission d ON d.company_contract_id = a.company_contract_id
  938. and a.product_id=c.product_id WHERE 1 = 1 `
  939. if condition != "" {
  940. sql += condition
  941. }
  942. sql += ` group by d.company_contract_id,d.permission_name order by start_date desc,company_id desc limit ?,?`
  943. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  944. return
  945. }
  946. type CompanyContractPermissionNameGroupCountResp struct {
  947. Total int `description:"数量"`
  948. PermissionName string `description:"权限名"`
  949. }
  950. // GetCompanyContractPermissionNameGroupCount 获取增量客户产品报表列表统计数据(根据合同来展示)
  951. func GetCompanyContractPermissionNameGroupCount(condition string, pars []interface{}) (items []*CompanyContractPermissionNameGroupCountResp, err error) {
  952. o := orm.NewOrm()
  953. sql := `SELECT
  954. COUNT( DISTINCT d.permission_name, d.company_contract_id ) total,
  955. d.permission_name
  956. FROM
  957. company_contract a
  958. JOIN company b ON a.company_id = b.company_id
  959. JOIN company_product c ON a.company_id = c.company_id
  960. AND a.product_id = c.product_id
  961. JOIN company_contract_permission d ON d.company_contract_id = a.company_contract_id
  962. WHERE
  963. 1 = 1 `
  964. //AND d.permission_name IN ( '医药', '消费', '科技', '智造', '策略','研选订阅','研选扣点包' ) `
  965. if condition != "" {
  966. sql += condition
  967. }
  968. sql += `GROUP BY d.permission_name `
  969. _, err = o.Raw(sql, pars).QueryRows(&items)
  970. return
  971. }