package com.liquidnet.service.goblin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.service.goblin.dto.vo.GoblinRechargeAmountVo;
import com.liquidnet.service.goblin.dto.vo.GoblinRechargeWristbandVo;
import com.liquidnet.service.goblin.dto.vo.MaiZhiAllVo.*;
import com.liquidnet.service.goblin.entity.GoblinRechargeAmount;
import com.liquidnet.service.goblin.entity.GoblinRechargeWristband;
import com.liquidnet.service.goblin.mapper.GoblinRechargeAmountMapper;
import com.liquidnet.service.goblin.mapper.GoblinRechargeWristbandMapper;
import com.liquidnet.service.goblin.service.IGoblinRechargeWristbandService;
import com.liquidnet.service.goblin.util.GoblinRedisUtils;
import com.liquidnet.service.goblin.util.ThirdMaiZhiUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.time.Instant;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 手环信息表 服务实现类
 * </p>
 *
 * @author jiangxiulong
 * @since 2025-07-03
 */
@Service
public class GoblinRechargeWristbandServiceImpl extends ServiceImpl<GoblinRechargeWristbandMapper, GoblinRechargeWristband> implements IGoblinRechargeWristbandService {

    @Autowired
    GoblinRedisUtils goblinRedisUtils;

    @Autowired
    ThirdMaiZhiUtils thirdMaiZhiUtils;

    @Autowired
    private GoblinRechargeAmountMapper rechargeAmountMapper;

    @Autowired
    private GoblinRechargeWristbandMapper rechargeWristbandMapper;

    @Override
    public GoblinRechargeWristbandVo getList() {
        GoblinRechargeWristbandVo result = goblinRedisUtils.getRechargeWristbandVo();

        if (result == null) {
            // 从数据库中查询数据
            List<GoblinRechargeWristband> rechargeWristbands = rechargeWristbandMapper.selectList(null);
            List<GoblinRechargeAmount> rechargeAmounts = rechargeAmountMapper.selectList(null);

            // 组合数据
            if (!CollectionUtils.isEmpty(rechargeAmounts) && !CollectionUtils.isEmpty(rechargeWristbands)) {
                GoblinRechargeWristbandVo wristbandVo = GoblinRechargeWristbandVo.getNew();
                wristbandVo.setWristbandId(rechargeWristbands.get(0).getWristbandId());
                wristbandVo.setName(rechargeWristbands.get(0).getName());
                wristbandVo.setPrice(rechargeWristbands.get(0).getPrice());
                List<GoblinRechargeAmountVo> list = new ArrayList<>();
                for (GoblinRechargeAmount amount : rechargeAmounts) {
                    GoblinRechargeAmountVo amountVo = GoblinRechargeAmountVo.getNew();
                    amountVo.setAmountId(amount.getAmountId());
                    amountVo.setName(amount.getName());
                    amountVo.setPrice(amount.getPrice());
                    list.add(amountVo);
                }
                wristbandVo.setAmonutList(list);

                goblinRedisUtils.setRechargeWristbandVo(wristbandVo);
                return wristbandVo;
            }
        }

        return result;
    }

    @Override
    public CheckIdcardResponse auth(IdCardParam param) {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            return thirdMaiZhiUtils.checkIdcard(accessToken, param.getIdcard(), param.getName());
        }
        return null;
    }

    @Override
    public OrderResponse createOrder(OrderParam param) {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            return thirdMaiZhiUtils.addOrder(accessToken, param);
        }
        return null;
    }

    @Override
    public OrderInfoResponse getOrder(OrderInfoParam param) {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            return thirdMaiZhiUtils.getOrder(accessToken, param);
        }
        return null;
    }

    @Override
    public GenerateDeviceCodeResponse getQrcode(String outno) {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            return thirdMaiZhiUtils.generateDeviceCode(accessToken, outno);
        }
        return null;
    }

    @Override
    public Boolean getDeviceNumber(Integer operationNo) {
        String accessToken = this.getAccessToken();
        String endtime = goblinRedisUtils.getDeviceNumberEndTime(operationNo);
        if (null != endtime) {
            Instant instant = Instant.EPOCH;
            long timestamp = instant.toEpochMilli();
            endtime = String.valueOf(timestamp);
        }
        long timestamp = Instant.now().toEpochMilli();
        if (null != accessToken) {
            DeviceNumberResponse res = thirdMaiZhiUtils.getDeviceNumber(accessToken, operationNo, endtime, String.valueOf(timestamp));
            if (null != res && res.getErrcode() == 200) {
                for (DeviceNumberResponse.NumberList info : res.getList()) {
                    // jxl todo 把分订单号对应的设备编号写入 redis and 数据库
                }
                goblinRedisUtils.setDeviceNumberEndTime(operationNo, String.valueOf(timestamp));
                return true;
            }
        }
        return false;
    }

    @Override
    public DeviceBalanceResponse getDeviceBalance(String cardno) {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            return thirdMaiZhiUtils.getDeviceBalance(accessToken, cardno);
        }
        return null;
    }

    @Override
    public DeviceRecordResponse getDeviceRecord(String cardno, Integer page) {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            return thirdMaiZhiUtils.getDeviceRecord(accessToken, cardno, page);
        }
        return null;
    }

    @Override
    public ChangeDeviceStatusResponse changeDeviceStatus(String cardno, int type) {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            return thirdMaiZhiUtils.changeDeviceStatus(accessToken, cardno, type);
        }
        return null;
    }

    @Override
    public Boolean refundRes() {
        String accessToken = this.getAccessToken();
        if (null != accessToken) {
            // jxl todo 查询所有退款中状态的设备编号 然后循环查询结果
            String cardno = "sss";
            ChangeDeviceStatusResponse res = thirdMaiZhiUtils.refundRes(accessToken, cardno);
            // jxl todo 把结果写入 redis and mysql
            return true;
        } else {
            return false;
        }
    }

    private String getAccessToken() {
        String token = goblinRedisUtils.getMaiZhiAccessToken();
        if (null == token) {
            AuthResponse response = thirdMaiZhiUtils.login("CM250001", "95b42275318c6df2d78c43525c2126e7", "380e8acd181abef8c6253721b05a54a1");
            if (response.getErrcode() == 200) {
                token = response.getData().getAccessToken();
                goblinRedisUtils.setMaiZhiAccessToken(token, response.getData().getExpire() - 200);
            }
        }
        return token;
    }

}