contract.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. package contract
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hz_crm_api/utils"
  6. "time"
  7. )
  8. // 合同
  9. type Contract struct {
  10. ContractId int `orm:"column(contract_id);pk"`
  11. ContractCode string `description:"合同编号,长度32位"`
  12. SellerId int `description:"所属销售id"`
  13. SellerName string `description:"所属销售名称"`
  14. ProductId int `description:"产品id,1:ficc;2:权益"`
  15. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  16. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  17. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已解约'"`
  18. StartDate time.Time `description:"合同开始日期"`
  19. EndDate time.Time `description:"合同结束日期"`
  20. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  21. Price float64 `description:"实际金额,优惠后的金额"`
  22. PayRemark string `description:"付款方式说明,长度255位"`
  23. PayChannel string `description:"付款渠道,长度255位"`
  24. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  25. CreditCode string `description:"社会统一信用代码,长度64位"`
  26. ProvinceId int `description:"省级id"`
  27. Province string `description:"省级名称,长度16位"`
  28. CityId int `description:"市级id"`
  29. City string `description:"市级名称,长度32位"`
  30. Address string `description:"详细地址"`
  31. Fax string `description:"传真,长度32位"`
  32. Phone string `description:"电话,长度32位"`
  33. Postcode string `description:"邮编,长度16位"`
  34. Remark string `description:"补充内容,长度255位"`
  35. SellerRemark string `description:"销售备注,长度255位"`
  36. ModifyContent string `description:"修改内容"`
  37. ApprovalRemark string `description:"审核备注"`
  38. FileUrl string `description:"合同文件地址"`
  39. CheckBackFileUrl string `description:"签回合同文件地址"`
  40. RescindFileUrl string `description:"解约合同文件地址"`
  41. TemplateId int `description:"模板id"`
  42. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  43. IsDelete int `description:"是否已经删除,0:未删除,1:已删除" json:"-"`
  44. ApproveTime time.Time `description:"审批时间"`
  45. InvalidTime time.Time `description:"作废时间"`
  46. CheckBackFileTime time.Time `description:"合同签回时间"`
  47. RescindTime time.Time `description:"解约时间"`
  48. ModifyTime time.Time `description:"合同最近一次修改时间"`
  49. CreateTime time.Time `description:"合同添加时间"`
  50. }
  51. // 更新合同基础信息
  52. func (contract *Contract) Update(cols []string) (err error) {
  53. o := orm.NewOrm()
  54. _, err = o.Update(contract, cols...)
  55. return
  56. }
  57. // 根据合同id获取合同信息
  58. func GetContractById(contractId int) (contractInfo *Contract, err error) {
  59. o := orm.NewOrm()
  60. sql := `select * from contract where contract_id = ? `
  61. err = o.Raw(sql, contractId).QueryRow(&contractInfo)
  62. return
  63. }
  64. // GetContractByCode 根据合同编号获取合同信息
  65. func GetContractByCode(contractCode string) (contractInfo *Contract, err error) {
  66. o := orm.NewOrm()
  67. sql := `select * from contract where contract_code = ? `
  68. err = o.Raw(sql, contractCode).QueryRow(&contractInfo)
  69. return
  70. }
  71. // 合同详情信息(包含服务信息等)
  72. type ContractDetail struct {
  73. ContractId int `description:"合同唯一id"`
  74. ContractCode string `description:"合同编号,长度32位"`
  75. SellerId int `description:"所属销售id"`
  76. SellerName string `description:"所属销售名称"`
  77. ProductId int `description:"产品id,1:ficc;2:权益"`
  78. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  79. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  80. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已解约'"`
  81. StartDate time.Time `description:"合同开始日期"`
  82. EndDate time.Time `description:"合同结束日期"`
  83. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  84. Price float64 `description:"实际金额,优惠后的金额"`
  85. PayRemark string `description:"付款方式说明,长度255位"`
  86. PayChannel string `description:"付款渠道,长度255位"`
  87. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  88. CreditCode string `description:"社会统一信用代码,长度64位"`
  89. ProvinceId int `description:"省级id"`
  90. Province string `description:"省级名称,长度16位"`
  91. CityId int `description:"市级id"`
  92. City string `description:"市级名称,长度32位"`
  93. Address string `description:"详细地址"`
  94. Fax string `description:"传真,长度32位"`
  95. Phone string `description:"电话,长度32位"`
  96. Postcode string `description:"邮编,长度16位"`
  97. Remark string `description:"补充内容,长度255位"`
  98. SellerRemark string `description:"销售备注,长度255位"`
  99. ModifyContent string `description:"修改内容"`
  100. ApprovalRemark string `description:"审核备注"`
  101. FileUrl string `description:"合同文件地址"`
  102. CheckBackFileUrl string `description:"签回合同文件地址"`
  103. RescindFileUrl string `description:"解约合同文件地址"`
  104. TemplateId int `description:"模板id"`
  105. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  106. IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"`
  107. ApproveTime time.Time `description:"审批时间"`
  108. InvalidTime time.Time `description:"作废时间"`
  109. CheckBackFileTime time.Time `description:"合同签回时间"`
  110. RescindTime time.Time `description:"解约时间"`
  111. ModifyTime time.Time `description:"合同最近一次修改时间"`
  112. CreateTime time.Time `description:"合同添加时间"`
  113. StartDateStr string `description:"合同起始时间(字符串形式)"`
  114. EndDateStr string `description:"合同结束时间(字符串形式)"`
  115. ApproveTimeStr string `description:"审批时间(字符串形式)"`
  116. InvalidTimeStr string `description:"作废时间(字符串形式)"`
  117. CheckBackFileTimeStr string `description:"合同签回时间(字符串形式)"`
  118. RescindTimeStr string `description:"解约时间(字符串形式)"`
  119. ModifyTimeStr string `description:"最近一次更新时间(字符串形式)"`
  120. CreateTimeStr string `description:"合同添加时间(字符串形式)"`
  121. Service []*ContractServiceAndDetail
  122. RelationContractDetailList []*ContractDetail `description:"关联合同详情"`
  123. PermissionLookList []*PermissionLookList `description:"合同里面的权限列表"`
  124. }
  125. type PermissionLookList struct {
  126. ClassifyName string `description:"分类"`
  127. Items []*PermissionLookItem
  128. }
  129. type PermissionLookItem struct {
  130. ChartPermissionId int `description:"权限id"`
  131. PermissionName string `description:"权限名称"`
  132. ClassifyName string `description:"分类"`
  133. }
  134. // 根据合同id获取合同详情信息
  135. func GetContractDetailById(contractId int) (contractInfo *ContractDetail, err error) {
  136. o := orm.NewOrm()
  137. sql := `select * from contract where contract_id = ? `
  138. err = o.Raw(sql, contractId).QueryRow(&contractInfo)
  139. return
  140. }
  141. // 合同添加
  142. func AddContract(contractInfo *Contract, contractServiceAndDetailList []*ContractServiceAndDetail, relationId int) (newContract *Contract, err error) {
  143. o := orm.NewOrm()
  144. to, err := o.Begin()
  145. if err != nil {
  146. return
  147. }
  148. defer func() {
  149. if err != nil {
  150. _ = to.Rollback()
  151. } else {
  152. _ = to.Commit()
  153. }
  154. }()
  155. //合同数据入库
  156. contractId, err := to.Insert(contractInfo)
  157. if err != nil {
  158. return
  159. }
  160. contractInfo.ContractId = int(contractId)
  161. for i := 0; i < len(contractServiceAndDetailList); i++ {
  162. //合同服务数据入库
  163. tmpContractService := contractServiceAndDetailList[i]
  164. contractService := &ContractService{
  165. ContractId: int(contractId),
  166. ProductId: contractInfo.ProductId,
  167. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  168. Title: tmpContractService.Title,
  169. Value: tmpContractService.Value,
  170. TableValue: tmpContractService.TableValue,
  171. HasDetail: tmpContractService.HasDetail,
  172. ChartPermissionId: tmpContractService.ChartPermissionId,
  173. CreateTime: time.Now(),
  174. }
  175. contractServiceId, serviceErr := to.Insert(contractService)
  176. if serviceErr != nil {
  177. err = serviceErr
  178. return
  179. }
  180. contractService.ContractServiceId = int(contractServiceId)
  181. //合同服务详情入库
  182. for j := 0; j < len(tmpContractService.DetailList); j++ {
  183. contractServiceDetail := tmpContractService.DetailList[j]
  184. //合同服务编号
  185. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  186. contractServiceDetail.ContractId = int(contractId)
  187. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  188. //合同服务详情入库
  189. contractServiceDetailId, detailErr := to.Insert(contractServiceDetail)
  190. if detailErr != nil {
  191. err = detailErr
  192. return
  193. }
  194. contractServiceDetail.Id = int(contractServiceDetailId)
  195. tmpContractService.DetailList[j] = contractServiceDetail
  196. }
  197. }
  198. //新增 业务合同 与 代付合同 的 关联关系
  199. if relationId > 0 {
  200. sql := ``
  201. if contractInfo.ContractBusinessType == "代付合同" {
  202. sql = `INSERT INTO contract_relation ( contract_id,payment_on_behalf_contract_id ) VALUES ( ` + fmt.Sprint(relationId) + `,` + fmt.Sprint(contractId) + ` )`
  203. } else {
  204. sql = `INSERT INTO contract_relation ( contract_id,payment_on_behalf_contract_id ) VALUES ( ` + fmt.Sprint(contractId) + `,` + fmt.Sprint(relationId) + ` )`
  205. }
  206. _, err = to.Raw(sql).Exec()
  207. if err != nil {
  208. return
  209. }
  210. }
  211. newContract = contractInfo
  212. return
  213. }
  214. type ContractList struct {
  215. ContractId int `description:"合同唯一id"`
  216. ContractCode string `description:"合同编号,长度32位"`
  217. SellerId int `description:"所属销售id"`
  218. SellerName string `description:"所属销售名称"`
  219. ProductId int `description:"产品id,1:ficc;2:权益"`
  220. ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"`
  221. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  222. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已签回','已解约'"`
  223. StartDate time.Time `json:"-" description:"合同开始日期"`
  224. EndDate time.Time `json:"-" description:"合同结束日期"`
  225. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  226. Price float64 `description:"实际金额,优惠后的金额"`
  227. PayRemark string `description:"付款方式说明,长度255位"`
  228. PayChannel string `description:"付款渠道,长度255位"`
  229. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  230. CreditCode string `description:"社会统一信用代码,长度64位"`
  231. ProvinceId int `description:"省级id"`
  232. Province string `description:"省级名称,长度16位"`
  233. CityId int `description:"市级id"`
  234. City string `description:"市级名称,长度32位"`
  235. Address string `description:"详细地址"`
  236. Fax string `description:"传真,长度32位"`
  237. Phone string `description:"电话,长度32位"`
  238. Postcode string `description:"邮编,长度16位"`
  239. Remark string `json:"-" description:"补充内容,长度255位"`
  240. ApprovalRemark string `description:"审核备注"`
  241. ModifyContent string `description:"修改内容"`
  242. FileUrl string `description:"合同文件地址"`
  243. CheckBackFileUrl string `description:"签回合同文件地址"`
  244. RescindFileUrl string `description:"解约合同文件地址"`
  245. TemplateId int `description:"模板id"`
  246. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  247. IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"`
  248. ModifyTime time.Time `json:"-" description:"合同最近一次修改时间"`
  249. CreateTime time.Time `json:"-" description:"合同添加时间"`
  250. ApproveTime time.Time `description:"审批时间"`
  251. InvalidTime time.Time `description:"作废时间"`
  252. CheckBackFileTime time.Time `description:"合同签回时间"`
  253. RescindTime time.Time `description:"解约时间"`
  254. StartDateStr string `description:"合同起始时间"`
  255. EndDateStr string `description:"合同结束时间"`
  256. ApproveTimeStr string `description:"审批时间(字符串形式)"`
  257. InvalidTimeStr string `description:"作废时间(字符串形式)"`
  258. CheckBackFileTimeStr string `description:"合同签回时间(字符串形式)"`
  259. RescindTimeStr string `description:"解约时间(字符串形式)"`
  260. ModifyTimeStr string `description:"最近一次更新时间(字符串形式)"`
  261. CreateTimeStr string `description:"合同添加时间(字符串形式)"`
  262. Service []*ContractServiceAndDetail
  263. OpButton ContractOpButton `description:"按钮操作权限"`
  264. RelationContractList []*RelationContractList `description:"关联合同信息"`
  265. }
  266. // ContractOpButton 合同操作权限
  267. type ContractOpButton struct {
  268. Cancel bool `description:"是否具有撤回操作权限,true才允许撤回操作"`
  269. UpdateFile bool `description:"是否具有更新合同附件权限,true才允许更新合同附件操作"`
  270. }
  271. // GetContractListCount 获取合同列表数据数量
  272. func GetContractListCount(condition, joinStr string, pars []interface{}) (count int, err error) {
  273. o := orm.NewOrm()
  274. sql := `select count(a.contract_id) AS COUNT from contract a ` + joinStr
  275. sql += ` where a.is_delete = 0 `
  276. sql += condition
  277. err = o.Raw(sql, pars).QueryRow(&count)
  278. return
  279. }
  280. // GetContractList 获取合同列表数据
  281. func GetContractList(condition, joinStr string, pars []interface{}, startSize, pageSize int) (list []*ContractList, err error) {
  282. o := orm.NewOrm()
  283. sql := "select a.* from contract a " + joinStr
  284. sql += ` where a.is_delete = 0 `
  285. sql += condition
  286. sql += ` order by modify_time desc LIMIT ?,? `
  287. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  288. return
  289. }
  290. // ContractCompanyNameList 合同中客户名称数据结构体
  291. type ContractCompanyNameList struct {
  292. CompanyName string `description:"客户名称"`
  293. }
  294. // GetContractCompanyNameList 获取合同中客户名称列表数据
  295. func GetContractCompanyNameList(condition string, pars []interface{}) (list []*ContractCompanyNameList, err error) {
  296. o := orm.NewOrm()
  297. sql := `select a.company_name from contract a where a.is_delete = 0 and price>paid_price`
  298. sql += condition
  299. sql += ` group by company_name order by modify_time desc`
  300. _, err = o.Raw(sql, pars).QueryRows(&list)
  301. return
  302. }
  303. // 修改合同
  304. func EditContract(contractInfo *Contract, updateDetailCol []string, contractServiceAndDetailList []*ContractServiceAndDetail, relationId int) (err error) {
  305. o := orm.NewOrm()
  306. to, err := o.Begin()
  307. if err != nil {
  308. return
  309. }
  310. defer func() {
  311. if err != nil {
  312. _ = to.Rollback()
  313. } else {
  314. _ = to.Commit()
  315. }
  316. }()
  317. contractId := contractInfo.ContractId
  318. //合同数据入库
  319. _, err = to.Update(contractInfo, updateDetailCol...)
  320. if err != nil {
  321. return
  322. }
  323. //删除合同的原始服务信息
  324. sql := `delete from contract_service where contract_id = ?`
  325. _, err = to.Raw(sql, contractId).Exec()
  326. //删除合同的原始服务详情信息
  327. sql = `delete from contract_service_detail where contract_id = ?`
  328. _, err = to.Raw(sql, contractId).Exec()
  329. for i := 0; i < len(contractServiceAndDetailList); i++ {
  330. //合同服务数据入库
  331. tmpContractService := contractServiceAndDetailList[i]
  332. contractService := &ContractService{
  333. ContractId: int(contractId),
  334. Title: tmpContractService.Title,
  335. ProductId: contractInfo.ProductId,
  336. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  337. ChartPermissionId: tmpContractService.ChartPermissionId,
  338. Value: tmpContractService.Value,
  339. TableValue: tmpContractService.TableValue,
  340. HasDetail: tmpContractService.HasDetail,
  341. CreateTime: time.Now(),
  342. }
  343. contractServiceId, serviceErr := to.Insert(contractService)
  344. if serviceErr != nil {
  345. err = serviceErr
  346. return
  347. }
  348. contractService.ContractServiceId = int(contractServiceId)
  349. //合同服务详情入库
  350. for j := 0; j < len(tmpContractService.DetailList); j++ {
  351. contractServiceDetail := tmpContractService.DetailList[j]
  352. //合同服务编号
  353. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  354. contractServiceDetail.ContractId = contractService.ContractId
  355. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  356. //合同服务详情入库
  357. contractServiceDetailId, detailErr := to.Insert(contractServiceDetail)
  358. if detailErr != nil {
  359. err = detailErr
  360. return
  361. }
  362. contractServiceDetail.Id = int(contractServiceDetailId)
  363. tmpContractService.DetailList[j] = contractServiceDetail
  364. }
  365. }
  366. //新增 业务合同 与 代付合同 的 关联关系
  367. if relationId > 0 {
  368. sql = ``
  369. if contractInfo.ContractBusinessType == "代付合同" {
  370. //删除 业务合同 与 代付合同 的 关联关系
  371. sql = `delete from contract_relation where payment_on_behalf_contract_id = ?`
  372. _, err = to.Raw(sql, contractId).Exec()
  373. sql = `INSERT INTO contract_relation ( contract_id,payment_on_behalf_contract_id ) VALUES ( ` + fmt.Sprint(relationId) + `,` + fmt.Sprint(contractId) + ` )`
  374. } else {
  375. //删除 业务合同 与 代付合同 的 关联关系
  376. sql = `delete from contract_relation where contract_id = ?`
  377. _, err = to.Raw(sql, contractId).Exec()
  378. sql = `INSERT INTO contract_relation ( contract_id,payment_on_behalf_contract_id ) VALUES ( ` + fmt.Sprint(contractId) + `,` + fmt.Sprint(relationId) + ` )`
  379. }
  380. _, err = to.Raw(sql).Exec()
  381. if err != nil {
  382. return
  383. }
  384. }
  385. return
  386. }
  387. // AddContractPdf 添加生成后的合同地址
  388. func AddContractPdf(contractId int, pdfUrl string) (err error) {
  389. o := orm.NewOrm()
  390. //删除合同的原始服务信息
  391. sql := `update contract set file_url=? where contract_id = ?`
  392. _, err = o.Raw(sql, pdfUrl, contractId).Exec()
  393. return
  394. }
  395. // DeleteContract 删除合同
  396. func DeleteContract(contractInfo *Contract) (err error) {
  397. o := orm.NewOrm()
  398. to, err := o.Begin()
  399. if err != nil {
  400. return
  401. }
  402. defer func() {
  403. if err != nil {
  404. _ = to.Rollback()
  405. } else {
  406. _ = to.Commit()
  407. }
  408. }()
  409. contractInfo.IsDelete = 1
  410. contractInfo.ModifyTime = time.Now()
  411. //合同数据入库
  412. _, err = to.Update(contractInfo)
  413. if err != nil {
  414. return
  415. }
  416. return
  417. }
  418. // InvalidContract 作废合同
  419. func InvalidContract(contractInfo *Contract) (err error) {
  420. o := orm.NewOrm()
  421. to, err := o.Begin()
  422. if err != nil {
  423. return
  424. }
  425. defer func() {
  426. if err != nil {
  427. _ = to.Rollback()
  428. } else {
  429. _ = to.Commit()
  430. }
  431. }()
  432. contractInfo.Status = "已作废"
  433. contractInfo.ModifyTime = time.Now()
  434. contractInfo.InvalidTime = time.Now()
  435. //合同数据入库
  436. _, err = to.Update(contractInfo, "Status", "ModifyTime", "InvalidTime")
  437. if err != nil {
  438. return
  439. }
  440. return
  441. }
  442. // GetCompanyContractCode 生成合同编号
  443. func GetCompanyContractCode(productId int, contractBusinessType string) (companyCode string, err error) {
  444. var num int
  445. o := orm.NewOrm()
  446. today := utils.GetToday(utils.FormatDate)
  447. sql := `SELECT COUNT(1) AS num FROM contract where create_time>=?`
  448. err = o.Raw(sql, today).QueryRow(&num)
  449. if err != nil {
  450. return
  451. }
  452. companyType := ""
  453. switch productId {
  454. case 1:
  455. companyType = "FICC"
  456. case 2:
  457. companyType = "EQ"
  458. }
  459. perStr := `HZ`
  460. switch contractBusinessType {
  461. case "业务合同":
  462. perStr = `HZ`
  463. case "代付合同":
  464. perStr = `DC`
  465. }
  466. companyCode = perStr + companyType + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  467. return
  468. }
  469. // GetSearchContractList 获取合同列表数据
  470. func GetSearchContractList(productId, sellerId int, keyword, creditCode string) (list []*ContractList, err error) {
  471. o := orm.NewOrm()
  472. sql := `select a.* from contract a left join company_contract b on a.contract_code=b.contract_code
  473. where a.status='已签回' AND a.contract_business_type="业务合同" AND is_delete = 0 AND a.product_id=? AND a.end_date>now()
  474. AND (b.contract_code is null or b.status = "已审批") and seller_id=?`
  475. //if companyIdStr != "" {
  476. // sql += ` AND b.company_id not in ( ` + companyIdStr + `) `
  477. //}
  478. if keyword != "" {
  479. sql += ` AND a.company_name like "%` + keyword + `%" `
  480. }
  481. if creditCode != "" {
  482. sql += ` AND a.credit_code = "` + creditCode + `" `
  483. }
  484. sql += ` order by a.end_date asc `
  485. _, err = o.Raw(sql, productId, sellerId).QueryRows(&list)
  486. return
  487. }
  488. // UpdateBusinessContractPaidPrice 更新合同已支付金额
  489. func UpdateBusinessContractPaidPrice(contractId int, updatePriceStr string) (err error) {
  490. o := orm.NewOrm()
  491. //删除合同的原始服务信息
  492. sql := `update contract set paid_price=paid_price` + updatePriceStr + ` where contract_id = ?`
  493. _, err = o.Raw(sql, contractId).Exec()
  494. return
  495. }
  496. // GetSearchListBySeal 获取用印所需合同列表
  497. func GetSearchListBySeal(condition string, pars []interface{}) (list []*ContractList, err error) {
  498. o := orm.NewOrm()
  499. sql := `SELECT * FROM contract WHERE 1=1 AND is_delete = 0 AND status = "已审批" `
  500. sql += condition
  501. sql += ` ORDER BY modify_time DESC`
  502. _, err = o.Raw(sql, pars).QueryRows(&list)
  503. return
  504. }
  505. // GetContractListByContractIds 根据审批单IDs获取审批节点列表
  506. func GetContractListByContractIds(contractIds string) (contractList []*ContractList, err error) {
  507. o := orm.NewOrm()
  508. sql := `SELECT * FROM contract WHERE contract_id IN (` + contractIds + `)`
  509. _, err = o.Raw(sql).QueryRows(&contractList)
  510. return
  511. }
  512. // GetLatestContractListByProductId 获取最新的续约合同ids
  513. func GetLatestContractListByProductId(productId int) (contractIds []string, err error) {
  514. o := orm.NewOrm()
  515. sql := `SELECT company_contract_id FROM company_contract WHERE product_id=? AND contract_type="续约合同" GROUP BY create_time DESC,company_id `
  516. _, err = o.Raw(sql, productId).QueryRows(&contractIds)
  517. return
  518. }
  519. // GetContractListByProductId 根据产品获取合同列表
  520. func GetContractListByProductId(productId int) (list []*ContractList, err error) {
  521. o := orm.NewOrm()
  522. sql := `SELECT * FROM contract WHERE is_delete = 0 AND product_id = ? ORDER BY create_time ASC`
  523. _, err = o.Raw(sql, productId).QueryRows(&list)
  524. return
  525. }
  526. // GetContractListByCompanyName 根据公司名称获取合同
  527. func GetContractListByCompanyName(companyName string) (item *ContractList, err error) {
  528. o := orm.NewOrm()
  529. sql := `SELECT * FROM contract WHERE is_delete = 0 AND company_name = ? ORDER BY create_time DESC`
  530. err = o.Raw(sql, companyName).QueryRow(&item)
  531. return
  532. }
  533. type RenewCompanyGroup struct {
  534. SellerId int `description:"所属销售id"`
  535. //AdminName string `description:"所属销售名称"`
  536. Num int `description:"汇总次数"`
  537. CompanyIds string `description:"客户id字符串"`
  538. }
  539. // GetExpiresCompanyGroupList 获取销售到期数据
  540. func GetExpiresCompanyGroupList(startDate, endDate time.Time, productId int) (list []*RenewCompanyGroup, err error) {
  541. o := orm.NewOrm()
  542. sql := `SELECT
  543. a.seller_id,
  544. count(
  545. DISTINCT ( a.company_id )) num,
  546. GROUP_CONCAT( DISTINCT a.company_id SEPARATOR ',' ) AS company_ids
  547. FROM
  548. company_product as a
  549. join company_contract as b on a.company_id=b.company_id and a.product_id=b.product_id
  550. WHERE
  551. a.product_id=?
  552. AND b.product_id=?
  553. AND
  554. b.end_date >= ?
  555. AND b.end_date < ?
  556. AND b.status = 1
  557. GROUP BY
  558. a.seller_id
  559. `
  560. _, err = o.Raw(sql, productId, productId, startDate, endDate).QueryRows(&list)
  561. return
  562. }
  563. // GetCountFormalCompany
  564. // @Description: 获取某个时间点前的合同客户数量
  565. // @author: Roc
  566. // @datetime 2023-12-05 09:36:28
  567. // @param endDate time.Time
  568. // @param productId int
  569. // @return total int
  570. // @return err error
  571. func GetCountFormalCompany(endDate time.Time, productId int) (total int, err error) {
  572. o := orm.NewOrm()
  573. sql := `select count(1) total from (
  574. SELECT
  575. a.seller_id
  576. FROM
  577. company_product as a
  578. join company_contract as b on a.company_id=b.company_id and a.product_id=b.product_id
  579. WHERE
  580. a.product_id=?
  581. AND b.product_id=?
  582. AND b.end_date < ?
  583. AND b.status = 1
  584. GROUP BY
  585. a.company_id
  586. ) z
  587. `
  588. err = o.Raw(sql, productId, productId, endDate).QueryRow(&total)
  589. return
  590. }