package com.supplier.service.impl;

import com.supplier.mapper.PersonalMapper;
import com.supplier.model.*;
import com.supplier.service.PersonalServce;
import com.supplier.util.SnowflakeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PersonalServiceImpl implements PersonalServce {
    //雪花算法
    private SnowflakeUtil snowflakeUtil = new SnowflakeUtil(1, 1, 1);

    @Autowired
    private PersonalMapper personalMapper;

    @Override
    public Supplier getPresonalMessage(String code) {
        return personalMapper.getPresonalMessage(code);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePhone(Supplier supplier) {
        personalMapper.updatePhone(supplier);
    }

    @Override
    public int checkCaptcha(Supplier supplier) {
        return personalMapper.checkCaptcha(supplier);
    }

    @Override
    public List<User> seleceWxUserCount(User user) {
        return personalMapper.seleceWxUserCount(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addWXUser(User user) {
        personalMapper.addWXUser(user);
    }

    @Override
    public int geThisUserID(String wxid) {
        return personalMapper.geThisUserID(wxid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUserWx(User user) {
        personalMapper.updateUserWx(user);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOffNum(SupplierRelease supplierRelease) {
        personalMapper.updateOffNum(supplierRelease);
    }

    @Override
    public Map<String, Object> getByNumber() {
        Map<String, Object> map = new HashMap<>();
        try{
            map.put("data", personalMapper.getByNumber());
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> payBlance(String offerNum, String code) {
        Map<String, Object> map = new HashMap<>();
        try{
            Map<String, Object> map1 = personalMapper.getByNumber();
            Supplier supplier = personalMapper.getPresonalMessage(code);
            System.out.println(offerNum);
            Double offerVal = (Integer.parseInt(offerNum) * Double.parseDouble(String.valueOf(map1.get("power_unit"))));
            if(Double.parseDouble(supplier.getBalanceOf()) < offerVal){
                map.put("msg", "500");
                map.put("errMsg", "余额不足请充值");
                return map;
            }
            //购买信息
            BuyOfferNum buyOfferNum = new BuyOfferNum()
                    .setOfferNum(offerNum)
                    .setCode(code)
                    .setPayAmount(offerVal.toString())
                    .setUuid(snowflakeUtil.nextId() + "");
            personalMapper.addBuyOfferNum(buyOfferNum); //购买次数记录
            personalMapper.minusBalanceOf(offerVal.toString(), code);//修改金额
            personalMapper.addOfferNum(offerNum, code);//增加次数
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addBuyOfferNum(BuyOfferNum buyOfferNum) {
        personalMapper.addBuyOfferNum(buyOfferNum); //购买次数记录
    }

    @Override
    public Map<String, Object> balanceOfPayments(String code) {
        Map<String, Object> map = new HashMap<>();
        try{
            map.put("data", personalMapper.balanceOfPayments(code));
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

    @Override
    public Map<String, Object> topUpNum(String code) {
        Map<String, Object> map = new HashMap<>();
        try{
            map.put("data", personalMapper.topUpNum(code));
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }
}
