company_approval.go 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667
  1. package company
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "github.com/rdlucklib/rdluck_tools/paging"
  5. "hongze/hz_crm_api/models/contract"
  6. "hongze/hz_crm_api/models/system"
  7. "hongze/hz_crm_api/services/alarm_msg"
  8. "hongze/hz_crm_api/utils"
  9. "strconv"
  10. "strings"
  11. "time"
  12. )
  13. type CompanyApproval struct {
  14. CompanyApprovalId int `orm:"column(company_approval_id);pk"`
  15. CompanyId int
  16. ProductId int
  17. ApproveStatus string `description:"'待审批','已审批','驳回','已撤回'"`
  18. ApproveUserId int `description:"审批人"`
  19. ApproveRoleTypeCode string `description:"审批人角色类型"`
  20. ApproveRealName string `description:"审批人姓名"`
  21. ApproveTime time.Time `description:"审批时间"`
  22. ApproveContent string `description:"待审内容"`
  23. ApplyMethod int `description:"申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议"`
  24. ApplyReasons string `description:"申请原因"`
  25. ApplyUserId int `description:"申请人"`
  26. ApplyRealName string `description:"申请人姓名"`
  27. CreateTime time.Time `description:"申请时间"`
  28. ModifyTime time.Time `description:"修改时间"`
  29. CompanyContractId int `description:"合同id"`
  30. IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
  31. }
  32. // AddCompanyApproval 新增客户产品
  33. func AddCompanyApproval(item *CompanyApproval) (lastId int, err error) {
  34. o := orm.NewOrm()
  35. to, err := o.Begin()
  36. if err != nil {
  37. return
  38. }
  39. defer func() {
  40. if err != nil {
  41. tmpErr := to.Rollback()
  42. if tmpErr != nil {
  43. go alarm_msg.SendAlarmMsg("AddCompanyApproval 事务回滚失败,Err:"+tmpErr.Error(), 3)
  44. }
  45. } else {
  46. err = to.Commit()
  47. }
  48. }()
  49. var newId int64
  50. newId, err = to.Insert(item)
  51. if err != nil {
  52. return
  53. }
  54. lastId = int(newId)
  55. sql := `UPDATE company_product SET approve_status=? WHERE company_id=? AND product_id=? `
  56. _, err = to.Raw(sql, item.ApproveStatus, item.CompanyId, item.ProductId).Exec()
  57. return
  58. }
  59. func GetCompanyApprovalCountById(companyId, productId int) (count int, err error) {
  60. sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND approve_status='待审批' `
  61. o := orm.NewOrm()
  62. err = o.Raw(sql, companyId, productId).QueryRow(&count)
  63. return
  64. }
  65. func GetCompanyApprovalById(companyId, productId int) (item *CompanyApproval, err error) {
  66. sql := `SELECT * FROM company_approval WHERE company_id=? AND product_id=? AND approve_status='待审批' order by company_approval_id desc LIMIT 1 `
  67. o := orm.NewOrm()
  68. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  69. return
  70. }
  71. func GetCompanyApprovalByApprovalId(companyApprovalId int) (item *CompanyApproval, err error) {
  72. sql := `SELECT * FROM company_approval WHERE company_approval_id=? LIMIT 1 `
  73. o := orm.NewOrm()
  74. err = o.Raw(sql, companyApprovalId).QueryRow(&item)
  75. return
  76. }
  77. type CompanyApprovalList struct {
  78. CompanyId int
  79. ProductId int
  80. CompanyName string `description:"客户名称"`
  81. CreditCode string `description:"社会统一信用码"`
  82. CompanyType string `description:"客户类型"`
  83. IndustryName string `description:"行业名称"`
  84. SellerName string `description:"销售名称"`
  85. CreateTime string `description:"创建时间"`
  86. ApprovalTime string `description:"提交申请时间"`
  87. ApproveTime string `description:"审批时间"`
  88. Status string `description:"客户状态"`
  89. ApproveContent string `description:"待审内容"`
  90. ApproveStatus string `description:"当前状态"`
  91. ApplyMethod int `description:"申请类型:1:试用->正式,2:冻结—>试用,3:流失—>正式,4:试用延期,5:原销售申请领取流失客户,6:正式客户申请服务更新"`
  92. ApplyReasons string `description:"申请理由"`
  93. FreezeStartDate string `description:"冻结开始日期"`
  94. FreezeEndDate string `description:"冻结结束日期"`
  95. FreezeExpireDays int `description:"冻结到期天数"`
  96. Address string `description:"地址"`
  97. Source string `description:"客户来源"`
  98. ApproveRemark string `description:"驳回理由"`
  99. StartDate string `description:"开始日期"`
  100. EndDate string `description:"结束日期"`
  101. ExpireDay int `description:"服务到期天数"`
  102. CompanyApprovalId int `description:"审批记录ID"`
  103. ApprovalCount int `description:"已延期审批次数"`
  104. CompanyContractId int `description:"合同id"`
  105. DelayPermission string `description:"延期申请品种"`
  106. ApplyRealName string `description:"申请人姓名"`
  107. OpButton ApprovalButton `description:"操作权限"`
  108. ApplyContractType int `description:"申请合同类型 0 非标 1 标准"`
  109. ApplyUserId int `description:"申请人编号id" json:"-"`
  110. }
  111. // ApprovalButton 审批操作权限
  112. type ApprovalButton struct {
  113. Cancel bool `description:"是否具有撤回权限,true才允许审核操作"`
  114. Approval bool `description:"是否具有审核操作权限,true才允许审核操作"`
  115. //Cancel bool `description:"是否具有撤回权限"`
  116. }
  117. type CompanyApprovalListResp struct {
  118. Paging *paging.PagingItem
  119. List []*CompanyApprovalList
  120. }
  121. func GetApprovalList(condition string, pars []interface{}, startSize, pageSize int) (items []*CompanyApprovalList, err error) {
  122. o := orm.NewOrm()
  123. sql := `SELECT a.company_id,b.product_id,a.company_name,a.credit_code,a.address,b.company_type,b.industry_name,
  124. b.seller_name,b.create_time,b.freeze_start_date,b.freeze_end_date,b.source,c.create_time AS approval_time,c.approve_time,b.status,
  125. c.approve_content,c.approve_status,c.apply_method,c.apply_reasons,c.approve_remark,b.start_date,b.end_date,c.company_approval_id,c.company_contract_id,
  126. c.apply_real_name,c.apply_user_id
  127. FROM company AS a
  128. INNER JOIN company_product AS b ON a.company_id=b.company_id
  129. INNER JOIN company_approval AS c ON b.company_id=c.company_id AND b.product_id=c.product_id
  130. WHERE 1=1 `
  131. if condition != "" {
  132. sql += condition
  133. }
  134. sql += ` ORDER BY c.modify_time DESC LIMIT ?,? `
  135. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  136. return
  137. }
  138. func GetApprovalCount(condition string, pars []interface{}) (count int, err error) {
  139. o := orm.NewOrm()
  140. sql := `SELECT COUNT(1) AS count
  141. FROM company AS a
  142. INNER JOIN company_product AS b ON a.company_id=b.company_id
  143. INNER JOIN company_approval AS c ON b.company_id=c.company_id AND b.product_id=c.product_id
  144. WHERE 1=1 `
  145. if condition != "" {
  146. sql += condition
  147. }
  148. sql += ` ORDER BY c.modify_time DESC `
  149. err = o.Raw(sql, pars).QueryRow(&count)
  150. return
  151. }
  152. // TryOutToFormal 试用转正式
  153. func TryOutToFormal(companyId, productId, sellerId, companyApprovalId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (newCompanyReportPermissionList []*CompanyReportPermission, err error) {
  154. o := orm.NewOrm()
  155. to, err := o.Begin()
  156. if err != nil {
  157. return
  158. }
  159. defer func() {
  160. if err != nil {
  161. tmpErr := to.Rollback()
  162. if tmpErr != nil {
  163. go alarm_msg.SendAlarmMsg("TryOutToFormal 事务回滚失败,Err:"+tmpErr.Error(), 3)
  164. }
  165. } else {
  166. err = to.Commit()
  167. }
  168. }()
  169. // 套餐类型为0时, 不更新套餐类型
  170. sql := `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,renewal_intention=0,is_suspend=0,is_formal=1,approve_status='已审批',start_date=?,end_date=?,package_type=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  171. if packageType == 0 {
  172. sql = `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,renewal_intention=0,is_suspend=0,is_formal=1,approve_status='已审批',start_date=?,end_date=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  173. _, err = to.Raw(sql, startDate, endDate, raiPackageType, companyId, productId).Exec()
  174. if err != nil {
  175. return
  176. }
  177. } else {
  178. _, err = to.Raw(sql, startDate, endDate, packageType, raiPackageType, companyId, productId).Exec()
  179. if err != nil {
  180. return
  181. }
  182. }
  183. sql = `UPDATE company SET type=1,last_updated_time=NOW() WHERE company_id=? `
  184. _, err = to.Raw(sql, companyId).Exec()
  185. if err != nil {
  186. return
  187. }
  188. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  189. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  190. if err != nil {
  191. return
  192. }
  193. items := make([]*CompanyReportPermission, 0)
  194. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  195. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  196. for _, pv := range items {
  197. cpLog := new(CompanyPermissionLog)
  198. cpLog.CompanyId = companyId
  199. cpLog.ChartPermissionId = pv.ChartPermissionId
  200. cpLog.CreateTime = time.Now()
  201. cpLog.SysUserId = sellerId
  202. cpLog.SysUserName = sellerName
  203. cpLog.StartDate = pv.StartDate
  204. cpLog.EndDate = pv.EndDate
  205. cpLog.ProductId = productId
  206. cpLog.ProductName = pv.ProductName
  207. go AddCompanyPermissionLog(cpLog)
  208. }
  209. sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?`
  210. _, err = to.Raw(sql, companyId, productId).Exec()
  211. if err != nil {
  212. return
  213. }
  214. contractPermission := make([]*CompanyReportPermission, 0)
  215. sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
  216. _, err = to.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
  217. if err != nil {
  218. return
  219. }
  220. for _, pv := range contractPermission {
  221. tmpCompanyReportPermission := &CompanyReportPermission{
  222. //CompanyReportPermissionId: 0,
  223. CompanyId: companyId,
  224. ReportPermissionId: pv.ChartPermissionId,
  225. CreatedTime: time.Now(),
  226. LastUpdatedTime: time.Now(),
  227. ChartPermissionId: pv.ChartPermissionId,
  228. StartDate: pv.StartDate,
  229. EndDate: pv.EndDate,
  230. ProductId: productId,
  231. ProductName: productName,
  232. CompanyContractId: companyContractId,
  233. Status: "正式",
  234. ModifyTime: time.Now(),
  235. IsUpgrade: pv.IsUpgrade,
  236. ExpensiveYx: pv.ExpensiveYx,
  237. }
  238. ////CRM13.2 升级套餐
  239. //if tmpCompanyReportPermission.ChartPermissionId > 10000 {
  240. // tmpCompanyReportPermission.ChartPermissionId -= 10000
  241. // tmpCompanyReportPermission.ReportPermissionId -= 10000
  242. // tmpCompanyReportPermission.IsUpgrade = 1
  243. //}
  244. newId, tmpErr := to.Insert(tmpCompanyReportPermission)
  245. if tmpErr != nil {
  246. err = tmpErr
  247. return
  248. }
  249. tmpCompanyReportPermission.CompanyReportPermissionId = newId
  250. newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
  251. //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  252. // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status,is_upgrade)
  253. // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?,?) `
  254. //_, err = to.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式", pv.IsUpgrade).Exec()
  255. //if err != nil {
  256. // return
  257. //}
  258. }
  259. sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
  260. _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
  261. if err != nil {
  262. return
  263. }
  264. return
  265. }
  266. // TryOutToFormalXClassRai 权益客户试用转正式
  267. func TryOutToFormalXClassRai(companyId, productId, sellerId, companyApprovalId, companyContractId int, startDate, endDate, sellerName, productName string, raiPackageType int) (newCompanyReportPermissionList []*CompanyReportPermission, err error) {
  268. o := orm.NewOrm()
  269. to, err := o.Begin()
  270. if err != nil {
  271. return
  272. }
  273. defer func() {
  274. if err != nil {
  275. tmpErr := to.Rollback()
  276. if tmpErr != nil {
  277. go alarm_msg.SendAlarmMsg("TryOutToFormalXClassRai 事务回滚失败,Err:"+tmpErr.Error(), 3)
  278. }
  279. } else {
  280. err = to.Commit()
  281. }
  282. }()
  283. //123月份申请转正通过时,将试用期限延长(更新)至4.30。456月份申请转正通过时,将试用期限延长至7.31。789月份申请转正通过时,将试用期限延长至10.31。101112月份申请转正通过时,将试用期限延长至次年1.31。
  284. endDate = utils.GetLastDayOfQuarter(time.Now()).Format(utils.FormatDate)
  285. // 套餐类型为0时, 不更新套餐类型,权益与FICC开始与结束时间一起修改
  286. sql := `UPDATE company_product SET status='永续',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,renewal_intention=0,is_suspend=0,is_formal=1,approve_status='已审批',end_date=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? `
  287. _, err = to.Raw(sql, endDate, raiPackageType, companyId).Exec()
  288. if err != nil {
  289. return
  290. }
  291. sql = `UPDATE company SET type=1,last_updated_time=NOW() WHERE company_id=? `
  292. _, err = to.Raw(sql, companyId).Exec()
  293. if err != nil {
  294. return
  295. }
  296. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  297. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  298. if err != nil {
  299. return
  300. }
  301. items := make([]*CompanyReportPermission, 0)
  302. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  303. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  304. for _, pv := range items {
  305. cpLog := new(CompanyPermissionLog)
  306. cpLog.CompanyId = companyId
  307. cpLog.ChartPermissionId = pv.ChartPermissionId
  308. cpLog.CreateTime = time.Now()
  309. cpLog.SysUserId = sellerId
  310. cpLog.SysUserName = sellerName
  311. cpLog.StartDate = pv.StartDate
  312. cpLog.EndDate = pv.EndDate
  313. cpLog.ProductId = productId
  314. cpLog.ProductName = pv.ProductName
  315. go AddCompanyPermissionLog(cpLog)
  316. }
  317. sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?`
  318. _, err = to.Raw(sql, companyId, productId).Exec()
  319. if err != nil {
  320. return
  321. }
  322. contractPermission := make([]*CompanyReportPermission, 0)
  323. sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
  324. _, err = to.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
  325. if err != nil {
  326. return
  327. }
  328. for _, pv := range contractPermission {
  329. tmpCompanyReportPermission := &CompanyReportPermission{
  330. //CompanyReportPermissionId: 0,
  331. CompanyId: companyId,
  332. ReportPermissionId: pv.ChartPermissionId,
  333. CreatedTime: time.Now(),
  334. LastUpdatedTime: time.Now(),
  335. ChartPermissionId: pv.ChartPermissionId,
  336. StartDate: pv.StartDate,
  337. EndDate: endDate,
  338. ProductId: productId,
  339. ProductName: productName,
  340. CompanyContractId: companyContractId,
  341. Status: "永续",
  342. ModifyTime: time.Now(),
  343. IsUpgrade: pv.IsUpgrade,
  344. ExpensiveYx: pv.ExpensiveYx,
  345. }
  346. newId, tmpErr := to.Insert(tmpCompanyReportPermission)
  347. if tmpErr != nil {
  348. err = tmpErr
  349. return
  350. }
  351. tmpCompanyReportPermission.CompanyReportPermissionId = newId
  352. newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
  353. }
  354. sql = `UPDATE company_report_permission SET status='永续', end_date=? WHERE company_id=? ` // 更改权限的开始时间结束时间
  355. _, err = to.Raw(sql, endDate, companyId).Exec()
  356. if err != nil {
  357. return
  358. }
  359. sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
  360. _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
  361. if err != nil {
  362. return
  363. }
  364. return
  365. }
  366. // FreezeToTryOut 冻结转试用
  367. func FreezeToTryOut(companyId, productId, sellerId, companyApprovalId, applyUserId int, sellerName, productName string) (newCompanyReportPermissionList []*CompanyReportPermission, startDate, endDate string, err error) {
  368. o := orm.NewOrm()
  369. to, err := o.Begin()
  370. if err != nil {
  371. return
  372. }
  373. defer func() {
  374. if err != nil {
  375. tmpErr := to.Rollback()
  376. if tmpErr != nil {
  377. go alarm_msg.SendAlarmMsg("FreezeToTryOut 事务回滚失败,Err:"+tmpErr.Error(), 3)
  378. }
  379. } else {
  380. err = to.Commit()
  381. }
  382. }()
  383. startDate = time.Now().Format(utils.FormatDate)
  384. endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  385. //sellerItem, err := system.GetSysAdminById(applyUserId)
  386. //if err != nil {
  387. // return
  388. //}
  389. //更新用户产品状态
  390. //6.9 fix 未续约强制弹窗要求客户冻结转试用后不弹床,取消清空冻结时间以标记曾经被冻结过
  391. sql := `UPDATE company_product SET status='试用',is_suspend=0,approve_status='已审批',freeze_start_date=null,freeze_end_date=null,
  392. start_date=?,end_date=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  393. _, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
  394. if err != nil {
  395. return
  396. }
  397. //更新用户状态
  398. sql = `UPDATE company SET type=2,last_updated_time=NOW() WHERE company_id=? `
  399. _, err = to.Raw(sql, companyId).Exec()
  400. if err != nil {
  401. return
  402. }
  403. //更新审批单
  404. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  405. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  406. if err != nil {
  407. return
  408. }
  409. //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
  410. items := make([]*CompanyReportPermission, 0)
  411. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  412. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  413. for _, pv := range items {
  414. cpLog := new(CompanyPermissionLog)
  415. cpLog.CompanyId = companyId
  416. cpLog.ChartPermissionId = pv.ChartPermissionId
  417. cpLog.CreateTime = time.Now()
  418. cpLog.SysUserId = sellerId
  419. cpLog.SysUserName = sellerName
  420. cpLog.StartDate = pv.StartDate
  421. cpLog.EndDate = pv.EndDate
  422. cpLog.ProductId = productId
  423. cpLog.ProductName = pv.ProductName
  424. go AddCompanyPermissionLog(cpLog)
  425. }
  426. //更新产品(2021年03月02日14:58:50注释,调整为从申请权限表中获取数据并修改)
  427. //sql = `UPDATE company_report_permission SET start_date=?,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
  428. //_, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
  429. //if err != nil {
  430. // return
  431. //}
  432. //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
  433. count, _ := GetCompanyReportPermissionCount(companyId, productId)
  434. if count > 0 {
  435. sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  436. _, err = to.Raw(sql, companyId, productId).Exec()
  437. if err != nil {
  438. return
  439. }
  440. }
  441. //获取需要试用的权限
  442. delayPermissionItems, tmpErr := GetDelayPermissionItems(companyId, companyApprovalId)
  443. if tmpErr != nil {
  444. err = tmpErr
  445. return
  446. }
  447. status := `试用`
  448. for _, v := range delayPermissionItems {
  449. tmpCompanyReportPermission := &CompanyReportPermission{
  450. //CompanyReportPermissionId: 0,
  451. CompanyId: companyId,
  452. ReportPermissionId: v.ChartPermissionId,
  453. CreatedTime: time.Now(),
  454. LastUpdatedTime: time.Now(),
  455. ChartPermissionId: v.ChartPermissionId,
  456. StartDate: v.StartDate,
  457. EndDate: v.EndDate,
  458. ProductId: productId,
  459. ProductName: productName,
  460. //CompanyContractId: companyContractId,
  461. Status: status,
  462. ModifyTime: time.Now(),
  463. //IsUpgrade: pv.IsUpgrade,
  464. }
  465. newId, tmpErr := to.Insert(tmpCompanyReportPermission)
  466. if tmpErr != nil {
  467. err = tmpErr
  468. return
  469. }
  470. tmpCompanyReportPermission.CompanyReportPermissionId = newId
  471. newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
  472. //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  473. // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,status)
  474. // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?) `
  475. //_, err = to.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, v.EndDate, productId, productName, status).Exec()
  476. //if err != nil {
  477. // return
  478. //}
  479. }
  480. return
  481. }
  482. // FreezeToTryOutXClassRai 权益冻结转X类试用
  483. func FreezeToTryOutXClassRai(companyId, productId, sellerId, companyApprovalId, applyUserId int, sellerName, productName string) (newCompanyReportPermissionList []*CompanyReportPermission, startDate, endDate string, err error) {
  484. o := orm.NewOrm()
  485. to, err := o.Begin()
  486. if err != nil {
  487. return
  488. }
  489. defer func() {
  490. if err != nil {
  491. tmpErr := to.Rollback()
  492. if tmpErr != nil {
  493. go alarm_msg.SendAlarmMsg("FreezeToTryOutXClassRai 事务回滚失败,Err:"+tmpErr.Error(), 3)
  494. }
  495. } else {
  496. err = to.Commit()
  497. }
  498. }()
  499. startDate = time.Now().Format(utils.FormatDate)
  500. endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  501. //sellerItem, err := system.GetSysAdminById(applyUserId)
  502. //if err != nil {
  503. // return
  504. //}
  505. //更新用户产品状态
  506. sql := `UPDATE company_product SET status='永续',is_suspend=0,approve_status='已审批',freeze_start_date=null,freeze_end_date=null,
  507. start_date=?,end_date=?,modify_time=NOW(),try_stage=1 WHERE company_id=? `
  508. _, err = to.Raw(sql, startDate, endDate, companyId).Exec()
  509. if err != nil {
  510. return
  511. }
  512. //更新用户状态
  513. sql = `UPDATE company SET type=5,last_updated_time=NOW() WHERE company_id=? `
  514. _, err = to.Raw(sql, companyId).Exec()
  515. if err != nil {
  516. return
  517. }
  518. //更新审批单
  519. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  520. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  521. if err != nil {
  522. return
  523. }
  524. //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
  525. items := make([]*CompanyReportPermission, 0)
  526. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  527. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  528. for _, pv := range items {
  529. cpLog := new(CompanyPermissionLog)
  530. cpLog.CompanyId = companyId
  531. cpLog.ChartPermissionId = pv.ChartPermissionId
  532. cpLog.CreateTime = time.Now()
  533. cpLog.SysUserId = sellerId
  534. cpLog.SysUserName = sellerName
  535. cpLog.StartDate = pv.StartDate
  536. cpLog.EndDate = pv.EndDate
  537. cpLog.ProductId = productId
  538. cpLog.ProductName = pv.ProductName
  539. go AddCompanyPermissionLog(cpLog)
  540. }
  541. //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
  542. count, _ := GetCompanyReportPermissionCount(companyId, productId)
  543. if count > 0 {
  544. sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  545. _, err = to.Raw(sql, companyId, productId).Exec()
  546. if err != nil {
  547. return
  548. }
  549. }
  550. sql = `UPDATE company_report_permission SET status='永续',start_date=?,end_date=? WHERE company_id=? ` // 更改权限的开始时间结束时间
  551. _, err = to.Raw(sql, startDate, endDate, companyId).Exec()
  552. if err != nil {
  553. return
  554. }
  555. //获取需要试用的权限
  556. delayPermissionItems, tmpErr := GetDelayPermissionItems(companyId, companyApprovalId)
  557. if tmpErr != nil {
  558. err = tmpErr
  559. return
  560. }
  561. status := `永续`
  562. for _, v := range delayPermissionItems {
  563. tmpCompanyReportPermission := &CompanyReportPermission{
  564. //CompanyReportPermissionId: 0,
  565. CompanyId: companyId,
  566. ReportPermissionId: v.ChartPermissionId,
  567. CreatedTime: time.Now(),
  568. LastUpdatedTime: time.Now(),
  569. ChartPermissionId: v.ChartPermissionId,
  570. StartDate: v.StartDate,
  571. EndDate: endDate,
  572. ProductId: productId,
  573. ProductName: productName,
  574. //CompanyContractId: companyContractId,
  575. Status: status,
  576. ModifyTime: time.Now(),
  577. //IsUpgrade: pv.IsUpgrade,
  578. }
  579. newId, tmpErr := to.Insert(tmpCompanyReportPermission)
  580. if tmpErr != nil {
  581. err = tmpErr
  582. return
  583. }
  584. tmpCompanyReportPermission.CompanyReportPermissionId = newId
  585. newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
  586. }
  587. return
  588. }
  589. // TryOutDelay 试用延期
  590. func TryOutDelay(companyId, productId, sellerId, companyApprovalId int, sellerName, endDate, productName string) (newCompanyReportPermissionList []*CompanyReportPermission, newEndDate string, err error) {
  591. o := orm.NewOrm()
  592. to, err := o.Begin()
  593. if err != nil {
  594. return
  595. }
  596. defer func() {
  597. if err != nil {
  598. tmpErr := to.Rollback()
  599. if tmpErr != nil {
  600. go alarm_msg.SendAlarmMsg("TryOutDelay 事务回滚失败,Err:"+tmpErr.Error(), 3)
  601. }
  602. } else {
  603. err = to.Commit()
  604. }
  605. }()
  606. endDateTime, err := time.Parse(utils.FormatDate, endDate)
  607. if err != nil {
  608. return
  609. }
  610. newEndDate = endDateTime.AddDate(0, 2, 0).Format(utils.FormatDate)
  611. //更新用户产品状态
  612. sql := `UPDATE company_product SET approve_status='已审批',is_suspend=0,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
  613. _, err = to.Raw(sql, newEndDate, companyId, productId).Exec()
  614. if err != nil {
  615. return
  616. }
  617. //更新审批单
  618. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  619. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  620. if err != nil {
  621. return
  622. }
  623. //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
  624. items := make([]*CompanyReportPermission, 0)
  625. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  626. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  627. for _, pv := range items {
  628. cpLog := new(CompanyPermissionLog)
  629. cpLog.CompanyId = companyId
  630. cpLog.ChartPermissionId = pv.ChartPermissionId
  631. cpLog.CreateTime = time.Now()
  632. cpLog.SysUserId = sellerId
  633. cpLog.SysUserName = sellerName
  634. cpLog.StartDate = pv.StartDate
  635. cpLog.EndDate = pv.EndDate
  636. cpLog.ProductId = productId
  637. cpLog.ProductName = pv.ProductName
  638. go AddCompanyPermissionLog(cpLog)
  639. }
  640. //获取需要延期的权限
  641. delayPermissionItems, err := GetDelayPermissionItems(companyId, companyApprovalId)
  642. if err != nil {
  643. return
  644. }
  645. status := `试用`
  646. for _, v := range delayPermissionItems {
  647. tmpCompanyReportPermission, tmpErr := GetCompanyPermission(companyId, v.ChartPermissionId)
  648. if tmpErr != nil {
  649. //没有数据的话
  650. if tmpErr.Error() == utils.ErrNoRow() {
  651. tmpCompanyReportPermission = &CompanyReportPermission{
  652. //CompanyReportPermissionId: 0,
  653. CompanyId: companyId,
  654. ReportPermissionId: v.ChartPermissionId,
  655. CreatedTime: time.Now(),
  656. LastUpdatedTime: time.Now(),
  657. ChartPermissionId: v.ChartPermissionId,
  658. StartDate: v.StartDate,
  659. EndDate: newEndDate,
  660. ProductId: productId,
  661. ProductName: productName,
  662. //CompanyContractId: companyContractId,
  663. Status: status,
  664. ModifyTime: time.Now(),
  665. //IsUpgrade: pv.IsUpgrade,
  666. }
  667. newId, tmpErr := to.Insert(tmpCompanyReportPermission)
  668. if tmpErr != nil {
  669. err = tmpErr
  670. return
  671. }
  672. tmpCompanyReportPermission.CompanyReportPermissionId = newId
  673. } else {
  674. // 其他异常的话,那么就返回报错
  675. err = tmpErr
  676. return
  677. }
  678. } else {
  679. // 修改原数据
  680. tmpCompanyReportPermission.EndDate = newEndDate
  681. tmpCompanyReportPermission.Status = status
  682. tmpCompanyReportPermission.ModifyTime = time.Now()
  683. _, err = to.Update(tmpCompanyReportPermission, "EndDate", "Status", "ModifyTime")
  684. if err != nil {
  685. return
  686. }
  687. }
  688. newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
  689. //isExist, tmpErr := GetCompanyPermissionCheck(companyId, v.ChartPermissionId)
  690. //if tmpErr != nil{
  691. // err = tmpErr
  692. // return
  693. //}
  694. //if isExist > 0 {
  695. // sql = `UPDATE company_report_permission SET end_date=?,status=?,modify_time=NOW() WHERE company_id=? AND product_id=? AND chart_permission_id=?`
  696. // _, err = to.Raw(sql, newEndDate, status, companyId, productId, v.ChartPermissionId).Exec()
  697. // if err != nil {
  698. // return
  699. // }
  700. //} else {
  701. // sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  702. // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,status)
  703. // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?) `
  704. // _, err = to.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, newEndDate, productId, productName, status).Exec()
  705. // if err != nil {
  706. // return
  707. // }
  708. //}
  709. }
  710. return
  711. }
  712. // 原销售申请领取
  713. func ApplyReceive(companyId, productId, sysUserId, companyApprovalId int, sellerItem *system.Admin, sysUserName, productName, sellerName string, sellerId int) (newCompanyReportPermissionList []*CompanyReportPermission, startDate, endDate string, err error) {
  714. o := orm.NewOrm()
  715. to, err := o.Begin()
  716. if err != nil {
  717. return
  718. }
  719. defer func() {
  720. if err != nil {
  721. tmpErr := to.Rollback()
  722. if tmpErr != nil {
  723. go alarm_msg.SendAlarmMsg("ApplyReceive 事务回滚失败,Err:"+tmpErr.Error(), 3)
  724. }
  725. } else {
  726. err = to.Commit()
  727. }
  728. }()
  729. startDate = time.Now().Format(utils.FormatDate)
  730. endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
  731. //更新用户产品状态
  732. sql := `UPDATE company_product SET status='试用',try_out_time=NULL,last_description_time=NULL,is_suspend=0,approve_status='已审批', start_date=?,end_date=?,seller_id=?,seller_name=?,group_id=?,department_id=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  733. _, err = to.Raw(sql, startDate, endDate, sellerItem.AdminId, sellerItem.RealName, sellerItem.GroupId, sellerItem.DepartmentId, companyId, productId).Exec()
  734. if err != nil {
  735. return
  736. }
  737. //更新用户状态
  738. sql = `UPDATE company SET type=2,last_updated_time=NOW() WHERE company_id=? `
  739. _, err = to.Raw(sql, companyId).Exec()
  740. if err != nil {
  741. return
  742. }
  743. //更新审批单
  744. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  745. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  746. if err != nil {
  747. return
  748. }
  749. //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
  750. items := make([]*CompanyReportPermission, 0)
  751. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  752. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  753. for _, pv := range items {
  754. cpLog := new(CompanyPermissionLog)
  755. cpLog.CompanyId = companyId
  756. cpLog.ChartPermissionId = pv.ChartPermissionId
  757. cpLog.CreateTime = time.Now()
  758. cpLog.SysUserId = sysUserId
  759. cpLog.SysUserName = sysUserName
  760. cpLog.StartDate = pv.StartDate
  761. cpLog.EndDate = pv.EndDate
  762. cpLog.ProductId = productId
  763. cpLog.ProductName = pv.ProductName
  764. go AddCompanyPermissionLog(cpLog)
  765. }
  766. //更新产品(2021年03月02日14:58:50注释,调整为从申请权限表中获取数据并修改)
  767. //sql = `UPDATE company_report_permission SET start_date=?,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
  768. //_, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
  769. //if err != nil {
  770. // return
  771. //}
  772. //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
  773. count, _ := GetCompanyReportPermissionCount(companyId, productId)
  774. if count > 0 {
  775. sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  776. _, err = to.Raw(sql, companyId, productId).Exec()
  777. if err != nil {
  778. return
  779. }
  780. }
  781. //修改 该联系人 与 当前销售的关系
  782. sql = `UPDATE user_seller_relation SET seller_id=?,seller = ? ,modify_time=NOW() WHERE company_id = ? AND product_id=?`
  783. _, err = to.Raw(sql, sellerId, sellerName, companyId, productId).Exec()
  784. //获取需要试用的权限
  785. delayPermissionItems, err := GetDelayPermissionItems(companyId, companyApprovalId)
  786. if err != nil {
  787. return
  788. }
  789. status := `试用`
  790. for _, v := range delayPermissionItems {
  791. tmpCompanyReportPermission := &CompanyReportPermission{
  792. //CompanyReportPermissionId: 0,
  793. CompanyId: companyId,
  794. ReportPermissionId: v.ChartPermissionId,
  795. CreatedTime: time.Now(),
  796. LastUpdatedTime: time.Now(),
  797. ChartPermissionId: v.ChartPermissionId,
  798. StartDate: v.StartDate,
  799. EndDate: v.EndDate,
  800. ProductId: productId,
  801. ProductName: productName,
  802. //CompanyContractId: companyContractId,
  803. Status: status,
  804. ModifyTime: time.Now(),
  805. //IsUpgrade: pv.IsUpgrade,
  806. }
  807. newId, tmpErr := to.Insert(tmpCompanyReportPermission)
  808. if tmpErr != nil {
  809. err = tmpErr
  810. return
  811. }
  812. tmpCompanyReportPermission.CompanyReportPermissionId = newId
  813. newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
  814. //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  815. // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,status)
  816. // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?) `
  817. //_, err = to.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, v.EndDate, productId, productName, status).Exec()
  818. //if err != nil {
  819. // return err
  820. //}
  821. }
  822. return
  823. }
  824. // ApplyServiceUpdate 申请服务更新
  825. func ApplyServiceUpdate(companyId, productId, sellerId, companyApprovalId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (newCompanyReportPermissionList []*CompanyReportPermission, err error) {
  826. o := orm.NewOrm()
  827. to, err := o.Begin()
  828. if err != nil {
  829. return
  830. }
  831. defer func() {
  832. if err != nil {
  833. tmpErr := to.Rollback()
  834. if tmpErr != nil {
  835. go alarm_msg.SendAlarmMsg("ApplyServiceUpdate 事务回滚失败,Err:"+tmpErr.Error(), 3)
  836. }
  837. } else {
  838. err = to.Commit()
  839. }
  840. }()
  841. items := make([]*CompanyReportPermission, 0)
  842. sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  843. _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
  844. if err != nil {
  845. return
  846. }
  847. //已存在的权限map
  848. nowCompanyReportPermissionMap := make(map[int]*CompanyReportPermission)
  849. //产品服务的开始、结束日期(非产品权限)
  850. updateStartDate := startDate
  851. updateStartDateTime, err := time.Parse(utils.FormatDate, updateStartDate)
  852. if err != nil {
  853. return
  854. }
  855. updateEndDate := endDate
  856. updateEndDateTime, err := time.Parse(utils.FormatDate, updateEndDate)
  857. if err != nil {
  858. return
  859. }
  860. for _, pv := range items {
  861. cpLog := new(CompanyPermissionLog)
  862. cpLog.CompanyId = companyId
  863. cpLog.ChartPermissionId = pv.ChartPermissionId
  864. cpLog.CreateTime = time.Now()
  865. cpLog.SysUserId = sellerId
  866. cpLog.SysUserName = sellerName
  867. cpLog.StartDate = pv.StartDate
  868. cpLog.EndDate = pv.EndDate
  869. cpLog.ProductId = productId
  870. cpLog.ProductName = pv.ProductName
  871. go AddCompanyPermissionLog(cpLog)
  872. //将权限插入到已存在的权限map中
  873. nowCompanyReportPermissionMap[pv.ChartPermissionId] = pv
  874. //校验原始数据中的开始日期是否小于合同内的开始日期,如果小于,那么变更为原先的合同开始日期
  875. tmpStartDate, tmpErr := time.Parse(utils.FormatDate, pv.StartDate)
  876. if tmpErr != nil {
  877. err = tmpErr
  878. return
  879. }
  880. if tmpStartDate.Before(updateStartDateTime) {
  881. updateStartDateTime = tmpStartDate
  882. }
  883. //校验原始数据中的结束日期是否大于合同内的结束日期,如果大于,那么变更为原先的合同结束日期
  884. tmpEndDate, tmpErr := time.Parse(utils.FormatDate, pv.EndDate)
  885. if tmpErr != nil {
  886. err = tmpErr
  887. return
  888. }
  889. if tmpEndDate.After(updateEndDateTime) {
  890. updateEndDateTime = tmpEndDate
  891. }
  892. }
  893. //删除所有权限
  894. //sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  895. //_, err = to.Raw(sql, companyId, productId).Exec()
  896. //if err != nil {
  897. // return
  898. //}
  899. //更新客户产品信息
  900. if packageType == 0 {
  901. sql = `UPDATE company_product SET status='正式',is_formal=1,is_suspend=0,approve_status='已审批',start_date=?,end_date=?,rai_package_type=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  902. _, err = to.Raw(sql, updateStartDateTime, updateEndDateTime, raiPackageType, companyId, productId).Exec()
  903. if err != nil {
  904. return
  905. }
  906. } else {
  907. sql = `UPDATE company_product SET status='正式',is_formal=1,is_suspend=0,approve_status='已审批',start_date=?,end_date=?,package_type=?,rai_package_type=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  908. _, err = to.Raw(sql, updateStartDateTime, updateEndDateTime, packageType, raiPackageType, companyId, productId).Exec()
  909. if err != nil {
  910. return
  911. }
  912. }
  913. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  914. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  915. if err != nil {
  916. return
  917. }
  918. contractPermission := make([]*CompanyReportPermission, 0)
  919. sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
  920. _, err = to.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
  921. if err != nil {
  922. return
  923. }
  924. for _, pv := range contractPermission {
  925. nowPermission, ok := nowCompanyReportPermissionMap[pv.ChartPermissionId]
  926. //判断是否已经存在该权限,如果存在的话,那么是修改,否则是新增
  927. if ok {
  928. needUpdate := false
  929. if nowPermission.Status != "正式" && nowPermission.Status != "永续" {
  930. needUpdate = true
  931. }
  932. //如果 需要更新 字段 为false,那么再去校验时间
  933. if needUpdate == false {
  934. //如果当前存该权限,那么去校验是否需要修改
  935. nowPermissionEndDateTime, tmpErr := time.Parse(utils.FormatDate, nowPermission.EndDate)
  936. if tmpErr != nil {
  937. err = tmpErr
  938. return
  939. }
  940. contractPermissionEndDateTime, tmpErr := time.Parse(utils.FormatDate, pv.EndDate)
  941. if tmpErr != nil {
  942. err = tmpErr
  943. return
  944. }
  945. if contractPermissionEndDateTime.After(nowPermissionEndDateTime) {
  946. needUpdate = true
  947. }
  948. }
  949. //如果 需要更新 字段 为 true,那么就去更新
  950. if needUpdate {
  951. nowPermission.EndDate = pv.EndDate
  952. nowPermission.LastUpdatedTime = time.Now()
  953. nowPermission.ModifyTime = time.Now()
  954. nowPermission.CompanyContractId = pv.CompanyContractId
  955. nowPermission.Status = "正式"
  956. nowPermission.IsUpgrade = pv.IsUpgrade
  957. nowPermission.ExpensiveYx = pv.ExpensiveYx
  958. _, err = to.Update(nowPermission, "EndDate", "LastUpdatedTime", "ModifyTime", "CompanyContractId", "Status", "IsUpgrade", "ExpensiveYx")
  959. if err != nil {
  960. return
  961. }
  962. //sql = `update company_report_permission set end_date=?,last_updated_time=now(),modify_time=now(),company_contract_id=?,status=? where company_report_permission_id=? `
  963. //_, tmpErr := to.Raw(sql, pv.EndDate, pv.CompanyContractId, "正式", nowPermission.CompanyReportPermissionId).Exec()
  964. //err = tmpErr
  965. //if err != nil {
  966. // return
  967. //}
  968. }
  969. } else {
  970. nowPermission = &CompanyReportPermission{
  971. //CompanyReportPermissionId: 0,
  972. CompanyId: companyId,
  973. ReportPermissionId: pv.ChartPermissionId,
  974. CreatedTime: time.Now(),
  975. LastUpdatedTime: time.Now(),
  976. ChartPermissionId: pv.ChartPermissionId,
  977. StartDate: pv.StartDate,
  978. EndDate: pv.EndDate,
  979. ProductId: productId,
  980. ProductName: productName,
  981. //CompanyContractId: companyContractId,
  982. Status: "正式",
  983. ModifyTime: time.Now(),
  984. IsUpgrade: pv.IsUpgrade,
  985. ExpensiveYx: pv.ExpensiveYx,
  986. }
  987. newId, tmpErr := to.Insert(nowPermission)
  988. if tmpErr != nil {
  989. err = tmpErr
  990. return
  991. }
  992. nowPermission.CompanyReportPermissionId = newId
  993. //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  994. // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status)
  995. //VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?) `
  996. //_, tmpErr := to.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式").Exec()
  997. //err = tmpErr
  998. //if err != nil {
  999. // return
  1000. //}
  1001. }
  1002. newCompanyReportPermissionList = append(newCompanyReportPermissionList, nowPermission)
  1003. }
  1004. sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
  1005. _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
  1006. if err != nil {
  1007. return
  1008. }
  1009. return
  1010. }
  1011. // ApplyApproveContract 更新审批单、合同状态
  1012. func ApplyApproveContract(companyId, productId, companyApprovalId, companyContractId int) (err error) {
  1013. o := orm.NewOrm()
  1014. to, err := o.Begin()
  1015. if err != nil {
  1016. return
  1017. }
  1018. defer func() {
  1019. if err != nil {
  1020. tmpErr := to.Rollback()
  1021. if tmpErr != nil {
  1022. go alarm_msg.SendAlarmMsg("ApplyApproveContract 事务回滚失败,Err:"+tmpErr.Error(), 3)
  1023. }
  1024. } else {
  1025. err = to.Commit()
  1026. }
  1027. }()
  1028. sql := `UPDATE company_product SET approve_status='已审批',renewal_intention=0,modify_time=NOW() WHERE company_id=? AND product_id=? `
  1029. _, err = to.Raw(sql, companyId, productId).Exec()
  1030. if err != nil {
  1031. return
  1032. }
  1033. //更新审批单
  1034. sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
  1035. _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
  1036. if err != nil {
  1037. return
  1038. }
  1039. //更新合同
  1040. sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
  1041. _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
  1042. if err != nil {
  1043. return
  1044. }
  1045. return
  1046. }
  1047. func GetCompanyApprovalDetailById(companyId, productId int) (item *CompanyApproval, err error) {
  1048. sql := `SELECT * FROM company_approval WHERE company_id=? AND product_id=? AND approve_status IN('驳回','已撤回') ORDER BY modify_time DESC LIMIT 1 `
  1049. o := orm.NewOrm()
  1050. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  1051. return
  1052. }
  1053. type CompanyApplyDeleteReq struct {
  1054. CompanyId int `description:"客户id"`
  1055. CompanyApprovalId int `description:"审批记录ID"`
  1056. }
  1057. // ApplyDelete 申请服务更新
  1058. func ApplyDelete(companyId, companyApprovalId, productId, companyContractId int) (err error) {
  1059. o := orm.NewOrm()
  1060. to, err := o.Begin()
  1061. if err != nil {
  1062. return
  1063. }
  1064. defer func() {
  1065. if err != nil {
  1066. tmpErr := to.Rollback()
  1067. if tmpErr != nil {
  1068. go alarm_msg.SendAlarmMsg("ApplyDelete 事务回滚失败,Err:"+tmpErr.Error(), 3)
  1069. }
  1070. } else {
  1071. err = to.Commit()
  1072. }
  1073. }()
  1074. sql := `DELETE FROM company_approval WHERE company_approval_id=? `
  1075. _, err = to.Raw(sql, companyApprovalId).Exec()
  1076. if err != nil {
  1077. return
  1078. }
  1079. sql = `DELETE FROM company_contract WHERE company_contract_id=? `
  1080. _, err = to.Raw(sql, companyContractId).Exec()
  1081. if err != nil {
  1082. return
  1083. }
  1084. sql = `UPDATE company_product SET approve_status='' WHERE company_id=? AND product_id=? `
  1085. _, err = to.Raw(sql, companyId, productId).Exec()
  1086. if err != nil {
  1087. return
  1088. }
  1089. //作废消息
  1090. sql = `UPDATE company_approval_message SET message_status=2 WHERE source_type=1 AND company_approval_id=? `
  1091. _, err = to.Raw(sql, companyApprovalId).Exec()
  1092. if err != nil {
  1093. return
  1094. }
  1095. return
  1096. }
  1097. // ModifyCompanyApproval 修改申请信息
  1098. func ModifyCompanyApproval(companyId, productId, companyApprovalId int, approveContent, applyReasons string) (err error) {
  1099. o := orm.NewOrm()
  1100. to, err := o.Begin()
  1101. if err != nil {
  1102. return
  1103. }
  1104. defer func() {
  1105. if err != nil {
  1106. utils.FileLog.Info("试用延期申请:Err %s", err.Error())
  1107. tmpErr := to.Rollback()
  1108. if tmpErr != nil {
  1109. go alarm_msg.SendAlarmMsg("ModifyCompanyApproval 事务回滚失败,Err:"+tmpErr.Error(), 3)
  1110. }
  1111. } else {
  1112. err = to.Commit()
  1113. }
  1114. }()
  1115. sql := `UPDATE company_approval SET approve_status = '待审批', approve_content=?,apply_reasons=?,modify_time=NOW() WHERE company_approval_id = ? `
  1116. _, err = to.Raw(sql, approveContent, applyReasons, companyApprovalId).Exec()
  1117. if err != nil {
  1118. return
  1119. }
  1120. sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
  1121. _, err = to.Raw(sql, companyId, productId).Exec()
  1122. utils.FileLog.Info("试用延期申请:Err %s,%s,%d", sql, companyId, productId)
  1123. return
  1124. }
  1125. // ModifyCompanyTurnPositive 修改申请信息
  1126. func ModifyCompanyTurnPositive(item *CompanyApplyTurnPositiveReq, productId, companyContractId, isFormal int, mapPermissionIdUpgrade map[int]int, expensiveYx int) (err error) {
  1127. o := orm.NewOrm()
  1128. to, err := o.Begin()
  1129. if err != nil {
  1130. return
  1131. }
  1132. defer func() {
  1133. if err != nil {
  1134. tmpErr := to.Rollback()
  1135. if tmpErr != nil {
  1136. go alarm_msg.SendAlarmMsg("ModifyCompanyTurnPositive 事务回滚失败,Err:"+tmpErr.Error(), 3)
  1137. }
  1138. } else {
  1139. err = to.Commit()
  1140. }
  1141. }()
  1142. sql := `UPDATE company_contract
  1143. SET
  1144. start_date = ?,
  1145. end_date = ?,
  1146. money = ?,
  1147. pay_method = ?,
  1148. pay_channel = ?,
  1149. img_url = ?,
  1150. package_type = ?,rai_package_type = ?,
  1151. modify_time = NOW()
  1152. WHERE company_contract_id = ? `
  1153. // 套餐类型为0时, 不更新套餐类型
  1154. if item.PackageType == 0 {
  1155. sql = `UPDATE company_contract SET start_date = ?, end_date = ?, money = ?, pay_method = ?, pay_channel = ?, img_url = ?, rai_package_type = ?, modify_time = NOW() WHERE company_contract_id = ? `
  1156. _, err = to.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, item.RaiPackageType, companyContractId).Exec()
  1157. if err != nil {
  1158. return
  1159. }
  1160. } else {
  1161. _, err = to.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, item.PackageType, item.RaiPackageType, companyContractId).Exec()
  1162. if err != nil {
  1163. return
  1164. }
  1165. }
  1166. sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
  1167. _, err = to.Raw(sql, companyContractId).Exec()
  1168. if err != nil {
  1169. return
  1170. }
  1171. permissionArr := strings.Split(item.PermissionIds, ",")
  1172. for _, v := range permissionArr {
  1173. contractPermission := new(CompanyContractPermission)
  1174. contractPermission.CompanyId = item.CompanyId
  1175. contractPermission.CompanyContractId = companyContractId
  1176. chartPermissionId, tmpErr := strconv.Atoi(v)
  1177. if tmpErr != nil {
  1178. err = tmpErr
  1179. return
  1180. }
  1181. // 买方研选5w
  1182. if chartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
  1183. contractPermission.ExpensiveYx = expensiveYx
  1184. }
  1185. contractPermission.ChartPermissionId = chartPermissionId
  1186. if _, ok := mapPermissionIdUpgrade[chartPermissionId]; ok {
  1187. contractPermission.IsUpgrade = 1
  1188. }
  1189. contractPermission.StartDate = item.StartDate
  1190. contractPermission.EndDate = item.EndDate
  1191. contractPermission.ModifyTime = time.Now()
  1192. contractPermission.CreateTime = time.Now()
  1193. tmpErr = AddCompanyContractPermission(contractPermission)
  1194. if tmpErr != nil {
  1195. err = tmpErr
  1196. return
  1197. }
  1198. //新增日志
  1199. {
  1200. itemLog := new(CompanyContractPermissionLog)
  1201. itemLog.CompanyId = item.CompanyId
  1202. itemLog.CompanyContractId = companyContractId
  1203. itemLog.ChartPermissionId = v
  1204. itemLog.StartDate = item.StartDate
  1205. itemLog.EndDate = item.EndDate
  1206. itemLog.ModifyTime = time.Now()
  1207. itemLog.CreateTime = time.Now()
  1208. itemLog.LogCreateTime = time.Now()
  1209. go AddCompanyContractPermissionLog(itemLog)
  1210. }
  1211. }
  1212. sql = `UPDATE company_approval SET approve_status = '待审批',modify_time=NOW(),is_formal=? WHERE company_approval_id = ? `
  1213. _, err = to.Raw(sql, isFormal, item.CompanyApprovalId).Exec()
  1214. if err != nil {
  1215. return
  1216. }
  1217. sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
  1218. _, err = to.Raw(sql, item.CompanyId, productId).Exec()
  1219. return
  1220. }
  1221. // ModifyCompanyTurnPositiveBySystemContract 修改申请信息
  1222. func ModifyCompanyTurnPositiveBySystemContract(contractDetail *contract.ContractDetail, permissionArr []int, companyId, productId, companyApprovalId, companyContractId, isFormal, packageType, raiPackageType int) (err error) {
  1223. o := orm.NewOrm()
  1224. to, err := o.Begin()
  1225. if err != nil {
  1226. return
  1227. }
  1228. defer func() {
  1229. if err != nil {
  1230. tmpErr := to.Rollback()
  1231. if tmpErr != nil {
  1232. go alarm_msg.SendAlarmMsg("ModifyCompanyTurnPositiveBySystemContract 事务回滚失败,Err:"+tmpErr.Error(), 3)
  1233. }
  1234. } else {
  1235. err = to.Commit()
  1236. }
  1237. }()
  1238. sql := `UPDATE company_contract
  1239. SET
  1240. start_date = ?,
  1241. end_date = ?,
  1242. money = ?,
  1243. pay_method = ?,
  1244. pay_channel = ?,
  1245. img_url = ?,package_type = ?,rai_package_type = ?,
  1246. modify_time = NOW()
  1247. WHERE company_contract_id = ? `
  1248. _, err = to.Raw(sql, contractDetail.StartDate, contractDetail.EndDate, contractDetail.Price, contractDetail.PayRemark, contractDetail.PayChannel, contractDetail.CheckBackFileUrl, packageType, raiPackageType, companyContractId).Exec()
  1249. if err != nil {
  1250. return
  1251. }
  1252. sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
  1253. _, err = to.Raw(sql, companyContractId).Exec()
  1254. if err != nil {
  1255. return
  1256. }
  1257. for _, chartPermissionId := range permissionArr {
  1258. contractPermission := new(CompanyContractPermission)
  1259. contractPermission.CompanyId = companyId
  1260. contractPermission.CompanyContractId = companyContractId
  1261. contractPermission.ChartPermissionId = chartPermissionId
  1262. contractPermission.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1263. contractPermission.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1264. contractPermission.ModifyTime = time.Now()
  1265. contractPermission.CreateTime = time.Now()
  1266. tmpErr := AddCompanyContractPermission(contractPermission)
  1267. if tmpErr != nil {
  1268. err = tmpErr
  1269. return
  1270. }
  1271. //新增日志
  1272. {
  1273. itemLog := new(CompanyContractPermissionLog)
  1274. itemLog.CompanyId = companyId
  1275. itemLog.CompanyContractId = companyContractId
  1276. itemLog.ChartPermissionId = strconv.Itoa(chartPermissionId)
  1277. itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
  1278. itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
  1279. itemLog.ModifyTime = time.Now()
  1280. itemLog.CreateTime = time.Now()
  1281. itemLog.LogCreateTime = time.Now()
  1282. go AddCompanyContractPermissionLog(itemLog)
  1283. }
  1284. }
  1285. sql = `UPDATE company_approval SET approve_status = '待审批',modify_time=NOW(),is_formal=? WHERE company_approval_id = ? `
  1286. _, err = to.Raw(sql, isFormal, companyApprovalId).Exec()
  1287. if err != nil {
  1288. return
  1289. }
  1290. sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
  1291. _, err = to.Raw(sql, companyId, productId).Exec()
  1292. return
  1293. }
  1294. // ModifyCompanyApplyServiceUpdate 修改申请信息
  1295. func ModifyCompanyApplyServiceUpdate(item *CompanyApplyServiceUpdateReq, productId, companyContractId, isFormal int, mapPermissionIdUpgrade, mapPermissionIdExpensive map[int]int) (err error) {
  1296. o := orm.NewOrm()
  1297. to, err := o.Begin()
  1298. if err != nil {
  1299. return
  1300. }
  1301. defer func() {
  1302. if err != nil {
  1303. tmpErr := to.Rollback()
  1304. if tmpErr != nil {
  1305. go alarm_msg.SendAlarmMsg("ModifyCompanyApplyServiceUpdate 事务回滚失败,Err:"+tmpErr.Error(), 3)
  1306. }
  1307. } else {
  1308. err = to.Commit()
  1309. }
  1310. }()
  1311. sql := `UPDATE company_contract
  1312. SET
  1313. start_date = ?,
  1314. end_date = ?,
  1315. money = ?,
  1316. pay_method = ?,
  1317. pay_channel = ?,
  1318. img_url = ?,package_type = ?,rai_package_type = ?,
  1319. modify_time = NOW()
  1320. WHERE company_contract_id = ? `
  1321. _, err = to.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, item.PackageType, item.RaiPackageType, companyContractId).Exec()
  1322. if err != nil {
  1323. return
  1324. }
  1325. sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
  1326. _, err = to.Raw(sql, companyContractId).Exec()
  1327. if err != nil {
  1328. return
  1329. }
  1330. permissionArr := strings.Split(item.PermissionIds, ",")
  1331. for _, v := range permissionArr {
  1332. contractPermission := new(CompanyContractPermission)
  1333. contractPermission.CompanyId = item.CompanyId
  1334. contractPermission.CompanyContractId = companyContractId
  1335. chartPermissionId, tmpErr := strconv.Atoi(v)
  1336. if tmpErr != nil {
  1337. err = tmpErr
  1338. return
  1339. }
  1340. contractPermission.ChartPermissionId = chartPermissionId
  1341. contractPermission.StartDate = item.StartDate
  1342. contractPermission.EndDate = item.EndDate
  1343. contractPermission.ModifyTime = time.Now()
  1344. contractPermission.CreateTime = time.Now()
  1345. // 升级/买方研选5w
  1346. p, _ := strconv.Atoi(v)
  1347. contractPermission.IsUpgrade = mapPermissionIdUpgrade[p]
  1348. contractPermission.ExpensiveYx = mapPermissionIdExpensive[p]
  1349. tmpErr = AddCompanyContractPermission(contractPermission)
  1350. if tmpErr != nil {
  1351. err = tmpErr
  1352. return
  1353. }
  1354. //新增日志
  1355. {
  1356. itemLog := new(CompanyContractPermissionLog)
  1357. itemLog.CompanyId = item.CompanyId
  1358. itemLog.CompanyContractId = companyContractId
  1359. itemLog.ChartPermissionId = v
  1360. itemLog.StartDate = item.StartDate
  1361. itemLog.EndDate = item.EndDate
  1362. itemLog.ModifyTime = time.Now()
  1363. itemLog.CreateTime = time.Now()
  1364. itemLog.LogCreateTime = time.Now()
  1365. go AddCompanyContractPermissionLog(itemLog)
  1366. }
  1367. }
  1368. sql = `UPDATE company_approval SET approve_status = '待审批',modify_time=NOW(),is_formal=? WHERE company_approval_id = ? `
  1369. _, err = to.Raw(sql, isFormal, item.CompanyApprovalId).Exec()
  1370. if err != nil {
  1371. return
  1372. }
  1373. sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
  1374. _, err = to.Raw(sql, item.CompanyId, productId).Exec()
  1375. return
  1376. }
  1377. // GetCompanyTryDelayApprovalCount 获取客户延期申请次数
  1378. func GetCompanyTryDelayApprovalCount(companyId, productId int) (count int, err error) {
  1379. sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND approve_content='试用延期' `
  1380. o := orm.NewOrm()
  1381. err = o.Raw(sql, companyId, productId).QueryRow(&count)
  1382. return
  1383. }
  1384. // GetFreezeToTryCompanyApprovalCount 客户冻结转试用次数(某个销售下)
  1385. func GetFreezeToTryCompanyApprovalCount(companyId, productId, applyUserId int) (count int, err error) {
  1386. sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND approve_content='冻结转试用' and apply_user_id = ? `
  1387. o := orm.NewOrm()
  1388. err = o.Raw(sql, companyId, productId, applyUserId).QueryRow(&count)
  1389. return
  1390. }
  1391. // GetCompanyApprovalReportRecordGroupList 获取销售分组数据
  1392. func GetCompanyApprovalReportRecordGroupList(condition string, pars []interface{}) (list []*CompanyReportRecordGroup, err error) {
  1393. o := orm.NewOrm()
  1394. sql := ` SELECT a.apply_user_id as admin_id,count(DISTINCT(a.company_id)) num,GROUP_CONCAT(DISTINCT a.company_id SEPARATOR ',') AS company_ids
  1395. FROM company_approval a join company_contract b on a.company_contract_id=b.company_contract_id
  1396. WHERE 1=1 and a.company_contract_id !=0
  1397. `
  1398. if condition != "" {
  1399. sql += condition
  1400. }
  1401. sql += ` GROUP BY a.apply_user_id`
  1402. _, err = o.Raw(sql, pars).QueryRows(&list)
  1403. return
  1404. }
  1405. // GetCompanyApprovalCount 客户申请的次数
  1406. func GetCompanyApprovalCount(companyId, productId, applyMethod int) (count int, err error) {
  1407. sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND apply_method= ? `
  1408. o := orm.NewOrm()
  1409. err = o.Raw(sql, companyId, productId, applyMethod).QueryRow(&count)
  1410. return
  1411. }
  1412. type RenewCompanyGroup struct {
  1413. SellerId int `description:"所属销售id"`
  1414. //AdminName string `description:"所属销售名称"`
  1415. Num int `description:"汇总次数"`
  1416. CompanyIds string `description:"客户id字符串"`
  1417. }
  1418. // GetNotRenewCompanyGroupList 获取销售未续约数据
  1419. func GetNotRenewCompanyGroupList(productId int) (list []*RenewCompanyGroup, err error) {
  1420. o := orm.NewOrm()
  1421. sql := `SELECT
  1422. cp.seller_id,
  1423. count(DISTINCT(cp.company_id)) num,
  1424. GROUP_CONCAT(DISTINCT cp.company_id SEPARATOR ',') AS company_ids
  1425. FROM
  1426. company_product AS cp
  1427. INNER JOIN company_operation_record AS op
  1428. WHERE
  1429. op.company_id = cp.company_id
  1430. AND cp.product_id = ?
  1431. AND op.product_id = ?
  1432. AND cp.STATUS IN ( "冻结", "流失" )
  1433. AND op.operation = "try_out"
  1434. `
  1435. //if condition != "" {
  1436. // sql += condition
  1437. //}
  1438. sql += ` GROUP BY cp.seller_id `
  1439. _, err = o.Raw(sql, productId, productId).QueryRows(&list)
  1440. return
  1441. }
  1442. // GetRenewFollowCompanyGroupList 获取销售续约跟进数据
  1443. func GetRenewFollowCompanyGroupList(ids string, productId int) (list []*RenewCompanyGroup, err error) {
  1444. o := orm.NewOrm()
  1445. sql := `SELECT
  1446. cp.seller_id,
  1447. count(DISTINCT(cp.company_id)) num,
  1448. GROUP_CONCAT(DISTINCT cp.company_id SEPARATOR ',') AS company_ids
  1449. FROM
  1450. company_product AS cp
  1451. INNER JOIN company_operation_record AS op
  1452. WHERE
  1453. op.company_id = cp.company_id
  1454. AND cp.product_id = ?
  1455. AND op.product_id = ?
  1456. AND cp.STATUS="试用"
  1457. AND op.operation = "try_out"
  1458. AND cp.company_id NOT IN (` + ids + `)
  1459. `
  1460. //if condition != "" {
  1461. // sql += condition
  1462. //}
  1463. sql += ` GROUP BY cp.seller_id `
  1464. _, err = o.Raw(sql, productId, productId).QueryRows(&list)
  1465. return
  1466. }
  1467. type CompanyApproveCreateTime struct {
  1468. CompanyId string
  1469. CreateTime time.Time
  1470. }
  1471. // GetNotRenewFollowCompanyGroupList 获取历史经历过冻结或流失的客户
  1472. func GetNotRenewFollowCompanyGroupList(productId int) (list []*CompanyApproveCreateTime, err error) {
  1473. o := orm.NewOrm()
  1474. sql := ` SELECT * FROM(
  1475. SELECT
  1476. cp.company_id,op.create_time
  1477. FROM
  1478. company_product AS cp
  1479. INNER JOIN company_operation_record AS op
  1480. WHERE
  1481. op.company_id = cp.company_id
  1482. AND cp.product_id = ?
  1483. AND op.product_id = ?
  1484. AND op.operation IN ( "freeze", "loss" )
  1485. GROUP BY cp.company_id,op.create_time DESC
  1486. )as a GROUP BY a.company_id
  1487. `
  1488. //sql += ` GROUP BY cp.seller_id `
  1489. _, err = o.Raw(sql, productId, productId).QueryRows(&list)
  1490. return
  1491. }
  1492. // GetCompanyApproveCreateTimeList 获取客户本轮合同开始时间
  1493. func GetCompanyApproveCreateTimeList(productId int) (list []*CompanyApproveCreateTime, err error) {
  1494. o := orm.NewOrm()
  1495. sql := `SELECT * FROM(
  1496. SELECT
  1497. cp.company_id,op.create_time
  1498. FROM
  1499. company_product AS cp
  1500. INNER JOIN company_operation_record AS op
  1501. WHERE
  1502. op.company_id = cp.company_id
  1503. AND cp.product_id = ?
  1504. AND op.product_id = ?
  1505. AND op.operation = "try_out"
  1506. GROUP BY cp.company_id,op.create_time DESC
  1507. )as a GROUP BY a.company_id
  1508. `
  1509. //sql += ` GROUP BY cp.seller_id `
  1510. _, err = o.Raw(sql, productId, productId).QueryRows(&list)
  1511. return
  1512. }
  1513. // GetExpiresCompanyGroupList 获取销售到期数据
  1514. func GetExpiresCompanyGroupList(startDate, endDate time.Time, productId int) (list []*RenewCompanyGroup, err error) {
  1515. o := orm.NewOrm()
  1516. sql := `SELECT
  1517. seller_id,
  1518. count(
  1519. DISTINCT ( company_id )) num,
  1520. GROUP_CONCAT( DISTINCT company_id SEPARATOR ',' ) AS company_ids
  1521. FROM
  1522. company_product
  1523. WHERE
  1524. product_id=?
  1525. AND
  1526. contract_end_date BETWEEN ?
  1527. AND ?
  1528. GROUP BY
  1529. seller_id
  1530. `
  1531. _, err = o.Raw(sql, productId, startDate, endDate).QueryRows(&list)
  1532. return
  1533. }
  1534. // GetRenewCompanyGroupList 获取销售续约数据
  1535. func GetRenewCompanyGroupList(startDate, endDate time.Time, contractId string) (list []*RenewCompanyGroup, err error) {
  1536. o := orm.NewOrm()
  1537. sql := ` SELECT
  1538. p.seller_id,
  1539. count(
  1540. DISTINCT ( c.company_id )) num,
  1541. GROUP_CONCAT( DISTINCT c.company_id SEPARATOR ',' ) AS company_ids
  1542. FROM
  1543. company_product as p
  1544. INNER JOIN company_contract as c
  1545. WHERE
  1546. c.modify_time BETWEEN ?
  1547. AND ?
  1548. AND c.company_product_id = p.company_product_id
  1549. AND c.status = 1
  1550. AND c.company_contract_id IN (` + contractId + `)
  1551. GROUP BY
  1552. p.seller_id;
  1553. `
  1554. _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
  1555. return
  1556. }