statistic_report.go 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087
  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. Operation string `description:"操作"`
  376. }
  377. // GetIncrementalNewCompanyCount 获取增量客户报表列表统计数据(根据合同来展示)
  378. func GetIncrementalNewCompanyCount(condition string, pars []interface{}) (total int, err error) {
  379. o := orm.NewOrm()
  380. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  381. JOIN company b ON a.company_id = b.company_id
  382. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  383. if condition != "" {
  384. sql += condition
  385. }
  386. //sql += " order by a.start_date desc "
  387. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  388. err = o.Raw(sql, pars).QueryRow(&total)
  389. return
  390. }
  391. // GetIncrementalNewCompanyProductCount 获取增量客户产品报表列表统计数据(根据合同来展示)
  392. func GetIncrementalNewCompanyProductCount(condition string, pars []interface{}) (total int, err error) {
  393. o := orm.NewOrm()
  394. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  395. JOIN company b ON a.company_id = b.company_id
  396. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  397. if condition != "" {
  398. sql += condition
  399. }
  400. //sql += " order by a.start_date desc "
  401. sql = `select count(1) count from (` + sql + ` group by company_id,product_id) f`
  402. err = o.Raw(sql, pars).QueryRow(&total)
  403. return
  404. }
  405. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  406. func GetIncrementalNewCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  407. o := orm.NewOrm()
  408. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  409. JOIN company b ON a.company_id = b.company_id
  410. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  411. if condition != "" {
  412. sql += condition
  413. }
  414. sql += " order by a.start_date desc "
  415. sql = `select *,count(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  416. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  417. return
  418. }
  419. // GetIncrementalNewCompanyCountV2 获取增量客户报表列表统计数据(根据合同来展示)
  420. func GetIncrementalNewCompanyCountV2(condition string, pars []interface{}) (total int, err error) {
  421. o := orm.NewOrm()
  422. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  423. JOIN company b ON a.company_id = b.company_id
  424. 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 `
  425. if condition != "" {
  426. sql1 += condition
  427. }
  428. 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>?) `
  429. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  430. JOIN company b ON a.company_id = b.company_id
  431. 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 `
  432. if condition != "" {
  433. sql2 += condition
  434. }
  435. 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>?) `
  436. sql := sql1 + ` union (` + sql2 + `) `
  437. sql = `select * from (` + sql + `) f group by company_id`
  438. //sql += " order by a.start_date desc "
  439. sql = `select count(1) count from (` + sql + `) g`
  440. pars = append(pars, pars...)
  441. err = o.Raw(sql, pars).QueryRow(&total)
  442. return
  443. }
  444. // GetIncrementalNewCompanyProductCountV2 获取增量客户产品报表列表统计数据(根据合同来展示)
  445. func GetIncrementalNewCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  446. o := orm.NewOrm()
  447. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  448. JOIN company b ON a.company_id = b.company_id
  449. 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 `
  450. if condition != "" {
  451. sql1 += condition
  452. }
  453. 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 > ?) `
  454. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  455. JOIN company b ON a.company_id = b.company_id
  456. 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 `
  457. if condition != "" {
  458. sql2 += condition
  459. }
  460. 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 > ?) `
  461. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f group by company_id,product_id`
  462. //sql := sql1 + ` union (` + sql2 + `) `
  463. //sql += " order by a.start_date desc "
  464. sql = `select count(1) count from (` + sql + `) g`
  465. pars = append(pars, pars...)
  466. err = o.Raw(sql, pars).QueryRow(&total)
  467. return
  468. }
  469. // GetIncrementalNewCompanyListV2 获取增量客户报表列表数据(根据合同来展示)
  470. func GetIncrementalNewCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  471. o := orm.NewOrm()
  472. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  473. JOIN company b ON a.company_id = b.company_id
  474. 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 `
  475. if condition != "" {
  476. sql1 += condition
  477. }
  478. 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 > ?) `
  479. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  480. JOIN company b ON a.company_id = b.company_id
  481. 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 `
  482. if condition != "" {
  483. sql2 += condition
  484. }
  485. 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 > ?) `
  486. //sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  487. // JOIN company b ON a.company_id = b.company_id
  488. // JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  489. //
  490. //if condition != "" {
  491. // sql += condition
  492. //}
  493. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  494. sql += " "
  495. sql = `select *,count(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  496. pars = append(pars, pars...)
  497. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  498. return
  499. }
  500. // 获取今日存量客户报表列表统计数据(根据合同来展示)
  501. func GetTodayStackCompanyProductCount(condition string, pars []interface{}) (total int, err error) {
  502. o := orm.NewOrm()
  503. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  504. JOIN company b ON a.company_id = b.company_id
  505. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  506. if condition != "" {
  507. sql += condition
  508. }
  509. //sql += " order by a.start_date desc "
  510. sql = `select count(1) count from (` + sql + ` ) f`
  511. err = o.Raw(sql, pars).QueryRow(&total)
  512. return
  513. }
  514. // GetTodayStackCompanyList 获取当天存量客户报表列表数据(根据合同来展示)
  515. func GetTodayStackCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  516. o := orm.NewOrm()
  517. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  518. JOIN company b ON a.company_id = b.company_id
  519. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  520. if condition != "" {
  521. sql += condition
  522. }
  523. sql += " order by a.start_date desc "
  524. sql = `select * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  525. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  526. return
  527. }
  528. // GetTodayStackCompanyProductCountV2 获取今日存量客户报表列表统计数据(续约客户数,根据合同来展示)
  529. func GetTodayStackCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  530. o := orm.NewOrm()
  531. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  532. JOIN company b ON a.company_id = b.company_id
  533. 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 `
  534. if condition != "" {
  535. sql1 += condition
  536. }
  537. 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>?) `
  538. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  539. JOIN company b ON a.company_id = b.company_id
  540. 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 `
  541. if condition != "" {
  542. sql2 += condition
  543. }
  544. 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>?) `
  545. sql := sql1 + ` union (` + sql2 + `) `
  546. sql = `select * from (` + sql + `) f group by company_id`
  547. //sql += " order by a.start_date desc "
  548. sql = `select count(1) count from (` + sql + `) g`
  549. pars = append(pars, pars...)
  550. err = o.Raw(sql, pars).QueryRow(&total)
  551. return
  552. }
  553. // GetTodayStackCompanyListV2 获取当天存量客户报表列表数据(续约客户,根据合同来展示)
  554. func GetTodayStackCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  555. o := orm.NewOrm()
  556. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  557. JOIN company b ON a.company_id = b.company_id
  558. 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 `
  559. if condition != "" {
  560. sql1 += condition
  561. }
  562. 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 > ?) `
  563. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  564. JOIN company b ON a.company_id = b.company_id
  565. 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 `
  566. if condition != "" {
  567. sql2 += condition
  568. }
  569. 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 > ?) `
  570. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  571. sql = `select * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  572. pars = append(pars, pars...)
  573. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  574. return
  575. }
  576. // 获取其他增量客户报表列表数据(根据合同来展示)
  577. func GetOtherIncrementalNewCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  578. o := orm.NewOrm()
  579. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  580. JOIN company b ON a.company_id = b.company_id
  581. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1
  582. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  583. if condition != "" {
  584. sql += condition
  585. }
  586. sql += " order by a.start_date desc "
  587. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  588. _, err = o.Raw(sql, pars).QueryRows(&items)
  589. return
  590. }
  591. // 获取存量客户未续约报表列表统计数据(根据合同来展示)
  592. func GetIncrementalNotRenewalCompanyTotalCount(condition string, pars []interface{}) (total int, err error) {
  593. o := orm.NewOrm()
  594. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  595. JOIN company b ON a.company_id = b.company_id
  596. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  597. if condition != "" {
  598. sql += condition
  599. }
  600. //sql += " order by a.start_date desc "
  601. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  602. err = o.Raw(sql, pars).QueryRow(&total)
  603. return
  604. }
  605. // 获取存量客户未续约报表列表数据(根据合同来展示)
  606. func GetIncrementalNotRenewalCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  607. o := orm.NewOrm()
  608. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  609. JOIN company b ON a.company_id = b.company_id
  610. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  611. if condition != "" {
  612. sql += condition
  613. }
  614. sql += " order by a.start_date desc "
  615. sql = `select *,count(*) count from (` + sql + `) b group by company_id order by end_date asc,company_id desc limit ?,?`
  616. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  617. return
  618. }
  619. // 获取存量客户未续约报表列表数据(根据合同来展示)
  620. func GetOtherIncrementalNotRenewalCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  621. o := orm.NewOrm()
  622. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  623. JOIN company b ON a.company_id = b.company_id
  624. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1
  625. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  626. if condition != "" {
  627. sql += condition
  628. }
  629. sql += " order by a.start_date desc "
  630. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  631. _, err = o.Raw(sql, pars).QueryRows(&items)
  632. return
  633. }
  634. // 获取试用客户报表列表统计数据(根据新增客户时间来展示)
  635. func GetIncrementalCompanyCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  636. o := orm.NewOrm()
  637. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  638. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  639. RIGHT JOIN company b ON a.company_id = b.company_id
  640. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  641. if condition != "" {
  642. sql += condition
  643. }
  644. //sql += " order by a.start_date desc "
  645. sql = `select count(1) count from (` + sql + ` group by company_id ) f `
  646. err = o.Raw(sql, pars).QueryRow(&total)
  647. return
  648. }
  649. // 获取试用客户报表列表统计数据(根据新增客户时间来展示)
  650. func GetIncrementalCompanyCountByOperationRecordRai(condition string, pars []interface{}) (total int, err error) {
  651. o := orm.NewOrm()
  652. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  653. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  654. RIGHT JOIN company b ON a.company_id = b.company_id
  655. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  656. if condition != "" {
  657. sql += condition
  658. }
  659. //sql += " order by a.start_date desc "
  660. sql = `select count(1) count from (` + sql + ` group by a.id ) f `
  661. err = o.Raw(sql, pars).QueryRow(&total)
  662. return
  663. }
  664. // 获取试用客户报表列表统计数据(根据新增客户和对应的产品时间来展示)
  665. func GetIncrementalCompanyProductCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  666. o := orm.NewOrm()
  667. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  668. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  669. RIGHT JOIN company b ON a.company_id = b.company_id
  670. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  671. if condition != "" {
  672. sql += condition
  673. }
  674. //sql += " order by a.start_date desc "
  675. sql = `select count(1) count from (` + sql + ` group by company_id,product_id ) f `
  676. err = o.Raw(sql, pars).QueryRow(&total)
  677. return
  678. }
  679. // 获取试用客户报表列表数据(根据新增客户时间来展示)(2022年02月09日14:47:45废弃)
  680. func GetIncrementalCompanyListByOperationRecordOld(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  681. o := orm.NewOrm()
  682. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  683. a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  684. RIGHT JOIN company b ON a.company_id = b.company_id
  685. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  686. if condition != "" {
  687. sql += condition
  688. }
  689. //sql += " order by a.start_date desc "
  690. 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 ?,? `
  691. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  692. return
  693. }
  694. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  695. func GetIncrementalCompanyListByOperationRecord(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  696. o := orm.NewOrm()
  697. // sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  698. //a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  699. // RIGHT JOIN company b ON a.company_id = b.company_id
  700. // JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  701. //查询出最大id
  702. sql1 := `SELECT max(id) id FROM company_operation_record a
  703. RIGHT JOIN company b ON a.company_id = b.company_id
  704. JOIN company_product c ON b.company_id = c.company_id
  705. AND a.product_id = c.product_id
  706. WHERE 1 = 1 `
  707. if condition != "" {
  708. sql1 += condition
  709. }
  710. sql1 += ` GROUP BY a.company_id, a.product_id `
  711. //查询真正的数据
  712. 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 FROM company_operation_record a
  713. RIGHT JOIN company b ON a.company_id = b.company_id
  714. JOIN company_product c ON b.company_id = c.company_id
  715. AND a.product_id = c.product_id
  716. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  717. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  718. return
  719. }
  720. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  721. func GetIncrementalCompanyListByOperationRecordRai(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  722. o := orm.NewOrm()
  723. //查询出最大id
  724. sql1 := `SELECT max(id) id FROM company_operation_record a
  725. RIGHT JOIN company b ON a.company_id = b.company_id
  726. JOIN company_product c ON b.company_id = c.company_id
  727. AND a.product_id = c.product_id
  728. WHERE 1 = 1 `
  729. if condition != "" {
  730. sql1 += condition
  731. }
  732. sql1 += ` GROUP BY a.id `
  733. //查询真正的数据
  734. 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 FROM company_operation_record a
  735. RIGHT JOIN company b ON a.company_id = b.company_id
  736. JOIN company_product c ON b.company_id = c.company_id
  737. AND a.product_id = c.product_id
  738. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  739. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  740. return
  741. }
  742. // GetIncrementalCompanyListByOperationRecordLast 获取试用客户报表列表数据(根据新增客户时间来展示)
  743. func GetIncrementalCompanyListByOperationRecordLast(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  744. o := orm.NewOrm()
  745. // sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  746. //a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  747. // RIGHT JOIN company b ON a.company_id = b.company_id
  748. // JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  749. //查询出最大id
  750. sql1 := `SELECT max(id) id FROM company_operation_record a
  751. RIGHT JOIN company b ON a.company_id = b.company_id
  752. JOIN company_product c ON b.company_id = c.company_id
  753. AND a.product_id = c.product_id
  754. WHERE 1 = 1 `
  755. if condition != "" {
  756. sql1 += condition
  757. }
  758. sql1 += ` GROUP BY a.company_id, a.product_id `
  759. //查询真正的数据
  760. 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
  761. RIGHT JOIN company b ON a.company_id = b.company_id
  762. JOIN company_product c ON b.company_id = c.company_id
  763. AND a.product_id = c.product_id
  764. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  765. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  766. return
  767. }
  768. // 根据操作记录获取其他增量客户报表列表数据(根据新增客户时间来展示)
  769. func GetOtherIncrementalCompanyListByOperationRecord(companyIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  770. o := orm.NewOrm()
  771. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  772. a.product_id,a.product_name,a.create_time,b.region_type FROM company_operation_record a
  773. RIGHT JOIN company b ON a.company_id = b.company_id
  774. 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 + `) `
  775. if condition != "" {
  776. sql += condition
  777. }
  778. //sql += " order by a.start_date desc "
  779. sql = `select * from (` + sql + ` order by create_time asc) f group by company_id`
  780. _, err = o.Raw(sql, pars).QueryRows(&items)
  781. return
  782. }
  783. // 获取当前客户报表列表统计数据(正式/试用)
  784. func GetCurrCompanyCount(condition string, pars []interface{}) (count int, err error) {
  785. o := orm.NewOrm()
  786. sql := `SELECT b.company_id from company a
  787. join company_product b on a.company_id=b.company_id WHERE 1 = 1 `
  788. if condition != "" {
  789. sql += condition
  790. }
  791. sql = `SELECT count(*) count from (` + sql + ` group by company_id) d`
  792. err = o.Raw(sql, pars).QueryRow(&count)
  793. return
  794. }
  795. // GetMoreRenewalReason 获取更多未续约说明列表
  796. func GetMoreRenewalReason(CompanyId, ProductId string) (items []*company.CompanyRenewalReason, err error) {
  797. o := orm.NewOrm()
  798. sql := "SELECT * FROM company_renewal_reason WHERE company_id=? AND product_id=? ORDER BY create_time DESC"
  799. _, err = o.Raw(sql, CompanyId, ProductId).QueryRows(&items)
  800. return
  801. }
  802. // GetMoreRenewalReason 获取更多未续约说明列表15.9.1
  803. func GetMoreRenewalReasoninit() (items []*company.CompanyRenewalReason, err error) {
  804. o := orm.NewOrm()
  805. sql := "SELECT * FROM company_renewal_reason WHERE product_id=2 ORDER BY create_time DESC"
  806. _, err = o.Raw(sql).QueryRows(&items)
  807. return
  808. }
  809. // GetLastContractMoney 获取上一份权益合同的金额
  810. func GetLastContractMoney(CompanyIds string) (items []*IncrementalList, err error) {
  811. o := orm.NewOrm()
  812. sql := `SELECT * FROM company_contract WHERE company_id IN (` + CompanyIds + `) AND product_id=2 AND status=1 ORDER BY create_time DESC `
  813. _, err = o.Raw(sql).QueryRows(&items)
  814. return
  815. }
  816. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  817. func GetIncrementalCompanyMergeList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  818. o := orm.NewOrm()
  819. 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
  820. JOIN company b ON a.company_id = b.company_id
  821. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  822. if condition != "" {
  823. sql += condition
  824. }
  825. sql += ` group by a.company_contract_id order by a.start_date desc,a.company_id desc limit ?,?`
  826. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  827. return
  828. }
  829. // GetIncrementalNewCompanyList 获取到期合同列表数据(根据合同来展示)
  830. func GetIncrementalCompanyMergeListEnd(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  831. o := orm.NewOrm()
  832. 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
  833. JOIN company b ON a.company_id = b.company_id
  834. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  835. if condition != "" {
  836. sql += condition
  837. }
  838. sql += ` group by a.company_contract_id order by a.end_date desc,a.company_id desc limit ?,?`
  839. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  840. return
  841. }
  842. // GetIncrementalCompanyListByOperationRecordMerge 未续约合同
  843. func GetIncrementalCompanyListByOperationRecordMerge(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  844. o := orm.NewOrm()
  845. //查询真正的数据
  846. 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 FROM company_contract a
  847. RIGHT JOIN company b ON a.company_id = b.company_id
  848. JOIN company_product c ON b.company_id = c.company_id
  849. AND a.product_id = c.product_id where 1=1 `
  850. //where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  851. // _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  852. if condition != "" {
  853. sql += condition
  854. }
  855. sql += ` group by a.company_contract_id order by end_date desc,company_id desc limit ?,?`
  856. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  857. return
  858. }
  859. func GetIncrementalCompanyListByOperationRecordMerge879() (items []*IncrementalList, err error) {
  860. o := orm.NewOrm()
  861. //查询真正的数据
  862. sql := `SELECT
  863. a.company_contract_id,
  864. a.contract_type,
  865. a.company_product_id,
  866. a.contract_code,
  867. a.pay_method,
  868. a.pay_channel,
  869. a.package_difference,
  870. a.company_id,
  871. a.start_date,
  872. a.end_date,
  873. a.money,
  874. b.company_name,
  875. c.seller_id,
  876. c.seller_name,
  877. a.product_id,
  878. a.product_name,
  879. a.create_time,
  880. b.region_type,
  881. c.renewal_reason,
  882. c.renewal_todo,
  883. c.STATUS
  884. FROM
  885. company_contract a
  886. RIGHT JOIN company b ON a.company_id = b.company_id
  887. JOIN company_product c ON b.company_id = c.company_id
  888. AND a.product_id = c.product_id
  889. WHERE
  890. 1 = 1
  891. AND c.product_id = 2
  892. AND a.STATUS = 1
  893. AND a.end_date >= '2017-09-05'
  894. AND a.end_date <= '2022-12-31'
  895. AND c.STATUS NOT IN ( "永续", "正式", "关闭" )
  896. AND a.company_contract_id NOT IN (SELECT company_contract_id FROM company_contract_no_renewed_ascribe )
  897. GROUP BY
  898. a.company_contract_id
  899. ORDER BY
  900. end_date DESC,
  901. company_id DESC limit 10
  902. `
  903. _, err = o.Raw(sql).QueryRows(&items)
  904. return
  905. }
  906. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  907. func GetIncrementalCompanyPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  908. o := orm.NewOrm()
  909. 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
  910. JOIN company b ON a.company_id = b.company_id
  911. JOIN company_product c ON a.company_id = c.company_id
  912. JOIN company_contract_permission d ON d.company_contract_id = a.company_contract_id
  913. and a.product_id=c.product_id WHERE 1 = 1 `
  914. if condition != "" {
  915. sql += condition
  916. }
  917. sql += ` group by d.company_contract_id,d.permission_name order by start_date desc,company_id desc limit ?,?`
  918. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  919. return
  920. }
  921. type CompanyContractPermissionNameGroupCountResp struct {
  922. Total int `description:"数量"`
  923. PermissionName string `description:"权限名"`
  924. }
  925. // GetCompanyContractPermissionNameGroupCount 获取增量客户产品报表列表统计数据(根据合同来展示)
  926. func GetCompanyContractPermissionNameGroupCount(condition string, pars []interface{}) (items []*CompanyContractPermissionNameGroupCountResp, err error) {
  927. o := orm.NewOrm()
  928. sql := `SELECT
  929. COUNT( DISTINCT d.permission_name, d.company_contract_id ) total,
  930. d.permission_name
  931. FROM
  932. company_contract a
  933. JOIN company b ON a.company_id = b.company_id
  934. JOIN company_product c ON a.company_id = c.company_id
  935. AND a.product_id = c.product_id
  936. JOIN company_contract_permission d ON d.company_contract_id = a.company_contract_id
  937. WHERE
  938. 1 = 1 `
  939. //AND d.permission_name IN ( '医药', '消费', '科技', '智造', '策略','研选订阅','研选扣点包' ) `
  940. if condition != "" {
  941. sql += condition
  942. }
  943. sql += `GROUP BY d.permission_name `
  944. _, err = o.Raw(sql, pars).QueryRows(&items)
  945. return
  946. }