company_contract.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package company_contract
  2. import (
  3. "github.com/beego/beego/v2/client/orm"
  4. "hongze/hongze_mobile_admin/models/tables/company_contract_permission"
  5. "hongze/hongze_mobile_admin/models/tables/company_report_permission"
  6. "strings"
  7. "time"
  8. )
  9. type CompanyContract struct {
  10. CompanyContractId int `orm:"column(company_contract_id);pk" description:"客户合同id"`
  11. ContractType string `description:"合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  12. ProductId int `description:"产品id"`
  13. ProductName string `description:"产品名称"`
  14. CompanyId int `description:"客户id"`
  15. CompanyProductId int `description:"客户产品id"`
  16. ContractCode string `description:"合同编码"`
  17. StartDate string `description:"合同开始时间"`
  18. EndDate string `description:"合同结束时间"`
  19. Money float64 `description:"合同金额"`
  20. PayMethod string `description:"支付方式"`
  21. PayChannel string `description:"支付渠道"`
  22. ImgUrl string `description:"合同图片,多个用#隔开"`
  23. CreateTime time.Time `description:"合同创建时间"`
  24. ModifyTime time.Time `description:"合同修改时间"`
  25. Status int `description:"状态"`
  26. Source string `description:"合同来源:上传附件, 系统合同"`
  27. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  28. }
  29. type CompanyContractDetail struct {
  30. CompanyContractId int
  31. CompanyId int `description:"客户id"`
  32. ProductId int `description:"产品id" json:"-"`
  33. ContractCode string `description:"合同编码"`
  34. ContractType string `description:"合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  35. StartDate string `description:"合同开始时间"`
  36. EndDate string `description:"合同结束时间"`
  37. Quarter string `description:"季度(X类试用客户使用)"`
  38. Money float64 `description:"合同金额"`
  39. PayMethod string `description:"支付方式"`
  40. PayChannel string `description:"支付渠道"`
  41. ImgUrl string `description:"合同图片,多个用#隔开"`
  42. CreateTime time.Time `description:"合同创建时间"`
  43. ModifyTime time.Time `description:"合同修改时间"`
  44. Status int `description:"状态"`
  45. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  46. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w套餐; 2-45w套餐"`
  47. Source string `description:"合同来源:上传附件, 系统合同"`
  48. PermissionList []*company_report_permission.PermissionLookList
  49. }
  50. func GetCompanyContractDetail(companyId, productId, companyContractId int) (item *CompanyContractDetail, err error) {
  51. o := orm.NewOrm()
  52. if companyContractId == 0 {
  53. sql := `SELECT * FROM company_contract WHERE company_id=? AND product_id=? AND status=0 `
  54. err = o.Raw(sql, companyId, productId).QueryRow(&item)
  55. } else {
  56. sql := `SELECT * FROM company_contract WHERE company_contract_id=? `
  57. err = o.Raw(sql, companyContractId).QueryRow(&item)
  58. }
  59. return
  60. }
  61. // GetCompanyContractPermissionCheckByContractId 根据合同id获取已经勾选的权限
  62. func GetCompanyContractPermissionCheckByContractId(companyId, companyContractId, permissionId int) (count int, err error) {
  63. o := orm.NewOrm()
  64. sql := `SELECT COUNT(1) AS count FROM company_contract_permission AS a WHERE a.company_id=? AND company_contract_id=? AND a.chart_permission_id=? `
  65. err = o.Raw(sql, companyId, companyContractId, permissionId).QueryRow(&count)
  66. return
  67. }
  68. // GetCompanyContractPermissionByCompanyContractId 根据合同ID获取开通的权限
  69. func GetCompanyContractPermissionByCompanyContractId(companyContractId int) (items []*company_contract_permission.CompanyContractPermission, err error) {
  70. o := orm.NewOrm()
  71. sql := `SELECT * FROM company_contract_permission WHERE company_contract_id =? ORDER BY modify_time DESC `
  72. _, err = o.Raw(sql, companyContractId).QueryRows(&items)
  73. return
  74. }
  75. func GetCompanyContractDetailByContractIds(contractIds string) (list []*CompanyContract, err error) {
  76. o := orm.NewOrm()
  77. sql := `SELECT * FROM company_contract WHERE company_contract_id in (` + contractIds + `)`
  78. _, err = o.Raw(sql).QueryRows(&list)
  79. return
  80. }
  81. // 根据合同id集合字符串获取权限列表数据
  82. type CompanyContractPermissionName struct {
  83. Id int `orm:"column(id);pk" description:"客户合同id"`
  84. CompanyId int `description:"客户id"`
  85. CompanyContractId int `description:"合同id"`
  86. ChartPermissionId int `description:"权限id"`
  87. ChartPermissionName string `description:"权限名称"`
  88. PermissionName string `description:"权限名称"`
  89. PermissionRemark string `description:"权限备注"`
  90. ClassifyName string `description:"权限分类名称"`
  91. StartDate string `description:"开始日期"`
  92. EndDate string `description:"结束日期"`
  93. CreateTime time.Time `json:"-" description:"创建时间"`
  94. ModifyTime time.Time `json:"-" description:"修改时间"`
  95. IsUpgrade int `description:"是否升级,1是,0否"`
  96. }
  97. // GetCompanyContractPermissionListByContractId 根据合同Id获取合同的权限列表
  98. func GetCompanyContractPermissionListByContractId(companyContractId int) (list []*CompanyContractPermissionName, err error) {
  99. o := orm.NewOrm()
  100. sql := `SELECT a.*,b.chart_permission_name,b.classify_name FROM company_contract_permission AS a left join chart_permission b on a.chart_permission_id = b.chart_permission_id WHERE company_contract_id =? `
  101. _, err = o.Raw(sql, companyContractId).QueryRows(&list)
  102. return
  103. }
  104. // 通过ID获取详情
  105. func GetCompanyContracDetail(condition string, pars []interface{}) (item *CompanyContractDetail, err error) {
  106. o := orm.NewOrm()
  107. sql := `SELECT * FROM company_contract WHERE 1= 1 ` + condition
  108. err = o.Raw(sql, pars).QueryRow(&item)
  109. return
  110. }
  111. // 获取列表详情
  112. func GetCompanyContracList(condition string, pars []interface{}) (items []*CompanyContractDetail, err error) {
  113. o := orm.NewOrm()
  114. sql := `SELECT * FROM company_contract WHERE 1= 1 ` + condition
  115. _, err = o.Raw(sql, pars).QueryRows(&items)
  116. return
  117. }
  118. // UpdateCompanyContractPackageDifference 更改合同与上一份合同金额对比信息
  119. func UpdateCompanyContractPackageDifference(packageDifference string, companyContractId int) (err error) {
  120. o := orm.NewOrm()
  121. sql := `UPDATE company_contract SET package_difference = ? WHERE company_contract_id=? `
  122. _, err = o.Raw(sql, packageDifference, companyContractId).Exec()
  123. return
  124. }
  125. // GetLastContractListByEndDate 通过最近一份合同的日期获取早于该合同的最晚一份合同
  126. func GetLastContractListByEndDate(companyId, productId int, endDate string) (item *CompanyContract, err error) {
  127. o := orm.NewOrm()
  128. sql := "SELECT * FROM company_contract where company_id = ? AND product_id= ? end_date < ? AND status = 1 ORDER BY end_date desc"
  129. err = o.Raw(sql, companyId, productId, endDate).QueryRow(&item)
  130. return
  131. }
  132. type CompanyContractRespInit struct {
  133. MaxCompanyContractId int
  134. CompanyContractId int `orm:"column(company_contract_id);pk" description:"客户合同id"`
  135. ContractType string `description:"合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  136. ProductId int `description:"产品id"`
  137. ProductName string `description:"产品名称"`
  138. CompanyId int `description:"客户id"`
  139. CompanyProductId int `description:"客户产品id"`
  140. ContractCode string `description:"合同编码"`
  141. StartDate string `description:"合同开始时间"`
  142. EndDate time.Time `description:"合同结束时间"`
  143. MaxStartDate time.Time `description:"合同结束时间"`
  144. Money float64 `description:"合同金额"`
  145. PayMethod string `description:"支付方式"`
  146. PayChannel string `description:"支付渠道"`
  147. ImgUrl string `description:"合同图片,多个用#隔开"`
  148. CreateTime time.Time `description:"合同创建时间"`
  149. ModifyTime time.Time `description:"合同修改时间"`
  150. Status int `description:"状态"`
  151. Source string `description:"合同来源,枚举值:上传附件、系统合同,默认上传附件"`
  152. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  153. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w大套餐; 2-45w大套餐"`
  154. PermissionName string `description:"权限名"`
  155. }
  156. // 获取最新的合同的开始时间
  157. func GetCompanyContracListMaxStartDate(condition string, pars []interface{}) (items []*CompanyContractRespInit, err error) {
  158. o := orm.NewOrm()
  159. sql := `SELECT
  160. MAX( a.start_date ) AS max_start_date,
  161. COUNT(a.company_id) AS cc ,
  162. a.*
  163. FROM
  164. company_contract a
  165. JOIN company b ON a.company_id = b.company_id
  166. JOIN company_product c ON a.company_id = c.company_id
  167. AND a.product_id = c.product_id
  168. WHERE
  169. 1 = 1
  170. AND c.product_id = 2
  171. AND a.not_renewal_hide = 0
  172. AND a.STATUS = 1
  173. AND a.company_contract_id NOT IN (SELECT company_contract_id FROM company_contract_no_renewed_ascribe )` + condition + ` GROUP BY a.company_id `
  174. _, err = o.Raw(sql, pars).QueryRows(&items)
  175. return
  176. }
  177. // 通过ID获取详情
  178. func GetCompanyContracListInit_CRM_16_1(condition string, pars []interface{}) (items []*CompanyContractRespInit, err error) {
  179. o := orm.NewOrm()
  180. sql := `SELECT
  181. a.*
  182. FROM
  183. company_contract a
  184. JOIN company b ON a.company_id = b.company_id
  185. JOIN company_product c ON a.company_id = c.company_id
  186. AND a.product_id = c.product_id
  187. WHERE
  188. 1 = 1
  189. AND c.product_id = 2
  190. AND a.not_renewal_hide = 0
  191. AND a.STATUS = 1
  192. AND a.company_contract_id NOT IN (SELECT company_contract_id FROM company_contract_no_renewed_ascribe ) ` + condition
  193. _, err = o.Raw(sql, pars).QueryRows(&items)
  194. return
  195. }
  196. // 根据合同ID对相关合同进行隐藏
  197. func UpdateCompanyContracthide(companyContractId []string) (err error) {
  198. o := orm.NewOrm()
  199. sql := `UPDATE company_contract SET not_renewal_hide = 1
  200. WHERE company_contract_id IN (` + strings.Join(companyContractId, ",") + `) `
  201. _, err = o.Raw(sql).Exec()
  202. return
  203. }