AntiFakeServiceImpl.java 9.96 KB
Newer Older
licc's avatar
licc committed
1 2
package cn.wisenergy.service.app.impl;

licc's avatar
licc committed
3
import cn.wisenergy.common.constant.CommonAttributes;
licc's avatar
licc committed
4 5
import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.R;
licc's avatar
licc committed
6
import cn.wisenergy.common.utils.StringUtil;
licc's avatar
licc committed
7 8
import cn.wisenergy.mapper.AntiFakeMapper;
import cn.wisenergy.model.app.AntiFake;
licc's avatar
licc committed
9
import cn.wisenergy.model.app.ProductInfo;
licc's avatar
licc committed
10
import cn.wisenergy.model.dto.AntiFakeQuery;
licc's avatar
licc committed
11 12 13
import cn.wisenergy.model.dto.CreateCodeVo;
import cn.wisenergy.model.vo.ReportCodeVo;
import cn.wisenergy.service.Manager.AntiFakeManger;
licc's avatar
licc committed
14 15 16
import cn.wisenergy.service.app.AntiFakeService;
import cn.wisenergy.service.util.CodeUtils;
import cn.wisenergy.service.util.QRCodeUtils;
licc's avatar
licc committed
17
import com.alibaba.excel.EasyExcel;
licc's avatar
licc committed
18
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
licc's avatar
licc committed
19
import com.github.pagehelper.PageInfo;
licc's avatar
licc committed
20
import lombok.extern.slf4j.Slf4j;
licc's avatar
licc committed
21
import org.apache.commons.codec.digest.DigestUtils;
licc's avatar
licc committed
22 23 24
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
licc's avatar
licc committed
25
import org.springframework.util.CollectionUtils;
licc's avatar
licc committed
26

licc's avatar
licc committed
27
import javax.servlet.http.HttpServletResponse;
licc's avatar
licc committed
28
import java.text.ParseException;
licc's avatar
licc committed
29
import java.util.*;
licc's avatar
licc committed
30 31 32 33 34 35 36 37 38

/**
 * @author 86187
 */
@Service
@Slf4j
public class AntiFakeServiceImpl extends ServiceImpl<AntiFakeMapper, AntiFake> implements AntiFakeService {

    private static final String DEST_PATH = "/opt/upload/video";
licc's avatar
licc committed
39 40

