company_contract.go 15 KB

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