contract_register.go 29 KB

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