    private static final String REQUEST_URL = "http://localhost:81/shop-mall/admin/scanCode?shaValue=";
licc's avatar
licc committed
41 42 43
    @Autowired
    private AntiFakeMapper antiFakeMapper;

licc's avatar
licc committed
44 45 46
    @Autowired
    private AntiFakeManger antiFakeManger;

licc's avatar
licc committed
47
    @Override
licc's avatar
licc committed
48
    public R<Boolean> createCode(CreateCodeVo codeVo) throws ParseException {
licc's avatar
licc committed
49 50
        log.info("AntiFakeServiceImpl[]createCode[]input.param.codeVo:" + codeVo);
        if (null == codeVo || StringUtils.isBlank(codeVo.getProduceTime())) {
licc's avatar
licc committed
51 52
            return R.error("入参不能为空!");
        }
licc's avatar
licc committed
53 54 55 56 57 58 59 60

        int number;
        if (null == codeVo.getNumber()) {
            number = 1000;
        } else {
            number = codeVo.getNumber();
        }

licc's avatar
licc committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        //获取最大批次号
        String batch = antiFakeMapper.getByBestBigNumber();
        String batchNumber = "";
        if (StringUtils.isBlank(batch)) {
            batchNumber = "10000001";
        } else {
            Integer sum = Integer.parseInt(batch) + 1;
            batchNumber = String.valueOf(sum);
        }

        int initBatchNo = 10000000;
        String realTime = DateUtil.getTime(new Date(), "yyyy-MM-dd");
        List<AntiFake> list = new ArrayList<>();

        for (int i = 0; i < number; i++) {
            int batchNo = initBatchNo + i + 1;
            AntiFake antiFake = new AntiFake();
            antiFake.setBatchNumber(batchNumber);
            antiFake.setProduceTime(realTime);

            //生成产品编码
licc's avatar
licc committed
82
            String productNo = CodeUtils.createProductNo(batchNumber, String.valueOf(batchNo), codeVo.getProduceTime());
licc's avatar
licc committed
83 84
            antiFake.setProductNo(productNo);
            antiFake.setStatus(0);
licc's avatar
licc committed
85
            antiFake.setUseStatus(0);
licc's avatar
licc committed
86

licc's avatar
licc committed
87
            //生成防伪码
licc's avatar
licc committed
88 89
            String sha = DigestUtils.sha1Hex(productNo).substring(0, 16);
            antiFake.setShaValue(sha);
licc's avatar
licc committed
90 91 92 93

            //生成六位随机数作为安全码
            int code = (int) ((Math.random() * 9 + 1) * 100000);
            antiFake.setSecurityCode(String.valueOf(code));
licc's avatar
licc committed
94 95
            antiFake.setScanNumber(0);
            antiFake.setProduceTime(codeVo.getProduceTime());
licc's avatar
licc committed
96 97 98
            list.add(antiFake);
        }

licc's avatar
licc committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        //初始化产品认证信息
        ProductInfo productInfo = new ProductInfo();
        productInfo.setBatchNumber(batchNumber);
        productInfo.setCompanyAddress("東京都千代田区東神田2丁目6番7号608");
        productInfo.setCompanyName("日本西田森製薬株式会社");
        productInfo.setCompanyUrl("www.nishidamori.co.jp");
        productInfo.setLogisticsUrl("中国国际货运航空 https://www.airchinacargo.com/en/search_order.php");
        productInfo.setBrand("西田森");
        productInfo.setProductCountry(codeVo.getProductCountry());
        productInfo.setRecordNumber(codeVo.getRecordNumber());
        productInfo.setTrackingNumber(codeVo.getTrackingNumber());
        productInfo.setValidTime(codeVo.getValidTime());
        productInfo.setSpecification(codeVo.getSpecification());
        productInfo.setBrandName(codeVo.getBrandName());

        //批量添加二维码 和产品认证信息
        boolean bool = antiFakeManger.addAntiFakeAndProductInfo(list, productInfo);
        if (!bool) {
            return R.ok(1, false);
licc's avatar
licc committed
118 119 120 121 122 123 124 125 126 127 128
        }
        return R.ok(0, true);
    }

    @Override
    public R<String> uploadImage() throws Exception {
        log.info("上传二维码防伪图测试");
        //生成二维码
        String url = QRCodeUtils.encode("2021-04022", DEST_PATH);
        return R.ok(url);
    }
licc's avatar
licc committed
129 130

    @Override
licc's avatar
licc committed
131
    public R<String> scanCode(String shaValue, String securityCode) {
licc's avatar
licc committed
132
        log.info("AntiFakeServiceImpl[]scanCode[]input.param.shaValue:" + shaValue);
licc's avatar
licc committed
133
        if (StringUtils.isBlank(shaValue) || StringUtils.isBlank(securityCode)) {
licc's avatar
licc committed
134 135 136 137
            return R.error("入参不能为空!");
        }

        //获取二维码信息
licc's avatar
licc committed
138
        AntiFake antiFake = antiFakeMapper.getByShaValue(shaValue);
licc's avatar
licc committed
139 140 141 142
        if (null == antiFake) {
            return R.error("无效二维码");
        }

licc's avatar
licc committed
143 144 145 146
        if (!securityCode.equals(antiFake.getSecurityCode())) {
            return R.error("安全码不匹配!");
        }

licc's avatar
licc committed
147
        if (antiFake.getStatus() == 1) {
licc's avatar
licc committed
148 149 150 151 152 153
            antiFake.setScanNumber(antiFake.getScanNumber() + 1);
            int count = antiFakeMapper.edit(antiFake);
            if (count == 0) {
                return R.error("扫描失败!");
            }
            return R.ok("防伪码号" + antiFake.getShaValue() + "的产品为非正品," +
licc's avatar
licc committed
154
                    "第一次扫描时间为:" + DateUtil.convertDateToStr(antiFake.getScanTime(), "yyyy-MM-dd HH:mm:ss")
licc's avatar
licc committed
155
                    + ",共扫描过" + antiFake.getScanNumber() + "次码");
licc's avatar
licc committed
156 157 158 159
        }

        antiFake.setStatus(1);
        antiFake.setScanTime(new Date());
licc's avatar
licc committed
160
        antiFake.setScanNumber(antiFake.getScanNumber() + 1);
licc's avatar
licc committed
161 162 163 164
        int count = antiFakeMapper.edit(antiFake);
        if (count == 0) {
            return R.error("扫描失败!");
        }
licc's avatar
licc committed
165
        return R.ok("防伪码号" + antiFake.getShaValue() + "的产品为正品," +
licc's avatar
licc committed
166
                "第一次扫描时间为:" + DateUtil.convertDateToStr(antiFake.getScanTime(), "yyyy-MM-dd HH:mm:ss")
licc's avatar
licc committed
167
                + ",共扫描过" + antiFake.getScanNumber() + "次码");
licc's avatar
licc committed
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
    }

