package controllers import ( "encoding/json" "fmt" "github.com/rdlucklib/rdluck_tools/paging" "hongze/hongze_mobile_admin/models" contractReq "hongze/hongze_mobile_admin/models/request/contract" contractResp "hongze/hongze_mobile_admin/models/response/contract" "hongze/hongze_mobile_admin/models/tables/contract" "hongze/hongze_mobile_admin/models/tables/contract_approval" "hongze/hongze_mobile_admin/models/tables/contract_relation" contractService "hongze/hongze_mobile_admin/services/contract" "hongze/hongze_mobile_admin/services/seal" "hongze/hongze_mobile_admin/utils" "path" "strconv" "strings" ) // 合同模块 type ContractCommon struct { BaseAuth } // @Title 上传签回附件 // @Description 上传签回附件接口 // @Param request body contract.UploadCheckBackFileReq true "type json string" // @Success Ret=200 上传成功 // @router /upload_check_back_file [post] func (c *ContractCommon) UploadCheckBackFile() { //var req contractReq.UploadCheckBackFileReq //err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) //if err != nil { // c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error()) // return //} ////合同编号 //if req.ContractId <= 0 { // c.FailWithMessage("请传入合同编号!", "请传入合同编号") // return //} // //if req.FileUrl == "" { // c.FailWithMessage("请先上传附件!", "请先上传附件") // return //} contractIdStr := c.Ctx.Request.Form.Get("ContractId") if contractIdStr == "" { c.FailWithMessage("合同ID必传!", "合同ID必传") return } contractId, err := strconv.Atoi(contractIdStr) if err != nil { c.FailWithMessage("合同ID异常!", "合同ID必传") return } //合同编号 if contractId <= 0 { c.FailWithMessage("请传入合同编号", "请传入合同编号") return } fileMulti, h, err := c.GetFile("file") if err != nil { c.FailWithMessage("获取资源信息失败", "获取资源信息失败,Err:"+err.Error()) return } ext := path.Ext(h.Filename) //err = contractService.UploadCheckBackFile(contractId, req.FileUrl, c.AdminWx) resourceUrl, err := contractService.UploadCheckBackFileByFile(contractId, ext, fileMulti, c.AdminWx) //更新用印中的签回合同 _, _ = seal.UploadCheckBackFile(contractId, resourceUrl, c.AdminWx) c.OkWithMessage("上传成功") } // Detail // @Title 获取合同详情 // @Description 获取合同详情接口 // @Param ContractId query int true "合同id" // @Success 200 {object} contract.ContractDetailResp // @router /detail [get] func (c *ContractCommon) Detail() { //合同类型、产品类型、合同状态、更新时间、所选销售 //关键字:合同编号、客户名称,社会信用码 contractId, _ := c.GetInt("ContractId") //合同id if contractId <= 0 { c.FailWithMessage("合同id必传!", "合同id必传!") return } //获取合同详情 contractInfo, flowNodeListResp, contractOpButton, err := contractService.GetContractDetailByContractId(contractId, c.AdminWx) if err != nil { c.FailWithMessage("获取合同详情失败!", "获取合同详情失败,ERR:"+err.Error()) return } resp := contractResp.ContractDetailResp{ ContractDetail: contractInfo, FlowNodeList: flowNodeListResp, OpButton: contractOpButton, } c.OkDetailed(resp, "获取成功") } // CompanyList // @Title 根据客户名称获取已存在合同系统中客户名称列表 // @Description 获取合同详情接口 // @Param CompanyName query string true "客户名称" // @Param Keyword query string true "关键字:客户名称、社会信用码" // @Param Status query string true "合同状态" // @Success 200 {object} []string // @router /company_list [get] func (c *ContractCommon) CompanyList() { sysUser := c.AdminWx keyword := c.GetString("Keyword") //搜索关键字 if keyword == "" { c.FailWithMessage("搜索关键字必传!", "搜索关键字必传!") return } status := c.GetString("Status") companyNameList := make([]string, 0) list, err := contract.GetCompanyNameList(c.AdminWx.AdminId, keyword, status) if err != nil { c.FailWithMessage("获取客户名称列表失败!", "获取客户名称列表失败,ERR:"+err.Error()) return } for _, v := range list { companyNameList = append(companyNameList, v.CompanyName) } //审批列表中(跟自己有关联的) childCondition := "" condition := "" childPars := make([]interface{}, 0) pars := make([]interface{}, 0) if status != "" { condition += `and c.status=? ` pars = append(pars, status) } //归属 //如果不是合规,那么只能查看跟自己有关联的 if status == "待审批" { condition += ` AND ((c.seller_id = ? and a.start_node_id = a.curr_node_id) OR (d.approve_user_id = ? )) and d.status="待审批" ` } else { condition += ` AND (c.seller_id = ? or (d.approve_user_id = ? and d.node_id <= a.curr_node_id and c.status != "已撤回") )` } pars = append(pars, sysUser.AdminId, sysUser.AdminId) condition += `and (company_name like '%` + keyword + `%' or credit_code like '%` + keyword + `%')` list2, err := contract_approval.GetCompanyNameListV2(childCondition, condition, childPars, pars) if err != nil { c.FailWithMessage("获取客户名称列表失败!", "获取客户名称列表失败,ERR:"+err.Error()) return } for _, v := range list2 { if !strings.Contains(strings.Join(companyNameList, ","), v.CompanyName) { companyNameList = append(companyNameList, v.CompanyName) } } c.OkDetailed(companyNameList, "获取成功") } // List // @Title 合同列表 // @Description 合同列表接口 // @Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'" // @Param Status query string false "合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'" // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益" // @Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00" // @Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59" // @Param SellerId query string false "选择的销售id" // @Param Keyword query string false "搜索关键字" // @Param KeywordEq query string false "搜索关键字(全等)" // @Success 200 {object} contract.ContractListResp // @router /list [get] func (c *ContractCommon) List() { //合同类型、产品类型、合同状态、更新时间、所选销售 //关键字:合同编号、客户名称,社会信用码 contractType := c.GetString("ContractType") status := c.GetString("Status") productId, _ := c.GetInt("ProductId") modifyStartTime := c.GetString("ModifyStartTime") modifyEndTime := c.GetString("ModifyEndTime") sellerIds := c.GetString("SellerId") keyword := c.GetString("Keyword") keywordEq := c.GetString("KeywordEq") condition := "" pars := make([]interface{}, 0) //如果不是超管或者合规,那么只能查看自己的合同 condition += ` AND seller_id = ? ` pars = append(pars, c.AdminWx.AdminId) //合同类型、、更新时间、所选销售 //关键字:合同编号、客户名称,社会信用码 if contractType != "" { condition += ` AND contract_type = ? ` pars = append(pars, contractType) } //合同状态 if status != "" { condition += ` AND status = ? ` pars = append(pars, status) } //产品类型 if productId > 0 { condition += ` AND product_id = ? ` pars = append(pars, productId) } //所选销售 if sellerIds != "" { condition += ` AND seller_id IN (` + sellerIds + `) ` } //更新开始时间 if modifyStartTime != "" { condition += ` AND modify_time >= ? ` pars = append(pars, modifyStartTime) } //更新结束时间 if modifyEndTime != "" { condition += ` AND modify_time <= ? ` pars = append(pars, modifyEndTime) } //关键字 if keyword != "" { condition += ` AND (contract_code LIKE '%` + keyword + `%' OR company_name LIKE '%` + keyword + `%' OR credit_code LIKE '%` + keyword + `%' ) ` } //关键字(全等) if keywordEq != "" { condition += ` AND c.company_name =? ` pars = append(pars, keywordEq) } pageSize, _ := c.GetInt("PageSize") currentIndex, _ := c.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) total, err := contract.GetContractListCount(condition, pars) if err != nil { c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error()) return } list, err := contract.GetContractList(condition, pars, startSize, pageSize) if err != nil { c.FailWithMessage("获取合同列表失败", "获取合同列表失败,Err:"+err.Error()) return } page := paging.GetPaging(currentIndex, pageSize, total) c.OkDetailed(contractResp.ContractListResp{ List: list, Paging: page, }, "获取成功") } // CompanyListBySeal // @Title 根据客户名称获取已存在合同系统中客户名称列表 // @Description 获取合同详情接口 // @Param CompanyName query string true "客户名称" // @Param Keyword query string true "关键字:客户名称、社会信用码" // @Param Status query string true "合同状态" // @Success 200 {object} []string // @router /companyListBySeal [get] func (c *ContractCommon) CompanyListBySeal() { sysUser := c.AdminWx keyword := c.GetString("Keyword") //搜索关键字 if keyword == "" { c.FailWithMessage("搜索关键字必传!", "搜索关键字必传!") return } status := c.GetString("Status") companyNameList := make([]string, 0) sellerId := 0 if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE { sellerId = c.AdminWx.AdminId } list, err := contract.GetCompanyNameList(sellerId, keyword, status) if err != nil { c.FailWithMessage("获取客户名称列表失败!", "获取客户名称列表失败,ERR:"+err.Error()) return } for _, v := range list { companyNameList = append(companyNameList, v.CompanyName) } // 13.6正式共享客户的处理 { // 通过关键词获取分配给当前销售的正式共享客户 kw := fmt.Sprint("%", keyword, "%") cond := ` AND share_seller_id = ? AND is_share = 1 AND company_name LIKE ?` pars := make([]interface{}, 0) pars = append(pars, sysUser.AdminId, kw) shares, e := models.GetCompanyListByCondition(cond, pars, []string{"company_id", "company_name"}, "") if e != nil { c.FailWithMessage("获取失败", "获取共享客户信息失败, Err: "+e.Error()) return } for _, v := range shares { if !utils.InArrayByStr(companyNameList, v.CompanyName) { companyNameList = append(companyNameList, v.CompanyName) } } } //审批列表中(跟自己有关联的) //childCondition := "" //condition := "" //childPars := make([]interface{}, 0) //pars := make([]interface{}, 0) //if status != "" { // condition += `and c.status=? ` // pars = append(pars, status) //} ////归属 ////如果不是合规,那么只能查看跟自己有关联的 //if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE { // if status == "待审批" { // condition += ` AND ((c.seller_id = ? and a.start_node_id = a.curr_node_id) OR (d.approve_user_id = ? )) and d.status="待审批" ` // } else { // condition += ` AND (c.seller_id = ? or d.approve_user_id = ?)` // } // pars = append(pars, sysUser.AdminId, sysUser.AdminId) //} // //condition += `and (company_name like '%` + keyword + `%' or credit_code like '%` + keyword + `%')` // //list2, err := contract_approval.GetCompanyNameListV2(childCondition, condition, childPars, pars) //if err != nil { // c.FailWithMessage("获取客户名称列表失败!", "获取客户名称列表失败,ERR:"+err.Error()) // return //} //for _, v := range list2 { // if !strings.Contains(strings.Join(companyNameList, ","), v.CompanyName) { // companyNameList = append(companyNameList, v.CompanyName) // } //} c.OkDetailed(companyNameList, "获取成功") } // ListBySeal // @Title 合同列表 // @Description 合同列表接口 // @Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'" // @Param Status query string false "合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'" // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益" // @Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00" // @Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59" // @Param SellerId query string false "选择的销售id" // @Param Keyword query string false "搜索关键字" // @Param KeywordEq query string false "搜索关键字(全等)" // @Success 200 {object} contract.ContractListResp // @router /listBySeal [get] func (c *ContractCommon) ListBySeal() { sysUser := c.AdminWx //合同类型、产品类型、合同状态、更新时间、所选销售 //关键字:合同编号、客户名称,社会信用码 contractType := c.GetString("ContractType") status := c.GetString("Status") productId, _ := c.GetInt("ProductId") modifyStartTime := c.GetString("ModifyStartTime") modifyEndTime := c.GetString("ModifyEndTime") sellerIds := c.GetString("SellerId") keyword := c.GetString("Keyword") keywordEq := c.GetString("KeywordEq") condition := "" pars := make([]interface{}, 0) //如果不是合规,那么只能查看自己的合同 if c.AdminWx.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE { condition += ` AND a.seller_id = ? ` pars = append(pars, c.AdminWx.AdminId) } //合同类型、、更新时间、所选销售 //关键字:合同编号、客户名称,社会信用码 if contractType != "" { condition += ` AND a.contract_type = ? ` pars = append(pars, contractType) } //合同状态 if status != "" { condition += ` AND a.status = ? ` pars = append(pars, status) } //产品类型 if productId > 0 { condition += ` AND a.product_id = ? ` pars = append(pars, productId) } //所选销售 if sellerIds != "" { condition += ` AND a.seller_id IN (` + sellerIds + `) ` } //更新开始时间 if modifyStartTime != "" { condition += ` AND a.modify_time >= ? ` pars = append(pars, modifyStartTime) } //更新结束时间 if modifyEndTime != "" { condition += ` AND a.modify_time <= ? ` pars = append(pars, modifyEndTime) } //关键字 if keyword != "" { condition += ` AND (a.contract_code LIKE '%` + keyword + `%' OR a.company_name LIKE '%` + keyword + `%' OR a.credit_code LIKE '%` + keyword + `%' ) ` } //关键字(全等) if keywordEq != "" { condition += ` AND c.company_name =? ` pars = append(pars, keywordEq) } pageSize, _ := c.GetInt("PageSize") currentIndex, _ := c.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) //total, err := contract.GetContractListCount(condition, pars) //if err != nil { // c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error()) // return //} // //list, err := contract.GetContractList(condition, pars, startSize, pageSize) //if err != nil { // c.FailWithMessage("获取合同列表失败", "获取合同列表失败,Err:"+err.Error()) // return //} // 13.6正式共享客户 joinStr := ` LEFT JOIN company AS b ON a.company_name = b.company_name ` // 非合规角色可查看自己的合同或是自己的共享客户下的非自己创建的合同 if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE { condition += ` AND (a.seller_id = ? OR (b.is_share = 1 AND b.share_seller_id = ?))` pars = append(pars, sysUser.AdminId, sysUser.AdminId) } total, err := contract.GetJoinContractListCount(condition, joinStr, pars) if err != nil { c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error()) return } list, e := contract.GetJoinContractList(condition, joinStr, pars, startSize, pageSize) if e != nil { c.FailWithMessage("获取失败", "获取合同列表失败,Err:"+err.Error()) return } if len(list) > 0 { // 代付合同查询代付用户名称 contractIdSlice := make([]string, 0) for i := 0; i < len(list); i++ { if list[i].ContractBusinessType == "代付合同" { contractIdSlice = append(contractIdSlice, strconv.Itoa(list[i].ContractId)) } } if len(contractIdSlice) > 0 { contractIdStr := strings.Join(contractIdSlice, ",") contractList, err := contract_relation.GetContractRelationListByPaymentOnBehalfContractIds(contractIdStr) if err != nil { c.FailWithMessage("获取失败", "获取代付合同对应的合同数据失败,Err:"+err.Error()) return } userCompanyNameMap := make(map[int]string, 0) for i := 0; i < len(contractList); i++ { userCompanyNameMap[contractList[i].PaymentOnBehalfContractId] = contractList[i].CompanyName } for _, item := range list { if item.ContractBusinessType == "代付合同" { item.UseCompanyName = userCompanyNameMap[item.ContractId] } } } } page := paging.GetPaging(currentIndex, pageSize, total) c.OkDetailed(contractResp.ContractListResp{ List: list, Paging: page, }, "获取成功") } // ListV2 // @Title 合同列表(包含待提交的) // @Description 合同列表接口(包含待提交的) // @Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'" // @Param Status query string false "合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'" // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益" // @Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00" // @Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59" // @Param SellerId query string false "选择的销售id" // @Param Keyword query string false "搜索关键字" // @Param KeywordEq query string false "搜索关键字(全等)" // @Success 200 {object} contract.ContractListRespV2 // @router /listV2 [get] func (c *ContractCommon) ListV2() { sysUser := c.AdminWx //合同类型、产品类型、合同状态、更新时间、所选销售 //关键字:合同编号、客户名称,社会信用码 contractType := c.GetString("ContractType") status := c.GetString("Status") productId, _ := c.GetInt("ProductId") modifyStartTime := c.GetString("ModifyStartTime") modifyEndTime := c.GetString("ModifyEndTime") sellerIds := c.GetString("SellerId") keyword := c.GetString("Keyword") keywordEq := c.GetString("KeywordEq") childCondition := "" condition := "" unionCondition := "" childPars := make([]interface{}, 0) pars := make([]interface{}, 0) unionPars := make([]interface{}, 0) //归属 if status == "待审批" { condition += ` AND ((c.seller_id = ? and a.start_node_id = a.curr_node_id) OR (d.approve_user_id = ? )) and d.status="待审批" ` } else { condition += ` AND (c.seller_id = ? or d.approve_user_id = ?)` } pars = append(pars, sysUser.AdminId, sysUser.AdminId) unionCondition += ` AND c.seller_id = ? ` unionPars = append(unionPars, sysUser.AdminId) //合同类型、、更新时间、所选销售 //关键字:合同编号、客户名称,社会信用码 if contractType != "" { condition += ` AND c.contract_type = ? ` pars = append(pars, contractType) unionCondition += ` AND c.contract_type = ? ` unionPars = append(unionPars, contractType) } //审批状态 if status != "" { if status == "已审批" { condition += ` AND c.status in ("已审批","已驳回") ` unionCondition += ` AND c.status in ("已审批","已驳回") ` } else { condition += ` AND c.status = ? ` unionCondition += ` AND c.status = ? ` if status == "处理中" { pars = append(pars, "待审批") unionPars = append(unionPars, "待审批") } else { pars = append(pars, status) unionPars = append(unionPars, status) } } } else { condition += ` AND c.status not in ("已撤回","待提交") ` unionCondition += ` AND c.status = "待提交" ` } //产品类型 if productId > 0 { condition += ` AND c.product_id = ? ` pars = append(pars, productId) unionCondition += ` AND c.product_id = ? ` unionPars = append(unionPars, productId) } //所选销售 if sellerIds != "" { condition += ` AND c.seller_id IN (` + sellerIds + `) ` unionCondition += ` AND c.seller_id IN (` + sellerIds + `) ` } //更新开始时间 if modifyStartTime != "" { condition += ` AND a.modify_time >= ? ` pars = append(pars, modifyStartTime) unionCondition += ` AND a.modify_time >= ? ` unionPars = append(unionPars, modifyStartTime) } //更新结束时间 if modifyEndTime != "" { condition += ` AND a.modify_time <= ? ` pars = append(pars, modifyEndTime) unionCondition += ` AND a.modify_time <= ? ` unionPars = append(unionPars, modifyEndTime) } //关键字 if keyword != "" { condition += ` AND (c.contract_code LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) ` unionCondition += ` AND (c.contract_code LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) ` } //关键字(全等) if keywordEq != "" { condition += ` AND c.company_name =? ` pars = append(pars, keywordEq) unionCondition += ` AND c.company_name =? ` unionPars = append(unionPars, keywordEq) } pageSize, _ := c.GetInt("PageSize") currentIndex, _ := c.GetInt("CurrentIndex") var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) //joinCondition := " and a.curr_node_id=d.node_id" joinCondition := "" total, err := contract_approval.GetContractListCountV2(childCondition, condition, joinCondition, unionCondition, childPars, pars, unionPars) if err != nil { c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error()) return } list, err := contract_approval.GetContractListV2(childCondition, condition, joinCondition, unionCondition, childPars, pars, unionPars, startSize, pageSize) if err != nil { c.FailWithMessage("获取合同列表失败", "获取合同列表失败,Err:"+err.Error()) return } page := paging.GetPaging(currentIndex, pageSize, total) c.OkDetailed(contractResp.ContractListRespV2{ List: list, Paging: page, }, "获取成功") } // @Title 作废合同 // @Description 作废合同接口 // @Param request body contract.InvalidReq true "type json string" // @Success Ret=200 作废成功 // @router /invalid [post] func (c *ContractCommon) Invalid() { var req contractReq.InvalidReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error()) return } //合同id if req.ContractId <= 0 { c.FailWithMessage("合同id必传!", "合同id必传!") return } err = contractService.InvalidContract(req.ContractId, c.AdminWx) if err != nil { c.FailWithMessage("作废合同失败!", "作废合同失败,Err:"+err.Error()) return } c.OkWithMessage("作废成功") return }