statistic_report.go 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  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. Id int `description:"合同id"`
  332. CompanyContractId int `description:"合同id"`
  333. ContractType string `description:"合同类型"`
  334. CompanyId int `description:"企业客户id"`
  335. CompanyName string `description:"企业客户名称"`
  336. ProductId int `description:"产品id"`
  337. ProductName string `description:"产品名称"`
  338. ProductStatus string `description:"产品名称"`
  339. CompanyProductId int `description:"客户购买产品授权id"`
  340. ContractCode string `description:"合同编码"`
  341. StartDate string `description:"合同开始日期"`
  342. EndDate string `description:"合同结束日期"`
  343. Money float64 `description:"合同金额"`
  344. PayMethod string `description:"付款方式"`
  345. PayChannel string `description:"付款渠道"`
  346. ImgUrl string `description:"合同图片"`
  347. CreateTime string `description:"合同创建时间"`
  348. ModifyTime string `description:"合同修改时间"`
  349. Status string `description:"合同审批状态,0:待审批,1:已审批;默认:1"`
  350. RegionType string `description:"企业客户所属区域;可选范围:国内,海外"`
  351. SellerId int `description:"归属销售id"`
  352. SellerName string `description:"归属销售名称"`
  353. SellerNameLast string `description:"合同到期之前最后所属归属销售名称"`
  354. ShareSeller string `description:"合同到期之前最后所属共享销售员"`
  355. ShareSellerLast string `description:"共享销售员"`
  356. ExpireDay string `description:"剩余可用天数"`
  357. PermissionList []*company.CompanyReportPermission `description:"产品权限"`
  358. Count int `json:"-" description:"合同数"`
  359. RenewalReason string `description:"未续约说明"`
  360. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  361. PackageDifference string `description:"和上一份合同的区别"`
  362. AscribeContent string `description:"归因标签说明"`
  363. IsShowNoRenewedNote bool `description:"是否展示未续约备注按钮"`
  364. Content string `description:"归因内容说明"`
  365. PermissionName string `description:"权限名"`
  366. PermissionNameExport string `description:"权限名导出时使用"`
  367. PermissionNameStatus string `description:"权限状态"`
  368. CompanyProductStatus string `description:"客户状态"`
  369. //CompanyContractIdGroup string `description:"表company_contract合并的 company_contract_id"`
  370. IsUserMaker int `description:"近四周之内是否包含决策人互动过 ,0否,1是"`
  371. SellerNameInit string `description:"权益初始化销售"`
  372. SellerIdInit int `description:"权益初始化销售ID"`
  373. SellerIdLast int `description:"合同到期之前最后所属销售ID"`
  374. ShareSellerInit string `description:"共享销售员"`
  375. Operation string `description:"共享销售员"`
  376. SysUserId int `description:"操作人用户id"`
  377. SysRealName string `description:"操作者名称"`
  378. }
  379. // GetIncrementalNewCompanyCount 获取增量客户报表列表统计数据(根据合同来展示)
  380. func GetIncrementalNewCompanyCount(condition string, pars []interface{}) (total int, err error) {
  381. o := orm.NewOrm()
  382. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  383. JOIN company b ON a.company_id = b.company_id
  384. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  385. if condition != "" {
  386. sql += condition
  387. }
  388. //sql += " order by a.start_date desc "
  389. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  390. err = o.Raw(sql, pars).QueryRow(&total)
  391. return
  392. }
  393. // GetIncrementalNewCompanyProductCount 获取增量客户产品报表列表统计数据(根据合同来展示)
  394. func GetIncrementalNewCompanyProductCount(condition string, pars []interface{}) (total int, err error) {
  395. o := orm.NewOrm()
  396. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  397. JOIN company b ON a.company_id = b.company_id
  398. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  399. if condition != "" {
  400. sql += condition
  401. }
  402. //sql += " order by a.start_date desc "
  403. sql = `select count(1) count from (` + sql + ` group by company_id,product_id) f`
  404. err = o.Raw(sql, pars).QueryRow(&total)
  405. return
  406. }
  407. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  408. func GetIncrementalNewCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  409. o := orm.NewOrm()
  410. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  411. JOIN company b ON a.company_id = b.company_id
  412. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  413. if condition != "" {
  414. sql += condition
  415. }
  416. sql += " order by a.start_date desc "
  417. sql = `select *,count(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  418. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  419. return
  420. }
  421. // GetIncrementalNewCompanyCountV2 获取增量客户报表列表统计数据(根据合同来展示)
  422. func GetIncrementalNewCompanyCountV2(condition string, pars []interface{}) (total int, err error) {
  423. o := orm.NewOrm()
  424. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  425. JOIN company b ON a.company_id = b.company_id
  426. 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 `
  427. if condition != "" {
  428. sql1 += condition
  429. }
  430. 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>?) `
  431. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  432. JOIN company b ON a.company_id = b.company_id
  433. 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 `
  434. if condition != "" {
  435. sql2 += condition
  436. }
  437. 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>?) `
  438. sql := sql1 + ` union (` + sql2 + `) `
  439. sql = `select * from (` + sql + `) f group by company_id`
  440. //sql += " order by a.start_date desc "
  441. sql = `select count(1) count from (` + sql + `) g`
  442. pars = append(pars, pars...)
  443. err = o.Raw(sql, pars).QueryRow(&total)
  444. return
  445. }
  446. // GetIncrementalNewCompanyProductCountV2 获取增量客户产品报表列表统计数据(根据合同来展示)
  447. func GetIncrementalNewCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  448. o := orm.NewOrm()
  449. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  450. JOIN company b ON a.company_id = b.company_id
  451. 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 `
  452. if condition != "" {
  453. sql1 += condition
  454. }
  455. 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 > ?) `
  456. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  457. JOIN company b ON a.company_id = b.company_id
  458. 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 `
  459. if condition != "" {
  460. sql2 += condition
  461. }
  462. 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 > ?) `
  463. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f group by company_id,product_id`
  464. //sql := sql1 + ` union (` + sql2 + `) `
  465. //sql += " order by a.start_date desc "
  466. sql = `select count(1) count from (` + sql + `) g`
  467. pars = append(pars, pars...)
  468. err = o.Raw(sql, pars).QueryRow(&total)
  469. return
  470. }
  471. // GetIncrementalNewCompanyListV2 获取增量客户报表列表数据(根据合同来展示)
  472. func GetIncrementalNewCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  473. o := orm.NewOrm()
  474. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  475. JOIN company b ON a.company_id = b.company_id
  476. 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 `
  477. if condition != "" {
  478. sql1 += condition
  479. }
  480. 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 > ?) `
  481. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  482. JOIN company b ON a.company_id = b.company_id
  483. 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 `
  484. if condition != "" {
  485. sql2 += condition
  486. }
  487. 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 > ?) `
  488. //sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  489. // JOIN company b ON a.company_id = b.company_id
  490. // JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  491. //
  492. //if condition != "" {
  493. // sql += condition
  494. //}
  495. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  496. sql += " "
  497. sql = `select *,count(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  498. pars = append(pars, pars...)
  499. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  500. return
  501. }
  502. // 获取今日存量客户报表列表统计数据(根据合同来展示)
  503. func GetTodayStackCompanyProductCount(condition string, pars []interface{}) (total int, err error) {
  504. o := orm.NewOrm()
  505. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  506. JOIN company b ON a.company_id = b.company_id
  507. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  508. if condition != "" {
  509. sql += condition
  510. }
  511. //sql += " order by a.start_date desc "
  512. sql = `select count(1) count from (` + sql + ` ) f`
  513. err = o.Raw(sql, pars).QueryRow(&total)
  514. return
  515. }
  516. // GetTodayStackCompanyList 获取当天存量客户报表列表数据(根据合同来展示)
  517. func GetTodayStackCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  518. o := orm.NewOrm()
  519. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  520. JOIN company b ON a.company_id = b.company_id
  521. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  522. if condition != "" {
  523. sql += condition
  524. }
  525. sql += " order by a.start_date desc "
  526. sql = `select * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  527. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  528. return
  529. }
  530. // GetTodayStackCompanyProductCountV2 获取今日存量客户报表列表统计数据(续约客户数,根据合同来展示)
  531. func GetTodayStackCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  532. o := orm.NewOrm()
  533. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  534. JOIN company b ON a.company_id = b.company_id
  535. 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 `
  536. if condition != "" {
  537. sql1 += condition
  538. }
  539. 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>?) `
  540. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  541. JOIN company b ON a.company_id = b.company_id
  542. 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 `
  543. if condition != "" {
  544. sql2 += condition
  545. }
  546. 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>?) `
  547. sql := sql1 + ` union (` + sql2 + `) `
  548. sql = `select * from (` + sql + `) f group by company_id`
  549. //sql += " order by a.start_date desc "
  550. sql = `select count(1) count from (` + sql + `) g`
  551. pars = append(pars, pars...)
  552. err = o.Raw(sql, pars).QueryRow(&total)
  553. return
  554. }
  555. // GetTodayStackCompanyListV2 获取当天存量客户报表列表数据(续约客户,根据合同来展示)
  556. func GetTodayStackCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  557. o := orm.NewOrm()
  558. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  559. JOIN company b ON a.company_id = b.company_id
  560. 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 `
  561. if condition != "" {
  562. sql1 += condition
  563. }
  564. 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 > ?) `
  565. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  566. JOIN company b ON a.company_id = b.company_id
  567. 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 `
  568. if condition != "" {
  569. sql2 += condition
  570. }
  571. 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 > ?) `
  572. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  573. sql = `select * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  574. pars = append(pars, pars...)
  575. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  576. return
  577. }
  578. // 获取其他增量客户报表列表数据(根据合同来展示)
  579. func GetOtherIncrementalNewCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  580. o := orm.NewOrm()
  581. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  582. JOIN company b ON a.company_id = b.company_id
  583. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1
  584. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  585. if condition != "" {
  586. sql += condition
  587. }
  588. sql += " order by a.start_date desc "
  589. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  590. _, err = o.Raw(sql, pars).QueryRows(&items)
  591. return
  592. }
  593. // 获取存量客户未续约报表列表统计数据(根据合同来展示)
  594. func GetIncrementalNotRenewalCompanyTotalCount(condition string, pars []interface{}) (total int, err error) {
  595. o := orm.NewOrm()
  596. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  597. JOIN company b ON a.company_id = b.company_id
  598. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  599. if condition != "" {
  600. sql += condition
  601. }
  602. //sql += " order by a.start_date desc "
  603. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  604. err = o.Raw(sql, pars).QueryRow(&total)
  605. return
  606. }
  607. // 获取存量客户未续约报表列表数据(根据合同来展示)
  608. func GetIncrementalNotRenewalCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  609. o := orm.NewOrm()
  610. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  611. JOIN company b ON a.company_id = b.company_id
  612. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  613. if condition != "" {
  614. sql += condition
  615. }
  616. sql += " order by a.start_date desc "
  617. sql = `select *,count(*) count from (` + sql + `) b group by company_id order by end_date asc,company_id desc limit ?,?`
  618. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  619. return
  620. }
  621. // 获取存量客户未续约报表列表数据(根据合同来展示)
  622. func GetOtherIncrementalNotRenewalCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  623. o := orm.NewOrm()
  624. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  625. JOIN company b ON a.company_id = b.company_id
  626. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1
  627. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  628. if condition != "" {
  629. sql += condition
  630. }
  631. sql += " order by a.start_date desc "
  632. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  633. _, err = o.Raw(sql, pars).QueryRows(&items)
  634. return
  635. }
  636. // 获取试用客户报表列表统计数据(根据新增客户时间来展示)
  637. func GetIncrementalCompanyCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  638. o := orm.NewOrm()
  639. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  640. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  641. RIGHT JOIN company b ON a.company_id = b.company_id
  642. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  643. if condition != "" {
  644. sql += condition
  645. }
  646. //sql += " order by a.start_date desc "
  647. sql = `select count(1) count from (` + sql + ` group by company_id ) f `
  648. err = o.Raw(sql, pars).QueryRow(&total)
  649. return
  650. }
  651. // 获取试用客户报表列表统计数据(根据新增客户时间来展示)
  652. func GetIncrementalCompanyCountByOperationRecordRai(condition string, pars []interface{}) (total int, err error) {
  653. o := orm.NewOrm()
  654. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  655. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  656. RIGHT JOIN company b ON a.company_id = b.company_id
  657. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  658. if condition != "" {
  659. sql += condition
  660. }
  661. //sql += " order by a.start_date desc "
  662. sql = `select count(1) count from (` + sql + ` group by a.id ) f `
  663. err = o.Raw(sql, pars).QueryRow(&total)
  664. return
  665. }
  666. // 获取试用客户报表列表统计数据(根据新增客户和对应的产品时间来展示)
  667. func GetIncrementalCompanyProductCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  668. o := orm.NewOrm()
  669. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  670. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  671. RIGHT JOIN company b ON a.company_id = b.company_id
  672. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  673. if condition != "" {
  674. sql += condition
  675. }
  676. //sql += " order by a.start_date desc "
  677. sql = `select count(1) count from (` + sql + ` group by company_id,product_id ) f `
  678. err = o.Raw(sql, pars).QueryRow(&total)
  679. return
  680. }
  681. // 获取试用客户报表列表数据(根据新增客户时间来展示)(2022年02月09日14:47:45废弃)
  682. func GetIncrementalCompanyListByOperationRecordOld(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  683. o := orm.NewOrm()
  684. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  685. a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  686. RIGHT JOIN company b ON a.company_id = b.company_id
  687. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  688. if condition != "" {
  689. sql += condition
  690. }
  691. //sql += " order by a.start_date desc "
  692. 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 ?,? `
  693. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  694. return
  695. }
  696. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  697. func GetIncrementalCompanyListByOperationRecord(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  698. o := orm.NewOrm()
  699. // sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  700. //a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  701. // RIGHT JOIN company b ON a.company_id = b.company_id
  702. // JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  703. //查询出最大id
  704. sql1 := `SELECT max(id) id FROM company_operation_record a
  705. RIGHT JOIN company b ON a.company_id = b.company_id
  706. JOIN company_product c ON b.company_id = c.company_id
  707. AND a.product_id = c.product_id
  708. WHERE 1 = 1 `
  709. if condition != "" {
  710. sql1 += condition
  711. }
  712. sql1 += ` GROUP BY a.company_id, a.product_id `
  713. //查询真正的数据
  714. 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
  715. RIGHT JOIN company b ON a.company_id = b.company_id
  716. JOIN company_product c ON b.company_id = c.company_id
  717. AND a.product_id = c.product_id
  718. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  719. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  720. return
  721. }
  722. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  723. func GetIncrementalCompanyListByOperationRecordRai(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  724. o := orm.NewOrm()
  725. //查询出最大id
  726. sql1 := `SELECT max(id) id FROM company_operation_record a
  727. RIGHT JOIN company b ON a.company_id = b.company_id
  728. JOIN company_product c ON b.company_id = c.company_id
  729. AND a.product_id = c.product_id
  730. WHERE 1 = 1 `
  731. if condition != "" {
  732. sql1 += condition
  733. }
  734. sql1 += ` GROUP BY a.id `
  735. //查询真正的数据
  736. 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
  737. RIGHT JOIN company b ON a.company_id = b.company_id
  738. JOIN company_product c ON b.company_id = c.company_id
  739. AND a.product_id = c.product_id
  740. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  741. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  742. return
  743. }
  744. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  745. func GetIncrementalCompanyListByOperationRecordIniut(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  746. o := orm.NewOrm()
  747. //查询真正的数据
  748. sql := `SELECT
  749. a.*,
  750. b.company_name
  751. FROM
  752. company_operation_record a
  753. RIGHT JOIN company b ON a.company_id = b.company_id
  754. JOIN company_product c ON b.company_id = c.company_id
  755. AND a.product_id = c.product_id
  756. WHERE
  757. 1 = 1
  758. AND c.product_id = 2
  759. AND a.create_time >= '2024-01-01'
  760. AND a.create_time < '2024-07-01'
  761. AND a.operation IN ( 'add', 'receive' )
  762. GROUP BY
  763. a.company_id, a.product_id`
  764. _, err = o.Raw(sql).QueryRows(&items)
  765. return
  766. }
  767. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  768. func GetIncrementalCompanyListByOperationRecordIniut3(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  769. o := orm.NewOrm()
  770. //查询真正的数据
  771. sql := `SELECT
  772. a.*,
  773. b.company_name
  774. FROM
  775. 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
  778. AND a.product_id = c.product_id
  779. WHERE
  780. 1 = 1
  781. AND c.product_id = 2
  782. AND a.create_time >= '2023-07-01'
  783. AND a.create_time <= '2023-09-30'
  784. AND a.operation IN ( 'add', 'receive' )
  785. GROUP BY
  786. a.company_id, a.product_id`
  787. _, err = o.Raw(sql).QueryRows(&items)
  788. return
  789. }
  790. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  791. func GetIncrementalCompanyListByOperationRecordIniut4(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  792. o := orm.NewOrm()
  793. //查询真正的数据
  794. sql := `SELECT
  795. a.*,
  796. b.company_name
  797. FROM
  798. company_operation_record a
  799. RIGHT JOIN company b ON a.company_id = b.company_id
  800. JOIN company_product c ON b.company_id = c.company_id
  801. AND a.product_id = c.product_id
  802. WHERE
  803. 1 = 1
  804. AND c.product_id = 2
  805. AND a.create_time >= '2023-10-01'
  806. AND a.create_time <= '2023-12-31'
  807. AND a.operation IN ( 'add', 'receive' )
  808. GROUP BY
  809. a.company_id, a.product_id`
  810. _, err = o.Raw(sql).QueryRows(&items)
  811. return
  812. }
  813. // GetIncrementalCompanyListByOperationRecordLast 获取试用客户报表列表数据(根据新增客户时间来展示)
  814. func GetIncrementalCompanyListByOperationRecordLast(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  815. o := orm.NewOrm()
  816. // sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  817. //a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  818. // RIGHT JOIN company b ON a.company_id = b.company_id
  819. // JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  820. //查询出最大id
  821. sql1 := `SELECT max(id) id FROM company_operation_record a
  822. RIGHT JOIN company b ON a.company_id = b.company_id
  823. JOIN company_product c ON b.company_id = c.company_id
  824. AND a.product_id = c.product_id
  825. WHERE 1 = 1 `
  826. if condition != "" {
  827. sql1 += condition
  828. }
  829. sql1 += ` GROUP BY a.company_id, a.product_id `
  830. //查询真正的数据
  831. 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
  832. RIGHT JOIN company b ON a.company_id = b.company_id
  833. JOIN company_product c ON b.company_id = c.company_id
  834. AND a.product_id = c.product_id
  835. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  836. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  837. return
  838. }
  839. // 根据操作记录获取其他增量客户报表列表数据(根据新增客户时间来展示)
  840. func GetOtherIncrementalCompanyListByOperationRecord(companyIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  841. o := orm.NewOrm()
  842. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  843. a.product_id,a.product_name,a.create_time,b.region_type FROM company_operation_record a
  844. RIGHT JOIN company b ON a.company_id = b.company_id
  845. 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 + `) `
  846. if condition != "" {
  847. sql += condition
  848. }
  849. //sql += " order by a.start_date desc "
  850. sql = `select * from (` + sql + ` order by create_time asc) f group by company_id`
  851. _, err = o.Raw(sql, pars).QueryRows(&items)
  852. return
  853. }
  854. // 获取当前客户报表列表统计数据(正式/试用)
  855. func GetCurrCompanyCount(condition string, pars []interface{}) (count int, err error) {
  856. o := orm.NewOrm()
  857. sql := `SELECT b.company_id from company a
  858. join company_product b on a.company_id=b.company_id WHERE 1 = 1 `
  859. if condition != "" {
  860. sql += condition
  861. }
  862. sql = `SELECT count(*) count from (` + sql + ` group by company_id) d`
  863. err = o.Raw(sql, pars).QueryRow(&count)
  864. return
  865. }
  866. // GetMoreRenewalReason 获取更多未续约说明列表
  867. func GetMoreRenewalReason(CompanyId, ProductId string) (items []*company.CompanyRenewalReason, err error) {
  868. o := orm.NewOrm()
  869. sql := "SELECT * FROM company_renewal_reason WHERE company_id=? AND product_id=? ORDER BY create_time DESC"
  870. _, err = o.Raw(sql, CompanyId, ProductId).QueryRows(&items)
  871. return
  872. }
  873. // GetMoreRenewalReason 获取更多未续约说明列表15.9.1
  874. func GetMoreRenewalReasoninit() (items []*company.CompanyRenewalReason, err error) {
  875. o := orm.NewOrm()
  876. sql := "SELECT * FROM company_renewal_reason WHERE product_id=2 ORDER BY create_time DESC"
  877. _, err = o.Raw(sql).QueryRows(&items)
  878. return
  879. }
  880. // GetLastContractMoney 获取上一份权益合同的金额
  881. func GetLastContractMoney(CompanyIds string) (items []*IncrementalList, err error) {
  882. o := orm.NewOrm()
  883. sql := `SELECT * FROM company_contract WHERE company_id IN (` + CompanyIds + `) AND product_id=2 AND status=1 ORDER BY create_time DESC `
  884. _, err = o.Raw(sql).QueryRows(&items)
  885. return
  886. }
  887. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  888. func GetIncrementalCompanyMergeList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  889. o := orm.NewOrm()
  890. 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
  891. JOIN company b ON a.company_id = b.company_id
  892. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  893. if condition != "" {
  894. sql += condition
  895. }
  896. sql += ` group by a.company_contract_id order by a.start_date desc,a.company_id desc limit ?,?`
  897. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  898. return
  899. }
  900. // 增量客户统计报表列表数据结构
  901. type CompanyContractGroupList struct {
  902. CompanyId int `description:"企业客户id"`
  903. CompanyCount int `description:"合同数"`
  904. }
  905. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  906. func GetCompanyContractGroupList(condition string, pars []interface{}) (items []*CompanyContractGroupList, err error) {
  907. o := orm.NewOrm()
  908. sql := `SELECT
  909. a.company_id,
  910. COUNT( a.company_id ) company_count
  911. FROM
  912. company_contract AS a
  913. INNER JOIN company_product AS b ON a.company_id = b.company_id
  914. WHERE b.product_id = 2 `
  915. if condition != "" {
  916. sql += condition
  917. }
  918. sql += ` GROUP BY a.company_id `
  919. _, err = o.Raw(sql, pars).QueryRows(&items)
  920. return
  921. }
  922. // GetIncrementalNewCompanyList 获取到期合同列表数据(根据合同来展示)
  923. func GetIncrementalCompanyMergeListEnd(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  924. o := orm.NewOrm()
  925. 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
  926. JOIN company b ON a.company_id = b.company_id
  927. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  928. if condition != "" {
  929. sql += condition
  930. }
  931. sql += ` group by a.company_contract_id order by a.end_date desc,a.company_id desc limit ?,?`
  932. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  933. return
  934. }
  935. // GetIncrementalCompanyListByOperationRecordMerge 未续约合同
  936. func GetIncrementalCompanyListByOperationRecordMerge(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  937. o := orm.NewOrm()
  938. //查询真正的数据
  939. 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
  940. RIGHT JOIN company b ON a.company_id = b.company_id
  941. JOIN company_product c ON b.company_id = c.company_id
  942. AND a.product_id = c.product_id where 1=1 `
  943. //where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  944. // _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  945. if condition != "" {
  946. sql += condition
  947. }
  948. sql += ` group by a.company_contract_id order by end_date desc,company_id desc limit ?,?`
  949. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  950. return
  951. }
  952. func GetIncrementalCompanyListByOperationRecordMerge879() (items []*IncrementalList, err error) {
  953. o := orm.NewOrm()
  954. //查询真正的数据
  955. sql := `SELECT
  956. a.company_contract_id,
  957. a.contract_type,
  958. a.company_product_id,
  959. a.contract_code,
  960. a.pay_method,
  961. a.pay_channel,
  962. a.package_difference,
  963. a.company_id,
  964. a.start_date,
  965. a.end_date,
  966. a.money,
  967. b.company_name,
  968. c.seller_id,
  969. c.seller_name,
  970. a.product_id,
  971. a.product_name,
  972. a.create_time,
  973. b.region_type,
  974. c.renewal_reason,
  975. c.renewal_todo,
  976. c.STATUS
  977. FROM
  978. company_contract a
  979. RIGHT JOIN company b ON a.company_id = b.company_id
  980. JOIN company_product c ON b.company_id = c.company_id
  981. AND a.product_id = c.product_id
  982. WHERE
  983. 1 = 1
  984. AND c.product_id = 2
  985. AND a.STATUS = 1
  986. AND a.end_date >= '2017-09-05'
  987. AND a.end_date <= '2022-12-31'
  988. AND c.STATUS NOT IN ( "永续", "正式", "关闭" )
  989. AND a.company_contract_id NOT IN (SELECT company_contract_id FROM company_contract_no_renewed_ascribe )
  990. GROUP BY
  991. a.company_contract_id
  992. ORDER BY
  993. end_date DESC,
  994. company_id DESC limit 10
  995. `
  996. _, err = o.Raw(sql).QueryRows(&items)
  997. return
  998. }
  999. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  1000. func GetIncrementalCompanyPermissionList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  1001. o := orm.NewOrm()
  1002. 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
  1003. JOIN company b ON a.company_id = b.company_id
  1004. JOIN company_product c ON a.company_id = c.company_id
  1005. JOIN company_contract_permission d ON d.company_contract_id = a.company_contract_id
  1006. and a.product_id=c.product_id WHERE 1 = 1 `
  1007. if condition != "" {
  1008. sql += condition
  1009. }
  1010. sql += ` group by d.company_contract_id,d.permission_name order by start_date desc,company_id desc limit ?,?`
  1011. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  1012. return
  1013. }
  1014. type CompanyContractPermissionNameGroupCountResp struct {
  1015. Total int `description:"数量"`
  1016. PermissionName string `description:"权限名"`
  1017. }
  1018. // GetCompanyContractPermissionNameGroupCount 获取增量客户产品报表列表统计数据(根据合同来展示)
  1019. func GetCompanyContractPermissionNameGroupCount(condition string, pars []interface{}) (items []*CompanyContractPermissionNameGroupCountResp, err error) {
  1020. o := orm.NewOrm()
  1021. sql := `SELECT
  1022. COUNT( DISTINCT d.permission_name, d.company_contract_id ) total,
  1023. d.permission_name
  1024. FROM
  1025. company_contract a
  1026. JOIN company b ON a.company_id = b.company_id
  1027. JOIN company_product c ON a.company_id = c.company_id
  1028. AND a.product_id = c.product_id
  1029. JOIN company_contract_permission d ON d.company_contract_id = a.company_contract_id
  1030. WHERE
  1031. 1 = 1 `
  1032. //AND d.permission_name IN ( '医药', '消费', '科技', '智造', '策略','研选订阅','研选扣点包' ) `
  1033. if condition != "" {
  1034. sql += condition
  1035. }
  1036. sql += `GROUP BY d.permission_name `
  1037. _, err = o.Raw(sql, pars).QueryRows(&items)
  1038. return
  1039. }