company_approval.go 53 KB

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