package contract import ( "fmt" "github.com/beego/beego/v2/client/orm" "hongze/hongze_mobile_admin/models/custom/contract" "hongze/hongze_mobile_admin/models/tables/company_report_permission" "hongze/hongze_mobile_admin/utils" "time" ) // 合同 type Contract struct { ContractId int `orm:"column(contract_id);pk"` ContractCode string `description:"合同编号,长度32位"` SellerId int `description:"所属销售id"` SellerName string `description:"所属销售名称"` ProductId int `description:"产品id,1:ficc;2:权益"` ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"` ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"` Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已签回','已解约'"` StartDate time.Time `description:"合同开始日期"` EndDate time.Time `description:"合同结束日期"` OriginalPrice float64 `description:"合同原金额,优惠前的金额"` Price float64 `description:"实际金额,优惠后的金额"` PayRemark string `description:"付款方式说明,长度255位"` PayChannel string `description:"付款渠道,长度255位"` CompanyName string `description:"客户名称,甲方名称,长度32位"` CreditCode string `description:"社会统一信用代码,长度64位"` ProvinceId int `description:"省级id"` Province string `description:"省级名称,长度16位"` CityId int `description:"市级id"` City string `description:"市级名称,长度32位"` Address string `description:"详细地址"` Fax string `description:"传真,长度32位"` Phone string `description:"电话,长度32位"` Postcode string `description:"邮编,长度16位"` Remark string `description:"补充内容,长度255位"` ModifyContent string `description:"修改内容"` ApprovalRemark string `description:"审核备注"` FileUrl string `description:"合同文件地址"` CheckBackFileUrl string `description:"签回合同文件地址"` RescindFileUrl string `description:"解约合同文件地址"` TemplateId int `description:"模板id"` SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"` IsDelete int `description:"是否已经删除,0:未删除,1:已删除",json:"-"` ApproveTime time.Time `description:"审批时间"` InvalidTime time.Time `description:"作废时间"` CheckBackFileTime time.Time `description:"合同签回时间"` RescindTime time.Time `description:"解约时间"` ModifyTime time.Time `description:"合同最近一次修改时间"` CreateTime time.Time `description:"合同添加时间"` } // 更新合同基础信息 func (contract *Contract) Update(cols []string) (err error) { o := orm.NewOrm() _, err = o.Update(contract, cols...) return } // 根据合同id获取合同信息 func GetContractById(contractId int) (contractInfo *Contract, err error) { o := orm.NewOrm() sql := `select * from contract where contract_id = ? ` err = o.Raw(sql, contractId).QueryRow(&contractInfo) return } // 合同详情信息(包含服务信息等) type ContractDetail struct { ContractId int `description:"合同唯一id"` ContractCode string `description:"合同编号,长度32位"` SellerId int `description:"所属销售id"` SellerName string `description:"所属销售名称"` ProductId int `description:"产品id,1:ficc;2:权益"` ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"` ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"` Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废','已解约'"` StartDate time.Time `description:"合同开始日期"` EndDate time.Time `description:"合同结束日期"` OriginalPrice float64 `description:"合同原金额,优惠前的金额"` Price float64 `description:"实际金额,优惠后的金额"` PayRemark string `description:"付款方式说明,长度255位"` PayChannel string `description:"付款渠道,长度255位"` CompanyName string `description:"客户名称,甲方名称,长度32位"` CreditCode string `description:"社会统一信用代码,长度64位"` ProvinceId int `description:"省级id"` Province string `description:"省级名称,长度16位"` CityId int `description:"市级id"` City string `description:"市级名称,长度32位"` Address string `description:"详细地址"` Fax string `description:"传真,长度32位"` Phone string `description:"电话,长度32位"` Postcode string `description:"邮编,长度16位"` Remark string `description:"补充内容,长度255位"` SellerRemark string `description:"销售备注,长度255位"` ModifyContent string `description:"修改内容"` ApprovalRemark string `description:"审核备注"` FileUrl string `description:"合同文件地址"` CheckBackFileUrl string `description:"签回合同文件地址"` RescindFileUrl string `description:"解约合同文件地址"` TemplateId int `description:"模板id"` SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"` IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"` ModifyTime time.Time `description:"合同最近一次修改时间"` CreateTime time.Time `description:"合同添加时间"` ApproveTime time.Time `description:"审批时间"` InvalidTime time.Time `description:"作废时间"` CheckBackFileTime time.Time `description:"合同签回时间"` RescindTime time.Time `description:"解约时间"` StartDateStr string `description:"合同起始时间"` EndDateStr string `description:"合同结束时间"` ModifyTimeStr string `description:"最近一次更新时间"` CreateTimeStr string `description:"合同添加时间"` ApproveTimeStr string `description:"审批时间"` InvalidTimeStr string `description:"作废时间"` CheckBackFileTimeStr string `description:"合同签回时间"` RescindTimeStr string `description:"解约时间"` Service []*contract.ContractServiceAndDetail PermissionLookList []*company_report_permission.PermissionLookList `description:"合同里面的权限列表"` RelationContractDetailList []*ContractDetail `description:"关联合同详情"` } // GetContractDetailById 根据合同id获取合同详情信息 func GetContractDetailById(contractId int) (contractInfo *ContractDetail, err error) { o := orm.NewOrm() sql := `select * from contract where contract_id = ? ` err = o.Raw(sql, contractId).QueryRow(&contractInfo) return } // 合同添加 func AddContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (newContract *Contract, err error) { o := orm.NewOrm() tx, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = tx.Rollback() } else { _ = tx.Commit() } }() //合同数据入库 contractId, err := tx.Insert(contractInfo) if err != nil { return } contractInfo.ContractId = int(contractId) for i := 0; i < len(contractServiceAndDetailList); i++ { //合同服务数据入库 tmpContractService := contractServiceAndDetailList[i] contractService := &contract.ContractService{ ContractId: int(contractId), ProductId: contractInfo.ProductId, ServiceTemplateId: tmpContractService.ServiceTemplateId, Title: tmpContractService.Title, Value: tmpContractService.Value, HasDetail: tmpContractService.HasDetail, CreateTime: time.Now(), } contractServiceId, serviceErr := tx.Insert(contractService) if serviceErr != nil { err = serviceErr return } contractService.ContractServiceId = int(contractServiceId) //合同服务详情入库 for j := 0; j < len(tmpContractService.DetailList); j++ { contractServiceDetail := tmpContractService.DetailList[j] //合同服务编号 contractServiceDetail.ContractServiceId = contractService.ContractServiceId contractServiceDetail.ContractId = int(contractId) contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId //合同服务详情入库 contractServiceDetailId, detailErr := tx.Insert(contractServiceDetail) if detailErr != nil { err = detailErr return } contractServiceDetail.Id = int(contractServiceDetailId) tmpContractService.DetailList[j] = contractServiceDetail } } newContract = contractInfo return } type ContractList struct { ContractId int `description:"合同唯一id"` ContractCode string `description:"合同编号,长度32位"` SellerId int `description:"所属销售id"` SellerName string `description:"所属销售名称"` ProductId int `description:"产品id,1:ficc;2:权益"` ContractBusinessType string `description:"合同业务类型,枚举值:'业务合同','代付合同'"` ContractType string `description:"合同类型,枚举值:'新签合同','续约合同','补充协议'"` Status string `description:"合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"` StartDate time.Time `description:"合同开始日期"` EndDate time.Time `description:"合同结束日期"` OriginalPrice float64 `description:"合同原金额,优惠前的金额"` Price float64 `description:"实际金额,优惠后的金额"` PayRemark string `description:"付款方式说明,长度255位"` CompanyName string `description:"客户名称,甲方名称,长度32位"` UseCompanyName string `description:"使用方名称,长度32位"` CreditCode string `description:"社会统一信用代码,长度64位"` ProvinceId int `description:"省级id"` Province string `description:"省级名称,长度16位"` CityId int `description:"市级id"` City string `description:"市级名称,长度32位"` Address string `description:"详细地址"` Fax string `description:"传真,长度32位"` Phone string `description:"电话,长度32位"` Postcode string `description:"邮编,长度16位"` Remark string `json:"-" description:"补充内容,长度255位"` SellerRemark string `description:"销售备注,长度255位"` ApprovalRemark string `description:"审核备注"` ModifyContent string `description:"修改内容"` FileUrl string `description:"合同文件地址"` CheckBackFileUrl string `description:"签回合同文件地址"` TemplateId int `description:"模板id"` SourceId int `description:"来源合同id,默认是0;如果是通过其他合同复制过来的,那么就是原合同的id"` IsDelete int `json:"-" description:"是否已经删除,0:未删除,1:已删除"` ModifyTime time.Time `description:"合同最近一次修改时间"` CreateTime time.Time `description:"合同添加时间"` ApproveTime time.Time `description:"审批时间"` InvalidTime time.Time `description:"作废时间"` CheckBackFileTime time.Time `description:"合同签回时间"` RescindTime time.Time `description:"解约时间"` StartDateStr string `description:"合同起始时间"` EndDateStr string `description:"合同结束时间"` ModifyTimeStr string `description:"最近一次更新时间"` CreateTimeStr string `description:"合同添加时间"` ApproveTimeStr string `description:"审批时间"` InvalidTimeStr string `description:"作废时间"` CheckBackFileTimeStr string `description:"合同签回时间"` RescindTimeStr string `description:"解约时间"` Service []*contract.ContractServiceAndDetail } // 获取合同列表数据数量 func GetContractListCount(condition string, pars []interface{}) (count int, err error) { o := orm.NewOrm() sql := "select count(*) AS COUNT from contract where 1=1 AND is_delete = 0 " sql += condition err = o.Raw(sql, pars).QueryRow(&count) return } // 获取合同列表数据 func GetContractList(condition string, pars []interface{}, startSize, pageSize int) (list []*ContractList, err error) { o := orm.NewOrm() sql := "select * from contract where 1=1 AND is_delete = 0 " sql += condition sql += ` order by modify_time desc LIMIT ?,? ` _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list) return } // GetJoinContractListCount 获取合同列表数据数量 func GetJoinContractListCount(condition, joinStr string, pars []interface{}) (count int, err error) { o := orm.NewOrm() sql := `select count(a.contract_id) AS COUNT from contract a ` + joinStr sql += ` where a.is_delete = 0 ` sql += condition err = o.Raw(sql, pars).QueryRow(&count) return } // GetJoinContractList 获取合同列表数据 func GetJoinContractList(condition, joinStr string, pars []interface{}, startSize, pageSize int) (list []*ContractList, err error) { o := orm.NewOrm() sql := "select a.* from contract a " + joinStr sql += ` where a.is_delete = 0 ` sql += condition sql += ` order by modify_time desc LIMIT ?,? ` _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&list) return } // 修改合同 func EditContract(contractInfo *Contract, contractServiceAndDetailList []*contract.ContractServiceAndDetail) (err error) { o := orm.NewOrm() tx, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = tx.Rollback() } else { _ = tx.Commit() } }() contractId := contractInfo.ContractId //合同数据入库 _, err = tx.Update(contractInfo) if err != nil { return } //删除合同的原始服务信息 sql := `delete from contract_service where contract_id = ?` _, err = tx.Raw(sql, contractId).Exec() //删除合同的原始服务详情信息 sql = `delete from contract_service_detail where contract_id = ?` _, err = tx.Raw(sql, contractId).Exec() for i := 0; i < len(contractServiceAndDetailList); i++ { //合同服务数据入库 tmpContractService := contractServiceAndDetailList[i] contractService := &contract.ContractService{ ContractId: int(contractId), Title: tmpContractService.Title, ProductId: contractInfo.ProductId, ServiceTemplateId: tmpContractService.ServiceTemplateId, Value: tmpContractService.Value, HasDetail: tmpContractService.HasDetail, CreateTime: time.Now(), } contractServiceId, serviceErr := tx.Insert(contractService) if serviceErr != nil { err = serviceErr return } contractService.ContractServiceId = int(contractServiceId) //合同服务详情入库 for j := 0; j < len(tmpContractService.DetailList); j++ { contractServiceDetail := tmpContractService.DetailList[j] //合同服务编号 contractServiceDetail.ContractServiceId = contractService.ContractServiceId contractServiceDetail.ContractId = contractService.ContractId contractServiceDetail.ServiceTemplateId = contractService.ServiceTemplateId //合同服务详情入库 contractServiceDetailId, detailErr := tx.Insert(contractServiceDetail) if detailErr != nil { err = detailErr return } contractServiceDetail.Id = int(contractServiceDetailId) tmpContractService.DetailList[j] = contractServiceDetail } } return } // 添加生成后的合同地址 func AddContractPdf(contractId int, pdfUrl string) (err error) { o := orm.NewOrm() //删除合同的原始服务信息 sql := `update contract set file_url=? where contract_id = ?` _, err = o.Raw(sql, pdfUrl, contractId).Exec() return } // 删除合同 func DeleteContract(contractInfo *Contract) (err error) { o := orm.NewOrm() tx, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = tx.Rollback() } else { _ = tx.Commit() } }() contractInfo.IsDelete = 1 contractInfo.ModifyTime = time.Now() //合同数据入库 _, err = tx.Update(contractInfo) if err != nil { return } return } // 作废合同 func InvalidContract(contractInfo *Contract) (err error) { o := orm.NewOrm() tx, err := o.Begin() if err != nil { return } defer func() { if err != nil { _ = tx.Rollback() } else { _ = tx.Commit() } }() contractInfo.Status = "已作废" contractInfo.ModifyTime = time.Now() contractInfo.InvalidTime = time.Now() //合同数据入库 _, err = tx.Update(contractInfo, "Status", "ModifyTime", "InvalidTime") if err != nil { return } return } // 生成合同编号 func GetCompanyContractCode(productId int) (companyCode string, err error) { var num int o := orm.NewOrm() today := utils.GetToday(utils.FormatDate) sql := `SELECT COUNT(1) AS num FROM contract where create_time>=?` err = o.Raw(sql, today).QueryRow(&num) if err != nil { return } companyType := "" switch productId { case 1: companyType = "FICC" case 2: companyType = "EQ" } companyCode = "HZ" + companyType + time.Now().Format("20060102") + fmt.Sprintf("%03d", num) return } type CompanyNameList struct { CompanyName string `description:"客户名称,甲方名称,长度32位"` } // 获取客户名称列表数据 func GetCompanyNameList(sellerId int, keyword, status string) (list []*CompanyNameList, err error) { o := orm.NewOrm() sql := `select * from contract where is_delete=0 and (company_name like '%` + keyword + `%' or credit_code like '%` + keyword + `%') ` if status != "" { sql += ` AND status='` + status + `' ` } pars := make([]interface{}, 0) if sellerId > 0 { sql += ` and seller_id=? ` pars = append(pars, sellerId) } sql += ` group by company_name order by modify_time desc ` _, err = o.Raw(sql, pars).QueryRows(&list) return } // UpdateBusinessContractPaidPrice 更新合同已支付金额 func UpdateBusinessContractPaidPrice(contractId int, updatePriceStr string) (err error) { o := orm.NewOrm() //删除合同的原始服务信息 sql := `update contract set paid_price=paid_price` + updatePriceStr + ` where contract_id = ?` _, err = o.Raw(sql, contractId).Exec() return }