12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373 |
- package company_approval
- import (
- "github.com/beego/beego/v2/client/orm"
- "hongze/hongze_mobile_admin/models/tables/admin"
- "hongze/hongze_mobile_admin/models/tables/company_delay_permission"
- "hongze/hongze_mobile_admin/models/tables/company_permission_log"
- "hongze/hongze_mobile_admin/models/tables/company_report_permission"
- "hongze/hongze_mobile_admin/services/alarm_msg"
- "hongze/hongze_mobile_admin/utils"
- "time"
- )
- // CompanyApproval 客户单
- 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"`
- }
- // AddCompanyApproval 新增客户产品
- func AddCompanyApproval(item *CompanyApproval) (lastId int, err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- var newId int64
- newId, err = tx.Insert(item)
- lastId = int(newId)
- if err != nil {
- return
- }
- sql := `UPDATE company_product SET approve_status=? WHERE company_id=? AND product_id=? `
- _, err = tx.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
- }
- // GetCompanyApprovalByCompanyId 根据客户编号、产品编号获取客户单
- func GetCompanyApprovalByCompanyId(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
- }
- // GetCompanyApprovalById 根据审批单id获取审批单详情
- func GetCompanyApprovalById(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
- }
- // GetCompanyApprovalByApprovalId 根据客户单id获取客户单
- 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
- }
- // CompanyApprovalList 客户审批单列表
- 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:"提交申请时间"`
- ApproveUserId int `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:"申请人姓名"`
- ApproveRoleTypeCode string `description:"审批人角色类型" json:"-"`
- OpButton bool `description:"是否审批操作权限"`
- CurrNodeId int `description:"当前审批操作节点"`
- ApplyContractType int `description:"申请合同类型 0 非标 1 标准"`
- ApplyUserId int `description:"申请人编号id" json:"-"`
- ContractSourceTag string `description:"合同来源标签:非标合同,标准合同"`
- }
- // GetApprovalList 获取客户单列表
- 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.approve_role_type_code,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
- }
- // GetApprovalInfoByApprovalId 根据客户单id获取客户客户审批单信息
- func GetApprovalInfoByApprovalId(condition string, pars []interface{}, companyApprovalId int) (item *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.approve_role_type_code,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 AND company_approval_id=?`
- if condition != "" {
- sql += condition
- }
- sql += ` ORDER BY c.modify_time DESC `
- err = o.Raw(sql, companyApprovalId, pars).QueryRow(&item)
- 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 []*company_report_permission.CompanyReportPermission, err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.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 = tx.Raw(sql, startDate, endDate, raiPackageType, companyId, productId).Exec()
- if err != nil {
- return
- }
- } else {
- _, err = tx.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 = tx.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 = tx.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- items := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(company_permission_log.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 company_permission_log.AddCompanyPermissionLog(cpLog)
- }
- sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?`
- _, err = tx.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- contractPermission := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
- _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
- if err != nil {
- return
- }
- for _, pv := range contractPermission {
- tmpCompanyReportPermission := &company_report_permission.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 := tx.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 = tx.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 = tx.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 []*company_report_permission.CompanyReportPermission, err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- //123月份申请转正通过时,将试用期限延长(更新)至4.30。456月份申请转正通过时,将试用期限延长至7.31。789月份申请转正通过时,将试用期限延长至10.31。101112月份申请转正通过时,将试用期限延长至次年1.31。
- endDate = utils.GetLastDayOfQuarter(time.Now()).Format(utils.FormatDate)
- // 当套餐类型为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='已审批',end_date=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? `
- _, err = tx.Raw(sql, endDate, raiPackageType, companyId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company SET type=1,last_updated_time=NOW() WHERE company_id=? `
- _, err = tx.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 = tx.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- items := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(company_permission_log.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 company_permission_log.AddCompanyPermissionLog(cpLog)
- }
- sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?`
- _, err = tx.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- contractPermission := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
- _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
- if err != nil {
- return
- }
- for _, pv := range contractPermission {
- tmpCompanyReportPermission := &company_report_permission.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 := tx.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 = tx.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 = tx.Raw(sql, companyContractId, companyId, productId).Exec()
- if err != nil {
- return
- }
- return
- }
- // FreezeToTryOut 冻结转试用
- func FreezeToTryOut(companyId, productId, sellerId, companyApprovalId, applyUserId int, sellerName, productName string) (newCompanyReportPermissionList []*company_report_permission.CompanyReportPermission, startDate, endDate string, err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- startDate = time.Now().Format(utils.FormatDate)
- endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- //sellerItem, err := admin.GetAdminById(applyUserId)
- //if err != nil {
- // return
- //}
- //更新用户产品状态
- //6.9 fix 未续约强制弹窗要求客户冻结转试用后不弹床,取消清空冻结时间以标记曾经被冻结过
- sql := `UPDATE company_product SET status='试用',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 = tx.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 = tx.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 = tx.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
- items := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(company_permission_log.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 company_permission_log.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 = tx.Raw(sql, startDate, endDate, companyId, productId).Exec()
- //if err != nil {
- // return
- //}
- //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
- count, _ := company_report_permission.GetCompanyReportPermissionCount(companyId, productId)
- if count > 0 {
- sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- }
- //获取需要试用的权限
- delayPermissionItems, err := company_delay_permission.GetDelayPermissionItems(companyId, companyApprovalId)
- if err != nil {
- return
- }
- status := `试用`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission := &company_report_permission.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 := tx.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 = tx.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, v.EndDate, productId, productName, status).Exec()
- //if err != nil {
- // return err
- //}
- }
- return
- }
- // FreezeToTryOutXClassRai 权益冻结转X类试用
- func FreezeToTryOutXClassRai(companyId, productId, sellerId, companyApprovalId, applyUserId int, sellerName, productName string) (newCompanyReportPermissionList []*company_report_permission.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([]*company_report_permission.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(company_permission_log.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 company_permission_log.AddCompanyPermissionLog(cpLog)
- }
- //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
- count, _ := company_report_permission.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 := company_delay_permission.GetDelayPermissionItems(companyId, companyApprovalId)
- if tmpErr != nil {
- err = tmpErr
- return
- }
- status := `永续`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission := &company_report_permission.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 []*company_report_permission.CompanyReportPermission, newEndDate string, err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.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='已审批',end_date=?,modify_time=NOW() WHERE company_id=? AND product_id=? `
- _, err = tx.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 = tx.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
- items := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(company_permission_log.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 company_permission_log.AddCompanyPermissionLog(cpLog)
- }
- //获取需要延期的权限
- delayPermissionItems, err := company_delay_permission.GetDelayPermissionItems(companyId, companyApprovalId)
- if err != nil {
- return
- }
- status := `试用`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission, tmpErr := company_report_permission.GetCompanyPermission(companyId, v.ChartPermissionId)
- if tmpErr != nil {
- //没有数据的话
- if tmpErr.Error() == utils.ErrNoRow() {
- tmpCompanyReportPermission = &company_report_permission.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 := tx.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 = tx.Update(tmpCompanyReportPermission, "EndDate", "Status", "ModifyTime")
- if err != nil {
- return
- }
- }
- newCompanyReportPermissionList = append(newCompanyReportPermissionList, tmpCompanyReportPermission)
- //isExist, err := company_report_permission.GetCompanyPermissionCheck(companyId, v.ChartPermissionId)
- //if err != nil {
- // return err
- //}
- //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 = tx.Raw(sql, newEndDate, status, companyId, productId, v.ChartPermissionId).Exec()
- // if err != nil {
- // return err
- // }
- //} 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 = tx.Raw(sql, companyId, v.ChartPermissionId, v.ChartPermissionId, v.StartDate, newEndDate, productId, productName, status).Exec()
- // if err != nil {
- // return err
- // }
- //}
- }
- return
- }
- // ApplyReceive 原销售申请领取
- func ApplyReceive(companyId, productId, sysUserId, companyApprovalId, applyUserId int, sysUserName, productName string) (newCompanyReportPermissionList []*company_report_permission.CompanyReportPermission, startDate, endDate string, err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- sellerItem, err := admin.GetAdminById(applyUserId)
- if err != nil {
- return
- }
- startDate = time.Now().Format(utils.FormatDate)
- endDate = time.Now().AddDate(0, 2, 0).Format(utils.FormatDate)
- //更新用户产品状态
- sql := `UPDATE company_product SET status='试用',approve_status='已审批',try_out_time=NULL,last_description_time=NULL, 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 = tx.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 = tx.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 = tx.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //查询该用户所有产品权限,并把当前数据做日志(允许添加不修改的数据)
- items := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
- for _, pv := range items {
- cpLog := new(company_permission_log.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 company_permission_log.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 = tx.Raw(sql, startDate, endDate, 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 = tx.Raw(sql, sellerItem.AdminId, sellerItem.RealName, companyId, productId).Exec()
- //查询原先是否已经存在权限,如果有权限了,那么需要先删除原有的权限
- count, _ := company_report_permission.GetCompanyReportPermissionCount(companyId, productId)
- if count > 0 {
- sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- }
- //获取需要试用的权限
- delayPermissionItems, err := company_delay_permission.GetDelayPermissionItems(companyId, companyApprovalId)
- if err != nil {
- return
- }
- status := `试用`
- for _, v := range delayPermissionItems {
- tmpCompanyReportPermission := &company_report_permission.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 := tx.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 = tx.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 []*company_report_permission.CompanyReportPermission, err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- items := make([]*company_report_permission.CompanyReportPermission, 0)
- sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
- if err != nil {
- return
- }
- //已存在的权限map
- nowCompanyReportPermissionMap := make(map[int]*company_report_permission.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(company_permission_log.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 company_permission_log.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 = tx.Raw(sql, companyId, productId).Exec()
- //if err != nil {
- // return
- //}
- //更新客户产品信息
- if packageType == 0 {
- // 套餐类型为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 = tx.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 = tx.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 = tx.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- contractPermission := make([]*company_report_permission.CompanyReportPermission, 0)
- sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
- _, err = tx.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 = tx.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 := tx.Raw(sql, pv.EndDate, pv.CompanyContractId, "正式", nowPermission.CompanyReportPermissionId).Exec()
- //err = tmpErr
- //if err != nil {
- // return
- //}
- }
- } else {
- nowPermission = &company_report_permission.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 := tx.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 := tx.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,modify_time=NOW() WHERE company_contract_id=? AND company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyContractId, companyId, productId).Exec()
- if err != nil {
- return
- }
- return
- }
- // ApproveRefuse 审批拒绝
- func ApproveRefuse(companyId, productId int, approveStatus, approveRemark string) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- sql := `UPDATE company_product
- SET
- approve_status = '驳回',
- modify_time=NOW()
- WHERE company_id = ? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).Exec()
- if err != nil {
- return
- }
- sql = ` UPDATE company_approval
- SET
- approve_status = '驳回',
- approve_remark=?,
- approve_time=NOW(),
- modify_time=NOW()
- WHERE company_id = ? AND product_id=? AND approve_status='待审批' `
- _, err = tx.Raw(sql, approveRemark, companyId, productId).Exec()
- return
- }
- // ApplyApproveContract 更新审批单、合同状态
- func ApplyApproveContract(companyId, productId, companyApprovalId, companyContractId int) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- sql := `UPDATE company_product SET approve_status='已审批',modify_time=NOW() WHERE company_id=? AND product_id=? `
- _, err = tx.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 = tx.Raw(sql, companyApprovalId, companyId, productId).Exec()
- if err != nil {
- return
- }
- //更新合同
- sql = `UPDATE company_contract SET status=1,modify_time=NOW() WHERE company_contract_id=? AND company_id=? AND product_id=? `
- _, err = tx.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
- }
- // ModifyCompanyApproval 修改申请信息
- func ModifyCompanyApproval(companyId, productId, companyApprovalId int, approveContent, applyReasons string) (err error) {
- o := orm.NewOrm()
- tx, err := o.Begin()
- if err != nil {
- return
- }
- defer func() {
- if err != nil {
- _ = tx.Rollback()
- } else {
- _ = tx.Commit()
- }
- }()
- sql := `UPDATE company_approval SET approve_status = '待审批', approve_content=?,apply_reasons=?,modify_time=NOW() WHERE company_approval_id = ? `
- _, err = tx.Raw(sql, approveContent, applyReasons, companyApprovalId).Exec()
- if err != nil {
- return
- }
- sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
- _, err = tx.Raw(sql, companyId, productId).Exec()
- utils.FileLog.Info("试用延期申请:Err %s,%s,%d", sql, companyId, productId)
- return
- }
- ////修改申请信息
- //func ModifyCompanyTurnPositive(item *CompanyApplyTurnPositiveReq, productId, companyContractId int) (err error) {
- // o := orm.NewOrm()
- // o.Begin()
- // defer func() {
- // if err != nil {
- // o.Rollback()
- // } else {
- // o.Commit()
- // }
- // }()
- // sql := `UPDATE company_contract
- // SET
- // start_date = ?,
- // end_date = ?,
- // money = ?,
- // pay_method = ?,
- // pay_channel = ?,
- // img_url = ?,
- // modify_time = NOW()
- // WHERE company_contract_id = ? `
- // _, err = o.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, companyContractId).Exec()
- // if err != nil {
- // return
- // }
- //
- // sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
- // _, err = o.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
- // contractPermission.ChartPermissionId = v
- // contractPermission.StartDate = item.StartDate
- // contractPermission.EndDate = item.EndDate
- // contractPermission.ModifyTime = time.Now()
- // contractPermission.CreateTime = time.Now()
- // err = AddCompanyContractPermission(contractPermission)
- // if err != nil {
- // 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() WHERE company_approval_id = ? `
- // _, err = o.Raw(sql, item.CompanyApprovalId).Exec()
- // if err != nil {
- // return
- // }
- // sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
- // _, err = o.Raw(sql, item.CompanyId, productId).Exec()
- // return
- //}
- //
- ////修改申请信息
- //func ModifyCompanyApplyServiceUpdate(item *CompanyApplyServiceUpdateReq, productId, companyContractId int) (err error) {
- // o := orm.NewOrm()
- // o.Begin()
- // defer func() {
- // if err != nil {
- // o.Rollback()
- // } else {
- // o.Commit()
- // }
- // }()
- // sql := `UPDATE company_contract
- // SET
- // start_date = ?,
- // end_date = ?,
- // money = ?,
- // pay_method = ?,
- // pay_channel = ?,
- // img_url = ?,
- // modify_time = NOW()
- // WHERE company_contract_id = ? `
- // _, err = o.Raw(sql, item.StartDate, item.EndDate, item.Money, item.PayMethod, item.PayChannel, item.ImgUrl, companyContractId).Exec()
- // if err != nil {
- // return
- // }
- //
- // sql = `DELETE FROM company_contract_permission WHERE company_contract_id = ? `
- // _, err = o.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
- // contractPermission.ChartPermissionId = v
- // contractPermission.StartDate = item.StartDate
- // contractPermission.EndDate = item.EndDate
- // contractPermission.ModifyTime = time.Now()
- // contractPermission.CreateTime = time.Now()
- // err = AddCompanyContractPermission(contractPermission)
- // if err != nil {
- // 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() WHERE company_approval_id = ? `
- // _, err = o.Raw(sql, item.CompanyApprovalId).Exec()
- // if err != nil {
- // return
- // }
- // sql = `UPDATE company_product SET approve_status='待审批' WHERE company_id=? AND product_id=? `
- // _, err = o.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
- }
- // GetApprovalListByApprovalIds 根据申请id字符串来获取申请单列表
- func GetApprovalListByApprovalIds(companyApprovalIds string) (list []*CompanyApproval, err error) {
- o := orm.NewOrm()
- sql := `SELECT * from company_approval WHERE company_approval_id in (` + companyApprovalIds + `)`
- _, err = o.Raw(sql).QueryRows(&list)
- return
- }
- type CompanyNameList struct {
- CompanyName string `description:"客户名称,甲方名称,长度32位"`
- }
- // GetCompanyNameList 获取客户名称列表数据
- func GetCompanyNameListOld(userId int, userRoleTypeCode, keyword string) (list []*CompanyNameList, err error) {
- o := orm.NewOrm()
- sql := `SELECT a.company_name 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 (c.apply_user_id=? or ( (c.approve_user_id =? or c.approve_user_id = 0) and approve_role_type_code=?) ) and( a.company_name like '%` + keyword + `%' or a.credit_code like '%` + keyword + `%') and c.approve_status !="已撤回"
- group by a.company_name order by c.modify_time DESC `
- _, err = o.Raw(sql, userId, userId, userRoleTypeCode).QueryRows(&list)
- return
- }
- // GetCompanyNameList 获取客户名称列表数据
- func GetCompanyNameList(userId int, keyword string) (list []*CompanyNameList, err error) {
- o := orm.NewOrm()
- sql := `SELECT e.company_name from contract_approval a
- join ( SELECT max( contract_approval_id ) max_id,contract_id FROM contract_approval where 1=1 and approval_type="company" GROUP BY contract_id ) b on a.contract_approval_id=b.max_id
- INNER JOIN company_approval AS c ON a.contract_id=c.company_approval_id
- INNER JOIN company_product AS d ON d.company_id=c.company_id AND d.product_id=c.product_id
- INNER JOIN company AS e ON e.company_id=d.company_id
- JOIN contract_approval_record f on a.contract_approval_id=f.contract_approval_id
- WHERE 1=1 and a.approval_type="company" AND (c.apply_user_id = ? or (f.approve_user_id = ? and f.node_id <= a.curr_node_id) ) AND c.approve_status !="已撤回" and( e.company_name like '%` + keyword + `%' or e.credit_code like '%` + keyword + `%') group by company_name`
- _, err = o.Raw(sql, userId, userId).QueryRows(&list)
- return
- }
|