12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667 |
- package company
- import (
- "github.com/beego/beego/v2/client/orm"
- "github.com/rdlucklib/rdluck_tools/paging"
- "hongze/hz_crm_api/models/contract"
- "hongze/hz_crm_api/models/system"
- "hongze/hz_crm_api/services/alarm_msg"
- "hongze/hz_crm_api/utils"
- "strconv"
- "strings"
- "time"
- )
- type CompanyApproval struct {
- CompanyApprovalId int `orm:"column(company_approval_id);pk"`
- CompanyId int
- ProductId int
- ApproveStatus string `description:"'待审批','已审批','驳回','已撤回'"`
- ApproveUserId int `description:"审批人"`
- ApproveRoleTypeCode string `description:"审批人角色类型"`
- ApproveRealName string `description:"审批人姓名"`
- ApproveTime time.Time `description:"审批时间"`
- ApproveContent string `description:"待审内容"`
- ApplyMethod int `description:"申请类型:1:试用->正式,2:冻结—>试用,3:试用延期,4:原销售申请领取流失客户,5:正式客户申请服务更新,6:正式客户补充协议"`
- ApplyReasons string `description:"申请原因"`
- ApplyUserId int `description:"申请人"`
- ApplyRealName string `description:"申请人姓名"`
- CreateTime time.Time `description:"申请时间"`
- ModifyTime time.Time `description:"修改时间"`
- CompanyContractId int `description:"合同id"`
- IsFormal int `description:"是否已经转正式,0是没有转正式,1是已经转过正式"`
- }
- // AddCompanyApproval 新增客户产品
- func AddCompanyApproval(item *CompanyApproval) (lastId int, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("AddCompanyApproval 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- var newId int64
- newId, err = to.Insert(item)
- if err != nil {
- return
- }
- lastId = int(newId)
- sql := `UPDATE company_product SET approve_status=? WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, item.ApproveStatus, item.CompanyId, item.ProductId).Exec()
- return
- }
- func GetCompanyApprovalCountById(companyId, productId int) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND approve_status='待审批' `
- o := orm.NewOrm()
- err = o.Raw(sql, companyId, productId).QueryRow(&count)
- return
- }
- func GetCompanyApprovalById(companyId, productId int) (item *CompanyApproval, err error) {
- sql := `SELECT * FROM company_approval WHERE company_id=? AND product_id=? AND approve_status='待审批' order by company_approval_id desc LIMIT 1 `
- o := orm.NewOrm()
- err = o.Raw(sql, companyId, productId).QueryRow(&item)
- return
- }
- func GetCompanyApprovalByApprovalId(companyApprovalId int) (item *CompanyApproval, err error) {
- sql := `SELECT * FROM company_approval WHERE company_approval_id=? LIMIT 1 `
- o := orm.NewOrm()
- err = o.Raw(sql, companyApprovalId).QueryRow(&item)
- return
- }
- type CompanyApprovalList struct {
- CompanyId int
- ProductId int
- CompanyName string `description:"客户名称"`
- CreditCode string `description:"社会统一信用码"`
- CompanyType string `description:"客户类型"`
- IndustryName string `description:"行业名称"`
- SellerName string `description:"销售名称"`
- CreateTime string `description:"创建时间"`
- ApprovalTime string `description:"提交申请时间"`
- ApproveTime string `description:"审批时间"`
- Status string `description:"客户状态"`
- ApproveContent string `description:"待审内容"`
- ApproveStatus string `description:"当前状态"`
- ApplyMethod int `description:"申请类型:1:试用->正式,2:冻结—>试用,3:流失—>正式,4:试用延期,5:原销售申请领取流失客户,6:正式客户申请服务更新"`
- ApplyReasons string `description:"申请理由"`
- FreezeStartDate string `description:"冻结开始日期"`
- FreezeEndDate string `description:"冻结结束日期"`
- FreezeExpireDays int `description:"冻结到期天数"`
- Address string `description:"地址"`
- Source string `description:"客户来源"`
- ApproveRemark string `description:"驳回理由"`
- StartDate string `description:"开始日期"`
- EndDate string `description:"结束日期"`
- ExpireDay int `description:"服务到期天数"`
- CompanyApprovalId int `description:"审批记录ID"`
- ApprovalCount int `description:"已延期审批次数"`
- CompanyContractId int `description:"合同id"`
- DelayPermission string `description:"延期申请品种"`
- ApplyRealName string `description:"申请人姓名"`
- OpButton ApprovalButton `description:"操作权限"`
- ApplyContractType int `description:"申请合同类型 0 非标 1 标准"`
- ApplyUserId int `description:"申请人编号id" json:"-"`
- }
- // ApprovalButton 审批操作权限
- type ApprovalButton struct {
- Cancel bool `description:"是否具有撤回权限,true才允许审核操作"`
- Approval bool `description:"是否具有审核操作权限,true才允许审核操作"`
- //Cancel bool `description:"是否具有撤回权限"`
- }
- type CompanyApprovalListResp struct {
- Paging *paging.PagingItem
- List []*CompanyApprovalList
- }
- func GetApprovalList(condition string, pars []interface{}, startSize, pageSize int) (items []*CompanyApprovalList, err error) {
- o := orm.NewOrm()
- sql := `SELECT a.company_id,b.product_id,a.company_name,a.credit_code,a.address,b.company_type,b.industry_name,
- 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,
- 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,
- c.apply_real_name,c.apply_user_id
- FROM company AS a
- INNER JOIN company_product AS b ON a.company_id=b.company_id
- INNER JOIN company_approval AS c ON b.company_id=c.company_id AND b.product_id=c.product_id
- WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY c.modify_time DESC LIMIT ?,? `
- _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
- return
- }
- func GetApprovalCount(condition string, pars []interface{}) (count int, err error) {
- o := orm.NewOrm()
- sql := `SELECT COUNT(1) AS count
- FROM company AS a
- INNER JOIN company_product AS b ON a.company_id=b.company_id
- INNER JOIN company_approval AS c ON b.company_id=c.company_id AND b.product_id=c.product_id
- WHERE 1=1 `
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY c.modify_time DESC `
- err = o.Raw(sql, pars).QueryRow(&count)
- return
- }
- // TryOutToFormal 试用转正式
- func TryOutToFormal(companyId, productId, sellerId, companyApprovalId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (newCompanyReportPermissionList []*CompanyReportPermission, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("TryOutToFormal 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- // 套餐类型为0时, 不更新套餐类型
- 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=? `
- if packageType == 0 {
- 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=? `
- _, err = to.Raw(sql, startDate, endDate, raiPackageType, companyId, productId).Exec()
- if err != nil {
- return
- }
- } else {
- _, err = to.Raw(sql, startDate, endDate, packageType, raiPackageType, companyId, productId).Exec()
- if err != nil {
- return
- }
- }
- sql = `UPDATE company SET type=1,last_updated_time=NOW() WHERE company_id=? `
- _, err = to.Raw(sql, companyId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- items := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(CompanyPermissionLog)
- cpLog.CompanyId = companyId
- cpLog.ChartPermissionId = pv.ChartPermissionId
- cpLog.CreateTime = time.Now()
- cpLog.SysUserId = sellerId
- cpLog.SysUserName = sellerName
- cpLog.StartDate = pv.StartDate
- cpLog.EndDate = pv.EndDate
- cpLog.ProductId = productId
- cpLog.ProductName = pv.ProductName
- go AddCompanyPermissionLog(cpLog)
- }
- sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?`
- _, err = to.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- contractPermission := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
- _, err = to.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
- if err != nil {
- return
- }
- for _, pv := range contractPermission {
- tmpCompanyReportPermission := &CompanyReportPermission{
- //CompanyReportPermissionId: 0,
- CompanyId: companyId,
- ReportPermissionId: pv.ChartPermissionId,
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ChartPermissionId: pv.ChartPermissionId,
- StartDate: pv.StartDate,
- EndDate: pv.EndDate,
- ProductId: productId,
- ProductName: productName,
- CompanyContractId: companyContractId,
- Status: "正式",
- ModifyTime: time.Now(),
- IsUpgrade: pv.IsUpgrade,
- ExpensiveYx: pv.ExpensiveYx,
- }
- ////CRM13.2 升级套餐
- //if tmpCompanyReportPermission.ChartPermissionId > 10000 {
- // tmpCompanyReportPermission.ChartPermissionId -= 10000
- // tmpCompanyReportPermission.ReportPermissionId -= 10000
- // tmpCompanyReportPermission.IsUpgrade = 1
- //}
- newId, tmpErr := to.Insert(tmpCompanyReportPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpCompanyReportPermission.CompanyReportPermissionId = newId
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
- //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
- // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status,is_upgrade)
- // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?,?) `
- //_, err = to.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式", pv.IsUpgrade).Exec()
- //if err != nil {
- // return
- //}
- }
- sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
- if err != nil {
- return
- }
- return
- }
- // TryOutToFormalXClassRai 权益客户试用转正式
- func TryOutToFormalXClassRai(companyId, productId, sellerId, companyApprovalId, companyContractId int, startDate, endDate, sellerName, productName string, raiPackageType int) (newCompanyReportPermissionList []*CompanyReportPermission, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("TryOutToFormalXClassRai 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- //123月份申请转正通过时,将试用期限延长(更新)至4.30。456月份申请转正通过时,将试用期限延长至7.31。789月份申请转正通过时,将试用期限延长至10.31。101112月份申请转正通过时,将试用期限延长至次年1.31。
- endDate = utils.GetLastDayOfQuarter(time.Now()).Format(utils.FormatDate)
- // 套餐类型为0时, 不更新套餐类型,权益与FICC开始与结束时间一起修改
- 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=? `
- _, err = to.Raw(sql, endDate, raiPackageType, companyId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company SET type=1,last_updated_time=NOW() WHERE company_id=? `
- _, err = to.Raw(sql, companyId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- items := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(CompanyPermissionLog)
- cpLog.CompanyId = companyId
- cpLog.ChartPermissionId = pv.ChartPermissionId
- cpLog.CreateTime = time.Now()
- cpLog.SysUserId = sellerId
- cpLog.SysUserName = sellerName
- cpLog.StartDate = pv.StartDate
- cpLog.EndDate = pv.EndDate
- cpLog.ProductId = productId
- cpLog.ProductName = pv.ProductName
- go AddCompanyPermissionLog(cpLog)
- }
- sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?`
- _, err = to.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- contractPermission := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
- _, err = to.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
- if err != nil {
- return
- }
- for _, pv := range contractPermission {
- tmpCompanyReportPermission := &CompanyReportPermission{
- //CompanyReportPermissionId: 0,
- CompanyId: companyId,
- ReportPermissionId: pv.ChartPermissionId,
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ChartPermissionId: pv.ChartPermissionId,
- StartDate: pv.StartDate,
- EndDate: endDate,
- ProductId: productId,
- ProductName: productName,
- CompanyContractId: companyContractId,
- Status: "永续",
- ModifyTime: time.Now(),
- IsUpgrade: pv.IsUpgrade,
- ExpensiveYx: pv.ExpensiveYx,
- }
- newId, tmpErr := to.Insert(tmpCompanyReportPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpCompanyReportPermission.CompanyReportPermissionId = newId
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
- }
- sql = `UPDATE company_report_permission SET status='永续', end_date=? WHERE company_id=? ` // 更改权限的开始时间结束时间
- _, err = to.Raw(sql, endDate, companyId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
- if err != nil {
- return
- }
- return
- }
- // FreezeToTryOut 冻结转试用
- func FreezeToTryOut(companyId, productId, sellerId, companyApprovalId, applyUserId int, sellerName, productName string) (newCompanyReportPermissionList []*CompanyReportPermission, startDate, endDate string, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("FreezeToTryOut 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- startDate = time.Now().Format(utils.FormatDate)
- endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- //sellerItem, err := system.GetSysAdminById(applyUserId)
- //if err != nil {
- // return
- //}
- //更新用户产品状态
- //6.9 fix 未续约强制弹窗要求客户冻结转试用后不弹床,取消清空冻结时间以标记曾经被冻结过
- sql := `UPDATE company_product SET status='试用',is_suspend=0,approve_status='已审批',freeze_start_date=null,freeze_end_date=null,
- start_date=?,end_date=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
- if err != nil {
- return
- }
- //更新用户状态
- sql = `UPDATE company SET type=2,last_updated_time=NOW() WHERE company_id=? `
- _, err = to.Raw(sql, companyId).Exec()
- if err != nil {
- return
- }
- //更新审批单
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
- items := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(CompanyPermissionLog)
- cpLog.CompanyId = companyId
- cpLog.ChartPermissionId = pv.ChartPermissionId
- cpLog.CreateTime = time.Now()
- cpLog.SysUserId = sellerId
- cpLog.SysUserName = sellerName
- cpLog.StartDate = pv.StartDate
- cpLog.EndDate = pv.EndDate
- cpLog.ProductId = productId
- cpLog.ProductName = pv.ProductName
- go AddCompanyPermissionLog(cpLog)
- }
- //更新产品(2021年03月02日14:58:50注释,调整为从申请权限表中获取数据并修改)
- //sql = `UPDATE company_report_permission SET start_date=?,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
- //_, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
- //if err != nil {
- // return
- //}
- //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
- count, _ := GetCompanyReportPermissionCount(companyId, productId)
- if count > 0 {
- sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- }
- //获取需要试用的权限
- delayPermissionItems, tmpErr := GetDelayPermissionItems(companyId, companyApprovalId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- status := `试用`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission := &CompanyReportPermission{
- //CompanyReportPermissionId: 0,
- CompanyId: companyId,
- ReportPermissionId: v.ChartPermissionId,
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ChartPermissionId: v.ChartPermissionId,
- StartDate: v.StartDate,
- EndDate: v.EndDate,
- ProductId: productId,
- ProductName: productName,
- //CompanyContractId: companyContractId,
- Status: status,
- ModifyTime: time.Now(),
- //IsUpgrade: pv.IsUpgrade,
- }
- newId, tmpErr := to.Insert(tmpCompanyReportPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpCompanyReportPermission.CompanyReportPermissionId = newId
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
- //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
- // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,status)
- // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?) `
- //_, err = to.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, v.EndDate, productId, productName, status).Exec()
- //if err != nil {
- // return
- //}
- }
- return
- }
- // FreezeToTryOutXClassRai 权益冻结转X类试用
- func FreezeToTryOutXClassRai(companyId, productId, sellerId, companyApprovalId, applyUserId int, sellerName, productName string) (newCompanyReportPermissionList []*CompanyReportPermission, startDate, endDate string, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("FreezeToTryOutXClassRai 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- startDate = time.Now().Format(utils.FormatDate)
- endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- //sellerItem, err := system.GetSysAdminById(applyUserId)
- //if err != nil {
- // return
- //}
- //更新用户产品状态
- sql := `UPDATE company_product SET status='永续',is_suspend=0,approve_status='已审批',freeze_start_date=null,freeze_end_date=null,
- start_date=?,end_date=?,modify_time=NOW(),try_stage=1 WHERE company_id=? `
- _, err = to.Raw(sql, startDate, endDate, companyId).Exec()
- if err != nil {
- return
- }
- //更新用户状态
- sql = `UPDATE company SET type=5,last_updated_time=NOW() WHERE company_id=? `
- _, err = to.Raw(sql, companyId).Exec()
- if err != nil {
- return
- }
- //更新审批单
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
- items := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(CompanyPermissionLog)
- cpLog.CompanyId = companyId
- cpLog.ChartPermissionId = pv.ChartPermissionId
- cpLog.CreateTime = time.Now()
- cpLog.SysUserId = sellerId
- cpLog.SysUserName = sellerName
- cpLog.StartDate = pv.StartDate
- cpLog.EndDate = pv.EndDate
- cpLog.ProductId = productId
- cpLog.ProductName = pv.ProductName
- go AddCompanyPermissionLog(cpLog)
- }
- //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
- count, _ := GetCompanyReportPermissionCount(companyId, productId)
- if count > 0 {
- sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- }
- sql = `UPDATE company_report_permission SET status='永续',start_date=?,end_date=? WHERE company_id=? ` // 更改权限的开始时间结束时间
- _, err = to.Raw(sql, startDate, endDate, companyId).Exec()
- if err != nil {
- return
- }
- //获取需要试用的权限
- delayPermissionItems, tmpErr := GetDelayPermissionItems(companyId, companyApprovalId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- status := `永续`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission := &CompanyReportPermission{
- //CompanyReportPermissionId: 0,
- CompanyId: companyId,
- ReportPermissionId: v.ChartPermissionId,
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ChartPermissionId: v.ChartPermissionId,
- StartDate: v.StartDate,
- EndDate: endDate,
- ProductId: productId,
- ProductName: productName,
- //CompanyContractId: companyContractId,
- Status: status,
- ModifyTime: time.Now(),
- //IsUpgrade: pv.IsUpgrade,
- }
- newId, tmpErr := to.Insert(tmpCompanyReportPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpCompanyReportPermission.CompanyReportPermissionId = newId
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
- }
- return
- }
- // TryOutDelay 试用延期
- func TryOutDelay(companyId, productId, sellerId, companyApprovalId int, sellerName, endDate, productName string) (newCompanyReportPermissionList []*CompanyReportPermission, newEndDate string, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("TryOutDelay 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- endDateTime, err := time.Parse(utils.FormatDate, endDate)
- if err != nil {
- return
- }
- newEndDate = endDateTime.AddDate(0, 2, 0).Format(utils.FormatDate)
- //更新用户产品状态
- sql := `UPDATE company_product SET approve_status='已审批',is_suspend=0,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, newEndDate, companyId, productId).Exec()
- if err != nil {
- return
- }
- //更新审批单
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
- items := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(CompanyPermissionLog)
- cpLog.CompanyId = companyId
- cpLog.ChartPermissionId = pv.ChartPermissionId
- cpLog.CreateTime = time.Now()
- cpLog.SysUserId = sellerId
- cpLog.SysUserName = sellerName
- cpLog.StartDate = pv.StartDate
- cpLog.EndDate = pv.EndDate
- cpLog.ProductId = productId
- cpLog.ProductName = pv.ProductName
- go AddCompanyPermissionLog(cpLog)
- }
- //获取需要延期的权限
- delayPermissionItems, err := GetDelayPermissionItems(companyId, companyApprovalId)
- if err != nil {
- return
- }
- status := `试用`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission, tmpErr := GetCompanyPermission(companyId, v.ChartPermissionId)
- if tmpErr != nil {
- //没有数据的话
- if tmpErr.Error() == utils.ErrNoRow() {
- tmpCompanyReportPermission = &CompanyReportPermission{
- //CompanyReportPermissionId: 0,
- CompanyId: companyId,
- ReportPermissionId: v.ChartPermissionId,
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ChartPermissionId: v.ChartPermissionId,
- StartDate: v.StartDate,
- EndDate: newEndDate,
- ProductId: productId,
- ProductName: productName,
- //CompanyContractId: companyContractId,
- Status: status,
- ModifyTime: time.Now(),
- //IsUpgrade: pv.IsUpgrade,
- }
- newId, tmpErr := to.Insert(tmpCompanyReportPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpCompanyReportPermission.CompanyReportPermissionId = newId
- } else {
- // 其他异常的话,那么就返回报错
- err = tmpErr
- return
- }
- } else {
- // 修改原数据
- tmpCompanyReportPermission.EndDate = newEndDate
- tmpCompanyReportPermission.Status = status
- tmpCompanyReportPermission.ModifyTime = time.Now()
- _, err = to.Update(tmpCompanyReportPermission, "EndDate", "Status", "ModifyTime")
- if err != nil {
- return
- }
- }
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
- //isExist, tmpErr := GetCompanyPermissionCheck(companyId, v.ChartPermissionId)
- //if tmpErr != nil{
- // err = tmpErr
- // return
- //}
- //if isExist > 0 {
- // sql = `UPDATE company_report_permission SET end_date=?,status=?,modify_time=NOW() WHERE company_id=? AND product_id=? AND chart_permission_id=?`
- // _, err = to.Raw(sql, newEndDate, status, companyId, productId, v.ChartPermissionId).Exec()
- // if err != nil {
- // return
- // }
- //} else {
- // sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
- // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,status)
- // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?) `
- // _, err = to.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, newEndDate, productId, productName, status).Exec()
- // if err != nil {
- // return
- // }
- //}
- }
- return
- }
- // 原销售申请领取
- func ApplyReceive(companyId, productId, sysUserId, companyApprovalId int, sellerItem *system.Admin, sysUserName, productName, sellerName string, sellerId int) (newCompanyReportPermissionList []*CompanyReportPermission, startDate, endDate string, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ApplyReceive 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- startDate = time.Now().Format(utils.FormatDate)
- endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- //更新用户产品状态
- 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=? `
- _, err = to.Raw(sql, startDate, endDate, sellerItem.AdminId, sellerItem.RealName, sellerItem.GroupId, sellerItem.DepartmentId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //更新用户状态
- sql = `UPDATE company SET type=2,last_updated_time=NOW() WHERE company_id=? `
- _, err = to.Raw(sql, companyId).Exec()
- if err != nil {
- return
- }
- //更新审批单
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
- items := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(CompanyPermissionLog)
- cpLog.CompanyId = companyId
- cpLog.ChartPermissionId = pv.ChartPermissionId
- cpLog.CreateTime = time.Now()
- cpLog.SysUserId = sysUserId
- cpLog.SysUserName = sysUserName
- cpLog.StartDate = pv.StartDate
- cpLog.EndDate = pv.EndDate
- cpLog.ProductId = productId
- cpLog.ProductName = pv.ProductName
- go AddCompanyPermissionLog(cpLog)
- }
- //更新产品(2021年03月02日14:58:50注释,调整为从申请权限表中获取数据并修改)
- //sql = `UPDATE company_report_permission SET start_date=?,end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
- //_, err = to.Raw(sql, startDate, endDate, companyId, productId).Exec()
- //if err != nil {
- // return
- //}
- //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
- count, _ := GetCompanyReportPermissionCount(companyId, productId)
- if count > 0 {
- sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- }
- //修改 该联系人 与 当前销售的关系
- sql = `UPDATE user_seller_relation SET seller_id=?,seller = ? ,modify_time=NOW() WHERE company_id = ? AND product_id=?`
- _, err = to.Raw(sql, sellerId, sellerName, companyId, productId).Exec()
- //获取需要试用的权限
- delayPermissionItems, err := GetDelayPermissionItems(companyId, companyApprovalId)
- if err != nil {
- return
- }
- status := `试用`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission := &CompanyReportPermission{
- //CompanyReportPermissionId: 0,
- CompanyId: companyId,
- ReportPermissionId: v.ChartPermissionId,
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ChartPermissionId: v.ChartPermissionId,
- StartDate: v.StartDate,
- EndDate: v.EndDate,
- ProductId: productId,
- ProductName: productName,
- //CompanyContractId: companyContractId,
- Status: status,
- ModifyTime: time.Now(),
- //IsUpgrade: pv.IsUpgrade,
- }
- newId, tmpErr := to.Insert(tmpCompanyReportPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- tmpCompanyReportPermission.CompanyReportPermissionId = newId
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
- //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
- // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,status)
- // VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?) `
- //_, err = to.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, v.EndDate, productId, productName, status).Exec()
- //if err != nil {
- // return err
- //}
- }
- return
- }
- // ApplyServiceUpdate 申请服务更新
- func ApplyServiceUpdate(companyId, productId, sellerId, companyApprovalId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (newCompanyReportPermissionList []*CompanyReportPermission, err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ApplyServiceUpdate 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- items := make([]*CompanyReportPermission, 0)
- sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).QueryRows(&items)
- if err != nil {
- return
- }
- //已存在的权限map
- nowCompanyReportPermissionMap := make(map[int]*CompanyReportPermission)
- //产品服务的开始、结束日期(非产品权限)
- updateStartDate := startDate
- updateStartDateTime, err := time.Parse(utils.FormatDate, updateStartDate)
- if err != nil {
- return
- }
- updateEndDate := endDate
- updateEndDateTime, err := time.Parse(utils.FormatDate, updateEndDate)
- if err != nil {
- return
- }
- for _, pv := range items {
- cpLog := new(CompanyPermissionLog)
- cpLog.CompanyId = companyId
- cpLog.ChartPermissionId = pv.ChartPermissionId
- cpLog.CreateTime = time.Now()
- cpLog.SysUserId = sellerId
- cpLog.SysUserName = sellerName
- cpLog.StartDate = pv.StartDate
- cpLog.EndDate = pv.EndDate
- cpLog.ProductId = productId
- cpLog.ProductName = pv.ProductName
- go AddCompanyPermissionLog(cpLog)
- //将权限插入到已存在的权限map中
- nowCompanyReportPermissionMap[pv.ChartPermissionId] = pv
- //校验原始数据中的开始日期是否小于合同内的开始日期,如果小于,那么变更为原先的合同开始日期
- tmpStartDate, tmpErr := time.Parse(utils.FormatDate, pv.StartDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpStartDate.Before(updateStartDateTime) {
- updateStartDateTime = tmpStartDate
- }
- //校验原始数据中的结束日期是否大于合同内的结束日期,如果大于,那么变更为原先的合同结束日期
- tmpEndDate, tmpErr := time.Parse(utils.FormatDate, pv.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if tmpEndDate.After(updateEndDateTime) {
- updateEndDateTime = tmpEndDate
- }
- }
- //删除所有权限
- //sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
- //_, err = to.Raw(sql, companyId, productId).Exec()
- //if err != nil {
- // return
- //}
- //更新客户产品信息
- if packageType == 0 {
- 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=? `
- _, err = to.Raw(sql, updateStartDateTime, updateEndDateTime, raiPackageType, companyId, productId).Exec()
- if err != nil {
- return
- }
- } else {
- 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=? `
- _, err = to.Raw(sql, updateStartDateTime, updateEndDateTime, packageType, raiPackageType, companyId, productId).Exec()
- if err != nil {
- return
- }
- }
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- contractPermission := make([]*CompanyReportPermission, 0)
- sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
- _, err = to.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
- if err != nil {
- return
- }
- for _, pv := range contractPermission {
- nowPermission, ok := nowCompanyReportPermissionMap[pv.ChartPermissionId]
- //判断是否已经存在该权限,如果存在的话,那么是修改,否则是新增
- if ok {
- needUpdate := false
- if nowPermission.Status != "正式" && nowPermission.Status != "永续" {
- needUpdate = true
- }
- //如果 需要更新 字段 为false,那么再去校验时间
- if needUpdate == false {
- //如果当前存该权限,那么去校验是否需要修改
- nowPermissionEndDateTime, tmpErr := time.Parse(utils.FormatDate, nowPermission.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- contractPermissionEndDateTime, tmpErr := time.Parse(utils.FormatDate, pv.EndDate)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- if contractPermissionEndDateTime.After(nowPermissionEndDateTime) {
- needUpdate = true
- }
- }
- //如果 需要更新 字段 为 true,那么就去更新
- if needUpdate {
- nowPermission.EndDate = pv.EndDate
- nowPermission.LastUpdatedTime = time.Now()
- nowPermission.ModifyTime = time.Now()
- nowPermission.CompanyContractId = pv.CompanyContractId
- nowPermission.Status = "正式"
- nowPermission.IsUpgrade = pv.IsUpgrade
- nowPermission.ExpensiveYx = pv.ExpensiveYx
- _, err = to.Update(nowPermission, "EndDate", "LastUpdatedTime", "ModifyTime", "CompanyContractId", "Status", "IsUpgrade", "ExpensiveYx")
- if err != nil {
- return
- }
- //sql = `update company_report_permission set end_date=?,last_updated_time=now(),modify_time=now(),company_contract_id=?,status=? where company_report_permission_id=? `
- //_, tmpErr := to.Raw(sql, pv.EndDate, pv.CompanyContractId, "正式", nowPermission.CompanyReportPermissionId).Exec()
- //err = tmpErr
- //if err != nil {
- // return
- //}
- }
- } else {
- nowPermission = &CompanyReportPermission{
- //CompanyReportPermissionId: 0,
- CompanyId: companyId,
- ReportPermissionId: pv.ChartPermissionId,
- CreatedTime: time.Now(),
- LastUpdatedTime: time.Now(),
- ChartPermissionId: pv.ChartPermissionId,
- StartDate: pv.StartDate,
- EndDate: pv.EndDate,
- ProductId: productId,
- ProductName: productName,
- //CompanyContractId: companyContractId,
- Status: "正式",
- ModifyTime: time.Now(),
- IsUpgrade: pv.IsUpgrade,
- ExpensiveYx: pv.ExpensiveYx,
- }
- newId, tmpErr := to.Insert(nowPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- nowPermission.CompanyReportPermissionId = newId
- //sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
- // chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status)
- //VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?) `
- //_, tmpErr := to.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式").Exec()
- //err = tmpErr
- //if err != nil {
- // return
- //}
- }
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, nowPermission)
- }
- sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
- if err != nil {
- return
- }
- return
- }
- // ApplyApproveContract 更新审批单、合同状态
- func ApplyApproveContract(companyId, productId, companyApprovalId, companyContractId int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ApplyApproveContract 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- sql := `UPDATE company_product SET approve_status='已审批',renewal_intention=0,modify_time=NOW() WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- //更新审批单
- sql = `UPDATE company_approval SET approve_status='已审批',approve_time=NOW(),modify_time=NOW() WHERE company_approval_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //更新合同
- sql = `UPDATE company_contract SET status=1 WHERE company_contract_id=? AND company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyContractId, companyId, productId).Exec()
- if err != nil {
- return
- }
- return
- }
- func GetCompanyApprovalDetailById(companyId, productId int) (item *CompanyApproval, err error) {
- sql := `SELECT * FROM company_approval WHERE company_id=? AND product_id=? AND approve_status IN('驳回','已撤回') ORDER BY modify_time DESC LIMIT 1 `
- o := orm.NewOrm()
- err = o.Raw(sql, companyId, productId).QueryRow(&item)
- return
- }
- type CompanyApplyDeleteReq struct {
- CompanyId int `description:"客户id"`
- CompanyApprovalId int `description:"审批记录ID"`
- }
- // ApplyDelete 申请服务更新
- func ApplyDelete(companyId, companyApprovalId, productId, companyContractId int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ApplyDelete 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- sql := `DELETE FROM company_approval WHERE company_approval_id=? `
- _, err = to.Raw(sql, companyApprovalId).Exec()
- if err != nil {
- return
- }
- sql = `DELETE FROM company_contract WHERE company_contract_id=? `
- _, err = to.Raw(sql, companyContractId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_product SET approve_status='' WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- //作废消息
- sql = `UPDATE company_approval_message SET message_status=2 WHERE source_type=1 AND company_approval_id=? `
- _, err = to.Raw(sql, companyApprovalId).Exec()
- if err != nil {
- return
- }
- return
- }
- // ModifyCompanyApproval 修改申请信息
- func ModifyCompanyApproval(companyId, productId, companyApprovalId int, approveContent, applyReasons string) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- utils.FileLog.Info("试用延期申请:Err %s", err.Error())
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ModifyCompanyApproval 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- sql := `UPDATE company_approval SET approve_status = '待审批', approve_content=?,apply_reasons=?,modify_time=NOW() WHERE company_approval_id = ? `
- _, err = to.Raw(sql, approveContent, applyReasons, companyApprovalId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).Exec()
- utils.FileLog.Info("试用延期申请:Err %s,%s,%d", sql, companyId, productId)
- return
- }
- // ModifyCompanyTurnPositive 修改申请信息
- func ModifyCompanyTurnPositive(item *CompanyApplyTurnPositiveReq, productId, companyContractId, isFormal int, mapPermissionIdUpgrade map[int]int, expensiveYx int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ModifyCompanyTurnPositive 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- sql := `UPDATE company_contract
- SET
- start_date = ?,
- end_date = ?,
- money = ?,
- pay_method = ?,
- pay_channel = ?,
- img_url = ?,
- package_type = ?,rai_package_type = ?,
- modify_time = NOW()
- WHERE company_contract_id = ? `
- // 套餐类型为0时, 不更新套餐类型
- if item.PackageType == 0 {
- 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 = ? `
- _, err = to.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, item.RaiPackageType, companyContractId).Exec()
- if err != nil {
- return
- }
- } else {
- _, err = to.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, item.PackageType, item.RaiPackageType, companyContractId).Exec()
- if err != nil {
- return
- }
- }
- sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
- _, err = to.Raw(sql, companyContractId).Exec()
- if err != nil {
- return
- }
- permissionArr := strings.Split(item.PermissionIds, ",")
- for _, v := range permissionArr {
- contractPermission := new(CompanyContractPermission)
- contractPermission.CompanyId = item.CompanyId
- contractPermission.CompanyContractId = companyContractId
- chartPermissionId, tmpErr := strconv.Atoi(v)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- // 买方研选5w
- if chartPermissionId == utils.CHART_PERMISSION_ID_YANXUAN {
- contractPermission.ExpensiveYx = expensiveYx
- }
- contractPermission.ChartPermissionId = chartPermissionId
- if _, ok := mapPermissionIdUpgrade[chartPermissionId]; ok {
- contractPermission.IsUpgrade = 1
- }
- contractPermission.StartDate = item.StartDate
- contractPermission.EndDate = item.EndDate
- contractPermission.ModifyTime = time.Now()
- contractPermission.CreateTime = time.Now()
- tmpErr = AddCompanyContractPermission(contractPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //新增日志
- {
- itemLog := new(CompanyContractPermissionLog)
- itemLog.CompanyId = item.CompanyId
- itemLog.CompanyContractId = companyContractId
- itemLog.ChartPermissionId = v
- itemLog.StartDate = item.StartDate
- itemLog.EndDate = item.EndDate
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- go AddCompanyContractPermissionLog(itemLog)
- }
- }
- sql = `UPDATE company_approval SET approve_status = '待审批',modify_time=NOW(),is_formal=? WHERE company_approval_id = ? `
- _, err = to.Raw(sql, isFormal, item.CompanyApprovalId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, item.CompanyId, productId).Exec()
- return
- }
- // ModifyCompanyTurnPositiveBySystemContract 修改申请信息
- func ModifyCompanyTurnPositiveBySystemContract(contractDetail *contract.ContractDetail, permissionArr []int, companyId, productId, companyApprovalId, companyContractId, isFormal, packageType, raiPackageType int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ModifyCompanyTurnPositiveBySystemContract 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- sql := `UPDATE company_contract
- SET
- start_date = ?,
- end_date = ?,
- money = ?,
- pay_method = ?,
- pay_channel = ?,
- img_url = ?,package_type = ?,rai_package_type = ?,
- modify_time = NOW()
- WHERE company_contract_id = ? `
- _, err = to.Raw(sql, contractDetail.StartDate, contractDetail.EndDate, contractDetail.Price, contractDetail.PayRemark, contractDetail.PayChannel, contractDetail.CheckBackFileUrl, packageType, raiPackageType, companyContractId).Exec()
- if err != nil {
- return
- }
- sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
- _, err = to.Raw(sql, companyContractId).Exec()
- if err != nil {
- return
- }
- for _, chartPermissionId := range permissionArr {
- contractPermission := new(CompanyContractPermission)
- contractPermission.CompanyId = companyId
- contractPermission.CompanyContractId = companyContractId
- contractPermission.ChartPermissionId = chartPermissionId
- contractPermission.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- contractPermission.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- contractPermission.ModifyTime = time.Now()
- contractPermission.CreateTime = time.Now()
- tmpErr := AddCompanyContractPermission(contractPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //新增日志
- {
- itemLog := new(CompanyContractPermissionLog)
- itemLog.CompanyId = companyId
- itemLog.CompanyContractId = companyContractId
- itemLog.ChartPermissionId = strconv.Itoa(chartPermissionId)
- itemLog.StartDate = contractDetail.StartDate.Format(utils.FormatDate)
- itemLog.EndDate = contractDetail.EndDate.Format(utils.FormatDate)
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- go AddCompanyContractPermissionLog(itemLog)
- }
- }
- sql = `UPDATE company_approval SET approve_status = '待审批',modify_time=NOW(),is_formal=? WHERE company_approval_id = ? `
- _, err = to.Raw(sql, isFormal, companyApprovalId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, companyId, productId).Exec()
- return
- }
- // ModifyCompanyApplyServiceUpdate 修改申请信息
- func ModifyCompanyApplyServiceUpdate(item *CompanyApplyServiceUpdateReq, productId, companyContractId, isFormal int, mapPermissionIdUpgrade, mapPermissionIdExpensive map[int]int) (err error) {
- o := orm.NewOrm()
- to, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- tmpErr := to.Rollback()
- if tmpErr != nil {
- go alarm_msg.SendAlarmMsg("ModifyCompanyApplyServiceUpdate 事务回滚失败,Err:"+tmpErr.Error(), 3)
- }
- } else {
- err = to.Commit()
- }
- }()
- sql := `UPDATE company_contract
- SET
- start_date = ?,
- end_date = ?,
- money = ?,
- pay_method = ?,
- pay_channel = ?,
- img_url = ?,package_type = ?,rai_package_type = ?,
- modify_time = NOW()
- WHERE company_contract_id = ? `
- _, err = to.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, item.PackageType, item.RaiPackageType, companyContractId).Exec()
- if err != nil {
- return
- }
- sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
- _, err = to.Raw(sql, companyContractId).Exec()
- if err != nil {
- return
- }
- permissionArr := strings.Split(item.PermissionIds, ",")
- for _, v := range permissionArr {
- contractPermission := new(CompanyContractPermission)
- contractPermission.CompanyId = item.CompanyId
- contractPermission.CompanyContractId = companyContractId
- chartPermissionId, tmpErr := strconv.Atoi(v)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- contractPermission.ChartPermissionId = chartPermissionId
- contractPermission.StartDate = item.StartDate
- contractPermission.EndDate = item.EndDate
- contractPermission.ModifyTime = time.Now()
- contractPermission.CreateTime = time.Now()
- // 升级/买方研选5w
- p, _ := strconv.Atoi(v)
- contractPermission.IsUpgrade = mapPermissionIdUpgrade[p]
- contractPermission.ExpensiveYx = mapPermissionIdExpensive[p]
- tmpErr = AddCompanyContractPermission(contractPermission)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- //新增日志
- {
- itemLog := new(CompanyContractPermissionLog)
- itemLog.CompanyId = item.CompanyId
- itemLog.CompanyContractId = companyContractId
- itemLog.ChartPermissionId = v
- itemLog.StartDate = item.StartDate
- itemLog.EndDate = item.EndDate
- itemLog.ModifyTime = time.Now()
- itemLog.CreateTime = time.Now()
- itemLog.LogCreateTime = time.Now()
- go AddCompanyContractPermissionLog(itemLog)
- }
- }
- sql = `UPDATE company_approval SET approve_status = '待审批',modify_time=NOW(),is_formal=? WHERE company_approval_id = ? `
- _, err = to.Raw(sql, isFormal, item.CompanyApprovalId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
- _, err = to.Raw(sql, item.CompanyId, productId).Exec()
- return
- }
- // GetCompanyTryDelayApprovalCount 获取客户延期申请次数
- func GetCompanyTryDelayApprovalCount(companyId, productId int) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND approve_content='试用延期' `
- o := orm.NewOrm()
- err = o.Raw(sql, companyId, productId).QueryRow(&count)
- return
- }
- // GetFreezeToTryCompanyApprovalCount 客户冻结转试用次数(某个销售下)
- func GetFreezeToTryCompanyApprovalCount(companyId, productId, applyUserId int) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND approve_content='冻结转试用' and apply_user_id = ? `
- o := orm.NewOrm()
- err = o.Raw(sql, companyId, productId, applyUserId).QueryRow(&count)
- return
- }
- // GetCompanyApprovalReportRecordGroupList 获取销售分组数据
- func GetCompanyApprovalReportRecordGroupList(condition string, pars []interface{}) (list []*CompanyReportRecordGroup, err error) {
- o := orm.NewOrm()
- 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
- FROM company_approval a join company_contract b on a.company_contract_id=b.company_contract_id
- WHERE 1=1 and a.company_contract_id !=0
- `
- if condition != "" {
- sql += condition
- }
- sql += ` GROUP BY a.apply_user_id`
- _, err = o.Raw(sql, pars).QueryRows(&list)
- return
- }
- // GetCompanyApprovalCount 客户申请的次数
- func GetCompanyApprovalCount(companyId, productId, applyMethod int) (count int, err error) {
- sql := `SELECT COUNT(1) AS count FROM company_approval WHERE company_id=? AND product_id=? AND apply_method= ? `
- o := orm.NewOrm()
- err = o.Raw(sql, companyId, productId, applyMethod).QueryRow(&count)
- return
- }
- type RenewCompanyGroup struct {
- SellerId int `description:"所属销售id"`
- //AdminName string `description:"所属销售名称"`
- Num int `description:"汇总次数"`
- CompanyIds string `description:"客户id字符串"`
- }
- // GetNotRenewCompanyGroupList 获取销售未续约数据
- func GetNotRenewCompanyGroupList(productId int) (list []*RenewCompanyGroup, err error) {
- o := orm.NewOrm()
- sql := `SELECT
- cp.seller_id,
- count(DISTINCT(cp.company_id)) num,
- GROUP_CONCAT(DISTINCT cp.company_id SEPARATOR ',') AS company_ids
- FROM
- company_product AS cp
- INNER JOIN company_operation_record AS op
- WHERE
- op.company_id = cp.company_id
- AND cp.product_id = ?
- AND op.product_id = ?
- AND cp.STATUS IN ( "冻结", "流失" )
- AND op.operation = "try_out"
- `
- //if condition != "" {
- // sql += condition
- //}
- sql += ` GROUP BY cp.seller_id `
- _, err = o.Raw(sql, productId, productId).QueryRows(&list)
- return
- }
- // GetRenewFollowCompanyGroupList 获取销售续约跟进数据
- func GetRenewFollowCompanyGroupList(ids string, productId int) (list []*RenewCompanyGroup, err error) {
- o := orm.NewOrm()
- sql := `SELECT
- cp.seller_id,
- count(DISTINCT(cp.company_id)) num,
- GROUP_CONCAT(DISTINCT cp.company_id SEPARATOR ',') AS company_ids
- FROM
- company_product AS cp
- INNER JOIN company_operation_record AS op
- WHERE
- op.company_id = cp.company_id
- AND cp.product_id = ?
- AND op.product_id = ?
- AND cp.STATUS="试用"
- AND op.operation = "try_out"
- AND cp.company_id NOT IN (` + ids + `)
- `
- //if condition != "" {
- // sql += condition
- //}
- sql += ` GROUP BY cp.seller_id `
- _, err = o.Raw(sql, productId, productId).QueryRows(&list)
- return
- }
- type CompanyApproveCreateTime struct {
- CompanyId string
- CreateTime time.Time
- }
- // GetNotRenewFollowCompanyGroupList 获取历史经历过冻结或流失的客户
- func GetNotRenewFollowCompanyGroupList(productId int) (list []*CompanyApproveCreateTime, err error) {
- o := orm.NewOrm()
- sql := ` SELECT * FROM(
- SELECT
- cp.company_id,op.create_time
- FROM
- company_product AS cp
- INNER JOIN company_operation_record AS op
- WHERE
- op.company_id = cp.company_id
- AND cp.product_id = ?
- AND op.product_id = ?
- AND op.operation IN ( "freeze", "loss" )
- GROUP BY cp.company_id,op.create_time DESC
- )as a GROUP BY a.company_id
- `
- //sql += ` GROUP BY cp.seller_id `
- _, err = o.Raw(sql, productId, productId).QueryRows(&list)
- return
- }
- // GetCompanyApproveCreateTimeList 获取客户本轮合同开始时间
- func GetCompanyApproveCreateTimeList(productId int) (list []*CompanyApproveCreateTime, err error) {
- o := orm.NewOrm()
- sql := `SELECT * FROM(
- SELECT
- cp.company_id,op.create_time
- FROM
- company_product AS cp
- INNER JOIN company_operation_record AS op
- WHERE
- op.company_id = cp.company_id
- AND cp.product_id = ?
- AND op.product_id = ?
- AND op.operation = "try_out"
- GROUP BY cp.company_id,op.create_time DESC
- )as a GROUP BY a.company_id
- `
- //sql += ` GROUP BY cp.seller_id `
- _, err = o.Raw(sql, productId, productId).QueryRows(&list)
- return
- }
- // GetExpiresCompanyGroupList 获取销售到期数据
- func GetExpiresCompanyGroupList(startDate, endDate time.Time, productId int) (list []*RenewCompanyGroup, err error) {
- o := orm.NewOrm()
- sql := `SELECT
- seller_id,
- count(
- DISTINCT ( company_id )) num,
- GROUP_CONCAT( DISTINCT company_id SEPARATOR ',' ) AS company_ids
- FROM
- company_product
- WHERE
- product_id=?
- AND
- contract_end_date BETWEEN ?
- AND ?
- GROUP BY
- seller_id
- `
- _, err = o.Raw(sql, productId, startDate, endDate).QueryRows(&list)
- return
- }
- // GetRenewCompanyGroupList 获取销售续约数据
- func GetRenewCompanyGroupList(startDate, endDate time.Time, contractId string) (list []*RenewCompanyGroup, err error) {
- o := orm.NewOrm()
- sql := ` SELECT
- p.seller_id,
- count(
- DISTINCT ( c.company_id )) num,
- GROUP_CONCAT( DISTINCT c.company_id SEPARATOR ',' ) AS company_ids
- FROM
- company_product as p
- INNER JOIN company_contract as c
- WHERE
- c.modify_time BETWEEN ?
- AND ?
- AND c.company_product_id = p.company_product_id
- AND c.status = 1
- AND c.company_contract_id IN (` + contractId + `)
- GROUP BY
- p.seller_id;
- `
- _, err = o.Raw(sql, startDate, endDate).QueryRows(&list)
- return
- }
|