contract.go 17 KB

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