statistic_report.go 50 KB

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