company_contract.go 17 KB

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