package cn.pluss.platform.jft.impl;

import cn.pluss.platform.converter.Converter;
import cn.pluss.platform.entity.*;
import cn.pluss.platform.jft.JftMercBaseInfoService;
import cn.pluss.platform.jft.JftMerchantRateRecordService;
import cn.pluss.platform.jft.JftMerchantRateService;
import cn.pluss.platform.mapper.JftMerchantRateRecordMapper;
import cn.pluss.platform.merchant.JftMercPaymentChannelService;
import cn.pluss.platform.notice.NoticeService;
import cn.pluss.platform.notice.template.NoticeTemplateService;
import cn.pluss.platform.user.impl.GeneralPushUtil;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.userInfo.UserInfoService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.web.client.RestTemplate;

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

/**
 * <p>
 * 缴费通商户费率表(新) 服务实现类
 * </p>
 *
 * @author Djh
 * @since 2022-06-21
 */
@Slf4j
@Service
public class JftMerchantRateRecordServiceImpl extends ServiceImpl<JftMerchantRateRecordMapper, JftMerchantRateRecord> implements JftMerchantRateRecordService {

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private JftMerchantRateService jmrService;

    @Autowired
    private UserAppService userAppService;

    @Autowired
    private UserInfoService uiService;

    @Autowired
    private JftMercPaymentChannelService jpcService;

    @Autowired
    private JftMercBaseInfoService jbiService;

    @Autowired
    private NoticeTemplateService nTempService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private GeneralPushUtil generalPushUtil;

    @Autowired
    private RestTemplate restTemplate;

    @Override
    public void onSuccess(JftMerchantRateRecord entity) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                LambdaQueryWrapper<JftMerchantRate> qWrapper = Wrappers.lambdaQuery();
                qWrapper.eq(JftMerchantRate::getMerchantId, entity.getMerchantId());

                JftMerchantRate rateNewExist = jmrService.getOne(qWrapper);

                final JftMercPaymentChannel jpc = jpcService.getByMerchantId(entity.getMerchantId());

                Converter.INSTANCE.cp(entity, rateNewExist);

                entity.setStatus("1");
                entity.setUpdateTime(new Date());
                jmrService.saveOrUpdate(rateNewExist);
                saveOrUpdate(entity);

                UserApp ua = userAppService.getUserAppByUserId(jpc.getUserId());

                UserInfo ui = uiService.getById(ua.getUserId());
                // 获取用户id
                List<String> aliasList = new ArrayList<>();
                aliasList.add(ua.getUserId() + "");

                // 获取上级用户id
//                UserApp uaParent = null;
//                if (ua.getParentId() != null) {
//                    LambdaQueryWrapper<UserApp> qWrapper4 = Wrappers.lambdaQuery();
//                    qWrapper4.eq(UserApp::getUserId, ua.getParentId());
//                    uaParent = userAppService.getOne(qWrapper4);
//                    aliasList.add(uaParent.getUserId() + "");
//                }

                // 获取通知模板
                NoticeTemplate template = nTempService.getNoticeTemplateByType("14");
                String templateContent;
                if (template == null) {
                    templateContent = "尊敬的商户“$1”，您的缴费通交易费率调整审核已通过，如有疑问请咨询您的业务员。";
                } else {
                    templateContent = template.getContent();
                }
                templateContent = templateContent.replace("$1", ua.getUserName());

                List<Notice> noticeList = new ArrayList<>();

                Notice notice = new Notice(1, -1, ua.getUserId() + "", ua.getUserName());
                notice.setConrtent(templateContent);
                notice.setDesc(templateContent);
                notice.setTitle("缴费通费率修改通知");
                noticeList.add(notice);

//                if (uaParent != null) {
//                    Notice notice2 = new Notice(1, -1, uaParent.getUserId() + "", uaParent.getUserName());
//                    notice2.setConrtent(templateContent);
//                    notice2.setDesc(templateContent);
//                    notice2.setTitle("费率修改通知");
//                    noticeList.add(notice2);
//                }

                noticeService.saveBatch(noticeList);
                generalPushUtil.sendAllPlatByAlias(aliasList, "费率修改通知", templateContent, "1");

            }
        });
    }

    @Override
    public void onFail(JftMerchantRateRecord entity) {
        transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus status) {
                JftMercPaymentChannel jpc = jpcService.getByMerchantId(entity.getMerchantId());
                UserApp ua = userAppService.getUserAppByUserId(jpc.getUserId() + "");

                List<String> aliasList = new ArrayList<>();
                aliasList.add(ua.getUserId() + "");

                // 获取上级用户id
//                UserApp uaParent = null;
//                if (ua.getParentId() != null) {
//                    LambdaQueryWrapper<UserApp> qWrapper4 = Wrappers.lambdaQuery();
//                    qWrapper4.eq(UserApp::getUserId, ua.getParentId());
//                    uaParent = userAppService.getOne(qWrapper4);
//                    aliasList.add(uaParent.getUserId() + "");
//                }

                // 失败
                entity.setStatus("2");
                saveOrUpdate(entity);

                List<Notice> noticeList = new ArrayList<>();

                Notice notice = new Notice(1, -1, ua.getUserId() + "", ua.getUserName());
                notice.setConrtent("缴费通费率修改失败");
                notice.setDesc("缴费通费率修改失败");
                notice.setTitle("缴费通费率修改通知");
                noticeList.add(notice);

//                if (uaParent != null) {
//                    Notice notice2 = new Notice(1, -1, uaParent.getUserId() + "", uaParent.getUserName());
//                    notice.setConrtent("费率修改失败");
//                    notice.setDesc("费率修改失败");
//                    notice.setTitle("费率修改通知");
//                    noticeList.add(notice2);
//                }

                noticeService.saveBatch(noticeList);
                generalPushUtil.sendAllPlatByAlias(aliasList, "费率修改通知", "缴费通费率修改失败", "1");

            }
        });
    }
}
