company_approval.go 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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. }
  1557. func GetCompanyApprovalList(condition string, pars []interface{}) (items []*CompanyApproval, err error) {
  1558. o := orm.NewOrm()
  1559. sql := `SELECT * FROM company_approval WHERE 1 = 1 `
  1560. if condition != "" {
  1561. sql += condition
  1562. }
  1563. _, err = o.Raw(sql, pars).QueryRows(&items)
  1564. return
  1565. }