    @Override
    public R<PageInfo<AntiFake>> getList(AntiFakeQuery query) {
        log.info("AntiFakeServiceImpl[]getList[]input.param.query:" + query);
        if (null == query) {
            return R.error("入参不能为空!");
        }

        pageHandle(query);

        Map<String, Object> map = new HashMap<>(8);
        int total = antiFakeMapper.count(map);

        map.put("startNum", query.getStartNum());
        map.put("endNum", query.getEndNum());
        List<AntiFake> list = antiFakeMapper.getList(map);
        PageInfo<AntiFake> info = new PageInfo<>();
        info.setPageSize(query.getPageSize());
        info.setPageNum(query.getPageNo());
        info.setTotal(total);
        info.setList(list);
        return R.ok(info);
    }

licc's avatar
licc committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    @Override
    public R<Boolean> report(String str, HttpServletResponse response) {
        log.info("AntiFakeServiceImpl[]report[]input.param.str:" + str);
        if (StringUtils.isBlank(str)) {
            return R.error("入参不能为空!");
        }

        //把字符转化为数字
        List<Long> array = StringUtil.strToLongArray(str);
        if (CollectionUtils.isEmpty(array)) {
            return R.ok(false);
        }

        //获取防伪二维码生产码
        List<AntiFake> result = antiFakeMapper.getProductNos(array);
licc's avatar
licc committed
208 209 210 211
        if (CollectionUtils.isEmpty(result)) {
            return R.ok(0, true);
        }

licc's avatar
licc committed
212 213 214 215 216 217 218 219 220 221 222 223
        for (AntiFake anti : result) {
            String url = REQUEST_URL + anti.getShaValue();
            anti.setShaValue(url);
        }

        //生成Excel
        try {
            //  设置内容格式 以及 编码方式
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");

            //  使用java8新特性的stream流去处理数据,把空的数据过滤掉
licc's avatar
licc committed
224 225 226 227 228 229
            List<ReportCodeVo> resultBo = new ArrayList<>();
            for (AntiFake antiFake : result) {
                ReportCodeVo reportCodeVo = new ReportCodeVo();
                reportCodeVo.setShaValue(antiFake.getShaValue());
                resultBo.add(reportCodeVo);
            }
licc's avatar
licc committed
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248

            //创建文件名称
            long lon = System.currentTimeMillis();

            response.setHeader("Content-disposition", "attachment;filename=" + lon + ".xlsx");

            //  sheet名称
            EasyExcel.write(response.getOutputStream(), ReportCodeVo.class).sheet(Long.toString(lon)).doWrite(resultBo);
            //修改二维码使用状态
            boolean bool = antiFakeManger.updateAntiFake(array);
            if (!bool) {
                return R.error("更新二维码使用状态失败!");
            }
            return R.ok(0, true);
        } catch (Exception e) {
            return R.ok(1, false);
        }
    }

licc's avatar
licc committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
    /**
     * 分页处理方法
     *
     * @param schemeVo 参数
     */
    private void pageHandle(AntiFakeQuery schemeVo) {
        Integer pageNum = schemeVo.getPageNo();
        Integer pageSize = schemeVo.getPageSize();
        if (null == pageSize || pageSize == 0) {
            pageSize = 10;
        }
        if (null == pageNum || pageNum == 0) {
            pageNum = 1;
        }
        Integer endNum = pageSize;
        Integer startNum = (pageNum - CommonAttributes.NUM_ONE) * pageSize;
        schemeVo.setEndNum(endNum);
        schemeVo.setStartNum(startNum);
        schemeVo.setPageNo(pageNum);
        schemeVo.setPageSize(pageSize);
    }
licc's avatar
licc committed
270
}