contract.go 15 KB

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