company_contract.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. package company
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "time"
  6. )
  7. type CompanyContract struct {
  8. CompanyContractId int `orm:"column(company_contract_id);pk" description:"客户合同id"`
  9. ContractType string `description:"合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  10. ProductId int `description:"产品id"`
  11. ProductName string `description:"产品名称"`
  12. CompanyId int `description:"客户id"`
  13. CompanyProductId int `description:"客户产品id"`
  14. ContractCode string `description:"合同编码"`
  15. StartDate string `description:"合同开始时间"`
  16. EndDate string `description:"合同结束时间"`
  17. Money float64 `description:"合同金额"`
  18. PayMethod string `description:"支付方式"`
  19. PayChannel string `description:"支付渠道"`
  20. ImgUrl string `description:"合同图片,多个用#隔开"`
  21. CreateTime time.Time `description:"合同创建时间"`
  22. ModifyTime time.Time `description:"合同修改时间"`
  23. Status int `description:"状态"`
  24. Source string `description:"合同来源,枚举值:上传附件、系统合同,默认上传附件"`
  25. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  26. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w大套餐; 2-45w大套餐"`
  27. RaiContractType string `description:"权益合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  28. }
  29. // 新增客户合同
  30. func AddCompanyContract(item *CompanyContract) (lastId int64, err error) {
  31. o := orm.NewOrm()
  32. lastId, err = o.Insert(item)
  33. return
  34. }
  35. func GetCompanyContractByCompanyId(companyId int) (items []*CompanyContract, err error) {
  36. o := orm.NewOrm()
  37. sql := `SELECT * FROM company_contract WHERE company_id =? ORDER BY modify_time DESC `
  38. _, err = o.Raw(sql, companyId).QueryRows(&items)
  39. return
  40. }
  41. func GetCompanyContractCode() (companyCode string, err error) {
  42. var num int
  43. o := orm.NewOrm()
  44. sql := `SELECT COUNT(1) AS num FROM company_contract`
  45. err = o.Raw(sql).QueryRow(&num)
  46. if err != nil {
  47. return
  48. }
  49. companyCode = "HZ" + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  50. return
  51. }
  52. type CompanyContractPermission struct {
  53. Id int `orm:"column(id);pk" description:"客户合同id"`
  54. CompanyId int `description:"客户id"`
  55. CompanyContractId int `description:"合同id"`
  56. ChartPermissionId int `description:"权限id"`
  57. IsUpgrade int `description:"是否升级,1是,0否"`
  58. ExpensiveYx int `description:"权益研选: 0-3w; 1-5w"`
  59. StartDate string `description:"开始日期"`
  60. EndDate string `description:"结束日期"`
  61. CreateTime time.Time `description:"创建时间"`
  62. ModifyTime time.Time `description:"修改时间"`
  63. ContractType string `description:"签约合同时,当前行业类型:枚举值:'行业新签','行业续约'"`
  64. PermissionName string `description:"权限名称"`
  65. Points float64 `description:"研选扣点包点数"`
  66. }
  67. // 新增客户合同
  68. func AddCompanyContractPermission(item *CompanyContractPermission) (err error) {
  69. o := orm.NewOrm()
  70. _, err = o.Insert(item)
  71. return
  72. }
  73. // MultiAddCompanyContractPermission 批量新增客户合同权限
  74. func MultiAddCompanyContractPermission(items []*CompanyContractPermission) (err error) {
  75. if len(items) == 0 {
  76. return
  77. }
  78. o := orm.NewOrm()
  79. _, err = o.InsertMulti(len(items), items)
  80. return
  81. }
  82. type CompanyContractPermissionLog struct {
  83. Id int `orm:"column(id);pk" description:"客户合同id"`
  84. CompanyId int `description:"客户id"`
  85. CompanyContractId int `description:"合同id"`
  86. ChartPermissionId string `description:"权限id"`
  87. StartDate string `description:"开始日期"`
  88. EndDate string `description:"结束日期"`
  89. CreateTime time.Time `description:"创建时间"`
  90. ModifyTime time.Time `description:"修改时间"`
  91. LogCreateTime time.Time
  92. }
  93. // 新增客户合同
  94. func AddCompanyContractPermissionLog(item *CompanyContractPermissionLog) (err error) {
  95. o := orm.NewOrm()
  96. _, err = o.Insert(item)
  97. return
  98. }
  99. // MultiAddCompanyContractPermissionLog 批量新增客户合同权限日志
  100. func MultiAddCompanyContractPermissionLog(items []*CompanyContractPermissionLog) (err error) {
  101. if len(items) == 0 {
  102. return
  103. }
  104. o := orm.NewOrm()
  105. _, err = o.InsertMulti(len(items), items)
  106. return
  107. }
  108. func GetCompanyContractPermissionCheck(companyId, permissionId int) (count int, err error) {
  109. o := orm.NewOrm()
  110. sql := `SELECT COUNT(1) AS count FROM company_contract_permission AS a WHERE a.company_id=? AND a.chart_permission_id=? `
  111. err = o.Raw(sql, companyId, permissionId).QueryRow(&count)
  112. return
  113. }
  114. func GetCompanyContractPermissionCheckByContractId(companyId, companyContractId, permissionId int) (count int, err error) {
  115. o := orm.NewOrm()
  116. 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=? `
  117. err = o.Raw(sql, companyId, companyContractId, permissionId).QueryRow(&count)
  118. return
  119. }
  120. // GetCompanyContractPermissionByCompanyContractId 根据合同ID获取开通的权限
  121. func GetCompanyContractPermissionByCompanyContractId(companyContractId int) (items []*CompanyContractPermission, err error) {
  122. o := orm.NewOrm()
  123. sql := `SELECT * FROM company_contract_permission WHERE company_contract_id =? ORDER BY modify_time DESC `
  124. _, err = o.Raw(sql, companyContractId).QueryRows(&items)
  125. return
  126. }
  127. // 根据合同id集合字符串获取权限列表数据
  128. type CompanyContractPermissionName struct {
  129. Id int `orm:"column(id);pk" description:"客户合同id"`
  130. CompanyId int `description:"客户id"`
  131. CompanyContractId int `description:"合同id"`
  132. ChartPermissionId int `description:"权限id"`
  133. ChartPermissionName string `description:"权限名称"`
  134. PermissionName string `description:"权限名称"`
  135. PermissionRemark string `description:"权限备注"`
  136. ClassifyName string `description:"权限分类名称"`
  137. StartDate string `description:"开始日期"`
  138. EndDate string `description:"结束日期"`
  139. CreateTime time.Time `json:"-" description:"创建时间"`
  140. ModifyTime time.Time `json:"-" description:"修改时间"`
  141. IsUpgrade int `description:"是否升级,1是,0否"`
  142. }
  143. // 根据企业客户id获取已经审核通过的合同列表
  144. func GetCompanyContractListByCompanyId(companyId int, condition string, pars []interface{}) (items []*CompanyContract, err error) {
  145. o := orm.NewOrm()
  146. sql := `SELECT * FROM company_contract WHERE company_id =? `
  147. if condition != "" {
  148. sql += condition
  149. }
  150. sql += " ORDER BY modify_time DESC"
  151. _, err = o.Raw(sql, companyId, pars).QueryRows(&items)
  152. return
  153. }
  154. // 根据合同Id集合获取合同的权限列表
  155. func GetCompanyContractPermissionListByContractIds(companyContractIds string) (list []*CompanyContractPermissionName, err error) {
  156. o := orm.NewOrm()
  157. sql := `SELECT a.*,b.permission_name,b.remark permission_remark,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 in (` + companyContractIds + `) `
  158. _, err = o.Raw(sql).QueryRows(&list)
  159. return
  160. }
  161. // GetCompanyContractDetailByCompanyContractId 根据客户合同id获取合同信息
  162. func GetCompanyContractDetailByCompanyContractId(companyContractId int) (item *CompanyContract, err error) {
  163. o := orm.NewOrm()
  164. sql := `SELECT * FROM company_contract WHERE company_contract_id =? ORDER BY modify_time DESC `
  165. err = o.Raw(sql, companyContractId).QueryRow(&item)
  166. return
  167. }
  168. // GetCompanyContractPermissionListByContractId 根据合同Id获取合同的权限列表
  169. func GetCompanyContractPermissionListByContractId(companyContractId int) (list []*CompanyContractPermissionName, err error) {
  170. o := orm.NewOrm()
  171. 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 =? `
  172. _, err = o.Raw(sql, companyContractId).QueryRows(&list)
  173. return
  174. }
  175. func GetCompanyContractUpgradePermissionCheckByContractId(companyId, companyContractId, permissionId, isUpgrade int) (count int, err error) {
  176. o := orm.NewOrm()
  177. 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=? AND a.is_upgrade=? `
  178. err = o.Raw(sql, companyId, companyContractId, permissionId, isUpgrade).QueryRow(&count)
  179. return
  180. }
  181. type CompanyContractResp struct {
  182. CompanyContractId int `orm:"column(company_contract_id);pk" description:"客户合同id"`
  183. ContractType string `description:"合同类型:枚举值:'新签合同','续约合同','补充协议'"`
  184. ProductId int `description:"产品id"`
  185. ProductName string `description:"产品名称"`
  186. CompanyId int `description:"客户id"`
  187. CompanyProductId int `description:"客户产品id"`
  188. ContractCode string `description:"合同编码"`
  189. StartDate string `description:"合同开始时间"`
  190. EndDate string `description:"合同结束时间"`
  191. Money float64 `description:"合同金额"`
  192. PayMethod string `description:"支付方式"`
  193. PayChannel string `description:"支付渠道"`
  194. ImgUrl string `description:"合同图片,多个用#隔开"`
  195. CreateTime time.Time `description:"合同创建时间"`
  196. ModifyTime time.Time `description:"合同修改时间"`
  197. Status int `description:"状态"`
  198. Source string `description:"合同来源,枚举值:上传附件、系统合同,默认上传附件"`
  199. PackageType int `description:"套餐类型,0:无,1:大套餐,2:小套餐"`
  200. RaiPackageType int `description:"权益套餐类型: 0-无; 1-70w大套餐; 2-45w大套餐"`
  201. PermissionName string `description:"权限名"`
  202. }
  203. // 获取合同列表
  204. func GetCompanyContractList(condition string, pars []interface{}) (items []*CompanyContractResp, err error) {
  205. o := orm.NewOrm()
  206. sql := `SELECT * FROM company_contract WHERE 1 = 1 `
  207. if condition != "" {
  208. sql += condition
  209. }
  210. _, err = o.Raw(sql, pars).QueryRows(&items)
  211. return
  212. }
  213. // 获取合同对应的权限列表
  214. func GetCompanyContractPermissionList(condition string, pars []interface{}) (items []*CompanyContractPermission, err error) {
  215. o := orm.NewOrm()
  216. sql := `SELECT * FROM company_contract_permission WHERE 1 = 1 `
  217. if condition != "" {
  218. sql += condition
  219. }
  220. _, err = o.Raw(sql, pars).QueryRows(&items)
  221. return
  222. }
  223. // UpdateCompanyContractPermissionMulti 批量修改
  224. func UpdateCompanyContractPermissionMulti(items []*CompanyContractPermission) (err error) {
  225. o, err := orm.NewOrm().Begin()
  226. if err != nil {
  227. return
  228. }
  229. defer func() {
  230. if err == nil {
  231. o.Commit()
  232. } else {
  233. o.Rollback()
  234. }
  235. }()
  236. //批量修改
  237. p, err := o.Raw("UPDATE company_contract_permission SET permission_name = ? ,contract_type = ? WHERE id = ?").Prepare()
  238. if err != nil {
  239. return
  240. }
  241. defer func() {
  242. _ = p.Close() // 别忘记关闭 statement
  243. }()
  244. for _, v := range items {
  245. _, err = p.Exec(v.PermissionName, v.ContractType, v.Id)
  246. if err != nil {
  247. return
  248. }
  249. }
  250. return
  251. }
  252. // 通过ID获取详情
  253. func GetCompanyContracDetail(condition string, pars []interface{}) (item *CompanyContractResp, err error) {
  254. o := orm.NewOrm()
  255. sql := `SELECT * FROM company_contract WHERE 1= 1 ` + condition
  256. err = o.Raw(sql, pars).QueryRow(&item)
  257. return
  258. }
  259. type GetCompanyContractDetailResp struct {
  260. Detail *CompanyContractResp
  261. }
  262. // UpdateCompanyContractPackageDifference 更改合同与上一份合同金额对比信息
  263. func UpdateCompanyContractPackageDifference(packageDifference string, companyContractId int) (err error) {
  264. o := orm.NewOrm()
  265. sql := `UPDATE company_contract SET package_difference = ? WHERE company_contract_id=? `
  266. _, err = o.Raw(sql, packageDifference, companyContractId).Exec()
  267. return
  268. }
  269. // 更新合同类型
  270. func UpdateCompanyContractType(contractType string, companyContractId int) (err error) {
  271. o := orm.NewOrm()
  272. sql := `UPDATE company_contract SET contract_type = ? WHERE company_contract_id=? `
  273. _, err = o.Raw(sql, contractType, companyContractId).Exec()
  274. return
  275. }
  276. // GetLastContractListByEndDate 通过最近一份合同的日期获取早于该合同的最晚一份合同
  277. func GetLastContractListByEndDate(companyId, productId int, endDate string) (item *CompanyContract, err error) {
  278. o := orm.NewOrm()
  279. sql := "SELECT * FROM company_contract where company_id = ? AND product_id= ? AND end_date < ? AND status = 1 ORDER BY end_date desc"
  280. err = o.Raw(sql, companyId, productId, endDate).QueryRow(&item)
  281. return
  282. }
  283. // 获取权益近一年是否有新签合同
  284. func GetCompanyContractCountRaiByLastYear(companyId int, startDate string) (count int, err error) {
  285. o := orm.NewOrm()
  286. sql := `SELECT COUNT(1) AS total FROM company_contract AS a WHERE product_id = 2 AND rai_contract_type = '新签合同' AND company_id = ? AND start_date > ? `
  287. err = o.Raw(sql, companyId, startDate).QueryRow(&count)
  288. return
  289. }
  290. // UpdateCompanyContractRaiContractType 更新某些合同权益的标识,为新签合同
  291. func UpdateCompanyContractRaiContractTypeInit(companyContractId string) (err error) {
  292. o := orm.NewOrm()
  293. sql := `UPDATE company_contract SET rai_contract_type = '新签合同' WHERE company_contract_id IN (` + companyContractId + `) `
  294. _, err = o.Raw(sql).Exec()
  295. return
  296. }
  297. // UpdateCompanyContractCompanyAscribeId 更改权益未续约归因
  298. func UpdateCompanyContractCompanyAscribeId(companyAscribId, companyContractId int) (err error) {
  299. o := orm.NewOrm()
  300. sql := `UPDATE company_contract SET company_ascribe_id = ? WHERE company_contract_id=? `
  301. _, err = o.Raw(sql, companyAscribId, companyContractId).Exec()
  302. return
  303. }
  304. // GetFirstContractRai 权益获取第一份合同
  305. func GetFirstContractRai(companyId int) (item *CompanyContract, err error) {
  306. o := orm.NewOrm()
  307. sql := " SELECT * FROM company_contract WHERE company_id = ? AND product_id= 2 AND status = 1 ORDER BY start_date ASC LIMIT 1 "
  308. err = o.Raw(sql, companyId).QueryRow(&item)
  309. return
  310. }