| @@ -4,10 +4,13 @@ import com.github.pagehelper.PageInfo; | |||
| import com.iformall.annotation.SystemControllerLog; | |||
| import com.iformall.annotation.TenantIgnore; | |||
| import com.iformall.common.ErrorCode; | |||
| import com.iformall.common.Result; | |||
| import com.iformall.common.ResultData; | |||
| import com.iformall.controller.base.BaseController; | |||
| import com.iformall.controller.mem.AsyncTask; | |||
| import com.iformall.domain.po.base.BaseEntity; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.domain.po.MallUserInfo; | |||
| import com.iformall.domain.po.WxCoupon; | |||
| import com.iformall.domain.po.WxCouponPassword; | |||
| import com.iformall.domain.vo.WxCouponPasswordCountInfoVO; | |||
| @@ -15,15 +18,27 @@ import com.iformall.domain.vo.WxCouponPasswordVo; | |||
| import com.iformall.enums.EnumCouponPasswordStatus; | |||
| import com.iformall.enums.EnumCouponSubsidyType; | |||
| import com.iformall.enums.EnumYesOrNo; | |||
| import com.iformall.exception.MallinkException; | |||
| import com.iformall.service.WxCouponPasswordService; | |||
| import com.iformall.utils.Constant; | |||
| import io.swagger.annotations.Api; | |||
| import io.swagger.annotations.ApiImplicitParam; | |||
| import io.swagger.annotations.ApiImplicitParams; | |||
| import io.swagger.annotations.ApiOperation; | |||
| import java.io.BufferedInputStream; | |||
| import java.io.File; | |||
| import java.io.FileOutputStream; | |||
| import java.io.IOException; | |||
| import java.util.concurrent.TimeUnit; | |||
| import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.data.redis.core.StringRedisTemplate; | |||
| import org.springframework.web.bind.annotation.*; | |||
| import org.springframework.web.multipart.MultipartFile; | |||
| /** | |||
| * @author gongbiao | |||
| @@ -36,6 +51,13 @@ public class WxCouponPasswordController extends BaseController { | |||
| @Autowired | |||
| private WxCouponPasswordService wxCouponPasswordService; | |||
| @Autowired | |||
| StringRedisTemplate stringRedisTemplate; | |||
| @Autowired | |||
| private AsyncTask asyncTask; | |||
| @Autowired | |||
| private String fmUploadDir; | |||
| @ApiOperation("分页列表接口") | |||
| @GetMapping("/list") | |||
| @@ -136,6 +158,89 @@ public class WxCouponPasswordController extends BaseController { | |||
| wxCouponPasswordService.setPrice(wxCouponPassword); | |||
| return new ResultData(); | |||
| } | |||
| @TenantIgnore | |||
| @PostMapping(value = "/importPrice", consumes = "multipart/*") | |||
| @SystemControllerLog(description = "消费卡批量导入面额") | |||
| public ResultData importPrice(@RequestParam("file") MultipartFile mFile) { | |||
| if (mFile.isEmpty()) { | |||
| throw new MallinkException(Result.ERROR, "上传文件不能为空"); | |||
| } | |||
| //得到当前用户ID | |||
| final MallUserInfo user = getUser(); | |||
| String userId = "" + user.getId(); | |||
| String importKey = Constant.importCardPrice + userId; | |||
| //查询当前用户得到的值是否为空,为空继续,不为空,返回模板正在导入 | |||
| Boolean allCount = stringRedisTemplate.opsForHash().hasKey(importKey, "allCount"); | |||
| if (allCount) { | |||
| return new ResultData(Result.SUCCESS, "模板正在导入"); | |||
| } | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", 0 + ""); | |||
| stringRedisTemplate.expire(importKey,30,TimeUnit.MINUTES); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allSuccessCount", 0 + ""); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "processCount", ""); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", ""); | |||
| String fpath = fmUploadDir; | |||
| File targetFile = new File(fpath); | |||
| if (!targetFile.exists()) { | |||
| targetFile.mkdirs(); | |||
| } | |||
| String fileName = "1.xlsx"; | |||
| int dot = mFile.getOriginalFilename().lastIndexOf('.'); | |||
| fileName = fileName + mFile.getOriginalFilename().substring(dot, mFile.getOriginalFilename().length()); | |||
| File lFile = new File(fpath + File.separator + fileName); | |||
| FileOutputStream fos = null; | |||
| BufferedInputStream fs = null; | |||
| try { | |||
| fos = new FileOutputStream(lFile); | |||
| fs = (BufferedInputStream) mFile.getInputStream(); | |||
| byte[] buffer = new byte[1024]; | |||
| int len = 0; | |||
| while ((len = fs.read(buffer)) != -1) { | |||
| fos.write(buffer, 0, len); | |||
| } | |||
| fos.close(); | |||
| fs.close(); | |||
| } catch (Exception e) { | |||
| stringRedisTemplate.expire(importKey,3,TimeUnit.SECONDS); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", "1"); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", "1"); | |||
| logger.error(e.getMessage()); | |||
| return new ResultData(ErrorCode.MEM_IMPORT_ERR.getCode(), "模板上传失败"); | |||
| } finally { | |||
| if (fos != null) { | |||
| try { | |||
| fos.close(); | |||
| } catch (IOException e) { | |||
| stringRedisTemplate.expire(importKey,3,TimeUnit.SECONDS); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", "1"); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", "1"); | |||
| logger.error(e.getMessage()); | |||
| return new ResultData(ErrorCode.MEM_IMPORT_ERR.getCode(), "模板上传失败"); | |||
| } | |||
| } | |||
| if (fs != null) { | |||
| try { | |||
| fs.close(); | |||
| } catch (IOException e) { | |||
| stringRedisTemplate.expire(importKey,3,TimeUnit.SECONDS); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "allCount", "1"); | |||
| stringRedisTemplate.opsForHash().putIfAbsent(importKey, "failCount", "1"); | |||
| logger.error(e.getMessage()); | |||
| return new ResultData(ErrorCode.MEM_IMPORT_ERR.getCode(), "模板上传失败"); | |||
| } | |||
| } | |||
| } | |||
| asyncTask.importExcelData(lFile, user, importKey); | |||
| return new ResultData(Result.SUCCESS, "模板正在导入"); | |||
| } | |||
| } | |||
| @@ -9,8 +9,10 @@ import com.iformall.domain.po.MallUserInfo; | |||
| import com.iformall.domain.po.WxTags; | |||
| import com.iformall.domain.vo.CUserBaseInfoT; | |||
| import com.iformall.domain.vo.MerchantPoiT; | |||
| import com.iformall.domain.vo.WxCardPriceTemplate; | |||
| import com.iformall.service.TtMerchantPoiService; | |||
| import com.iformall.service.WxCUserBasicInfoService; | |||
| import com.iformall.service.WxCouponPasswordService; | |||
| import com.iformall.service.WxTagsService; | |||
| import org.apache.shiro.session.UnknownSessionException; | |||
| import org.slf4j.Logger; | |||
| @@ -36,6 +38,9 @@ public class AsyncTask { | |||
| @Autowired | |||
| private WxTagsService wxTagsService; | |||
| @Autowired | |||
| private WxCouponPasswordService wxCouponPasswordService; | |||
| @Autowired | |||
| StringRedisTemplate stringRedisTemplate; | |||
| @@ -46,7 +51,6 @@ public class AsyncTask { | |||
| if (value == null) { | |||
| value = ""; | |||
| } | |||
| System.out.println(name + " + " + value.toString()); | |||
| return super.importHandler(obj, name, value); | |||
| } | |||
| @@ -58,7 +62,17 @@ public class AsyncTask { | |||
| if (value == null) { | |||
| value = ""; | |||
| } | |||
| System.out.println(name + " + " + value.toString()); | |||
| return super.importHandler(obj, name, value); | |||
| } | |||
| } | |||
| private class CardPriceExcelHandler extends ExcelDataHandlerDefaultImpl<WxCardPriceTemplate> { | |||
| @Override | |||
| public Object importHandler(WxCardPriceTemplate obj, String name, Object value) { | |||
| if (value == null) { | |||
| value = ""; | |||
| } | |||
| return super.importHandler(obj, name, value); | |||
| } | |||
| @@ -195,4 +209,63 @@ public class AsyncTask { | |||
| } | |||
| } | |||
| } | |||
| @Async | |||
| public void importCardPriceData(File file, MallUserInfo user, String importKey) { | |||
| ImportParams params = new ImportParams(); | |||
| // 需要验证 | |||
| params.setImportFields(new String[]{"卡ID", "面额(元)"}); | |||
| IExcelDataHandler<WxCardPriceTemplate> handler = new AsyncTask.CardPriceExcelHandler(); | |||
| handler.setNeedHandlerFields(new String[]{"卡ID","面额(元)"}); | |||
| params.setNeedVerify(true); | |||
| ExcelImportResult<WxCardPriceTemplate> datalist = null; | |||
| try { | |||
| datalist = ExcelImportUtil.importExcelMore(file, WxCardPriceTemplate.class, params); | |||
| } catch (Exception e) { | |||
| set_redis_value(importKey, "1", "0", "0", "1", true); | |||
| logger.error(e.getMessage()); | |||
| // 删除缓存文件 | |||
| file.delete(); | |||
| return; | |||
| } | |||
| // 删除缓存文件 | |||
| file.delete(); | |||
| if(datalist == null) { | |||
| logger.error("导入模板失败: 模板数据解析失败"); | |||
| set_redis_value(importKey, "1", "0", "0", "1", true); | |||
| return; | |||
| } | |||
| List<WxCardPriceTemplate> successList = datalist.getList(); | |||
| List<WxCardPriceTemplate> failList = datalist.getFailList(); | |||
| logger.info("验证通过的数量: " + successList.size()); | |||
| logger.info("验证未通过的数量: " + failList.size()); | |||
| int total = successList.size() + failList.size(); | |||
| int all_success = successList.size(); | |||
| int all_fail = failList.size(); | |||
| //添加到redis里 | |||
| set_redis_value(importKey, "" + total, "" + all_success, "0", "" + all_fail, total == all_fail); | |||
| if(successList.size() > 0) { | |||
| try { | |||
| successList.parallelStream().forEach(poiBase -> { | |||
| try { | |||
| wxCouponPasswordService.importPrice(user,importKey, poiBase); | |||
| } catch (UnknownSessionException ue) { | |||
| logger.error("session :"+ue.getMessage()); | |||
| } | |||
| }); | |||
| } catch (Exception e) { | |||
| set_redis_value(importKey, "1", "0", "0", "1", true); | |||
| e.printStackTrace(); | |||
| logger.error("导入模板失败:"+e.getMessage()); | |||
| } | |||
| } | |||
| } | |||
| } | |||
| @@ -0,0 +1,24 @@ | |||
| package com.iformall.domain.vo; | |||
| import cn.afterturn.easypoi.excel.annotation.Excel; | |||
| import com.baomidou.mybatisplus.annotation.TableField; | |||
| import lombok.Data; | |||
| import javax.validation.constraints.NotNull; | |||
| import javax.validation.constraints.Pattern; | |||
| import java.io.Serializable; | |||
| import java.math.BigDecimal; | |||
| @Data | |||
| public class WxCardPriceTemplate implements Serializable { | |||
| private static final long serialVersionUID = -2321650008236516884L; | |||
| @NotNull | |||
| @Excel(name="卡密ID",width = 20,orderNum = "1") | |||
| @io.swagger.annotations.ApiModelProperty(value="ID",name="couponPasswordId") | |||
| private String couponPasswordId; | |||
| @NotNull | |||
| @Excel(name="面额(元)",width = 20,orderNum = "2") | |||
| @io.swagger.annotations.ApiModelProperty(value="面额(元)",name="price") | |||
| private String price; | |||
| } | |||
| @@ -1,8 +1,10 @@ | |||
| package com.iformall.service; | |||
| import com.github.pagehelper.PageInfo; | |||
| import com.iformall.domain.po.MallUserInfo; | |||
| import com.iformall.domain.po.WxCouponPassword; | |||
| import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.domain.vo.WxCardPriceTemplate; | |||
| import com.iformall.domain.vo.WxCouponPasswordCountInfoVO; | |||
| import com.iformall.domain.vo.WxCouponPasswordVo; | |||
| @@ -79,4 +81,6 @@ public interface WxCouponPasswordService { | |||
| void startOrStop(WxCouponPassword wxCouponPassword); | |||
| void setPrice(WxCouponPassword wxCouponPassword); | |||
| void importPrice(MallUserInfo user,String importKey, WxCardPriceTemplate template); | |||
| } | |||
| @@ -3,6 +3,7 @@ package com.iformall.service.impl; | |||
| import com.github.pagehelper.PageHelper; | |||
| import com.github.pagehelper.PageInfo; | |||
| import com.iformall.common.IdWorker; | |||
| import com.iformall.domain.po.MallUserInfo; | |||
| import com.iformall.domain.po.WxCUserBasicInfo; | |||
| import com.iformall.domain.po.WxCardInfo; | |||
| import com.iformall.domain.po.WxCoupon; | |||
| @@ -15,6 +16,7 @@ import com.iformall.domain.po.base.TenantEntity; | |||
| import com.iformall.enums.EnumCouponOrderStatus; | |||
| import com.iformall.enums.EnumCouponPasswordStatus; | |||
| import com.iformall.enums.EnumYesOrNo; | |||
| import com.iformall.domain.vo.WxCardPriceTemplate; | |||
| import com.iformall.domain.vo.WxCouponOrderBVo; | |||
| import com.iformall.domain.vo.WxCouponPasswordCountInfoVO; | |||
| import com.iformall.domain.vo.WxCouponPasswordVo; | |||
| @@ -34,6 +36,7 @@ import org.slf4j.Logger; | |||
| import org.slf4j.LoggerFactory; | |||
| import org.springframework.beans.factory.annotation.Autowired; | |||
| import org.springframework.context.annotation.Lazy; | |||
| import org.springframework.data.redis.core.StringRedisTemplate; | |||
| import org.springframework.stereotype.Service; | |||
| import javax.servlet.http.HttpServletRequest; | |||
| @@ -48,6 +51,7 @@ import java.util.Iterator; | |||
| import java.util.List; | |||
| import java.util.Map; | |||
| import java.util.Random; | |||
| import java.util.concurrent.TimeUnit; | |||
| @Service | |||
| public class WxCouponPasswordServiceImpl implements WxCouponPasswordService { | |||
| @@ -65,6 +69,9 @@ public class WxCouponPasswordServiceImpl implements WxCouponPasswordService { | |||
| @Autowired | |||
| ExcelService excelService; | |||
| @Autowired | |||
| StringRedisTemplate stringRedisTemplate; | |||
| @Override | |||
| public PageInfo<WxCouponPassword> listAsPage(WxCouponPassword record, Integer pageIndex, Integer pageSize) { | |||
| @@ -340,5 +347,33 @@ public class WxCouponPasswordServiceImpl implements WxCouponPasswordService { | |||
| public void setPrice(WxCouponPassword wxCouponPassword) { | |||
| wxCouponPasswordMapper.setPrice(wxCouponPassword); | |||
| } | |||
| @Override | |||
| public void importPrice(MallUserInfo user,String importKey, WxCardPriceTemplate template) { | |||
| if (StringUtils.isBlank(template.getCouponPasswordId())) { | |||
| stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1); | |||
| logger.error("卡密ID为空", template.toString()); | |||
| return; | |||
| } | |||
| if (StringUtils.isBlank(template.getPrice())) { | |||
| stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1); | |||
| logger.error("面额为空", template.toString()); | |||
| return; | |||
| } | |||
| try { | |||
| WxCouponPassword password = new WxCouponPassword(); | |||
| password.updateTenantInfo(user); | |||
| password.setId(Long.parseLong(template.getCouponPasswordId())); | |||
| password.setPrice(new BigDecimal(template.getPrice()).multiply(new BigDecimal(100)).intValue()); | |||
| setPrice(password); | |||
| }catch(Exception e) { | |||
| stringRedisTemplate.opsForHash().increment(importKey, "processCount", 1); | |||
| logger.error("import price error.",e); | |||
| return; | |||
| } | |||
| stringRedisTemplate.opsForHash().increment(importKey,"processCount",1); | |||
| stringRedisTemplate.expire(importKey,10, TimeUnit.SECONDS); | |||
| } | |||
| } | |||
| @@ -88,6 +88,9 @@ public class Constant { | |||
| // 导入会员 | |||
| public static final String importMemPrev = "importmem:"; | |||
| public static final String importInvestCustomerPrev = "importinvestcustomer:"; | |||
| //导入卡面额 | |||
| public static final String importCardPrice = "importCardPrice:"; | |||
| // 导入POI | |||
| public static final String importPoiPrev = "importpoi:"; | |||