package com.fastbee.waterele.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.waterele.domain.vo.UnchargeAmountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.fastbee.waterele.mapper.MaRechargerecordMapper;
import com.fastbee.waterele.domain.MaRechargerecord;
import com.fastbee.waterele.service.IMaRechargerecordService;

/**
 * 充值记录Service业务层处理
 *
 * @author kerwincui
 * @date 2024-08-12
 */
@Service
public class MaRechargerecordServiceImpl implements IMaRechargerecordService {
    @Autowired
    private MaRechargerecordMapper maRechargerecordMapper;

    /**
     * 查询充值记录
     *
     * @param id 充值记录主键
     * @return 充值记录
     */
    @Override
    public MaRechargerecord selectMaRechargerecordById(Long id) {
        return maRechargerecordMapper.selectMaRechargerecordById(id);
    }

    /**
     * 查询充值记录列表
     *
     * @param maRechargerecord 充值记录
     * @return 充值记录
     */
    @Override
    public List<MaRechargerecord> selectMaRechargerecordList(MaRechargerecord maRechargerecord) {
        return maRechargerecordMapper.selectMaRechargerecordList(maRechargerecord);
    }

    /**
     * 新增充值记录
     *
     * @param maRechargerecord 充值记录
     * @return 结果
     */
    @Override
    public int insertMaRechargerecord(MaRechargerecord maRechargerecord) {
        maRechargerecord.setCreateTime(DateUtils.getNowDate());
//        https://zhwly.sdhzwl.cn/api/java/xinjiangup/recharge?cardNum=1&areaCode=123&jine=20
        String url = "https://zhwly.sdhzwl.cn/api/java/xinjiangup/recharge?cardNum="
                + maRechargerecord.getCardNum()+"&areaCode="+maRechargerecord.getAreaCode()+"&jine="+maRechargerecord.getInvestval();
        String result = HttpUtil.get(url);
        JSONObject jsonObject = new JSONObject(result);
        if(jsonObject != null){
            if(jsonObject.containsKey("code") && Integer.parseInt(jsonObject.get("code").toString()) == 200){
                return maRechargerecordMapper.insertMaRechargerecord(maRechargerecord);
            }
        }
        return 0;

    }

    /**
     * 修改充值记录
     *
     * @param maRechargerecord 充值记录
     * @return 结果
     */
    @Override
    public int updateMaRechargerecord(MaRechargerecord maRechargerecord) {
        maRechargerecord.setUpdateTime(DateUtils.getNowDate());
        return maRechargerecordMapper.updateMaRechargerecord(maRechargerecord);
    }

    /**
     * 批量删除充值记录
     *
     * @param ids 需要删除的充值记录主键
     * @return 结果
     */
    @Override
    public int deleteMaRechargerecordByIds(Long[] ids) {
        return maRechargerecordMapper.deleteMaRechargerecordByIds(ids);
    }

    /**
     * 删除充值记录信息
     *
     * @param id 充值记录主键
     * @return 结果
     */
    @Override
    public int deleteMaRechargerecordById(Long id) {
        return maRechargerecordMapper.deleteMaRechargerecordById(id);
    }

    @Override
    public UnchargeAmountVo queryUnchargeAmount(MaRechargerecord maRechargerecord) {
        maRechargerecord.setIsRecharge(2);
        List<MaRechargerecord> maRechargerecords = maRechargerecordMapper.selectMaRechargerecordList(maRechargerecord);
        float total = 0;
        for (MaRechargerecord record : maRechargerecords) {
            total += Float.parseFloat(record.getInvestval());
        }
        UnchargeAmountVo vo = new UnchargeAmountVo();
        vo.setInvestVal(total);
        vo.setCardNum(maRechargerecord.getCardNum());
        vo.setAreaCode(maRechargerecord.getAreaCode());
        return vo;
    }

    @Override
    public String rechargeReply(MaRechargerecord maRechargerecord) {
        if (StringUtils.isNotEmpty(maRechargerecord.getCardNum()) && StringUtils.isNotEmpty(maRechargerecord.getAreaCode())) {
            if (maRechargerecord.getIsRecharge() == 1) {
                maRechargerecord.setUpdateTime(new Date());
                int count = maRechargerecordMapper.updateByCardNumAndAreaCode(maRechargerecord);
            }
        }
        return "更新成功";
    }
}
