contract.go 14 KB

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