package com.izouma.awesomeadmin.service.impl;

import com.izouma.awesomeadmin.constant.AppConstant;
import com.izouma.awesomeadmin.dao.AlipayTempMapper;
import com.izouma.awesomeadmin.dao.MemberCoinMapper;
import com.izouma.awesomeadmin.dto.Page;
import com.izouma.awesomeadmin.model.AlipayTemp;
import com.izouma.awesomeadmin.model.MemberCoin;
import com.izouma.awesomeadmin.model.UserInfo;
import com.izouma.awesomeadmin.service.MemberCoinService;
import com.izouma.awesomeadmin.service.UserInfoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * service接口实现类
 */
@Service
public class MemberCoinServiceImpl implements MemberCoinService {

    private static Logger logger = Logger.getLogger(MemberCoinServiceImpl.class);

    @Autowired
    private MemberCoinMapper memberCoinMapper;

    @Autowired
    private AlipayTempMapper alipayTempMapper;


    @Autowired
    private UserInfoService userInfoService;

    @Override
    public List<MemberCoin> getMemberCoinList(MemberCoin record) {

        logger.info("getMemberCoinList");
        try {

            return memberCoinMapper.queryAllMemberCoin(record);
        } catch (Exception e) {
            logger.error("getMemberCoinList", e);
        }

        return null;
    }

    @Override
    public List<MemberCoin> getMemberCoinByPage(Page page, MemberCoin record) {

        logger.info("getMemberCoinByPage");
        try {

            Map<String, Object> parameter = new HashMap<String, Object>();
            parameter.put("record", record);
            parameter.put(AppConstant.PAGE, page);

            return memberCoinMapper.queryMemberCoinByPage(parameter);
        } catch (Exception e) {
            logger.error("getMemberCoinByPage", e);
        }

        return null;
    }

    @Override
    public MemberCoin getMemberCoinById(String id) {

        logger.info("getMemberCoinyId");
        try {

            return memberCoinMapper.selectByPrimaryKey(Integer.valueOf(id));
        } catch (Exception e) {
            logger.error("getMemberCoinById", e);
        }

        return null;
    }

    @Override
    public MemberCoin getMemberCoin(MemberCoin record) {

        logger.info("getMemberCoin");
        try {

            return memberCoinMapper.queryMemberCoin(record);
        } catch (Exception e) {
            logger.error("getMemberCoin", e);
        }

        return null;
    }

    @Override
    public boolean createMemberCoin(MemberCoin record) {

        logger.info("createMemberCoin");
        try {

            int updates = memberCoinMapper.insertSelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("createMemberCoin", e);
        }

        return false;
    }

    @Override
    public boolean deleteMemberCoin(String id) {

        logger.info("deleteMemberCoin");
        try {

            int updates = memberCoinMapper.delete(id);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("deleteMemberCoin", e);
        }

        return false;
    }

    @Override
    public boolean updateMemberCoin(MemberCoin record) {

        logger.info("updateMemberCoin");
        try {

            int updates = memberCoinMapper.updateByPrimaryKeySelective(record);

            if (updates > 0) {
                return true;
            }
        } catch (Exception e) {
            logger.error("updateMemberCoin", e);
        }

        return false;
    }

    /**
     * <p>支付宝支付成功回调接口。充值</p>
     *
     * @param tradeNo
     * @param tradeStatus
     */
    @Override
    public void alipaySuccess(String tradeNo, String tradeStatus) {

        logger.info("alipaySuccess：支付宝支付成功回调接口 tradeNo：" + tradeNo + ", tradeStatus:" + tradeStatus);
        try {
            AlipayTemp alipayTemp = new AlipayTemp();
            alipayTemp.setOutTradeNo(tradeNo);
            alipayTemp = alipayTempMapper.queryAlipayTemp(alipayTemp);

            if (alipayTemp != null) {

                if (!AppConstant.Aliapi.TRADE_SUCCESS.equals(alipayTemp.getTradeStatus())) {

                    UserInfo userInfo = userInfoService.getUserInfoById(alipayTemp.getOrderId());

                    Double moneyCoin = userInfo.getMoneyCoin();

                    MemberCoin memberCoin = new MemberCoin();

                    memberCoin.setCreateUser("支付宝充值");
                    memberCoin.setUserId(alipayTemp.getOrderId());
                    memberCoin.setMoney(String.valueOf(alipayTemp.getTotalAmount()));
                    memberCoin.setBalance(String.valueOf(moneyCoin + alipayTemp.getTotalAmount().doubleValue()));
                    memberCoin.setTypeFlag(AppConstant.CoinType.RECHARGE);
                    memberCoin.setRemark("线上充值：" + alipayTemp.getTotalAmount());
                    memberCoinMapper.insertSelective(memberCoin);


                    UserInfo updateUser = new UserInfo();
                    updateUser.setId(userInfo.getId());
                    updateUser.setMoneyCoin(moneyCoin + alipayTemp.getTotalAmount().doubleValue());
                    userInfoService.updateUserInfo(updateUser);


                    alipayTemp.setTradeStatus(tradeStatus);

                    alipayTempMapper.updateByPrimaryKeySelective(alipayTemp);
                }

            }

        } catch (Exception e) {
            logger.error("alipaySuccess：支付宝支付成功回调接口异常 tradeNo：" + tradeNo + ", tradeStatus:" + tradeStatus, e);
        }

    }


    @Override
    public int song(MemberCoin memberCoin) {

        logger.info("song：送电子币  ");
        try {

            int key = 0;

            double money = Double.valueOf(memberCoin.getMoney());


            int userId = Integer.valueOf(memberCoin.getUserId());
            UserInfo userInfo = userInfoService.getUserInfoById(userId + "");

            UserInfo updateUser = new UserInfo();
            updateUser.setId(userId);


            double balance = userInfo.getMoneyCoin();
            double moneyCoin = balance + money;

            memberCoin.setCreateUser("管理员");
            memberCoin.setBalance(String.valueOf(moneyCoin));
            memberCoin.setTypeFlag(AppConstant.CoinType.SONG);
            memberCoin.setRemark("后台赠送：" + money + "余额");
            key += memberCoinMapper.insertSelective(memberCoin);


            updateUser.setMoneyCoin(moneyCoin);
            userInfoService.updateUserInfo(updateUser);


            return key;
        } catch (Exception e) {
            logger.error("song：送电子币异常  ", e);
        }

        return 0;
    }

}

