contract.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. package contract
  2. import (
  3. "fmt"
  4. "hongze/hongze_mobile_admin/models/custom/contract"
  5. "hongze/hongze_mobile_admin/utils"
  6. "rdluck_tools/orm"
  7. "time"
  8. )
  9. //合同
  10. type Contract struct {
  11. ContractId int `orm:"column(contract_id);pk"`
  12. ContractCode string `description:"合同编号,长度32位"`
  13. SellerId int `description:"所属销售id"`
  14. SellerName string `description:"所属销售名称"`
  15. ProductId int `description:"产品id,1:ficc;2:权益"`
  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. ModifyContent string `description:"修改内容"`
  36. ApprovalRemark string `description:"审核备注"`
  37. FileUrl string `description:"合同文件地址"`
  38. CheckBackFileUrl string `description:"签回合同文件地址"`
  39. RescindFileUrl string `description:"解约合同文件地址"`
  40. TemplateId int `description:"模板id"`
  41. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  42. IsDelete int `description:"是否已经删除,0:未删除,1:已删除",json:"-"`
  43. ApproveTime time.Time `description:"审批时间"`
  44. InvalidTime time.Time `description:"作废时间"`
  45. CheckBackFileTime time.Time `description:"合同签回时间"`
  46. RescindTime time.Time `description:"解约时间"`
  47. ModifyTime time.Time `description:"合同最近一次修改时间"`
  48. CreateTime time.Time `description:"合同添加时间"`
  49. }
  50. //更新合同基础信息
  51. func (contract *Contract) Update(cols []string) (err error) {
  52. o := orm.NewOrm()
  53. _, err = o.Update(contract, cols...)
  54. return
  55. }
  56. //根据合同id获取合同信息
  57. func GetContractById(contractId int) (contractInfo *Contract, err error) {
  58. o := orm.NewOrm()
  59. sql := `select * from contract where contract_id = ? `
  60. err = o.Raw(sql, contractId).QueryRow(&contractInfo)
  61. return
  62. }
  63. //合同详情信息(包含服务信息等)
  64. type ContractDetail struct {
  65. ContractId int `description:"合同唯一id"`
  66. ContractCode string `description:"合同编号,长度32位"`
  67. SellerId int `description:"所属销售id"`
  68. SellerName string `description:"所属销售名称"`
  69. ProductId int `description:"产品id,1:ficc;2:权益"`
  70. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  71. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已解约'"`
  72. StartDate time.Time `description:"合同开始日期"`
  73. EndDate time.Time `description:"合同结束日期"`
  74. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  75. Price float64 `description:"实际金额,优惠后的金额"`
  76. PayRemark string `description:"付款方式说明,长度255位"`
  77. PayChannel string `description:"付款渠道,长度255位"`
  78. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  79. CreditCode string `description:"社会统一信用代码,长度64位"`
  80. ProvinceId int `description:"省级id"`
  81. Province string `description:"省级名称,长度16位"`
  82. CityId int `description:"市级id"`
  83. City string `description:"市级名称,长度32位"`
  84. Address string `description:"详细地址"`
  85. Fax string `description:"传真,长度32位"`
  86. Phone string `description:"电话,长度32位"`
  87. Postcode string `description:"邮编,长度16位"`
  88. Remark string `description:"补充内容,长度255位"`
  89. SellerRemark string `description:"销售备注,长度255位"`
  90. ModifyContent string `description:"修改内容"`
  91. ApprovalRemark string `description:"审核备注"`
  92. FileUrl string `description:"合同文件地址"`
  93. CheckBackFileUrl string `description:"签回合同文件地址"`
  94. RescindFileUrl string `description:"解约合同文件地址"`
  95. TemplateId int `description:"模板id"`
  96. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  97. IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"`
  98. ModifyTime time.Time `description:"合同最近一次修改时间"`
  99. CreateTime time.Time `description:"合同添加时间"`
  100. ApproveTime time.Time `description:"审批时间"`
  101. InvalidTime time.Time `description:"作废时间"`
  102. CheckBackFileTime time.Time `description:"合同签回时间"`
  103. RescindTime time.Time `description:"解约时间"`
  104. StartDateStr string `description:"合同起始时间"`
  105. EndDateStr string `description:"合同结束时间"`
  106. ModifyTimeStr string `description:"最近一次更新时间"`
  107. CreateTimeStr string `description:"合同添加时间"`
  108. ApproveTimeStr string `description:"审批时间"`
  109. InvalidTimeStr string `description:"作废时间"`
  110. CheckBackFileTimeStr string `description:"合同签回时间"`
  111. RescindTimeStr string `description:"解约时间"`
  112. Service []*contract.ContractServiceAndDetail
  113. }
  114. //根据合同id获取合同详情信息
  115. func GetContractDetailById(contractId int) (contractInfo *ContractDetail, err error) {
  116. o := orm.NewOrm()
  117. sql := `select * from contract where contract_id = ? `
  118. err = o.Raw(sql, contractId).QueryRow(&contractInfo)
  119. return
  120. }
  121. //合同添加
  122. func AddContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (newContract *Contract, err error) {
  123. o := orm.NewOrm()
  124. o.Begin()
  125. defer func() {
  126. if err != nil {
  127. o.Rollback()
  128. } else {
  129. o.Commit()
  130. }
  131. }()
  132. //合同数据入库
  133. contractId, err := o.Insert(contractInfo)
  134. if err != nil {
  135. return
  136. }
  137. contractInfo.ContractId = int(contractId)
  138. for i := 0; i < len(contractServiceAndDetailList); i++ {
  139. //合同服务数据入库
  140. tmpContractService := contractServiceAndDetailList[i]
  141. contractService := &contract.ContractService{
  142. ContractId: int(contractId),
  143. ProductId: contractInfo.ProductId,
  144. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  145. Title: tmpContractService.Title,
  146. Value: tmpContractService.Value,
  147. HasDetail: tmpContractService.HasDetail,
  148. CreateTime: time.Now(),
  149. }
  150. contractServiceId, serviceErr := o.Insert(contractService)
  151. if serviceErr != nil {
  152. err = serviceErr
  153. return
  154. }
  155. contractService.ContractServiceId = int(contractServiceId)
  156. //合同服务详情入库
  157. for j := 0; j < len(tmpContractService.DetailList); j++ {
  158. contractServiceDetail := tmpContractService.DetailList[j]
  159. //合同服务编号
  160. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  161. contractServiceDetail.ContractId = int(contractId)
  162. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  163. //合同服务详情入库
  164. contractServiceDetailId, detailErr := o.Insert(contractServiceDetail)
  165. if detailErr != nil {
  166. err = detailErr
  167. return
  168. }
  169. contractServiceDetail.Id = int(contractServiceDetailId)
  170. tmpContractService.DetailList[j] = contractServiceDetail
  171. }
  172. }
  173. newContract = contractInfo
  174. return
  175. }
  176. type ContractList struct {
  177. ContractId int `description:"合同唯一id"`
  178. ContractCode string `description:"合同编号,长度32位"`
  179. SellerId int `description:"所属销售id"`
  180. SellerName string `description:"所属销售名称"`
  181. ProductId int `description:"产品id,1:ficc;2:权益"`
  182. ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"`
  183. Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"`
  184. StartDate time.Time `description:"合同开始日期"`
  185. EndDate time.Time `description:"合同结束日期"`
  186. OriginalPrice float64 `description:"合同原金额,优惠前的金额"`
  187. Price float64 `description:"实际金额,优惠后的金额"`
  188. PayRemark string `description:"付款方式说明,长度255位"`
  189. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  190. UseCompanyName string `description:"使用方名称,长度32位"`
  191. CreditCode string `description:"社会统一信用代码,长度64位"`
  192. ProvinceId int `description:"省级id"`
  193. Province string `description:"省级名称,长度16位"`
  194. CityId int `description:"市级id"`
  195. City string `description:"市级名称,长度32位"`
  196. Address string `description:"详细地址"`
  197. Fax string `description:"传真,长度32位"`
  198. Phone string `description:"电话,长度32位"`
  199. Postcode string `description:"邮编,长度16位"`
  200. Remark string `json:"-" description:"补充内容,长度255位"`
  201. SellerRemark string `description:"销售备注,长度255位"`
  202. ApprovalRemark string `description:"审核备注"`
  203. ModifyContent string `description:"修改内容"`
  204. FileUrl string `description:"合同文件地址"`
  205. CheckBackFileUrl string `description:"签回合同文件地址"`
  206. TemplateId int `description:"模板id"`
  207. SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"`
  208. IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"`
  209. ModifyTime time.Time `description:"合同最近一次修改时间"`
  210. CreateTime time.Time `description:"合同添加时间"`
  211. ApproveTime time.Time `description:"审批时间"`
  212. InvalidTime time.Time `description:"作废时间"`
  213. CheckBackFileTime time.Time `description:"合同签回时间"`
  214. RescindTime time.Time `description:"解约时间"`
  215. StartDateStr string `description:"合同起始时间"`
  216. EndDateStr string `description:"合同结束时间"`
  217. ModifyTimeStr string `description:"最近一次更新时间"`
  218. CreateTimeStr string `description:"合同添加时间"`
  219. ApproveTimeStr string `description:"审批时间"`
  220. InvalidTimeStr string `description:"作废时间"`
  221. CheckBackFileTimeStr string `description:"合同签回时间"`
  222. RescindTimeStr string `description:"解约时间"`
  223. Service []*contract.ContractServiceAndDetail
  224. }
  225. //获取合同列表数据数量
  226. func GetContractListCount(condition string, pars []interface{}) (count int, err error) {
  227. o := orm.NewOrm()
  228. sql := "select count(*) AS COUNT from contract where 1=1 AND is_delete = 0 "
  229. sql += condition
  230. err = o.Raw(sql, pars).QueryRow(&count)
  231. return
  232. }
  233. //获取合同列表数据
  234. func GetContractList(condition string, pars []interface{}, startSize, pageSize int) (list []*ContractList, err error) {
  235. o := orm.NewOrm()
  236. sql := "select * from contract where 1=1 AND is_delete = 0 "
  237. sql += condition
  238. sql += ` order by modify_time desc LIMIT ?,? `
  239. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list)
  240. return
  241. }
  242. //修改合同
  243. func EditContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (err error) {
  244. o := orm.NewOrm()
  245. o.Begin()
  246. defer func() {
  247. if err != nil {
  248. o.Rollback()
  249. } else {
  250. o.Commit()
  251. }
  252. }()
  253. contractId := contractInfo.ContractId
  254. //合同数据入库
  255. _, err = o.Update(contractInfo)
  256. if err != nil {
  257. return
  258. }
  259. //删除合同的原始服务信息
  260. sql := `delete from contract_service where contract_id = ?`
  261. _, err = o.Raw(sql, contractId).Exec()
  262. //删除合同的原始服务详情信息
  263. sql = `delete from contract_service_detail where contract_id = ?`
  264. _, err = o.Raw(sql, contractId).Exec()
  265. for i := 0; i < len(contractServiceAndDetailList); i++ {
  266. //合同服务数据入库
  267. tmpContractService := contractServiceAndDetailList[i]
  268. contractService := &contract.ContractService{
  269. ContractId: int(contractId),
  270. Title: tmpContractService.Title,
  271. ProductId: contractInfo.ProductId,
  272. ServiceTemplateId: tmpContractService.ServiceTemplateId,
  273. Value: tmpContractService.Value,
  274. HasDetail: tmpContractService.HasDetail,
  275. CreateTime: time.Now(),
  276. }
  277. contractServiceId, serviceErr := o.Insert(contractService)
  278. if serviceErr != nil {
  279. err = serviceErr
  280. return
  281. }
  282. contractService.ContractServiceId = int(contractServiceId)
  283. //合同服务详情入库
  284. for j := 0; j < len(tmpContractService.DetailList); j++ {
  285. contractServiceDetail := tmpContractService.DetailList[j]
  286. //合同服务编号
  287. contractServiceDetail.ContractServiceId = contractService.ContractServiceId
  288. contractServiceDetail.ContractId = contractService.ContractId
  289. contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId
  290. //合同服务详情入库
  291. contractServiceDetailId, detailErr := o.Insert(contractServiceDetail)
  292. if detailErr != nil {
  293. err = detailErr
  294. return
  295. }
  296. contractServiceDetail.Id = int(contractServiceDetailId)
  297. tmpContractService.DetailList[j] = contractServiceDetail
  298. }
  299. }
  300. return
  301. }
  302. //添加生成后的合同地址
  303. func AddContractPdf(contractId int, pdfUrl string) (err error) {
  304. o := orm.NewOrm()
  305. //删除合同的原始服务信息
  306. sql := `update contract set file_url=? where contract_id = ?`
  307. _, err = o.Raw(sql, pdfUrl, contractId).Exec()
  308. return
  309. }
  310. //删除合同
  311. func DeleteContract(contractInfo *Contract) (err error) {
  312. o := orm.NewOrm()
  313. o.Begin()
  314. defer func() {
  315. if err != nil {
  316. o.Rollback()
  317. } else {
  318. o.Commit()
  319. }
  320. }()
  321. contractInfo.IsDelete = 1
  322. contractInfo.ModifyTime = time.Now()
  323. //合同数据入库
  324. _, err = o.Update(contractInfo)
  325. if err != nil {
  326. return
  327. }
  328. return
  329. }
  330. //作废合同
  331. func InvalidContract(contractInfo *Contract) (err error) {
  332. o := orm.NewOrm()
  333. o.Begin()
  334. defer func() {
  335. if err != nil {
  336. o.Rollback()
  337. } else {
  338. o.Commit()
  339. }
  340. }()
  341. contractInfo.Status = "已作废"
  342. contractInfo.ModifyTime = time.Now()
  343. contractInfo.InvalidTime = time.Now()
  344. //合同数据入库
  345. _, err = o.Update(contractInfo, "Status", "ModifyTime", "InvalidTime")
  346. if err != nil {
  347. return
  348. }
  349. return
  350. }
  351. //生成合同编号
  352. func GetCompanyContractCode(productId int) (companyCode string, err error) {
  353. var num int
  354. o := orm.NewOrm()
  355. today := utils.GetToday(utils.FormatDate)
  356. sql := `SELECT COUNT(1) AS num FROM contract where create_time>=?`
  357. err = o.Raw(sql, today).QueryRow(&num)
  358. if err != nil {
  359. return
  360. }
  361. companyType := ""
  362. switch productId {
  363. case 1:
  364. companyType = "FICC"
  365. case 2:
  366. companyType = "EQ"
  367. }
  368. companyCode = "HZ" + companyType + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  369. return
  370. }
  371. type CompanyNameList struct {
  372. CompanyName string `description:"客户名称,甲方名称,长度32位"`
  373. }
  374. //获取客户名称列表数据
  375. func GetCompanyNameList(sellerId int, keyword, status string) (list []*CompanyNameList, err error) {
  376. o := orm.NewOrm()
  377. sql := `select * from contract where is_delete=0 and (company_name like '%` + keyword + `%' or credit_code like '%` + keyword + `%') `
  378. if status != "" {
  379. sql += ` AND status='` + status + `' `
  380. }
  381. pars := make([]interface{}, 0)
  382. if sellerId > 0 {
  383. sql += ` and seller_id=? `
  384. pars = append(pars, sellerId)
  385. }
  386. sql += ` group by company_name order by modify_time desc `
  387. _, err = o.Raw(sql, pars).QueryRows(&list)
  388. return
  389. }