package com.ym.mec.biz.service.impl;

import com.ym.mec.biz.dal.dao.ReplacementInstrumentActivityDao;
import com.ym.mec.biz.dal.dao.ReplacementInstrumentCooperationDao;
import com.ym.mec.biz.dal.dao.SysConfigDao;
import com.ym.mec.biz.dal.dto.ReplacementInstrumentActivityStatDto;
import com.ym.mec.biz.dal.entity.ReplacementInstrumentActivity;
import com.ym.mec.biz.dal.entity.ReplacementInstrumentCooperation;
import com.ym.mec.biz.dal.enums.MessageTypeEnum;
import com.ym.mec.biz.dal.enums.YesOrNoEnum;
import com.ym.mec.biz.dal.page.ReplacementInstrumentCooperationQueryInfo;
import com.ym.mec.biz.service.ReplacementInstrumentCooperationService;
import com.ym.mec.biz.service.SysMessageService;
import com.ym.mec.common.dal.BaseDAO;
import com.ym.mec.common.exception.BizException;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.service.impl.BaseServiceImpl;
import com.ym.mec.thirdparty.message.MessageSenderPluginContext;
import com.ym.mec.util.date.DateUtil;
import com.ym.mec.util.http.HttpUtil;
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 ReplacementInstrumentCooperationServiceImpl extends BaseServiceImpl<Integer, ReplacementInstrumentCooperation> implements ReplacementInstrumentCooperationService {

    @Autowired
    private ReplacementInstrumentCooperationDao replacementInstrumentCooperationDao;
    @Autowired
    private ReplacementInstrumentActivityDao replacementInstrumentActivityDao;
    @Autowired
    private SysMessageService sysMessageService;
    @Autowired
    private SysConfigDao sysConfigDao;

    @Override
    public BaseDAO<Integer, ReplacementInstrumentCooperation> getDAO() {
        return replacementInstrumentCooperationDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReplacementInstrumentCooperation openPay(Integer id) {
        ReplacementInstrumentCooperation replacementInstrumentCooperation = replacementInstrumentCooperationDao.get(id);
        if (replacementInstrumentCooperation == null) {
            throw new BizException("合作单位乐器置换活动不存在");
        }
        if (YesOrNoEnum.YES.equals(replacementInstrumentCooperation.getOpenPay())) {
            throw new BizException("已开启缴费，不能重复开启");
        }
        replacementInstrumentCooperation.setOpenPay(YesOrNoEnum.YES);
        replacementInstrumentCooperationDao.update(replacementInstrumentCooperation);

        //发送推送链接
        //家长您好！您订购的 {乐器品牌}品牌乐器已经开始接受预定，请点击{链接地址}完成付款，付款后15个工作日以内到货，如有疑问请咨询乐团管理老师。
        List<ReplacementInstrumentActivityStatDto> pushUsers = replacementInstrumentActivityDao.getPushUsers(id);

        replacementInstrumentActivityDao.openPay(id);


        String baseApiUrl = sysConfigDao.findConfigValue("base_api_url");
        String pushUrl = baseApiUrl + "/#/questionGoodsSale?id=";
        for (ReplacementInstrumentActivityStatDto pushUser : pushUsers) {
            Map<Integer, String> userMap = new HashMap<>();
            Map<Integer, String> userPhoneMap = new HashMap<>();
            userMap.put(pushUser.getUserId(), pushUser.getUserId().toString());
            sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.JIGUANG, MessageTypeEnum.STUDENT_SMS_PUSH_REPLACEMENT_PAY,
                    userMap, null, 0, "5?" + pushUrl + pushUser.getId(), "STUDENT", pushUser.getBrand(), "");

            userPhoneMap.put(pushUser.getUserId(), pushUser.getMobileNo());
            sysMessageService.batchSendMessage(MessageSenderPluginContext.MessageSender.AWSMS, MessageTypeEnum.STUDENT_SMS_PUSH_REPLACEMENT_PAY,
                    userPhoneMap, null, 0, null, null, pushUser.getBrand(), HttpUtil.getSortUrl(pushUrl + pushUser.getId()));
        }

        return replacementInstrumentCooperation;
    }

    @Override
    public PageInfo<ReplacementInstrumentCooperation> getPageList(ReplacementInstrumentCooperationQueryInfo queryInfo) {
        PageInfo<ReplacementInstrumentCooperation> pageInfo = queryPage(queryInfo);
        if (pageInfo.getRows().size() > 0) {
            List<ReplacementInstrumentCooperation> replacementInstruments = replacementInstrumentCooperationDao.countAllReplacementsInfo();
            for (ReplacementInstrumentCooperation row : pageInfo.getRows()) {
                for (ReplacementInstrumentCooperation replacementInstrument : replacementInstruments) {
                    if (!row.getId().equals(replacementInstrument.getId())) continue;
                    row.setActiveNum(replacementInstrument.getActiveNum());
                    row.setReplaceNum(replacementInstrument.getReplaceNum());
                    row.setReplaceScale(new BigDecimal(replacementInstrument.getReplaceNum()).multiply(new BigDecimal(100)).
                            divide(new BigDecimal(replacementInstrument.getActiveNum()), 2, BigDecimal.ROUND_HALF_UP));
                    row.setPayScale(
                            replacementInstrument.getReplaceNum() <= 0 ? new BigDecimal("0.00") :
                                    new BigDecimal(replacementInstrument.getPayNum()).multiply(new BigDecimal(100)).divide(new BigDecimal(replacementInstrument.getReplaceNum()), 2, BigDecimal.ROUND_HALF_UP)
                    );
                }
            }

        }
        return pageInfo;
    }

    @Override
    public ReplacementInstrumentCooperation findByCooperationOrganIdAndTopicId(Integer cooperationOrganId, Integer topicId) {
        return replacementInstrumentCooperationDao.findByCooperationOrganIdAndTopicId(cooperationOrganId, topicId);
    }
}