statistic_report.go 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  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. CompanyProductId int `description:"客户购买产品授权id"`
  338. ContractCode string `description:"合同编码"`
  339. StartDate string `description:"合同开始日期"`
  340. EndDate string `description:"合同结束日期"`
  341. Money float64 `description:"合同金额"`
  342. PayMethod string `description:"付款方式"`
  343. PayChannel string `description:"付款渠道"`
  344. ImgUrl string `description:"合同图片"`
  345. CreateTime string `description:"合同创建时间"`
  346. ModifyTime string `description:"合同修改时间"`
  347. Status string `description:"合同审批状态,0:待审批,1:已审批;默认:1"`
  348. RegionType string `description:"企业客户所属区域;可选范围:国内,海外"`
  349. SellerId int `description:"归属销售id"`
  350. SellerName string `description:"归属销售名称"`
  351. ExpireDay string `description:"剩余可用天数"`
  352. PermissionList []*company.CompanyReportPermission `description:"产品权限"`
  353. Count int `json:"-" description:"合同数"`
  354. RenewalReason string `description:"未续约说明"`
  355. RenewalTodo string `description:"未续约说明中的待办事项说明"`
  356. PackageDifference string `description:"和上一份合同的区别"`
  357. AscribeContent string `description:"归因标签说明"`
  358. IsShowNoRenewedNote bool `description:"是否展示未续约备注按钮"`
  359. Content string `description:"归因内容说明"`
  360. PermissionName string `description:"权限名"`
  361. //CompanyContractIdGroup string `description:"表company_contract合并的 company_contract_id"`
  362. }
  363. // GetIncrementalNewCompanyCount 获取增量客户报表列表统计数据(根据合同来展示)
  364. func GetIncrementalNewCompanyCount(condition string, pars []interface{}) (total int, err error) {
  365. o := orm.NewOrm()
  366. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  367. JOIN company b ON a.company_id = b.company_id
  368. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  369. if condition != "" {
  370. sql += condition
  371. }
  372. //sql += " order by a.start_date desc "
  373. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  374. err = o.Raw(sql, pars).QueryRow(&total)
  375. return
  376. }
  377. // GetIncrementalNewCompanyProductCount 获取增量客户产品报表列表统计数据(根据合同来展示)
  378. func GetIncrementalNewCompanyProductCount(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,product_id) f`
  388. err = o.Raw(sql, pars).QueryRow(&total)
  389. return
  390. }
  391. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  392. func GetIncrementalNewCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  393. o := orm.NewOrm()
  394. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason 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(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  402. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  403. return
  404. }
  405. // GetIncrementalNewCompanyCountV2 获取增量客户报表列表统计数据(根据合同来展示)
  406. func GetIncrementalNewCompanyCountV2(condition string, pars []interface{}) (total int, err error) {
  407. o := orm.NewOrm()
  408. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name 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 and a.product_id=1 `
  411. if condition != "" {
  412. sql1 += condition
  413. }
  414. 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>?) `
  415. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  416. JOIN company b ON a.company_id = b.company_id
  417. 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 `
  418. if condition != "" {
  419. sql2 += condition
  420. }
  421. 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>?) `
  422. sql := sql1 + ` union (` + sql2 + `) `
  423. sql = `select * from (` + sql + `) f group by company_id`
  424. //sql += " order by a.start_date desc "
  425. sql = `select count(1) count from (` + sql + `) g`
  426. pars = append(pars, pars...)
  427. err = o.Raw(sql, pars).QueryRow(&total)
  428. return
  429. }
  430. // GetIncrementalNewCompanyProductCountV2 获取增量客户产品报表列表统计数据(根据合同来展示)
  431. func GetIncrementalNewCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  432. o := orm.NewOrm()
  433. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  434. JOIN company b ON a.company_id = b.company_id
  435. 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 `
  436. if condition != "" {
  437. sql1 += condition
  438. }
  439. 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 > ?) `
  440. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  441. JOIN company b ON a.company_id = b.company_id
  442. 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 `
  443. if condition != "" {
  444. sql2 += condition
  445. }
  446. 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 > ?) `
  447. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f group by company_id,product_id`
  448. //sql := sql1 + ` union (` + sql2 + `) `
  449. //sql += " order by a.start_date desc "
  450. sql = `select count(1) count from (` + sql + `) g`
  451. pars = append(pars, pars...)
  452. err = o.Raw(sql, pars).QueryRow(&total)
  453. return
  454. }
  455. // GetIncrementalNewCompanyListV2 获取增量客户报表列表数据(根据合同来展示)
  456. func GetIncrementalNewCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  457. o := orm.NewOrm()
  458. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  459. JOIN company b ON a.company_id = b.company_id
  460. 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 `
  461. if condition != "" {
  462. sql1 += condition
  463. }
  464. 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 > ?) `
  465. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  466. JOIN company b ON a.company_id = b.company_id
  467. 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 `
  468. if condition != "" {
  469. sql2 += condition
  470. }
  471. 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 > ?) `
  472. //sql := `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 `
  475. //
  476. //if condition != "" {
  477. // sql += condition
  478. //}
  479. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  480. sql += " "
  481. sql = `select *,count(*) count from (` + sql + `) b group by company_id,product_id order by start_date desc,company_id desc limit ?,?`
  482. pars = append(pars, pars...)
  483. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  484. return
  485. }
  486. // 获取今日存量客户报表列表统计数据(根据合同来展示)
  487. func GetTodayStackCompanyProductCount(condition string, pars []interface{}) (total int, err error) {
  488. o := orm.NewOrm()
  489. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  490. JOIN company b ON a.company_id = b.company_id
  491. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  492. if condition != "" {
  493. sql += condition
  494. }
  495. //sql += " order by a.start_date desc "
  496. sql = `select count(1) count from (` + sql + ` ) f`
  497. err = o.Raw(sql, pars).QueryRow(&total)
  498. return
  499. }
  500. // GetTodayStackCompanyList 获取当天存量客户报表列表数据(根据合同来展示)
  501. func GetTodayStackCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  502. o := orm.NewOrm()
  503. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason 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 * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  511. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  512. return
  513. }
  514. // GetTodayStackCompanyProductCountV2 获取今日存量客户报表列表统计数据(续约客户数,根据合同来展示)
  515. func GetTodayStackCompanyProductCountV2(condition string, pars []interface{}) (total int, err error) {
  516. o := orm.NewOrm()
  517. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name 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 and a.product_id=1 `
  520. if condition != "" {
  521. sql1 += condition
  522. }
  523. 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>?) `
  524. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  525. JOIN company b ON a.company_id = b.company_id
  526. 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 `
  527. if condition != "" {
  528. sql2 += condition
  529. }
  530. 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>?) `
  531. sql := sql1 + ` union (` + sql2 + `) `
  532. sql = `select * from (` + sql + `) f group by company_id`
  533. //sql += " order by a.start_date desc "
  534. sql = `select count(1) count from (` + sql + `) g`
  535. pars = append(pars, pars...)
  536. err = o.Raw(sql, pars).QueryRow(&total)
  537. return
  538. }
  539. // GetTodayStackCompanyListV2 获取当天存量客户报表列表数据(续约客户,根据合同来展示)
  540. func GetTodayStackCompanyListV2(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  541. o := orm.NewOrm()
  542. sql1 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  543. JOIN company b ON a.company_id = b.company_id
  544. 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 `
  545. if condition != "" {
  546. sql1 += condition
  547. }
  548. 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 > ?) `
  549. sql2 := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  550. JOIN company b ON a.company_id = b.company_id
  551. 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 `
  552. if condition != "" {
  553. sql2 += condition
  554. }
  555. 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 > ?) `
  556. sql := `select * from (` + sql1 + ` union (` + sql2 + `) ` + `) f order by start_date desc`
  557. sql = `select * from (` + sql + `) b order by start_date desc,company_id desc limit ?,?`
  558. pars = append(pars, pars...)
  559. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  560. return
  561. }
  562. // 获取其他增量客户报表列表数据(根据合同来展示)
  563. func GetOtherIncrementalNewCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  564. o := orm.NewOrm()
  565. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name 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
  568. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  569. if condition != "" {
  570. sql += condition
  571. }
  572. sql += " order by a.start_date desc "
  573. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  574. _, err = o.Raw(sql, pars).QueryRows(&items)
  575. return
  576. }
  577. // 获取存量客户未续约报表列表统计数据(根据合同来展示)
  578. func GetIncrementalNotRenewalCompanyTotalCount(condition string, pars []interface{}) (total int, err error) {
  579. o := orm.NewOrm()
  580. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name FROM company_contract a
  581. JOIN company b ON a.company_id = b.company_id
  582. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE a.status = 1 `
  583. if condition != "" {
  584. sql += condition
  585. }
  586. //sql += " order by a.start_date desc "
  587. sql = `select count(1) count from (` + sql + ` group by company_id) f`
  588. err = o.Raw(sql, pars).QueryRow(&total)
  589. return
  590. }
  591. // 获取存量客户未续约报表列表数据(根据合同来展示)
  592. func GetIncrementalNotRenewalCompanyList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, 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(*) count from (` + sql + `) b group by company_id order by end_date asc,company_id desc limit ?,?`
  602. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  603. return
  604. }
  605. // 获取存量客户未续约报表列表数据(根据合同来展示)
  606. func GetOtherIncrementalNotRenewalCompanyList(companyIds, companyContractIds, condition string, pars []interface{}) (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. AND a.company_id in (` + companyIds + `) AND company_contract_id not in (` + companyContractIds + `) `
  612. if condition != "" {
  613. sql += condition
  614. }
  615. sql += " order by a.start_date desc "
  616. sql = `select *,count(*) count from (` + sql + `) b group by company_id `
  617. _, err = o.Raw(sql, pars).QueryRows(&items)
  618. return
  619. }
  620. // 获取试用客户报表列表统计数据(根据新增客户时间来展示)
  621. func GetIncrementalCompanyCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  622. o := orm.NewOrm()
  623. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  624. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  625. RIGHT JOIN company b ON a.company_id = b.company_id
  626. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  627. if condition != "" {
  628. sql += condition
  629. }
  630. //sql += " order by a.start_date desc "
  631. sql = `select count(1) count from (` + sql + ` group by company_id ) f `
  632. err = o.Raw(sql, pars).QueryRow(&total)
  633. return
  634. }
  635. // 获取试用客户报表列表统计数据(根据新增客户和对应的产品时间来展示)
  636. func GetIncrementalCompanyProductCountByOperationRecord(condition string, pars []interface{}) (total int, err error) {
  637. o := orm.NewOrm()
  638. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  639. a.product_id,a.product_name,b.region_type,c.renewal_reason FROM company_operation_record a
  640. RIGHT JOIN company b ON a.company_id = b.company_id
  641. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  642. if condition != "" {
  643. sql += condition
  644. }
  645. //sql += " order by a.start_date desc "
  646. sql = `select count(1) count from (` + sql + ` group by company_id,product_id ) f `
  647. err = o.Raw(sql, pars).QueryRow(&total)
  648. return
  649. }
  650. // 获取试用客户报表列表数据(根据新增客户时间来展示)(2022年02月09日14:47:45废弃)
  651. func GetIncrementalCompanyListByOperationRecordOld(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  652. o := orm.NewOrm()
  653. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  654. a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  655. RIGHT JOIN company b ON a.company_id = b.company_id
  656. JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  657. if condition != "" {
  658. sql += condition
  659. }
  660. //sql += " order by a.start_date desc "
  661. 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 ?,? `
  662. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  663. return
  664. }
  665. // GetIncrementalCompanyListByOperationRecord 获取试用客户报表列表数据(根据新增客户时间来展示)
  666. func GetIncrementalCompanyListByOperationRecord(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  667. o := orm.NewOrm()
  668. // sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  669. //a.product_id,a.product_name,a.create_time,b.region_type,c.renewal_reason,c.status FROM company_operation_record a
  670. // RIGHT JOIN company b ON a.company_id = b.company_id
  671. // JOIN company_product c ON b.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  672. //查询出最大id
  673. sql1 := `SELECT max(id) id FROM company_operation_record a
  674. RIGHT JOIN company b ON a.company_id = b.company_id
  675. JOIN company_product c ON b.company_id = c.company_id
  676. AND a.product_id = c.product_id
  677. WHERE 1 = 1 `
  678. if condition != "" {
  679. sql1 += condition
  680. }
  681. sql1 += ` GROUP BY a.company_id, a.product_id `
  682. //查询真正的数据
  683. sql := `SELECT a.id, a.company_id, b.company_name, c.seller_id, c.seller_name, 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
  684. RIGHT JOIN company b ON a.company_id = b.company_id
  685. JOIN company_product c ON b.company_id = c.company_id
  686. AND a.product_id = c.product_id
  687. where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  688. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  689. return
  690. }
  691. // 根据操作记录获取其他增量客户报表列表数据(根据新增客户时间来展示)
  692. func GetOtherIncrementalCompanyListByOperationRecord(companyIds, condition string, pars []interface{}) (items []*IncrementalList, err error) {
  693. o := orm.NewOrm()
  694. sql := `SELECT a.id,a.company_id,b.company_name,c.seller_id,c.seller_name,
  695. a.product_id,a.product_name,a.create_time,b.region_type FROM company_operation_record a
  696. RIGHT JOIN company b ON a.company_id = b.company_id
  697. 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 + `) `
  698. if condition != "" {
  699. sql += condition
  700. }
  701. //sql += " order by a.start_date desc "
  702. sql = `select * from (` + sql + ` order by create_time asc) f group by company_id`
  703. _, err = o.Raw(sql, pars).QueryRows(&items)
  704. return
  705. }
  706. // 获取当前客户报表列表统计数据(正式/试用)
  707. func GetCurrCompanyCount(condition string, pars []interface{}) (count int, err error) {
  708. o := orm.NewOrm()
  709. sql := `SELECT b.company_id from company a
  710. join company_product b on a.company_id=b.company_id WHERE 1 = 1 `
  711. if condition != "" {
  712. sql += condition
  713. }
  714. sql = `SELECT count(*) count from (` + sql + ` group by company_id) d`
  715. err = o.Raw(sql, pars).QueryRow(&count)
  716. return
  717. }
  718. // GetMoreRenewalReason 获取更多未续约说明列表
  719. func GetMoreRenewalReason(CompanyId, ProductId string) (items []*company.CompanyRenewalReason, err error) {
  720. o := orm.NewOrm()
  721. sql := "SELECT * FROM company_renewal_reason WHERE company_id=? AND product_id=? ORDER BY create_time DESC"
  722. _, err = o.Raw(sql, CompanyId, ProductId).QueryRows(&items)
  723. return
  724. }
  725. // GetLastContractMoney 获取上一份权益合同的金额
  726. func GetLastContractMoney(CompanyIds string) (items []*IncrementalList, err error) {
  727. o := orm.NewOrm()
  728. sql := `SELECT * FROM company_contract WHERE company_id IN (` + CompanyIds + `) AND product_id=2 AND status=1 ORDER BY create_time DESC `
  729. _, err = o.Raw(sql).QueryRows(&items)
  730. return
  731. }
  732. // GetIncrementalNewCompanyList 获取增量客户报表列表数据(根据合同来展示)
  733. func GetIncrementalCompanyMergeList(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  734. o := orm.NewOrm()
  735. sql := `SELECT a.*,b.region_type,c.seller_id,c.seller_name,b.company_name,c.renewal_reason FROM company_contract a
  736. JOIN company b ON a.company_id = b.company_id
  737. JOIN company_product c ON a.company_id = c.company_id and a.product_id=c.product_id WHERE 1 = 1 `
  738. if condition != "" {
  739. sql += condition
  740. }
  741. sql += ` group by a.company_contract_id order by start_date desc,company_id desc limit ?,?`
  742. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  743. return
  744. }
  745. // GetIncrementalCompanyListByOperationRecordMerge 未续约合同
  746. func GetIncrementalCompanyListByOperationRecordMerge(condition string, pars []interface{}, startSize, pageSize int) (items []*IncrementalList, err error) {
  747. o := orm.NewOrm()
  748. ////查询出最大id
  749. //sql1 := `SELECT max(id) id FROM company_operation_record a
  750. // RIGHT JOIN company b ON a.company_id = b.company_id
  751. // JOIN company_product c ON b.company_id = c.company_id
  752. // AND a.product_id = c.product_id
  753. //WHERE 1 = 1 `
  754. //if condition != "" {
  755. // sql1 += condition
  756. //}
  757. //sql1 += ` GROUP BY a.company_id, a.product_id `
  758. //查询真正的数据
  759. 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, c.seller_id, c.seller_name, a.product_id, a.product_name, a.create_time, b.region_type, c.renewal_reason, c.renewal_todo, c.status FROM company_contract a
  760. RIGHT JOIN company b ON a.company_id = b.company_id
  761. JOIN company_product c ON b.company_id = c.company_id
  762. AND a.product_id = c.product_id where 1=1 `
  763. //where a.id in (` + sql1 + `) order by create_time DESC,company_id DESC limit ?,?`
  764. // _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  765. if condition != "" {
  766. sql += condition
  767. }
  768. sql += ` group by a.company_contract_id order by start_date desc,company_id desc limit ?,?`
  769. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  770. return
  771. }