package com.supplier.service.impl;

import com.supplier.controller.WxRefundContoller;
import com.supplier.mapper.PersonalMapper;
import com.supplier.mapper.WxPayMapper;
import com.supplier.model.BuyOfferNum;
import com.supplier.model.WxPay;
import com.supplier.model.WxRefund;
import com.supplier.model.WxWithdrawal;
import com.supplier.service.WxPayService;
import com.supplier.util.AESCrypt;
import com.supplier.util.DesDecry;
import com.supplier.util.DesEncry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WxPayServiceImpl implements WxPayService {
    @Autowired
    private WxPayMapper wxPayMapper;
    @Autowired
    private PersonalMapper personalMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pay(WxPay wxPay) {
        wxPayMapper.deletePay(wxPay);
        wxPayMapper.pay(wxPay);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void payUpdate(WxPay wxPay) {
        wxPayMapper.payUpdate(wxPay);
    }

    @Override
    public int getPolling(String releUuid) {
        return wxPayMapper.getPolling(releUuid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void wxCash(WxWithdrawal wxWithdrawal) {
        wxPayMapper.wxCash(wxWithdrawal);
    }

    @Override
    public WxPay getRefundVal(String releUuid) {
        return wxPayMapper.getRefundVal(releUuid);
    }

    @Override
    public Map<String, Object> getUserRefund(WxRefund wxRefund) {
        Map<String, Object> map = new HashMap<>();
        try{
            wxRefund.setPage((wxRefund.getPage() - 1) * wxRefund.getLimit());
            List<WxRefund> list = wxPayMapper.getUserRefund(wxRefund);
            List<WxRefund> list1 = new ArrayList<>();
            for(WxRefund wxRefund1 : list){
                wxRefund1.setReleaseUuid(DesEncry.encrypt(DesEncry.encrypt(wxRefund1.getReleaseUuid())));
                list1.add(wxRefund1);
            }
            map.put("data", list1);
            map.put("count", wxPayMapper.getUserRefundCount(wxRefund));
            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 updateRefundType(String releUuid, String refundState) {
        wxPayMapper.updateRefundType(releUuid, refundState);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> refusedRefund(WxRefund wxRefund) {
        Map<String, Object> map = new HashMap<>();
        try{
            wxRefund.setReleaseUuid(DesDecry.decrypt(DesDecry.decrypt(wxRefund.getReleaseUuid())));
            WxRefund refund = wxPayMapper.getRefund(wxRefund);
            if(refund.getRefundState().equals("1")){
                map.put("msg", "500");
                map.put("errMsg", "该退款申请已同意，操作无效");
                return map;
            }
            if(wxRefund.getRefundState().equals("1")){
                WxPay wxPay = wxPayMapper.getRefundVal(wxRefund.getReleaseUuid());//获取付该报价单付款的信息
                if(null == wxPay){
                    map.put("msg", "500");
                    map.put("errMsg", "未查询到该报价单的支付信息");
                    return map;
                }
                if(Double.parseDouble(refund.getRefundFee()) > Double.parseDouble(wxPay.getTotalFee())){
                    map.put("msg", "500");
                    map.put("errMsg", "退款金额大于支付金额");
                    return map;
                }
                Map<String, Object> map1 = WxRefundContoller.refund(refund.getRefundFee(), wxPay);
                if(map1.get("msg").equals("200")){
                    wxPayMapper.updateRefundType(wxRefund.getReleaseUuid(), wxRefund.getRefundState());
                    return map1;
                } else {
                    return map1;
                }
            } else {
                wxPayMapper.updateRefundType(wxRefund.getReleaseUuid(), wxRefund.getRefundState());
            }
            map.put("msg", "200");
            map.put("data", "操作成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void buySupplierBalance(WxPay wxPay) {
        try{
            wxPayMapper.buySupplierBalance(wxPay);//充值记录
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBuySupplier(WxPay buySupplier) {
        try{
            wxPayMapper.updateBuySupplier(buySupplier); //完善充值记录
            //供应商余额增加
            wxPayMapper.updateBalance(Double.parseDouble(buySupplier.getTotalFee()), buySupplier.getCode());//金额充值
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public int getTopUp(WxPay wxPay) {
        return wxPayMapper.getTopUp(wxPay);
    }

    @Override
    public void getTopUpTimeOut() {
        try{
            //充值记录
            List<WxPay> list = wxPayMapper.getTopUpTimeOut();
            for(WxPay wxPay : list){
                wxPayMapper.delTopOut(wxPay);
            }
            //购买次数记录
            List<BuyOfferNum> list1 = wxPayMapper.getOfferNumTimeOut();
            for(BuyOfferNum buyOfferNum : list1){
                wxPayMapper.delOfferNum(buyOfferNum);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBuyOfferNum(BuyOfferNum buyOfferNum) {
        try{
            wxPayMapper.updateBuyOfferNum(buyOfferNum);//完善购买记录
            //供应商报价单次数增加
            personalMapper.addOfferNum(buyOfferNum.getOfferNum(), buyOfferNum.getCode());//增加次数
        }catch (Exception e){
            e.printStackTrace();
        }
    }

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

    @Override
    public Map<String, Object> getAuditRefund(WxRefund wxRefund) {
        Map<String, Object> map = new HashMap<>();
        try{
            wxRefund.setPage((wxRefund.getPage() - 1) * wxRefund.getLimit());
            List<WxRefund> list = wxPayMapper.getAuditRefund(wxRefund);
            List<WxRefund> list1 = new ArrayList<>();
            for(WxRefund wxRefund1 : list){
                wxRefund1.setReleaseUuid(DesEncry.encrypt(DesEncry.encrypt(wxRefund1.getReleaseUuid())));
                list1.add(wxRefund1);
            }
            map.put("data", list1);
            map.put("count", wxPayMapper.getAuditRefundCount(wxRefund));
            map.put("msg", "200");
        }catch (Exception e){
            e.printStackTrace();
            map.put("msg", "500");
            map.put("errMsg", "服务器请求异常，请稍后再试");
        }
        return map;
    }
}
