package com.huiju.estateagents.redpack.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiju.estateagents.base.ResponseBean;
import com.huiju.estateagents.common.CommConstant;
import com.huiju.estateagents.common.StringUtils;
import com.huiju.estateagents.redpack.entity.TaOrgAccount;
import com.huiju.estateagents.redpack.entity.TaRedPacketClient;
import com.huiju.estateagents.redpack.entity.TaRedPacketMoney;
import com.huiju.estateagents.redpack.mapper.TaOrgAccountMapper;
import com.huiju.estateagents.redpack.mapper.TaOrgOrderMapper;
import com.huiju.estateagents.redpack.mapper.TaRedPacketMoneyMapper;
import com.huiju.estateagents.redpack.service.ITaRedPacketClientService;
import com.huiju.estateagents.redpack.service.ITaRedPacketMoneyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 红包金额设置  服务实现类
 * </p>
 *
 * @author fxf
 * @since 2020-06-03
 */
@Service
public class TaRedPacketMoneyServiceImpl extends ServiceImpl<TaRedPacketMoneyMapper, TaRedPacketMoney> implements ITaRedPacketMoneyService {

    @Autowired
    private ITaRedPacketClientService iTaRedPacketClientService;

    @Autowired
    private TaOrgOrderMapper taOrgOrderMapper;

    @Autowired
    private TaOrgAccountMapper taOrgAccountMapper;

    @Override
    public ResponseBean getRedPacketMoneyById(String activityId, Integer orgId) {
        ResponseBean responseBean = new ResponseBean();

        // 获取红包金额设置
        TaRedPacketMoney taRedPacketMoney = getById(activityId);
        if (taRedPacketMoney == null) {
            responseBean.addError("fail");
            return responseBean;
        }

        // 获取红包客户端消息配置
        TaRedPacketClient taRedPacketClient = iTaRedPacketClientService.getById(activityId);
        if (taRedPacketClient != null) {
            taRedPacketMoney.setMchName(taRedPacketClient.getMchName());
            taRedPacketMoney.setActivityName(taRedPacketClient.getActivityName());
            taRedPacketMoney.setBlessing(taRedPacketClient.getBlessing());
            taRedPacketMoney.setRemark(taRedPacketClient.getRemark());
        }
        Map<String, Object> result = taOrgOrderMapper.getRedPacketSendInfo(CommConstant.RED_PACKET, activityId, orgId);
        taRedPacketMoney.setSendNum((Long) result.get("sendNum"));
        taRedPacketMoney.setSendTotalAmount(((BigDecimal)result.get("sendTotalAmount")).longValue());

        QueryWrapper<TaOrgAccount> orgAccountQueryWrapper = new QueryWrapper<>();
        orgAccountQueryWrapper.eq("org_id", orgId);
        orgAccountQueryWrapper.eq("status", CommConstant.STATUS_NORMAL);
        TaOrgAccount taOrgAccount = taOrgAccountMapper.selectOne(orgAccountQueryWrapper);
        if (taOrgAccount != null) {
            taRedPacketMoney.setAvailableBalance(taOrgAccount.getAvailableBalance());
        }

        responseBean.addSuccess(taRedPacketMoney);
        return responseBean;
    }

    @Override
    public ResponseBean updateRedPacketMoney(TaRedPacketMoney taRedPacketMoney) {
        ResponseBean responseBean = new ResponseBean();

        // 更新红包金额设置
        TaRedPacketMoney money = getById(taRedPacketMoney.getActivityId());
        if (money.getBudget() != taRedPacketMoney.getBudget()) {
            taRedPacketMoney.setBalance(money.getBalance() + (taRedPacketMoney.getBudget() - money.getBudget()));
        }

        updateById(taRedPacketMoney);
        // 更新红包客户端消息配置
        if (!StringUtils.isEmpty(taRedPacketMoney.getActivityName()) || !StringUtils.isEmpty(taRedPacketMoney.getMchName())
                || !StringUtils.isEmpty(taRedPacketMoney.getBlessing()) || !StringUtils.isEmpty(taRedPacketMoney.getRemark())) {
            TaRedPacketClient taRedPacketClient = new TaRedPacketClient();
            taRedPacketClient.setActivityId(taRedPacketMoney.getActivityId());
            taRedPacketClient.setMchName(taRedPacketMoney.getMchName());
            taRedPacketClient.setActivityName(taRedPacketMoney.getActivityName());
            taRedPacketClient.setBlessing(taRedPacketMoney.getBlessing());
            taRedPacketClient.setRemark(taRedPacketMoney.getRemark());
            iTaRedPacketClientService.updateById(taRedPacketClient);
        }

        responseBean.addSuccess("success");
        return responseBean;
    }
}
