company.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package company
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_task/models"
  5. "hongze/hongze_task/models/company_contract"
  6. "hongze/hongze_task/utils"
  7. "time"
  8. )
  9. // TryOutToFormal 试用转正式
  10. func TryOutToFormal(companyId, productId, sellerId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (err error) {
  11. o := orm.NewOrm()
  12. tx, err := o.Begin()
  13. if err != nil {
  14. return err
  15. }
  16. defer func() {
  17. if err != nil {
  18. tx.Rollback()
  19. } else {
  20. tx.Commit()
  21. }
  22. }()
  23. // 套餐类型为0时, 不更新套餐类型
  24. 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,start_date=?,end_date=?,package_type=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  25. if packageType == 0 {
  26. 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,start_date=?,end_date=?,rai_package_type=?,modify_time=NOW(),formal_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  27. _, err = tx.Raw(sql, startDate, endDate, raiPackageType, companyId, productId).Exec()
  28. if err != nil {
  29. return
  30. }
  31. } else {
  32. _, err = tx.Raw(sql, startDate, endDate, packageType, raiPackageType, companyId, productId).Exec()
  33. if err != nil {
  34. return
  35. }
  36. }
  37. sql = `UPDATE company SET type=1,last_updated_time=NOW() WHERE company_id=? `
  38. _, err = tx.Raw(sql, companyId).Exec()
  39. if err != nil {
  40. return
  41. }
  42. items := make([]*models.CompanyReportPermission, 0)
  43. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  44. _, err = o.Raw(sql, companyId, productId).QueryRows(&items)
  45. for _, pv := range items {
  46. cpLog := new(models.CompanyPermissionLog)
  47. cpLog.CompanyId = companyId
  48. cpLog.ChartPermissionId = pv.ChartPermissionId
  49. cpLog.CreateTime = time.Now()
  50. cpLog.SysUserId = sellerId
  51. cpLog.SysUserName = sellerName
  52. cpLog.StartDate = pv.StartDate
  53. cpLog.EndDate = pv.EndDate
  54. cpLog.ProductId = productId
  55. cpLog.ProductName = pv.ProductName
  56. go models.AddCompanyPermissionLog(cpLog)
  57. }
  58. sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=?`
  59. _, err = tx.Raw(sql, companyId, productId).Exec()
  60. if err != nil {
  61. return
  62. }
  63. contractPermission := make([]*models.CompanyReportPermission, 0)
  64. sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
  65. _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
  66. if err != nil {
  67. return
  68. }
  69. for _, pv := range contractPermission {
  70. sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  71. chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status,is_upgrade,expensive_yx)
  72. VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?,?,?) `
  73. _, err = tx.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式", pv.IsUpgrade, pv.ExpensiveYx).Exec()
  74. if err != nil {
  75. return
  76. }
  77. }
  78. return
  79. }
  80. // 服务更新
  81. func ApplyServiceUpdateOld(companyId, productId, sellerId, companyContractId int, startDate, endDate, sellerName, productName string) (err error) {
  82. o := orm.NewOrm()
  83. tx, err := o.Begin()
  84. if err != nil {
  85. return err
  86. }
  87. defer func() {
  88. if err != nil {
  89. tx.Rollback()
  90. } else {
  91. tx.Commit()
  92. }
  93. }()
  94. sql := `UPDATE company_product SET status='正式',start_date=?,end_date=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  95. _, err = tx.Raw(sql, startDate, endDate, companyId, productId).Exec()
  96. if err != nil {
  97. return
  98. }
  99. //合同权限变更
  100. items := make([]*models.CompanyReportPermission, 0)
  101. sql = `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  102. _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
  103. if err != nil {
  104. return
  105. }
  106. for _, pv := range items {
  107. cpLog := new(models.CompanyPermissionLog)
  108. cpLog.CompanyId = companyId
  109. cpLog.ChartPermissionId = pv.ChartPermissionId
  110. cpLog.CreateTime = time.Now()
  111. cpLog.SysUserId = sellerId
  112. cpLog.SysUserName = sellerName
  113. cpLog.StartDate = pv.StartDate
  114. cpLog.EndDate = pv.EndDate
  115. cpLog.ProductId = productId
  116. cpLog.ProductName = pv.ProductName
  117. go models.AddCompanyPermissionLog(cpLog)
  118. }
  119. sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  120. _, err = tx.Raw(sql, companyId, productId).Exec()
  121. if err != nil {
  122. return
  123. }
  124. contractPermission := make([]*models.CompanyReportPermission, 0)
  125. sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
  126. _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
  127. if err != nil {
  128. return
  129. }
  130. for _, pv := range contractPermission {
  131. sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  132. chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status)
  133. VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?) `
  134. _, err = tx.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式").Exec()
  135. if err != nil {
  136. return
  137. }
  138. }
  139. return
  140. }
  141. func ApplyServiceUpdate(companyId, productId, sellerId, companyContractId int, startDate, endDate, sellerName, productName string, packageType, raiPackageType int) (err error) {
  142. o := orm.NewOrm()
  143. tx, err := o.Begin()
  144. if err != nil {
  145. return err
  146. }
  147. defer func() {
  148. if err != nil {
  149. tx.Rollback()
  150. } else {
  151. tx.Commit()
  152. }
  153. }()
  154. //合同权限变更
  155. items := make([]*models.CompanyReportPermission, 0)
  156. sql := `SELECT * FROM company_report_permission WHERE company_id=? AND product_id=? `
  157. _, err = tx.Raw(sql, companyId, productId).QueryRows(&items)
  158. if err != nil {
  159. return
  160. }
  161. //已存在的权限map
  162. nowCompanyReportPermissionMap := make(map[int]*models.CompanyReportPermission)
  163. //产品服务的开始、结束日期(非产品权限)
  164. updateStartDate := startDate
  165. updateStartDateTime, err := time.ParseInLocation(utils.FormatDate, updateStartDate, time.Local)
  166. if err != nil {
  167. return
  168. }
  169. updateEndDate := endDate
  170. updateEndDateTime, err := time.ParseInLocation(utils.FormatDate, updateEndDate, time.Local)
  171. if err != nil {
  172. return
  173. }
  174. for _, pv := range items {
  175. cpLog := new(models.CompanyPermissionLog)
  176. cpLog.CompanyId = companyId
  177. cpLog.ChartPermissionId = pv.ChartPermissionId
  178. cpLog.CreateTime = time.Now()
  179. cpLog.SysUserId = sellerId
  180. cpLog.SysUserName = sellerName
  181. cpLog.StartDate = pv.StartDate
  182. cpLog.EndDate = pv.EndDate
  183. cpLog.ProductId = productId
  184. cpLog.ProductName = pv.ProductName
  185. go models.AddCompanyPermissionLog(cpLog)
  186. //将权限插入到已存在的权限map中
  187. nowCompanyReportPermissionMap[pv.ChartPermissionId] = pv
  188. //校验原始数据中的开始日期是否小于合同内的开始日期,如果小于,那么变更为原先的合同开始日期
  189. tmpStartDate, tmpErr := time.ParseInLocation(utils.FormatDate, pv.StartDate, time.Local)
  190. if tmpErr != nil {
  191. err = tmpErr
  192. return
  193. }
  194. if tmpStartDate.Before(updateStartDateTime) {
  195. updateStartDateTime = tmpStartDate
  196. }
  197. //校验原始数据中的结束日期是否大于合同内的结束日期,如果大于,那么变更为原先的合同结束日期
  198. tmpEndDate, tmpErr := time.ParseInLocation(utils.FormatDate, pv.EndDate, time.Local)
  199. if tmpErr != nil {
  200. err = tmpErr
  201. return
  202. }
  203. if tmpEndDate.After(updateEndDateTime) {
  204. updateEndDateTime = tmpEndDate
  205. }
  206. }
  207. //删除所有权限
  208. //sql = `DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  209. //_, err = o.Raw(sql, companyId, productId).Exec()
  210. //if err != nil {
  211. // return
  212. //}
  213. //更新客户产品信息
  214. if packageType == 0 {
  215. // 套餐类型为0时, 不更新套餐类型
  216. sql = `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,is_formal=1,is_suspend=0,start_date=?,end_date=?,rai_package_type=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  217. _, err = tx.Raw(sql, updateStartDateTime, updateEndDateTime, raiPackageType, companyId, productId).Exec()
  218. if err != nil {
  219. return
  220. }
  221. } else {
  222. sql = `UPDATE company_product SET status='正式',try_out_time=NULL,last_description_time=NULL,freeze_time=NULL,is_formal=1,is_suspend=0,start_date=?,end_date=?,package_type=?,rai_package_type=?,modify_time=NOW(),try_stage=1 WHERE company_id=? AND product_id=? `
  223. _, err = tx.Raw(sql, updateStartDateTime, updateEndDateTime, packageType, raiPackageType, companyId, productId).Exec()
  224. if err != nil {
  225. return
  226. }
  227. }
  228. contractPermission := make([]*company_contract.CompanyContractPermission, 0)
  229. sql = `SELECT * FROM company_contract_permission WHERE company_contract_id=? AND company_id=? `
  230. _, err = tx.Raw(sql, companyContractId, companyId).QueryRows(&contractPermission)
  231. if err != nil {
  232. return
  233. }
  234. for _, pv := range contractPermission {
  235. nowPermission, ok := nowCompanyReportPermissionMap[pv.ChartPermissionId]
  236. //判断是否已经存在该权限,如果存在的话,那么是修改,否则是新增
  237. if ok {
  238. needUpdate := false
  239. if nowPermission.Status != "正式" && nowPermission.Status != "永续" {
  240. needUpdate = true
  241. }
  242. //如果 需要更新 字段 为false,那么再去校验时间
  243. if needUpdate == false {
  244. //如果当前存该权限,那么去校验是否需要修改
  245. nowPermissionEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, nowPermission.EndDate, time.Local)
  246. if tmpErr != nil {
  247. err = tmpErr
  248. return
  249. }
  250. contractPermissionEndDateTime, tmpErr := time.ParseInLocation(utils.FormatDate, pv.EndDate, time.Local)
  251. if tmpErr != nil {
  252. err = tmpErr
  253. return
  254. }
  255. if contractPermissionEndDateTime.After(nowPermissionEndDateTime) {
  256. needUpdate = true
  257. }
  258. }
  259. //如果 需要更新 字段 为 true,那么就去更新
  260. if needUpdate {
  261. sql = `update company_report_permission set end_date=?,last_updated_time=now(),modify_time=now(),company_contract_id=?,status=?,is_upgrade=?,expensive_yx=? where company_report_permission_id=? `
  262. _, tmpErr := tx.Raw(sql, pv.EndDate, companyContractId, "正式", pv.IsUpgrade, pv.ExpensiveYx, nowPermission.CompanyReportPermissionId).Exec()
  263. err = tmpErr
  264. if err != nil {
  265. return
  266. }
  267. }
  268. } else {
  269. sql = `INSERT INTO company_report_permission(company_id, report_permission_id,created_time, last_updated_time,
  270. chart_permission_id, start_date,end_date,product_id,product_name, modify_time,company_contract_id,status,is_upgrade,expensive_yx)
  271. VALUES(?,?,NOW(),NOW(),?,?,?,?,?,NOW(),?,?,?,?) `
  272. _, tmpErr := tx.Raw(sql, companyId, pv.ChartPermissionId, pv.ChartPermissionId, pv.StartDate, pv.EndDate, productId, productName, companyContractId, "正式", pv.IsUpgrade, pv.ExpensiveYx).Exec()
  273. err = tmpErr
  274. if err != nil {
  275. return
  276. }
  277. }
  278. }
  279. return
  280. }