package com.ruoyi.web.controller.project; import java.io.*; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.*; import com.ruoyi.common.config.RuoYiConfig; import com.ruoyi.common.constant.HttpStatus; import com.ruoyi.common.core.domain.entity.SysUser; import com.ruoyi.common.core.domain.model.LoginUser; import com.ruoyi.common.exception.ServiceException; import com.ruoyi.common.utils.SecurityUtils; import com.ruoyi.project.domain.*; import com.ruoyi.project.domain.dto.ServiceReimbursementDto; import com.ruoyi.project.domain.vo.CheckFundVO; import com.ruoyi.project.domain.vo.SpFinancialExpensesIn; import com.ruoyi.project.domain.vo.SpFinancialExpensesReimbursementOut; import com.ruoyi.project.service.*; import com.ruoyi.system.domain.SysPost; import com.ruoyi.system.service.ISysPostService; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.commons.lang.ArrayUtils; import org.apache.ibatis.annotations.Options; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.ruoyi.common.annotation.Log; import com.ruoyi.common.annotation.RepeatSubmit; import com.ruoyi.common.core.controller.BaseController; import com.ruoyi.common.core.domain.AjaxResult; import com.ruoyi.common.enums.BusinessType; import com.ruoyi.common.utils.poi.ExcelUtil; import com.ruoyi.common.core.page.TableDataInfo; import freemarker.template.Configuration; /** * 报销申请Controller * * @author ruoyi * @date 2022-01-24 */ @Api("报销申请") @RestController @RequestMapping("/project/reimbursement") public class ServiceReimbursementController extends BaseController { @Autowired private IServiceReimbursementService serviceReimbursementService; @Autowired private IServiceReimbursementdetailService serviceReimbursementdetailService; @Autowired private IServiceFundflowruleService serviceFundflowruleService; @Autowired private IServiceFundflowService serviceFundflowService; @Autowired private ISysPostService postService; @Autowired private IServiceSystemmessageService ServiceSystemmessage; private static Configuration configuration = null; public ServiceReimbursementController() { configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); } static String cashUnitLeft[] = { "元", "拾", "佰", "仟", "万", "拾", "佰", "仟","亿", "拾", "佰", "仟", "万" }; static String cashUnitRight[] = { "角", "分", "厘" }; static String upperNumber[] = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒","捌", "玖" }; /** * 查询报销申请列表 */ @ApiOperation("查询报销申请列表") //@PreAuthorize("@ss.hasPermi('project:reimbursement:list')") @Log(title = "查询报销申请列表", businessType = BusinessType.OTHER) @GetMapping("/list") public TableDataInfo list(ServiceReimbursement serviceReimbursement) { SysUser user = SecurityUtils.getLoginUser().getUser(); serviceReimbursement.setCreateBy(user.getUserName()); startPage(); List list = serviceReimbursementService.queryList(serviceReimbursement); return getDataTable(list); } /** * 根据日期查询报销申请列表 */ @ApiOperation("根据日期查询报销申请列表") @GetMapping("/listWithDate") public TableDataInfo listWithDate(ServiceReimbursementDto serviceReimbursementdto) { startPage(); List list = serviceReimbursementService.selectSearchList(serviceReimbursementdto); return getDataTable(list); } /** * 根据权限显示审核列表 */ @ApiOperation("根据权限显示审核列表") @Log(title = "根据权限显示审核列表", businessType = BusinessType.OTHER) @GetMapping("/listbypower") public TableDataInfo getListBypower(SpFinancialExpensesIn spFinancialExpensesIn) { LoginUser loginUser = getLoginUser(); String APPLICANT=spFinancialExpensesIn.getAPPLICANT(); String APPLICATIONBEGTIME=spFinancialExpensesIn.getAPPLICATIONBEGTIME(); String APPLICATIONENDTIME=spFinancialExpensesIn.getAPPLICATIONENDTIME(); Integer CHECKFLAG=spFinancialExpensesIn.getCHECKFLAG(); Integer APPLYTYPE=spFinancialExpensesIn.getAPPLYTYPE(); Integer pageNum=spFinancialExpensesIn.getPageNum(); Integer pageSize=spFinancialExpensesIn.getPageSize(); if(pageNum==null) { pageNum=1; } if(pageSize==null) { pageSize=10; } if(APPLICANT==null) { APPLICANT=""; } if(APPLICATIONBEGTIME==null) { APPLICATIONBEGTIME=""; } if(APPLICATIONENDTIME==null) { APPLICATIONENDTIME=""; } //startPage(); List list = serviceReimbursementService.getListBypower(loginUser.getUsername(),1,APPLICANT,APPLICATIONBEGTIME,APPLICATIONENDTIME,loginUser.getDeptId().toString(),CHECKFLAG,APPLYTYPE); return getCustomDataTable(list,pageNum,pageSize); } /** * 导出报销申请列表 */ @ApiOperation("导出报销申请列表") //@PreAuthorize("@ss.hasPermi('project:reimbursement:export')") @Log(title = "报销申请", businessType = BusinessType.EXPORT) @GetMapping("/export") public AjaxResult export(ServiceReimbursement serviceReimbursement) { List list = serviceReimbursementService.queryList(serviceReimbursement); ExcelUtil util = new ExcelUtil(ServiceReimbursement.class); return util.exportExcel(list, "报销申请数据"); } /** * 获取报销申请详细信息 */ @ApiOperation("获取报销申请详细信息") //@PreAuthorize("@ss.hasPermi('project:reimbursement:query')") @GetMapping(value = "/{id}") public AjaxResult getInfo(@PathVariable("id") Long id) { return AjaxResult.success(serviceReimbursementService.getById(id)); } @GetMapping("/getMaxId") public AjaxResult getMaxId(){ return AjaxResult.success(serviceReimbursementService.getMaxId()); } @GetMapping("/getRBDetailList/{id}") public AjaxResult getRBDetailList(@PathVariable("id") Long id){ return AjaxResult.success(serviceReimbursementService.getRBDetailList(id)); } /** * 新增报销申请 */ @ApiOperation("新增报销申请") //@PreAuthorize("@ss.hasPermi('project:reimbursement:add')") @Log(title = "报销申请", businessType = BusinessType.INSERT) @PostMapping @RepeatSubmit @Options(useGeneratedKeys = true, keyProperty = "id") public AjaxResult add(@RequestBody ServiceReimbursement serviceReimbursement) { boolean b = serviceReimbursementService.save(serviceReimbursement); Long id = serviceReimbursement.getId(); return AjaxResult.success(id); } /** * 差旅费上报 */ @ApiOperation("差旅费上报") @Log(title = "差旅费上报", businessType = BusinessType.OTHER) @PostMapping("/travelexpensereport") public AjaxResult travelexpensereport(@RequestBody CheckFundVO checkFundVO) { ServiceReimbursement serviceReimbursement= serviceReimbursementService.getById(checkFundVO.getFundid()); if(serviceReimbursement!=null) { Integer TotalLevel=0; LoginUser loginUser = getLoginUser(); SysUser user = loginUser.getUser(); List postids =postService.selectPostListByUserId(loginUser.getUserId()); if(!postids.contains(2)) { serviceReimbursement.setFlowlevel(1); serviceReimbursement.setRecordstatus(2); } else { serviceReimbursement.setFlowlevel(0); serviceReimbursement.setRecordstatus(0); } if(!postids.contains(2)) { ServiceFundflow serviceFundflow = new ServiceFundflow(); serviceFundflow.setFundid(serviceReimbursement.getId()); serviceFundflow.setCheckuserno(user.getUserName()); serviceFundflow.setCheckusername(user.getNickName()); serviceFundflow.setFundtype(1); serviceFundflow.setApplytype("0"); serviceFundflow.setFlowconclusion(1); serviceFundflow.setFlowcontent("非专职人员直接进入二级审核"); serviceFundflow.setFlowlevel(1); serviceFundflowService.save(serviceFundflow); } serviceReimbursementService.updateById(serviceReimbursement); return AjaxResult.success(); } else { return AjaxResult.error(HttpStatus.NO_CONTENT,"费用编号不正确"); } } /** * 审核费用 */ @ApiOperation("审核费用") @Log(title = "审核费用", businessType = BusinessType.OTHER) @PostMapping("/checkfund") public AjaxResult checkFund(@RequestBody CheckFundVO checkFundVO) { ServiceReimbursement serviceReimbursement= serviceReimbursementService.getById(checkFundVO.getFundid()); if(serviceReimbursement!=null) { Integer TotalLevel=0; LoginUser loginUser = getLoginUser(); ServiceFundflowrule serviceFundflowrule=new ServiceFundflowrule(); serviceFundflowrule.setFundtype(1); serviceFundflowrule.setApplytype("0"); serviceFundflowrule.setCheckuserno(loginUser.getUsername()); List serviceFundflowrules= serviceFundflowruleService.queryList(serviceFundflowrule); if(serviceFundflowrules==null || serviceFundflowrules.stream().count()==0) { return AjaxResult.error(HttpStatus.ERROR,"当前人员无此记录审核权限"); } if(serviceFundflowrules.get(0).getFlowlevel()-1!=serviceReimbursement.getFlowlevel()) { return AjaxResult.error(HttpStatus.ERROR,"当前人员与此记录的审核级别不符"); } if(serviceFundflowrules!=null && serviceFundflowrules.stream().count()>0) { TotalLevel=serviceFundflowrules.get(0).getTotallevel(); } Integer CheckFlag=0; Integer RecordStatus=serviceReimbursement.getRecordstatus(); Integer FlowLevel=serviceReimbursement.getFlowlevel(); Integer OriginalFlowLevel=FlowLevel; if(checkFundVO.getFlowconclusion()==1) { CheckFlag=1; if(TotalLevel==0) { RecordStatus=99; } else { if(TotalLevel==FlowLevel+1) { RecordStatus=99; } else { RecordStatus = (FlowLevel+1)*2; } } FlowLevel=FlowLevel+1; } else { CheckFlag=2; RecordStatus = (FlowLevel+1)*2 - 1; FlowLevel=(FlowLevel-1); if(FlowLevel<0) { FlowLevel=0; } } serviceReimbursement.setRecordstatus(RecordStatus); serviceReimbursement.setFlowlevel(FlowLevel); ServiceFundflow serviceFundflow=new ServiceFundflow(); SysUser user = loginUser.getUser(); serviceFundflow.setFundid(serviceReimbursement.getId()); serviceFundflow.setCheckuserno(user.getUserName()); serviceFundflow.setCheckusername(user.getNickName()); serviceFundflow.setFundtype(1); serviceFundflow.setApplytype("0"); serviceFundflow.setFlowconclusion(CheckFlag); serviceFundflow.setFlowcontent(checkFundVO.getFlowcontent()); serviceFundflow.setFlowlevel(OriginalFlowLevel+1); serviceFundflowService.save(serviceFundflow); List postids =postService.selectPostListByUserId(loginUser.getUserId()); if(!postids.contains(2)) { if(CheckFlag==2 && OriginalFlowLevel==1) { ServiceFundflow serviceFundflowAuto=new ServiceFundflow(); serviceFundflowAuto.setFundid(serviceReimbursement.getId()); serviceFundflowAuto.setCheckuserno(user.getUserName()); serviceFundflowAuto.setCheckusername(user.getNickName()); serviceFundflowAuto.setFundtype(1); serviceFundflowAuto.setApplytype("0"); serviceFundflowAuto.setFlowconclusion(CheckFlag); serviceFundflowAuto.setFlowcontent("非专职人员二级审核拒绝后直接退回到修改状态"); serviceFundflowAuto.setFlowlevel(1); serviceFundflowService.save(serviceFundflowAuto); serviceReimbursement.setRecordstatus(1); } } serviceReimbursementService.updateById(serviceReimbursement); ServiceSystemmessage serviceSystemmessage=new ServiceSystemmessage(); serviceSystemmessage.setFundtype(1); serviceSystemmessage.setApplytype("0"); serviceSystemmessage.setSenduserno(user.getUserName()); serviceSystemmessage.setSendusername(user.getNickName()); serviceSystemmessage.setReceiveuserno(serviceReimbursement.getUserno()); serviceSystemmessage.setReceiveusername(serviceReimbursement.getUsername()); serviceSystemmessage.setIsread(0); serviceSystemmessage.setMessagetype(1); serviceSystemmessage.setRelevantno(serviceReimbursement.getId()); if(CheckFlag==1) { //通过 serviceSystemmessage.setMessagetitle(""+(OriginalFlowLevel+1)+"级审核通过"); serviceSystemmessage.setMessagecontent("您"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(serviceReimbursement.getCreateTime())+"提交的关于捐献案例【"+serviceReimbursement.getDonorname()+"】的差旅费申请已通过"+(OriginalFlowLevel+1)+"级审核"); } else { //驳回 serviceSystemmessage.setMessagetitle(""+(OriginalFlowLevel+1)+"级审核驳回"); serviceSystemmessage.setMessagecontent("您"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(serviceReimbursement.getCreateTime())+"提交的关于捐献案例【"+serviceReimbursement.getDonorname()+"】的差旅费申请已被"+(OriginalFlowLevel+1)+"级审核驳回,原因为"+checkFundVO.getFlowcontent()+""); } ServiceSystemmessage.save(serviceSystemmessage); return AjaxResult.success(); } else { return AjaxResult.error(HttpStatus.NO_CONTENT,"费用编号不正确"); } } /** * 修改报销申请 */ @ApiOperation("修改报销申请") //@PreAuthorize("@ss.hasPermi('project:reimbursement:edit')") @Log(title = "报销申请", businessType = BusinessType.UPDATE) @PutMapping @RepeatSubmit public AjaxResult edit(@RequestBody ServiceReimbursement serviceReimbursement) { return toAjax(serviceReimbursementService.updateById(serviceReimbursement)); } /** * 删除报销申请 */ @ApiOperation("删除报销申请") //@PreAuthorize("@ss.hasPermi('project:reimbursement:remove')") @Log(title = "报销申请", businessType = BusinessType.DELETE) @DeleteMapping("/{ids}") public AjaxResult remove(@PathVariable Long[] ids) { return toAjax(serviceReimbursementService.removeByIds(Arrays.asList(ids))); } /** * 下载工作人员差旅费报销单 */ @ApiOperation("差旅费报销申请单") @GetMapping(value = "/download/{id}") public Map downloadInfo(@PathVariable("id") Long id) throws IOException { Map dataMap = new HashMap(); getData(dataMap,id); String filePath = getClass().getResource("/template/").getPath(); System.out.println(filePath); //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库教程装载, configuration.setDirectoryForTemplateLoading(new File(filePath)); Template t=null; try { //捐献表.ftl为要装载的模板 t = configuration.getTemplate("差旅费报销申请单.ftl"); } catch (IOException e) { e.printStackTrace(); } String newTime = String.valueOf(Calendar.getInstance().getTimeInMillis()); String name = "差旅费报销申请单_"+dataMap.get("XM")+"_"+ newTime; //输出文档路径及名称 File outFile = new File(RuoYiConfig.getProfile()+"/download/wordtemplate/"+name+".doc"); Writer out = null; try { out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"utf-8")); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { t.process(dataMap, out); } catch (TemplateException e) { e.printStackTrace(); } Map map = new HashMap<>(); map.put("downloadUrl", "/profile/download/wordtemplate/"+name+".doc"); return map; } private void getData(Map dataMap,Long id) { ServiceReimbursement serviceReimbursement = serviceReimbursementService.getById(id); if (serviceReimbursement == null) { throw new ServiceException("下载失败,用户信息出错", HttpStatus.NO_CONTENT); } Date dt = serviceReimbursement.getCreateTime(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date = formatter.format(dt); String time = date.substring(0, 10); dataMap.put("TBYYMMDD", time); dataMap.put("BXDFJ", serviceReimbursement.getAttachcount() == 0 ? " " : "" + serviceReimbursement.getAttachcount()); dataMap.put("JXZXM", serviceReimbursement.getDonorname() == null ? "" : serviceReimbursement.getDonorname()); dataMap.put("JSR", serviceReimbursement.getUsername() == null ? "" : serviceReimbursement.getUsername()); dataMap.put("FYXM1", "差旅费报销总金额: " + String.format("%.2f", serviceReimbursement.getAmountrequested()).toString() + "元"); dataMap.put("FYXM2", ""); dataMap.put("FYXM3", ""); dataMap.put("JEXS", serviceReimbursement.getAmountrequested()); //double temp = 0; //temp = Math.round(serviceReimbursement.getAmountrequested() * 100) * 0.01d; dataMap.put("JEDS", convert(serviceReimbursement.getAmountrequested()) + "整"); String remark = ""; remark += serviceReimbursement.getCosttypename() == null ? "" : serviceReimbursement.getCosttypename() + "("; remark += serviceReimbursement.getTravelers() == null ? "" : serviceReimbursement.getTravelers() + ": "; remark += serviceReimbursement.getIdcardno() == null ? "" : serviceReimbursement.getIdcardno() + "; "; remark += serviceReimbursement.getDepositbank() == null ? "" : serviceReimbursement.getDepositbank() + ": "; remark += serviceReimbursement.getBankcardno() == null ? "" : serviceReimbursement.getBankcardno() + ")"; remark += "\n"; remark += serviceReimbursement.getRemark() == null ? "" : serviceReimbursement.getRemark(); dataMap.put("BXBZ", remark); dataMap.put("YYMMDD", time); dataMap.put("FJ", serviceReimbursement.getAttachcount() == 0 ? " " : "" + serviceReimbursement.getAttachcount()); dataMap.put("FP", serviceReimbursement.getInvoicecount() == 0 ? " " : "" + serviceReimbursement.getInvoicecount()); dataMap.put("XM", serviceReimbursement.getTravelers() == null ? "" : serviceReimbursement.getTravelers()); dataMap.put("BXR", serviceReimbursement.getUsername() == null ? "" : serviceReimbursement.getUsername()); dataMap.put("QYZZ", serviceReimbursement.getManagername() == null ? "" : serviceReimbursement.getManagername()); dataMap.put("CCSY", serviceReimbursement.getReason() == null ? "" : serviceReimbursement.getReason()); List rd = serviceReimbursementdetailService.getAllDetailsByRBID(id); if (rd == null) { throw new ServiceException("下载失败,没有对应信息", HttpStatus.NO_CONTENT); } List> newsList = new ArrayList>(); int days = 0; double te = 0; double cf = 0; double he = 0; double oe = 0; double fe = 0; double fa = 0; double hj = 0; double qt = 0; for(ServiceReimbursementdetail s : rd) { Map map=new HashMap(); Date st = s.getStarttime(); SimpleDateFormat formatter_st = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date_st = formatter_st.format(st); String year_st = date_st.substring(0, 4); String month_st = date_st.substring(5, 7); String day_st = date_st.substring(8, 10); map.put("DY", year_st == null ? "" : year_st); map.put("DM", month_st == null ? "" : month_st); map.put("DDD", day_st == null ? "" : day_st); map.put("DA", s.getDeparture() == null ? "" : s.getDeparture()); Date et = s.getEndtime(); SimpleDateFormat formatter_et = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String date_et = formatter_et.format(et); String year_et = date_et.substring(0, 4); String month_et = date_et.substring(5, 7); String day_et = date_et.substring(8, 10); map.put("AY", year_et == null ? "" : year_et); map.put("AM", month_et == null ? "" : month_et); map.put("AD", day_et == null ? "" : day_et); map.put("AA", s.getDestination() == null ? "" : s.getDestination()); map.put("ND", s.getDays() == 0 ? "" : s.getDays() + ""); days += s.getDays(); map.put("JT", s.getTraffictype() == null ? "" : s.getTraffictype()); map.put("QTFSM", s.getOtherfeedesc() == null ? "" : s.getOtherfeedesc()); map.put("JTF", s.getTrafficexpense() == 0.00 ? "" : String.format("%.2f", s.getTrafficexpense()).toString() + ""); te += s.getTrafficexpense(); map.put("SNJT", s.getCityfee() == 0.00 ? "" : String.format("%.2f", s.getCityfee()).toString() + ""); cf += s.getCityfee(); map.put("ZSF", s.getHotelexpense() == 0.00 ? "" : String.format("%.2f", s.getHotelexpense()).toString() + ""); he += s.getHotelexpense(); map.put("ZF", s.getOtherexpense() == 0.00 ? "" : String.format("%.2f", s.getOtherexpense()).toString() + ""); oe += s.getOtherexpense(); map.put("HSBX", s.getFoodexpenses() == 0.00 ? "" : String.format("%.2f",s.getFoodexpenses()).toString() + ""); fe += s.getFoodexpenses(); map.put("HSBZ", s.getFoodallowance() == 0.00 ? "" : String.format("%.2f",s.getFoodallowance()).toString() + ""); fa += s.getFoodallowance(); map.put("QTF", s.getOtherfeeamount() == 0.00 ? "" : String.format("%.2f",s.getOtherfeeamount()).toString() + ""); qt += s.getOtherfeeamount(); map.put("HJ", String.format("%.2f", s.getTrafficexpense() + s.getCityfee() + s.getHotelexpense() + s.getOtherexpense() + s.getFoodexpenses() + s.getFoodallowance() + s.getOtherfeeamount()).toString() + ""); hj += s.getTrafficexpense() + s.getCityfee() + s.getHotelexpense() + s.getOtherexpense() + s.getFoodexpenses() + s.getFoodallowance() + s.getOtherfeeamount(); //hj = Math.round(hj * 100) * 0.01d; DecimalFormat df = new DecimalFormat("0.00"); hj = new Double(df.format(hj).toString()); newsList.add(map); } dataMap.put("list",newsList); dataMap.put("TND", "" + days); dataMap.put("TJT", ""); dataMap.put("TJTF", "" + String.format("%.2f",te).toString() + ""); dataMap.put("TSNJT", "" + String.format("%.2f",cf).toString() + ""); dataMap.put("TZSF", "" + String.format("%.2f", he).toString() + ""); dataMap.put("TZF", "" + String.format("%.2f", oe).toString() + ""); dataMap.put("THSBX", "" + String.format("%.2f", fe).toString() + ""); dataMap.put("THSBZ", "" + String.format("%.2f", fa).toString() + ""); dataMap.put("TQTF", "" + String.format("%.2f", qt).toString() + ""); dataMap.put("THJ", "" + String.format("%.2f", hj).toString() + ""); dataMap.put("DS", convert(hj) + "整"); //dataMap.put("DS", "整"); dataMap.put("XS", String.format("%.2f", hj).toString() + ""); dataMap.put("R", serviceReimbursement.getCosttypename() == null ? "" : serviceReimbursement.getCosttypename()); } public static String convert(Double money) { String smoney = money.toString(); try{ if (smoney.indexOf(".") != -1) { //把数值分为整数型和带小数的数值分开处理。 String left = smoney.substring(0, smoney.indexOf(".")); String right = smoney.substring(smoney.indexOf(".") + 1); String result = convertLeft(left) + convertRight(right); return result; } else { return convertLeft(smoney); } }catch(IllegalArgumentException e){ System.out.println(e.getMessage()); System.out.println("请输入转换范围内现金"); return null; } } private static String convertLeft(String left) { //处理整数部分。 int length = left.length(); //根据单位'亿','万','元',把整数部分分为3种情况处理。 if (length <= 4) { //金额在千元以内。 if(length == 1 && Integer.valueOf(left) == 0) //金额为'0'元时的特殊情况。 return "零元"; return convertPart(left, length); } else if (length <= 8) { //金额在千万元以内。 String part1 = left.substring(0,length - 4); String part2 = left.substring(length - 4, length); String result1 = convertPart(part1, length); String result2 = convertPart(part2, 4); return result1 + result2; } else if (length <= 12) { //金额在千亿元以内。 String part1 = left.substring(0, length - 8); //截取单位为'亿'部分数值。 String part2 = left.substring(length - 8, length - 4); //截取单位为'万'部分数值。 String part3 = left.substring(length - 4, length); //截取单位为'元'部分数值。 String result1 = convertPart(part1, length); //转换单位为'亿'部分数值。 String result2 = convertPart(part2, 8); //转换单位为'万'部分数值。 String result3 = convertPart(part3, 4); //转换单位为'元'部分数值。 String result = result1 + result2 + result3; return result; } else { throw new IllegalArgumentException("超出转换数值范围!"); } } private static String convertRight(String right) { //处理小数部分。 String result = ""; String number = ""; String unit = ""; int length = right.length(); for (int i = 0; i < length; i++) { String detail = right.substring(i, i + 1); int value = Integer.valueOf(detail); number = upperNumber[value]; unit = cashUnitRight[i]; result = result.concat(number).concat(unit); } result = result.replaceAll("零角", "").replaceAll("零分", "").replaceAll("零厘", ""); return result; } private static String convertPart(String part, int position) { String result = ""; String number = ""; String unit = ""; if(part.equals("0000") && position == 8) //用于排除单位为'万'时,四位数值均为'0'的情况。 return ""; for (int i = 0; i < part.length(); i++) { int value = Integer.valueOf(String.valueOf(part.charAt(i))); number = upperNumber[value]; unit = cashUnitLeft[position - 1 - i]; result = result.concat(number).concat(unit); } result = result.replaceAll("零仟", "零").replaceAll("零佰", "零") //把单位'亿','万','元'放到最后替换。 .replaceAll("零拾", "零").replaceAll("零零", "零") .replaceAll("零零", "零").replaceAll("零亿", "亿") .replaceAll("零万", "万").replaceAll("零元", "元"); // result = result.replaceAll("零仟", "零"); // result = result.replaceAll("零佰", "零"); // result = result.replaceAll("零拾", "零"); // result = result.replaceAll("零零", "零"); // result = result.replaceAll("零零", "零"); // result = result.replaceAll("零亿", "亿"); // result = result.replaceAll("零万", "万"); // result = result.replaceAll("零元", "元"); System.out.println(result); return result; } }