package cn.wisenergy.service.app.impl;

import cn.wisenergy.common.constant.CommonAttributes;
import cn.wisenergy.common.utils.DateUtil;
import cn.wisenergy.common.utils.R;
import cn.wisenergy.mapper.AntiFakeMapper;
import cn.wisenergy.model.app.AccountInfo;
import cn.wisenergy.model.app.AntiFake;
import cn.wisenergy.model.dto.AccountInfoQuery;
import cn.wisenergy.model.dto.AntiFakeQuery;
import cn.wisenergy.service.app.AntiFakeService;
import cn.wisenergy.service.util.CodeUtils;
import cn.wisenergy.service.util.FileUtils;
import cn.wisenergy.service.util.QRCodeUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.*;

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

    private static final String DEST_PATH = "/opt/upload/video";
    @Autowired
    private AntiFakeMapper antiFakeMapper;

    @Override
    public R<Boolean> createCode(Integer number) throws Exception {
        log.info("AntiFakeServiceImpl[]createCode[]input.param.number:" + number);
        if (null == number) {
            return R.error("入参不能为空!");
        }
        //获取最大批次号
        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.setBatchNo(String.valueOf(batchNo));
            antiFake.setBatchNumber(batchNumber);
            antiFake.setProduceTime(realTime);

            //生成产品编码
            String productNo = CodeUtils.createProductNo(batchNumber, antiFake.getBatchNo());
            antiFake.setProductNo(productNo);
            antiFake.setStatus(0);

            //生成二维码
            String url = QRCodeUtils.encode(productNo, DEST_PATH);
            antiFake.setUrl(url);
            list.add(antiFake);
        }

        //批量添加二维码
        int count = antiFakeMapper.creates(list);
        if (count != list.size()) {
            return R.error("生成防伪二维码失败!");
        }
        return R.ok(0, true);
    }

    @Override
    public R<String> decode(MultipartFile multipartFile) throws Exception {
        log.info("AntiFakeServiceImpl[]decode[]input.param.multipartFile:" + multipartFile);
        if (null == multipartFile) {
            return R.error("入参为空");
        }

        File file = FileUtils.multipartFileToFile(multipartFile);

        String result = QRCodeUtils.decode(file);
        return R.ok(result);
    }

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

    @Override
    public R<String> scanCode(String produceNo) {
        log.info("AntiFakeServiceImpl[]scanCode[]input.param.produceNo:" + produceNo);
        if (StringUtils.isBlank(produceNo)) {
            return R.error("入参不能为空!");
        }

        //获取二维码信息
        AntiFake antiFake = antiFakeMapper.getByProductNo(produceNo);
        if (null == antiFake) {
            return R.error("无效二维码");
        }

        if (antiFake.getStatus() == 1) {
            return R.ok("该产品防伪码已于" + antiFake.getScanTime() + "被扫描,批次号为:"
                    + antiFake.getBatchNumber() + ",编号为:" + antiFake.getBatchNo() + "");
        }

        antiFake.setStatus(1);
        antiFake.setScanTime(new Date());
        int count = antiFakeMapper.edit(antiFake);
        if (count == 0) {
            return R.error("扫描失败!");
        }
        return R.ok("该防伪二维码第一次被扫描!");
    }

    @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);
    }

    /**
     * 分页处理方法
     *
     * @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);
    }
}
