package controllers import ( "encoding/json" "github.com/rdlucklib/rdluck_tools/paging" approval2 "hongze/hongze_mobile_admin/models/request/approval" approvalResp "hongze/hongze_mobile_admin/models/response/approval" "hongze/hongze_mobile_admin/models/tables/chart_permission" "hongze/hongze_mobile_admin/models/tables/company_approval" "hongze/hongze_mobile_admin/models/tables/company_contract" "hongze/hongze_mobile_admin/models/tables/company_delay_permission" "hongze/hongze_mobile_admin/models/tables/company_product" "hongze/hongze_mobile_admin/models/tables/company_report_permission" "hongze/hongze_mobile_admin/models/tables/contract" "hongze/hongze_mobile_admin/models/tables/contract_approval" "hongze/hongze_mobile_admin/models/tables/contract_approval_record" company_apply "hongze/hongze_mobile_admin/services/company_approval" contractService "hongze/hongze_mobile_admin/services/contract" "hongze/hongze_mobile_admin/utils" "strconv" "strings" "time" ) // ListV2 // @Title 获取审批列表接口 // @Description 获取审批列表接口 // @Param PageSize query int true "每页数据条数" // @Param CurrentIndex query int true "当前页页码,从1开始" // @Param Status query string true "状态:'待审批','已审批','驳回','已撤回'" // @Param KeyWord query string true "搜索关键词" // @Param Keyword query string true "搜索关键词" // @Param KeywordEq query string true "搜索关键词(全等)" // @Success 200 {object} approval.CompanyApprovalListResp // @router /list [get] func (c *ApprovalCommon) ListV2() { sysUser := c.AdminWx pageSize, _ := c.GetInt("PageSize") currentIndex, _ := c.GetInt("CurrentIndex") status := c.GetString("Status") keyword := c.GetString("Keyword") keywordEq := c.GetString("KeywordEq") condition := "" pars := make([]interface{}, 0) ////如果不是超管或者合规,那么只能查看自己的合同 //join表字段 //归属 if status == "待审批" { condition += ` AND ( (((c.apply_user_id = ? and a.start_node_id = a.curr_node_id) OR (f.approve_user_id = ? and a.curr_node_id=f.node_id ) ) and c.approve_status="待审批") or (f.approve_user_id = ? and a.curr_node_id > f.node_id and f.node_type="cc") )` pars = append(pars, sysUser.AdminId, sysUser.AdminId, sysUser.AdminId) } else { condition += ` AND (c.apply_user_id = ? or (f.approve_user_id = ? and f.node_id <= a.curr_node_id) )` pars = append(pars, sysUser.AdminId, sysUser.AdminId) } //审批状态 if status != "" { if status == "已审批" { condition += ` AND c.approve_status in ("已审批","驳回") ` } else { condition += ` AND c.approve_status = ? ` if status == "处理中" { pars = append(pars, "待审批") } else { pars = append(pars, status) } } } else { condition += ` AND c.approve_status != "已撤回" ` } //关键字 if keyword != "" { condition += ` AND ( e.company_name LIKE '%` + keyword + `%' OR e.credit_code LIKE '%` + keyword + `%' ) ` } //关键字(全等) if keywordEq != "" { condition += ` AND e.company_name =? ` pars = append(pars, keywordEq) } var startSize int if pageSize <= 0 { pageSize = utils.PageSize20 } if currentIndex <= 0 { currentIndex = 1 } startSize = paging.StartIndex(currentIndex, pageSize) total, err := contract_approval.GetCompanyApprovalCount(condition, pars) if err != nil { c.FailWithMessage("获取失败", "获取数据总数失败,Err:"+err.Error()) return } list, err := contract_approval.GetCompanyApprovalList(condition, pars, startSize, pageSize) if err != nil { c.FailWithMessage("获取审批列表失败!", "获取审批列表失败,Err:"+err.Error()) return } respList := make([]*company_approval.CompanyApprovalList, 0) listLen := len(list) if listLen > 0 { contractApprovalIdSlice := make([]string, 0) companyContractIdSlice := make([]string, 0) for i := 0; i < len(list); i++ { contractApprovalIdSlice = append(contractApprovalIdSlice, strconv.Itoa(list[i].ContractApprovalId)) companyContractIdSlice = append(companyContractIdSlice, strconv.Itoa(list[i].CompanyContractId)) } contractApprovalIdStr := strings.Join(contractApprovalIdSlice, ",") companyContractMap := make(map[int]*company_contract.CompanyContract) if len(companyContractIdSlice) > 0 { companyContractIdStr := strings.Join(companyContractIdSlice, ",") companyContractList, e := company_contract.GetCompanyContractDetailByContractIds(companyContractIdStr) if e == nil { for _, v := range companyContractList { companyContractMap[v.CompanyContractId] = v } } } //获取审批流列表数据 contractApprovalRecordList, err := contract_approval_record.GetContractApprovalRecordList(contractApprovalIdStr, sysUser.AdminId) if err != nil { c.FailWithMessage("获取审批流列表失败!", "获取审批流列表失败,Err:"+err.Error()) return } contractApprovalRecordMap := make(map[int]*contract_approval_record.ContractApprovalRecord) for i := 0; i < len(contractApprovalRecordList); i++ { contractApprovalRecordMap[contractApprovalRecordList[i].ContractApprovalId] = contractApprovalRecordList[i] } for i := 0; i < len(list); i++ { item := list[i] list[i].StartDateStr = item.StartDate.Format(utils.FormatDate) list[i].EndDateStr = item.EndDate.Format(utils.FormatDate) list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime) list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime) list[i].ApproveTimeStr = item.ApproveTime.Format(utils.FormatDateTime) //合同审批权限 ApprovalButton := false var contractDetail contract.ContractDetail jsonErr := json.Unmarshal([]byte(list[i].ContractDetail), &contractDetail) if jsonErr != nil { c.FailWithMessage("审批合同数据转换失败!", "审批合同数据转换失败,Err:"+jsonErr.Error()) return } list[i].ContractInfo = contractDetail //申请转正时合同相关信息 if item.CompanyContractId > 0 { if com, ok := companyContractMap[item.CompanyContractId]; ok { if com.Source == "系统合同" { item.ContractSourceTag = "标准合同" } else { item.ContractSourceTag = "非标合同" } } } //校验最新审批流数据 if contractApprovalRecord, has := contractApprovalRecordMap[item.ContractApprovalId]; has { //审批流id list[i].ContractApprovalRecordId = contractApprovalRecord.ContractApprovalRecordId //审批时间 list[i].ApproveTime = contractApprovalRecord.ApproveTime list[i].ApproveTimeStr = contractApprovalRecord.ApproveTime.Format(utils.FormatDateTime) //判断是否有操作权限 list[i].Status = contractApprovalRecord.Status //判断是否处于待审批状态 if contractApprovalRecord.Status == "待审批" { //如果不是审批者,那么不能审批操作 if contractApprovalRecord.NodeType != "check" { continue } //如果没有指定审批人的话 if contractApprovalRecord.ApproveUserId <= 0 { //判断是否与审批角色一致,如果一致,那么允许操作审批 if contractApprovalRecord.ApproveRoleTypeCode == sysUser.RoleTypeCode { ApprovalButton = true } } else if contractApprovalRecord.ApproveUserId == sysUser.AdminId { //如果指定审批人就是自己的话,那么操作审批 ApprovalButton = true } } } //撤销权限(本人才有,然后没有审核操作过的才允许撤销) //if item.Status == "待审批" && item.StartNodeId == item.CurrNodeId && item.ApplyUserId == sysUser.AdminId { // opButton.Cancel = true //} respTmp := &company_approval.CompanyApprovalList{ CompanyId: item.CompanyId, ProductId: item.ProductId, CompanyName: item.CompanyName, CreditCode: item.CreditCode, CompanyType: item.CompanyType, IndustryName: item.IndustryName, SellerName: item.SellerName, CreateTime: item.CreateTimeStr, ApprovalTime: item.CreateTime.Format(utils.FormatDateTime), ApproveTime: item.ApproveTimeStr, Status: item.CompanyProductStatus, ApproveContent: item.ApplyContent, ApproveStatus: item.ApproveStatus, ApplyMethod: item.ApplyMethod, ApplyReasons: item.ApplyReasons, FreezeStartDate: item.FreezeStartDate, FreezeEndDate: item.FreezeEndDate, FreezeExpireDays: item.FreezeExpireDays, Address: item.Address, Source: item.Source, ApproveRemark: item.ApproveRemark, StartDate: item.StartDate.Format(utils.FormatDate), EndDate: item.EndDate.Format(utils.FormatDate), ExpireDay: item.ExpireDay, CompanyApprovalId: item.CompanyApprovalId, //ApprovalCount int `description:"已延期审批次数"` CompanyContractId: item.CompanyContractId, ContractSourceTag: item.ContractSourceTag, //DelayPermission : ApplyRealName: item.ApplyRealName, ApplyUserId: item.ApplyUserId, OpButton: ApprovalButton, } if respTmp.FreezeStartDate != "" && respTmp.FreezeEndDate != "" && respTmp.Status == utils.COMPANY_STATUS_FREEZE { freezeEndDate, _ := time.Parse(utils.FormatDate, item.FreezeEndDate) if time.Now().Before(freezeEndDate) { expireDays := freezeEndDate.Sub(time.Now()).Hours() / 24 respTmp.FreezeExpireDays = int(expireDays) } } if respTmp.StartDate != "" && respTmp.EndDate != "" { endDate, _ := time.Parse(utils.FormatDate, respTmp.EndDate) if time.Now().Before(endDate) { expireDays := endDate.Sub(time.Now()).Hours() / 24 list[i].ExpireDay = int(expireDays) } } switch item.ApplyMethod { case 2: //客户冻结转试用次数 approvalCount, err := company_approval.GetFreezeToTryCompanyApprovalCount(respTmp.CompanyId, respTmp.ProductId, respTmp.ApplyUserId) if err != nil && err.Error() != utils.ErrNoRow() { c.FailWithMessage("获取失败", "获取冻结转试用审批次数失败,Err:"+err.Error()) return } respTmp.ApprovalCount = approvalCount case 3: delayPermission, err := company_delay_permission.GetApplyDelayPermission(respTmp.CompanyApprovalId) if err != nil && err.Error() != utils.ErrNoRow() { c.FailWithMessage("获取失败", "获取延期审批权限失败,Err:"+err.Error()) return } respTmp.DelayPermission = delayPermission //客户审批试用延期次数 approvalCount, err := company_approval.GetCompanyTryDelayApprovalCount(respTmp.CompanyId, respTmp.ProductId) if err != nil && err.Error() != utils.ErrNoRow() { c.FailWithMessage("获取失败", "获取延期审批次数失败,Err:"+err.Error()) return } respTmp.ApprovalCount = approvalCount } respList = append(respList, respTmp) } } page := paging.GetPaging(currentIndex, pageSize, total) c.OkDetailed(approvalResp.CompanyApprovalListResp{ List: respList, Paging: page, }, "获取成功") } // @Title 获取审批单详情接口 // @Description 获取审批单详情接口 // @Param ApprovalId query int true "审批单id" // @Success 200 {object} approval.CompanyApprovalDetailResp // @router /detail [get] func (c *ApprovalCommon) Detail() { companyApprovalId, _ := c.GetInt("ApprovalId") //if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN { //审批 // condition += ` AND c.approve_role_type_code=? ` // pars = append(pars, roleTypeCode) //} else if roleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || roleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER { //销售 // condition += ` AND c.apply_user_id=? ` // pars = append(pars, sysUser.AdminId) //} item, flowNodeListResp, err := company_apply.GetApprovalDetailByCompanyApprovalId(companyApprovalId, c.AdminWx) if err != nil { c.FailWithMessage("获取失败", "获取数据失败,Err:"+err.Error()) return } resp := approvalResp.CompanyApprovalDetailResp{ CompanyApprovalDetail: item, FlowNodeList: flowNodeListResp, } //获取权限列表 //子权限切片集合 var permissionClassifyArr []*chart_permission.ChartPermission if item.ProductId == 1 { // 查询一级分类名称 permissionClassifyArr, err = chart_permission.GetPermissionFirstByProductId(item.ProductId) if err != nil { c.FailWithMessage("获取失败", "获取审批权限失败,Err:"+err.Error()) return } } else { permissionClassifyArr, err = chart_permission.GetPermissionFirstByProductId(item.ProductId) if err != nil { c.FailWithMessage("获取失败", "获取审批权限失败,Err:"+err.Error()) return } } //获取需要审批的权限 delayPermissionList, err := company_delay_permission.GetDelayPermissionItems(item.CompanyId, item.CompanyApprovalId) if err != nil && err.Error() != utils.ErrNoRow() { c.FailWithMessage("获取失败", "获取审批权限失败,Err:"+err.Error()) return } delayPermissionIdMap := make(map[int]int) for _, delayPermission := range delayPermissionList { delayPermissionIdMap[delayPermission.ChartPermissionId] = 0 } //遍历获取 for _, v := range permissionClassifyArr { checkList := make([]int, 0) plist := new(company_report_permission.PermissionLookList) items, err := company_report_permission.GetPermissionLookByParentId(item.ProductId, v.ChartPermissionId) if err != nil { c.FailWithMessage("获取失败", "获取权限信息失败,Err:"+err.Error()) return } // 权益主客观均选择的情况下进行合并 //equityPermissionNameCount := make(map[string]int, 0) for _, n := range items { if _, ok := delayPermissionIdMap[n.ChartPermissionId]; ok { checkList = append(checkList, n.ChartPermissionId) } //equityPermissionNameCount[n.PermissionName] += 1 } //newItems := make([]*company_report_permission.PermissionLookItem, 0) //mergedMap := make(map[string]bool, 0) //for _, n := range items { // if equityPermissionNameCount[n.PermissionName] == 2 && !mergedMap[n.PermissionName] { // newItems = append(newItems, n) // mergedMap[n.PermissionName] = true // } // if equityPermissionNameCount[n.PermissionName] == 1 { // n.PermissionName = n.Remark // newItems = append(newItems, n) // } //} //plist.Items = newItems plist.Items = items plist.ClassifyName = v.ChartPermissionName plist.CheckList = checkList if item.ProductId == 1 { resp.FiccPermissionList = append(resp.FiccPermissionList, plist) } else { // CRM8.8-权限主客观合并 rp := make([]*company_report_permission.PermissionLookList, 0) rp = append(rp, plist) newPermissionLookList := contractService.HandleEquityPermissionLookList(rp) //resp.PermissionList = append(resp.PermissionList, plist) resp.PermissionList = newPermissionLookList } } c.OkDetailed(resp, "获取成功") } // ApplyApprove // @Title 审批 // @Description 审批接口 // @Param request body approval.CompanyApplyApproveReq true "type json string" // @Success Ret=200 审批成功 // @router /approve [post] func (c *ApprovalCommon) ApplyApprove() { sysUser := c.AdminWx var req approval2.CompanyApplyApproveReq err := json.Unmarshal(c.Ctx.Input.RequestBody, &req) if err != nil { c.FailWithMessage("参数解析异常!", "参数解析失败,Err:"+err.Error()) return } //roleTypeCode := sysUser.RoleTypeCode //productId := services.GetProductId(roleTypeCode) //if productId == 0 { // c.FailWithMessage("参数错误", "参数解析失败,Err:"+"客户类型为:"+roleTypeCode) // return //} if req.CompanyId <= 0 { c.FailWithMessage("参数错误", "客户Id小于等于0") return } var condition string var pars []interface{} approvalItem, err := company_approval.GetApprovalInfoByApprovalId(condition, pars, req.CompanyApprovalId) if err != nil { c.FailWithMessage("获取审批信息失败", "获取信息失败,Err:"+err.Error()) } if req.Status != 1 && req.Status != 2 { c.FailWithMessage("参数错误", "审批状态Id小于等于0") return } companyProduct, err := company_product.GetCompanyProductByCompanyIdAndProductId(req.CompanyId, approvalItem.ProductId) if err != nil { c.FailWithMessage("获取信息失败", "获取信息失败,Err:"+err.Error()) return } if companyProduct == nil { c.FailWithMessage("获取审批信息失败", "获取信息失败,companyProduct is null") return } //CompanyApprovalId //数据校验(校验是否具有审批权限) _, _, approvalRecord, err := company_apply.CheckApproveAuth(req.CompanyId, approvalItem.ProductId, sysUser) if err != nil { c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error()) return } //真实审核操作 if req.Status == 1 { err = company_apply.Approved(approvalRecord, sysUser, req.Remark) if err != nil { c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error()) return } } else { err = company_apply.Reject(approvalRecord, sysUser, req.Remark) if err != nil { c.FailWithMessage("审核失败!", "审核失败,Err:"+err.Error()) return } } c.OkWithMessage("审批操作成功") }