package com.younike.system.service.impl;

import com.google.zxing.WriterException;
import com.younike.common.core.domain.AjaxResult;
import com.younike.common.utils.AliyunOSSUtil;
import com.younike.common.utils.SecurityUtils;
import com.younike.common.utils.bean.BeanUtils;
import com.younike.common.utils.id.IDUtils;
import com.younike.common.utils.qrcode.QRCodeGenerator;
import com.younike.common.utils.sign.Base64;
import com.younike.system.domain.AccessPasscode;
import com.younike.system.domain.Order;
import com.younike.system.domain.vo.AccessPasscodeVO;
import com.younike.system.enums.OrderStateEnum;
import com.younike.system.mapper.AccessPasscodeMapper;
import com.younike.system.mapper.OrderMapper;
import com.younike.system.param.AccessPasscodeParam;
import com.younike.system.param.AccessPasscodeQueryParam;
import com.younike.system.service.IAccessPasscodeService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * 门岗通行证Service业务层处理
 *
 * @author dail
 * @date 2022-04-15
 */
@Service
@Log4j2
public class AccessPasscodeServiceImpl implements IAccessPasscodeService {

    @Autowired
    private AccessPasscodeMapper accessPasscodeMapper;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private OrderMapper orderMapper;


    /**
     * 根据订单信息生成门岗通行证
     *
     * @param accessPasscodeParam
     * @return
     */
    @Override
    public AjaxResult saveAccessPasscode(AccessPasscodeParam accessPasscodeParam) {
        // 根据订单id和核销码查询是否生成过门岗通行证
        AccessPasscode accessPasscode = accessPasscodeMapper.selectByOrderIdAndOrderVerificationCode(accessPasscodeParam.getOrderId(), accessPasscodeParam.getOrderVerificationCode());
        if (accessPasscode != null) {
            return AjaxResult.success(accessPasscode);
        }
        // 查询订单信息
        Order order = orderMapper.selectOrderByOrderId(accessPasscodeParam.getOrderId());
        if (order == null) {
            return AjaxResult.error("生成门岗通行证时未查询到订单信息!");
        }
        if (!OrderStateEnum.PENDING_SERVICE.getCode().equals(order.getOrderState()) && !"003".equals(order.getOrderType())) {
            return AjaxResult.error("生成门岗通行证时关联订单状态不合法!");
        }
        // 生成门岗通行证id
        Long accessPasscodeId = IDUtils.nextLongId();
        String jumpUrl = "https://server.uxianxin.com?orderId=" + accessPasscodeParam.getOrderId() + "&accessPasscodeId=" + accessPasscodeId;
//        // 生成二维码并上传oss
//        String name = String.valueOf(accessPasscodeParam.getOrderId());
//        if (StringUtils.isNotBlank(accessPasscodeParam.getOrderVerificationCode())) {
//            name = name + "-" + accessPasscodeParam.getOrderVerificationCode();
//        }
//        name = name + ".png";
//        MultipartFile multipartFile = null;
        String qrCodeEncode = StringUtils.EMPTY;
        try {
            qrCodeEncode = Base64.encode(QRCodeGenerator.getQRCodeImage(jumpUrl, 300, 300));
        } catch (IOException | WriterException e) {
            log.error("门岗通行证二维码生成出错:" + e.getMessage());
            e.printStackTrace();
            return AjaxResult.error("门岗通行证二维码生成出错");
        }
        if (StringUtils.isBlank(qrCodeEncode)) {
            return AjaxResult.error("门岗通行证二维码生成Base64编码出错!");
        }
//        String url = StringUtils.EMPTY;
//        try {
//            url = aliyunOSSUtil.uploadImage(multipartFile);
//            if (StringUtils.isBlank(url)) {
//                return AjaxResult.error("门岗通行证二维码上传OSS出错,未获取到图片文件url!");
//            }
//        } catch (Exception e) {
//            log.error("门岗通行证二维码上传OSS出错:" + e.getMessage());
//            e.printStackTrace();
//            return AjaxResult.error("门岗通行证二维码上传OSS出错");
//        }
        // 保存门岗通行证
        AccessPasscode newAccessPasscode = new AccessPasscode();
        BeanUtils.copyProperties(accessPasscodeParam, newAccessPasscode);
        newAccessPasscode.setAccessPasscodeId(accessPasscodeId);
        if (accessPasscodeParam.getMemberId() != null) {
            newAccessPasscode.setCreateBy(String.valueOf(accessPasscodeParam.getMemberId()));
            newAccessPasscode.setUpdateBy(String.valueOf(accessPasscodeParam.getMemberId()));
        } else {
            newAccessPasscode.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
            newAccessPasscode.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        }
        newAccessPasscode.setQrCodeUrl(qrCodeEncode);
        int result = accessPasscodeMapper.insertSelective(newAccessPasscode);
        if (result <= 0) {
            return AjaxResult.error("门岗通行证保存失败!");
        }

        return AjaxResult.success(newAccessPasscode);
    }

    /**
     * 根据ID查询门岗二维码信息
     *
     * @param accessPasscodeId
     * @return
     */
    @Override
    public AccessPasscode getByAccessPasscodeId(Long accessPasscodeId) {
        return accessPasscodeMapper.selectByPrimaryKey(accessPasscodeId);
    }

    /**
     * 根据订单ID和核销码查询门岗二维码信息
     *
     * @param queryParam
     * @return
     */
    @Override
    public AccessPasscode getByOrderIdAndOrderVerificationCode(AccessPasscodeQueryParam queryParam) {
        return accessPasscodeMapper.selectByOrderIdAndOrderVerificationCode(queryParam.getOrderId(), queryParam.getOrderVerificationCode());
    }
}
