contract_register.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. package fms
  2. import (
  3. "hongze/fms_api/global"
  4. "hongze/fms_api/models/base"
  5. "hongze/fms_api/utils"
  6. "time"
  7. )
  8. // ContractRegister 合同登记表
  9. type ContractRegister struct {
  10. ContractRegisterId int `gorm:"primaryKey;column:contract_register_id" json:"contract_register_id" description:"登记ID"`
  11. ContractCode string `gorm:"column:contract_code" json:"contract_code" description:"合同编号"`
  12. RelateContractCode string `gorm:"column:relate_contract_code" json:"relate_contract_code" description:"关联合同编号"`
  13. CrmContractId int `gorm:"column:crm_contract_id" json:"crm_contract_id" description:"CRM系统-合同ID"`
  14. ContractSource int `gorm:"column:contract_source" json:"contract_source" description:"合同来源: 0-非系统合同导入; 1-CRM合同导入"`
  15. CompanyName string `gorm:"column:company_name" json:"company_name" description:"客户名称"`
  16. ActualCompanyName string `gorm:"column:actual_company_name" json:"actual_company_name" description:"实际使用方"`
  17. ProductIds string `gorm:"column:product_ids" json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  18. SellerId int `gorm:"column:seller_id" json:"seller_id" description:"CRM系统-销售ID"`
  19. SellerName string `gorm:"column:seller_name" json:"seller_name" description:"CRM系统-销售名称"`
  20. RaiSellerId int `gorm:"column:rai_seller_id" json:"rai_seller_id" description:"CRM系统-权益销售ID"`
  21. RaiSellerName string `gorm:"column:rai_seller_name" json:"rai_seller_name" description:"CRM系统-权益销售名称"`
  22. ContractType int `gorm:"column:contract_type" json:"contract_type" description:"合同类型: 1-新签; 2-续约; 3-代付; 4-补充协议"`
  23. ContractAmount float64 `gorm:"column:contract_amount" json:"contract_amount" description:"合同金额"`
  24. InvoicedAmount float64 `gorm:"column:invoiced_amount" json:"invoiced_amount" description:"开票金额"`
  25. PaymentAmount float64 `gorm:"column:payment_amount" json:"payment_amount" description:"到款金额"`
  26. CurrencyUnit string `gorm:"column:currency_unit" json:"currency_unit" description:"货币国际代码"`
  27. RMBRate float64 `gorm:"column:rmb_rate" json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  28. StartDate time.Time `gorm:"column:start_date" json:"start_date" description:"合同开始日期"`
  29. EndDate time.Time `gorm:"column:end_date" json:"end_date" description:"合同结束日期"`
  30. SignDate time.Time `gorm:"column:sign_date" json:"sign_date" description:"合同签订日期"`
  31. AgreedPayTime string `gorm:"column:agreed_pay_time" json:"agreed_pay_time" description:"约定付款时间(如:生效日起10日内)"`
  32. ContractStatus int `gorm:"column:contract_status" json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
  33. RegisterStatus int `gorm:"column:register_status" json:"register_status" description:"登记状态: 1-进行中; 2-已完成;"`
  34. Remark string `gorm:"column:remark" json:"remark" description:"备注信息"`
  35. ServiceRemark string `gorm:"column:service_remark" json:"service_remark" description:"套餐备注信息"`
  36. HasPayment int `gorm:"column:has_payment" json:"has_payment" description:"是否有代付: 0-无; 1-有"`
  37. NewCompany int `gorm:"column:new_company" json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  38. IsDeleted int `gorm:"column:is_deleted" json:"is_deleted" description:"是否已删除: 0-正常; 1-已删除"`
  39. base.TimeBase
  40. }
  41. func (c *ContractRegister) TableName() string {
  42. return "contract_register"
  43. }
  44. func (c *ContractRegister) Create() (err error) {
  45. err = global.DEFAULT_MYSQL.Create(c).Error
  46. return
  47. }
  48. func (c *ContractRegister) AddInBatches(list []*ContractRegister) (err error) {
  49. err = global.DEFAULT_MYSQL.CreateInBatches(list, len(list)).Error
  50. return
  51. }
  52. func (c *ContractRegister) Update(updateCols []string) (err error) {
  53. err = global.DEFAULT_MYSQL.Model(c).Select(updateCols).Updates(c).Error
  54. return
  55. }
  56. func (c *ContractRegister) Fetch(id int) (item *ContractRegister, err error) {
  57. err = global.DEFAULT_MYSQL.Model(c).Where("is_deleted = 0 AND contract_register_id = ?", id).First(&item).Error
  58. return
  59. }
  60. func (c *ContractRegister) FetchByCondition(condition string, pars []interface{}) (item *ContractRegister, err error) {
  61. err = global.DEFAULT_MYSQL.Model(c).
  62. Where("is_deleted = 0").
  63. Where(condition, pars...).
  64. First(&item).Error
  65. return
  66. }
  67. func (c *ContractRegister) List(condition string, pars []interface{}) (list []*ContractRegister, err error) {
  68. list = make([]*ContractRegister, 0)
  69. err = global.DEFAULT_MYSQL.Model(c).
  70. Where("is_deleted = 0").
  71. Where(condition, pars...).
  72. Find(&list).Error
  73. return
  74. }
  75. func (c *ContractRegister) PageList(page base.IPage, condition string, pars []interface{}) (count int64, results []*ContractRegister, err error) {
  76. results = make([]*ContractRegister, 0)
  77. query := global.DEFAULT_MYSQL.Model(c).
  78. Where("is_deleted = 0").
  79. Where(condition, pars...)
  80. query.Count(&count)
  81. if len(page.GetOrderItemsString()) > 0 {
  82. query = query.Order(page.GetOrderItemsString())
  83. }
  84. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&results).Error
  85. return
  86. }
  87. // ContractRegisterListReq 合同登记列表请求体
  88. type ContractRegisterListReq struct {
  89. Keyword string `json:"keyword" form:"keyword" binding:"omitempty" description:"关键词"`
  90. StartDate string `json:"start_date" form:"start_date" binding:"omitempty,datetime=2006-01-02" description:"合同开始日期"`
  91. EndDate string `json:"end_date" form:"end_date" binding:"omitempty,datetime=2006-01-02" description:"合同结束日期"`
  92. ServiceTypes string `json:"service_types" form:"service_types" description:"套餐类型"`
  93. ContractType int `json:"contract_type" form:"contract_type" description:"合同类型"`
  94. RegisterStatus int `json:"register_status" form:"register_status" description:"登记状态"`
  95. base.PageReq
  96. }
  97. // ContractRegisterItem 合同登记
  98. type ContractRegisterItem struct {
  99. ContractRegisterId int `json:"contract_register_id" description:"登记ID"`
  100. ContractCode string `json:"contract_code" description:"合同编号"`
  101. RelateContractCode string `json:"relate_contract_code" description:"关联合同编号"`
  102. CrmContractId int `json:"crm_contract_id" description:"CRM系统-合同ID"`
  103. ContractSource int `json:"contract_source" description:"合同来源: 0-非系统合同导入; 1-CRM合同导入"`
  104. CompanyName string `json:"company_name" description:"客户名称"`
  105. ActualCompanyName string `json:"actual_company_name" description:"实际使用方"`
  106. ProductIds string `json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  107. SellerId int `json:"seller_id" description:"CRM系统-销售ID"`
  108. SellerName string `json:"seller_name" description:"CRM系统-销售名称"`
  109. RaiSellerId int `json:"rai_seller_id" description:"CRM系统-权益销售ID"`
  110. RaiSellerName string `json:"rai_seller_name" description:"CRM系统-权益销售名称"`
  111. ContractType int `json:"contract_type" description:"合同类型: 1-新签; 2-续约"`
  112. ContractAmount float64 `json:"contract_amount" description:"合同金额"`
  113. InvoicedAmount float64 `json:"invoiced_amount" description:"开票金额"`
  114. PaymentAmount float64 `json:"payment_amount" description:"到款金额"`
  115. CurrencyUnit string `json:"currency_unit" description:"货币国际代码"`
  116. RMBRate float64 `json:"rmb_rate" description:"人民币汇率(create_time当日)"`
  117. StartDate string `json:"start_date" description:"合同开始日期"`
  118. EndDate string `json:"end_date" description:"合同结束日期"`
  119. SignDate string `json:"sign_date" description:"合同签订日期"`
  120. AgreedPayTime string `json:"agreed_pay_time" description:"约定付款时间(如:生效日起10日内)"`
  121. ContractStatus int `json:"contract_status" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回"`
  122. RegisterStatus int `json:"register_status" description:"登记状态: 1-进行中; 2-已完成;"`
  123. Remark string `json:"remark" description:"备注信息"`
  124. ServiceRemark string `json:"service_remark" description:"套餐备注信息"`
  125. HasPayment int `json:"has_payment" description:"是否有代付: 0-无; 1-有"`
  126. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  127. CreateTime string `json:"create_time" description:"登记时间"`
  128. SellerIds string `json:"seller_ids"`
  129. }
  130. // ContractRegisterList 合同登记列表
  131. type ContractRegisterList struct {
  132. *ContractRegisterItem
  133. ServicesName string `json:"services" description:"套餐名称"`
  134. InvoiceList []*ContractInvoiceItem `json:"invoice_list" description:"开票信息"`
  135. PaymentList []*ContractInvoiceItem `json:"payment_list" description:"到款信息"`
  136. }
  137. // ContractRegisterDelReq 合同登记-删除请求体
  138. type ContractRegisterDelReq struct {
  139. ContractRegisterId int `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
  140. }
  141. // ContractRegisterUpdateStatusReq 合同登记-修改合同状态请求体
  142. type ContractRegisterUpdateStatusReq struct {
  143. ContractRegisterId int `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
  144. ContractStatus int `json:"contract_status" binding:"oneof=1 2 3 4 5" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回; 4-已终止, 5-邮件备案"`
  145. }
  146. // ContractRegisterAddReq 新增合同登记请求体
  147. type ContractRegisterAddReq struct {
  148. ContractCode string `json:"contract_code" binding:"required" description:"合同编号"`
  149. RelateContractCode string `json:"relate_contract_code" description:"关联合同编号"`
  150. CrmContractId int `json:"crm_contract_id" description:"CRM系统-合同ID"`
  151. ContractSource int `json:"contract_source" binding:"oneof=0 1" description:"合同来源: 0-非系统合同导入; 1-CRM合同导入"`
  152. CompanyName string `json:"company_name" binding:"required" description:"客户名称"`
  153. ActualCompanyName string `json:"actual_company_name" description:"实际使用方"`
  154. SellerIds string `json:"seller_ids" binding:"required" description:"CRM系统-销售ID"`
  155. ContractType int `json:"contract_type" binding:"oneof=1 2 3 4" description:"合同类型: 1-新签; 2-续约; 3-代付; 4-补充协议"`
  156. ContractAmount float64 `json:"contract_amount" binding:"required" description:"合同金额"`
  157. CurrencyUnit string `json:"currency_unit" binding:"required" description:"货币单位"`
  158. StartDate string `json:"start_date" binding:"required" description:"合同开始日期"`
  159. EndDate string `json:"end_date" binding:"required" description:"合同结束日期"`
  160. SignDate string `json:"sign_date" description:"合同签订日期"`
  161. AgreedPayTime string `json:"agreed_pay_time" description:"约定付款时间(如:生效日起10日内)"`
  162. ContractStatus int `json:"contract_status" binding:"oneof=1 2 3 4 5" description:"合同状态: 1-已审批; 2-单章寄出; 3-已签回; 4-已终止; 5-邮件备案"`
  163. Remark string `json:"remark" description:"备注信息"`
  164. ProductIds string `json:"product_ids" description:"产品ID:1-FICC; 2-权益, 如果两者都有,则用英文逗号拼接"`
  165. ServiceRemark string `json:"service_remark" description:"套餐备注"`
  166. HasPayment int `json:"has_payment" description:"是否有代付: 0-无; 1-有"`
  167. NewCompany int `json:"new_company" description:"是否为新客户: 0-否; 1-是"`
  168. Supplement int `json:"supplement" description:"是否为补录合同: 0-否; 1-是"`
  169. PrePayId int `json:"pre_pay_id" description:"预到款信息ID"`
  170. Services []ContractServiceAddReq `json:"services" description:"服务套餐内容"`
  171. ServiceAmount []ContractServiceAmountAddReq `json:"service_amount" description:"服务套餐金额"`
  172. }
  173. // ContractRegisterEditReq 编辑合同登记请求体
  174. type ContractRegisterEditReq struct {
  175. ContractRegisterId int `json:"contract_register_id" binding:"required,gte=1" description:"登记ID"`
  176. ContractRegisterAddReq
  177. }
  178. // CreateContractRegisterAndServices 新增合同登记及套餐
  179. func CreateContractRegisterAndServices(item *ContractRegister, serviceDetail []*ContractServiceAndDetail, serviceAmount []*ContractServiceAmount) (err error) {
  180. tx := global.DEFAULT_MYSQL.Begin()
  181. defer func() {
  182. if err != nil {
  183. tx.Rollback()
  184. } else {
  185. tx.Commit()
  186. }
  187. }()
  188. // 合同登记
  189. tx.Create(item)
  190. //nowTime := time.Now().Local()
  191. for i := 0; i < len(serviceDetail); i++ {
  192. // 合同服务
  193. t := serviceDetail[i]
  194. contractService := &ContractService{
  195. ContractRegisterId: item.ContractRegisterId,
  196. ProductId: t.ProductId,
  197. ServiceTemplateId: t.ServiceTemplateId,
  198. Title: t.Title,
  199. Value: t.Value,
  200. TableValue: t.TableValue,
  201. HasDetail: t.HasDetail,
  202. ChartPermissionId: t.ChartPermissionId,
  203. ChartPermissionIds: t.ChartPermissionIds,
  204. }
  205. contractService.Set()
  206. tx.Create(contractService)
  207. //// 合同服务详情
  208. //for j := 0; j < len(t.Detail); j++ {
  209. // contractServiceDetail := t.Detail[j]
  210. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  211. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  212. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  213. // contractServiceDetail.CreateTime = nowTime
  214. // tx.Create(contractServiceDetail)
  215. // t.Detail[j] = contractServiceDetail
  216. //}
  217. }
  218. for i := 0; i < len(serviceAmount); i++ {
  219. // 合同服务
  220. t := serviceAmount[i]
  221. tmp := &ContractServiceAmount{
  222. ContractRegisterId: item.ContractRegisterId,
  223. ProductId: t.ProductId,
  224. ServiceAmount: t.ServiceAmount,
  225. CurrencyUnit: t.CurrencyUnit,
  226. }
  227. tx.Create(tmp)
  228. }
  229. return
  230. }
  231. // UpdateContractRegister 更新合同登记
  232. func UpdateContractRegister(item *ContractRegister, updateCols []string, serviceDetail []*ContractServiceAndDetail,
  233. invoiceList []*ContractInvoice, invoiceUpdateCols []string, invoiceHandleType int, serviceAmount []*ContractServiceAmount) (err error) {
  234. tx := global.DEFAULT_MYSQL.Begin()
  235. defer func() {
  236. if err != nil {
  237. tx.Rollback()
  238. } else {
  239. tx.Commit()
  240. }
  241. }()
  242. // 更新合同登记
  243. if e := tx.Model(item).Select(updateCols).Updates(item).Error; e != nil {
  244. err = e
  245. return
  246. }
  247. // 删除原服务及详情信息
  248. sql := `DELETE FROM contract_service WHERE contract_register_id = ?`
  249. tx.Exec(sql, item.ContractRegisterId)
  250. sql = `DELETE FROM contract_service_detail WHERE contract_register_id = ?`
  251. tx.Exec(sql, item.ContractRegisterId)
  252. sql = `DELETE FROM contract_service_amount WHERE contract_register_id = ?`
  253. tx.Exec(sql, item.ContractRegisterId)
  254. // 新增合同服务
  255. //nowTime := time.Now().Local()
  256. for i := 0; i < len(serviceDetail); i++ {
  257. // 合同服务
  258. t := serviceDetail[i]
  259. contractService := &ContractService{
  260. ContractRegisterId: item.ContractRegisterId,
  261. ProductId: t.ProductId,
  262. ServiceTemplateId: t.ServiceTemplateId,
  263. Title: t.Title,
  264. Value: t.Value,
  265. TableValue: t.TableValue,
  266. HasDetail: t.HasDetail,
  267. ChartPermissionId: t.ChartPermissionId,
  268. ChartPermissionIds: t.ChartPermissionIds,
  269. }
  270. contractService.Set()
  271. tx.Create(contractService)
  272. //// 合同服务详情
  273. //for j := 0; j < len(t.Detail); j++ {
  274. // contractServiceDetail := t.Detail[j]
  275. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  276. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  277. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  278. // contractServiceDetail.CreateTime = nowTime
  279. // tx.Create(contractServiceDetail)
  280. // t.Detail[j] = contractServiceDetail
  281. //}
  282. }
  283. for i := 0; i < len(serviceAmount); i++ {
  284. // 合同服务
  285. t := serviceAmount[i]
  286. tmp := &ContractServiceAmount{
  287. ContractRegisterId: item.ContractRegisterId,
  288. ProductId: t.ProductId,
  289. ServiceAmount: t.ServiceAmount,
  290. CurrencyUnit: t.CurrencyUnit,
  291. }
  292. tx.Create(tmp)
  293. }
  294. // 开票到款操作类型: 0-无; 1-更新; 2-删除;
  295. if invoiceHandleType == 2 {
  296. sql = `UPDATE contract_invoice SET is_deleted = 1 WHERE contract_register_id = ?`
  297. if e := tx.Exec(sql, item.ContractRegisterId).Error; e != nil {
  298. err = e
  299. return
  300. }
  301. }
  302. if invoiceHandleType == 1 {
  303. for i := range invoiceList {
  304. if e := tx.Model(invoiceList[i]).Select(invoiceUpdateCols).Updates(invoiceList[i]).Error; e != nil {
  305. err = e
  306. return
  307. }
  308. }
  309. }
  310. return
  311. }
  312. // ContractRegisterDetailReq 合同登记-详情请求体
  313. type ContractRegisterDetailReq struct {
  314. ContractRegisterId int `json:"contract_register_id" form:"contract_register_id" binding:"required,gte=1"`
  315. }
  316. // ContractRegisterDetail-合同登记详情
  317. type ContractRegisterDetail struct {
  318. *ContractRegisterItem
  319. ServiceList []*ContractServiceAndDetail `json:"service_list" description:"合同服务及详情"`
  320. InvoiceList []*ContractInvoiceItem `json:"invoice_list" description:"开票信息"`
  321. PaymentList []*ContractInvoiceItem `json:"payment_list" description:"到款信息"`
  322. Logs []*ContractRegisterLogItem `json:"logs" description:"登记日志"`
  323. ServiceAmountList []*ContractServiceAmountItem `json:"service_amount_list" description:"合同套餐金额"`
  324. }
  325. // GetContractRegisterItemById 获取合同登记信息
  326. func GetContractRegisterItemById(id int) (result *ContractRegisterItem, err error) {
  327. item := new(ContractRegister)
  328. err = global.DEFAULT_MYSQL.Model(ContractRegister{}).
  329. Where("is_deleted = 0 AND contract_register_id = ?", id).
  330. First(&item).Error
  331. if err != nil && err != utils.ErrNoRow {
  332. return
  333. }
  334. result = formatContractRegister2Item(item)
  335. return
  336. }
  337. // GetContractRegisterItemPageList 获取合同登记列表-分页
  338. func GetContractRegisterItemPageList(page base.IPage, condition string, pars []interface{}) (count int64, results []*ContractRegisterItem, err error) {
  339. list := make([]*ContractRegister, 0)
  340. query := global.DEFAULT_MYSQL.Table("contract_register").
  341. Where("is_deleted = 0").
  342. Where("register_status <> 0")
  343. Where(condition, pars...)
  344. query.Count(&count)
  345. if len(page.GetOrderItemsString()) > 0 {
  346. query = query.Order(page.GetOrderItemsString())
  347. }
  348. err = query.Limit(int(page.GetPageSize())).Offset(int(page.Offset())).Find(&list).Error
  349. if err != nil {
  350. return
  351. }
  352. for i := range list {
  353. results = append(results, formatContractRegister2Item(list[i]))
  354. }
  355. return
  356. }
  357. // formatContractRegister2Item 格式化ContractRegister
  358. func formatContractRegister2Item(item *ContractRegister) (formatItem *ContractRegisterItem) {
  359. formatItem = new(ContractRegisterItem)
  360. formatItem.ContractRegisterId = item.ContractRegisterId
  361. formatItem.ContractCode = item.ContractCode
  362. formatItem.RelateContractCode = item.RelateContractCode
  363. formatItem.CrmContractId = item.CrmContractId
  364. formatItem.ContractSource = item.ContractSource
  365. formatItem.CompanyName = item.CompanyName
  366. formatItem.ActualCompanyName = item.ActualCompanyName
  367. formatItem.ProductIds = item.ProductIds
  368. formatItem.SellerId = item.SellerId
  369. formatItem.SellerName = item.SellerName
  370. formatItem.RaiSellerId = item.RaiSellerId
  371. formatItem.RaiSellerName = item.RaiSellerName
  372. formatItem.ContractType = item.ContractType
  373. formatItem.ContractAmount = item.ContractAmount
  374. formatItem.InvoicedAmount = item.InvoicedAmount
  375. formatItem.PaymentAmount = item.PaymentAmount
  376. formatItem.CurrencyUnit = item.CurrencyUnit
  377. formatItem.RMBRate = item.RMBRate
  378. formatItem.StartDate = utils.TimeTransferString(utils.FormatDate, item.StartDate)
  379. formatItem.EndDate = utils.TimeTransferString(utils.FormatDate, item.EndDate)
  380. formatItem.SignDate = utils.TimeTransferString(utils.FormatDate, item.SignDate)
  381. formatItem.AgreedPayTime = item.AgreedPayTime
  382. formatItem.ContractStatus = item.ContractStatus
  383. formatItem.RegisterStatus = item.RegisterStatus
  384. formatItem.Remark = item.Remark
  385. formatItem.ServiceRemark = item.ServiceRemark
  386. formatItem.HasPayment = item.HasPayment
  387. formatItem.NewCompany = item.NewCompany
  388. formatItem.CreateTime = utils.TimeTransferString(utils.FormatDateTime, item.CreateTime)
  389. return
  390. }
  391. // CreateImportContractRegister 新增导入的合同登记及套餐、开票到款信息
  392. func CreateImportContractRegister(item *ContractRegister, serviceList []*ContractService, invoiceList []*ContractInvoice, serviceAmountList []*ContractServiceAmount) (newId int, err error) {
  393. tx := global.DEFAULT_MYSQL.Begin()
  394. defer func() {
  395. if err != nil {
  396. tx.Rollback()
  397. } else {
  398. tx.Commit()
  399. }
  400. }()
  401. // 新增合同登记
  402. if e := tx.Create(item).Error; e != nil {
  403. err = e
  404. return
  405. }
  406. newId = item.ContractRegisterId
  407. // 新增套餐
  408. if len(serviceList) > 0 {
  409. // 过滤重复的数组
  410. serviceMap := make(map[int]struct{})
  411. addServiceList := make([]*ContractService, 0)
  412. for i := range serviceList {
  413. if _, ok := serviceMap[serviceList[i].ServiceTemplateId]; !ok {
  414. serviceList[i].ContractRegisterId = newId
  415. addServiceList = append(addServiceList, serviceList[i])
  416. serviceMap[serviceList[i].ServiceTemplateId] = struct{}{}
  417. }
  418. }
  419. if e := tx.CreateInBatches(addServiceList, len(addServiceList)).Error; e != nil {
  420. err = e
  421. return
  422. }
  423. }
  424. // 新增合同套餐金额相关信息
  425. if len(serviceAmountList) > 0 {
  426. for i := range serviceAmountList {
  427. serviceAmountList[i].ContractRegisterId = newId
  428. }
  429. if e := tx.CreateInBatches(serviceAmountList, len(serviceAmountList)).Error; e != nil {
  430. err = e
  431. return
  432. }
  433. }
  434. // 新增开票/到款
  435. if len(invoiceList) > 0 {
  436. for i := range invoiceList {
  437. invoiceList[i].ContractRegisterId = newId
  438. }
  439. if e := tx.CreateInBatches(invoiceList, len(invoiceList)).Error; e != nil {
  440. err = e
  441. return
  442. }
  443. }
  444. return
  445. }
  446. func GetContractInfoByRegisterIds(registerIds []int) (list []ContractRegister, err error) {
  447. err = global.DEFAULT_MYSQL.Model(ContractRegister{}).
  448. Where("is_deleted = 0 AND contract_register_id in ?", registerIds).
  449. Find(&list).Error
  450. return
  451. }
  452. // CreateContractRegisterAndServices 新增合同登记及套餐
  453. func CreateContractRegisterAndServicesAndPayMent(item *ContractRegister, serviceDetail []*ContractServiceAndDetail) (err error) {
  454. tx := global.DEFAULT_MYSQL.Begin()
  455. defer func() {
  456. if err != nil {
  457. tx.Rollback()
  458. } else {
  459. tx.Commit()
  460. }
  461. }()
  462. // 合同登记
  463. tx.Create(item)
  464. //nowTime := time.Now().Local()
  465. for i := 0; i < len(serviceDetail); i++ {
  466. // 合同服务
  467. t := serviceDetail[i]
  468. contractService := &ContractService{
  469. ContractRegisterId: item.ContractRegisterId,
  470. ProductId: t.ProductId,
  471. ServiceTemplateId: t.ServiceTemplateId,
  472. Title: t.Title,
  473. Value: t.Value,
  474. TableValue: t.TableValue,
  475. HasDetail: t.HasDetail,
  476. ChartPermissionId: t.ChartPermissionId,
  477. ChartPermissionIds: t.ChartPermissionIds,
  478. }
  479. contractService.Set()
  480. tx.Create(contractService)
  481. //// 合同服务详情
  482. //for j := 0; j < len(t.Detail); j++ {
  483. // contractServiceDetail := t.Detail[j]
  484. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  485. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  486. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  487. // contractServiceDetail.CreateTime = nowTime
  488. // tx.Create(contractServiceDetail)
  489. // t.Detail[j] = contractServiceDetail
  490. //}
  491. }
  492. return
  493. }
  494. type CheckContractNameReq struct {
  495. CompanyName string `json:"company_name" form:"company_name" description:"客户名称"`
  496. StartDate string `json:"start_date" form:"start_date" description:"合同开始日期"`
  497. EndDate string `json:"end_date" form:"end_date" description:"合同结束日期"`
  498. }
  499. type CheckContractNameResp struct {
  500. Exist int `json:"exist" description:"是否存在相似的合同:0不存在,1存在"`
  501. }
  502. // UpdateContractPreRegister 更新合同预登记
  503. func UpdateContractPreRegister(item *ContractRegister, updateCols []string, serviceDetail []*ContractServiceAndDetail,
  504. invoiceList []*ContractInvoice, invoiceUpdateCols []string) (err error) {
  505. tx := global.DEFAULT_MYSQL.Begin()
  506. defer func() {
  507. if err != nil {
  508. tx.Rollback()
  509. } else {
  510. tx.Commit()
  511. }
  512. }()
  513. // 更新合同登记
  514. if e := tx.Model(item).Select(updateCols).Updates(item).Error; e != nil {
  515. err = e
  516. return
  517. }
  518. // 删除原服务及详情信息
  519. sql := `DELETE FROM contract_service WHERE contract_register_id = ?`
  520. tx.Exec(sql, item.ContractRegisterId)
  521. // 新增合同服务
  522. //nowTime := time.Now().Local()
  523. for i := 0; i < len(serviceDetail); i++ {
  524. // 合同服务
  525. t := serviceDetail[i]
  526. contractService := &ContractService{
  527. ContractRegisterId: item.ContractRegisterId,
  528. ProductId: t.ProductId,
  529. ServiceTemplateId: t.ServiceTemplateId,
  530. Title: t.Title,
  531. Value: t.Value,
  532. TableValue: t.TableValue,
  533. HasDetail: t.HasDetail,
  534. ChartPermissionId: t.ChartPermissionId,
  535. ChartPermissionIds: t.ChartPermissionIds,
  536. }
  537. contractService.Set()
  538. tx.Create(contractService)
  539. //// 合同服务详情
  540. //for j := 0; j < len(t.Detail); j++ {
  541. // contractServiceDetail := t.Detail[j]
  542. // contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  543. // contractServiceDetail.ContractRegisterId = item.ContractRegisterId
  544. // contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  545. // contractServiceDetail.CreateTime = nowTime
  546. // tx.Create(contractServiceDetail)
  547. // t.Detail[j] = contractServiceDetail
  548. //}
  549. }
  550. for i := range invoiceList {
  551. if e := tx.Model(invoiceList[i]).Select(invoiceUpdateCols).Updates(invoiceList[i]).Error; e != nil {
  552. err = e
  553. return
  554. }
  555. }
  556. return
  557. }