java xxljob 根据参数运行业务

发布时间 2023-04-04 14:31:55作者: 何苦->

配置定时任务不启动,手动执行 根据传入的参数完成既定的业务


	/**
	 * 自定义增删除平台酒体数据
	 * 参数:startDate,endDate [yyyy-MM-dd)
	 *
	 * @return {@link * @return: com.xxl.job.core.biz.model.ReturnT<java.lang.String>}
	 * @author: xxx
	 * @date 2023/3/12
	 **/
	@XxlJob("wineMonthCustomHandler")
	public ReturnT<String> wineMonthCustomHandler() throws Exception {
		String param = XxlJobHelper.getJobParam();
		XxlJobHelper.log("========= wineMonthCustomHandler param = {} ===============", param);
		if (StringUtils.isEmpty(param)) {
			XxlJobHelper.log("========= 参数不能为空 ===============");
			return ReturnT.FAIL;
		}
		String s = null;
		try {
			s = wineMonthService.wineMonthCustom(param);
		}catch (Exception e) {
			XxlJobHelper.log("========= wineMonthCustomHandler {} ===============", e.getMessage());
		}

		XxlJobHelper.log("========= wineMonthCustomHandler end [{}] ===============", s);
		return ReturnT.SUCCESS;
	}
        @Override
	public String wineMonthCustom(String param) {
		String[] split = param.split(";|,|;|,|\\|");
		if (split.length != 3) {
			throw new BusinessException("参数不能多于或少于3个 格式:202302,平台名称,删除/开启");
		}
		String yyyyMM = null;
		String distributorName = null;
		Integer status = null;
		String tempName = null;

		List<String> temp = Arrays.asList(split);
		List<String> open = Arrays.asList("开","启", "Y","y","增", "加");
		List<String> close = Arrays.asList("关","闭", "N","n","删","除");

		Iterator it = temp.iterator();
		while(it.hasNext()){
			if (ObjectUtil.isNotEmpty(tempName)) {
				distributorName = tempName;
			}
			tempName = (String) it.next();
			try{
				yyyyMM = DateUtil.format(DateUtil.parse(tempName, "yyyyMM"), "yyyyMM");
				tempName = null;
			} catch (Exception e) {
			}
			if (checkExist(tempName, open)){
				status = TourDistributorNew.IS_DELETED_0;
				tempName = null;
			}
			if (checkExist(tempName, close)){
				status = TourDistributorNew.IS_DELETED_1;
				tempName = null;
			}
		}
		if (ObjectUtil.isNotEmpty(tempName) && ObjectUtil.isEmpty(distributorName)) {
			distributorName = tempName;
		}
		if (ObjectUtil.isEmpty(yyyyMM)) {
			throw new BusinessException("日期没有");
		}
		if (ObjectUtil.isEmpty(status)) {
			throw new BusinessException("状态没有");
		}
		if (ObjectUtil.isEmpty(distributorName)) {
			throw new BusinessException("平台名称没有");
		}
		WineMonth wineMonth = new WineMonth();
		wineMonth.setMonth(yyyyMM);
		wineMonth.setDistributorName(distributorName);
		wineMonth.setIsDeleted(status);
		WineMonthMaster wineMonthMaster = new WineMonthMaster();
		BeanUtil.copyProperties(wineMonth, wineMonthMaster);
		Long aLong = wineMonthMapper.selectAllCount(wineMonth);
		long count = iuGoodsWineService.count(Wrappers.<UGoodsWine>lambdaQuery().eq(UGoodsWine::getShow, 1));
		if (count < aLong && TourDistributorNew.IS_DELETED_0.equals(status)) {
			throw new BusinessException("酒体数据数量{" + aLong + "} 小于当前执行恢复删除数量 {" + count + "}");
		}
		int update1 = wineMonthMapper.updateNative(wineMonth);
		boolean update2 = iWineMonthMasterService.updateNative(wineMonthMaster);
		String format = String.format("平台名称 {%s} 日期 {%s} 状态 {%s} 总计查询到 {%s} 条数据 WineMonth表更新状态 {%s} WineMonthMaster表更新状态 {%s}",distributorName,yyyyMM,status, (ObjectUtil.isEmpty(aLong) ? 0 : aLong.toString()), update1, update2);
		log.info(format);
		return format;
	}