package com.yonge.cooleshow.biz.dal.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonge.cooleshow.biz.dal.entity.CooperateTenant;
import com.yonge.cooleshow.biz.dal.enums.ClientEnum;
import com.yonge.cooleshow.biz.dal.enums.MessageTypeEnum;
import com.yonge.cooleshow.biz.dal.mapper.CooperateTenantMapper;
import com.yonge.cooleshow.biz.dal.service.CooperateTenantService;
import com.yonge.cooleshow.biz.dal.service.SmsCodeService;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.service.SysMessageService;
import com.yonge.cooleshow.biz.dal.wrapper.CooperateTenantWrapper;
import com.yonge.cooleshow.common.constant.SysConfigConstant;
import com.yonge.toolset.base.exception.BizException;
import com.yonge.toolset.thirdparty.message.MessageSenderPluginContext;
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.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 合作机构
 * 2023-11-06 14:05:19
 */
@Slf4j
@Service
public class CooperateTenantServiceImpl extends ServiceImpl<CooperateTenantMapper, CooperateTenant> implements CooperateTenantService {

    @Autowired
    private SysMessageService sysMessageService;

    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 查询详情
     *
     * @param id 详情ID
     * @return CooperateTenant
     */
    @Override
    public CooperateTenant detail(Long id) {

        return baseMapper.selectById(id);
    }

    /**
     * 分页查询
     *
     * @param page  IPage<CooperateTenant>
     * @param query CooperateTenantWrapper.CooperateTenantQuery
     * @return IPage<CooperateTenant>
     */
    @Override
    public IPage<CooperateTenant> selectPage(IPage<CooperateTenant> page,
                                             CooperateTenantWrapper.CooperateTenantQuery query) {

        return page.setRecords(baseMapper.selectPage(page, query));
    }

    /**
     * 添加
     *
     * @param cooperateTenant CooperateTenantWrapper.CooperateTenant
     * @return Boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(CooperateTenantWrapper.CooperateTenant cooperateTenant) {
        String phone = cooperateTenant.getPhone();
        CooperateTenant one = this.lambdaQuery().eq(CooperateTenant::getPhone, phone).last("limit 1").one();
        if (one != null) {
            one.setCooperateTenantName(cooperateTenant.getCooperateTenantName());
            one.setCooperateTenantNature(cooperateTenant.getCooperateTenantNature());
            one.setContacts(cooperateTenant.getContacts());
            one.setUpdateTime(new Date());
            this.updateById(one);
        } else {
            this.save(JSON.parseObject(cooperateTenant.jsonString(), CooperateTenant.class));
        }
        // 发送短信
        String configValue = sysConfigService.findConfigValue(SysConfigConstant.ADD_COOPERATE_TENANT_SEND_MSG_PHONE);
        if (StringUtils.isNotEmpty(configValue)) {
            try {
                Map<Long, String> receivers = new HashMap<>();
                receivers.put(0L, configValue);
                sysMessageService.batchSendMessage(
                        MessageSenderPluginContext.MessageSender.AWSMS,
                        MessageTypeEnum.ADD_COOPERATE_TENANT,
                        receivers, null, 0, null, null,
                        cooperateTenant.getCooperateTenantName(),
                        cooperateTenant.getContacts(),
                        cooperateTenant.getCooperateTenantNature().getDesc(),
                        cooperateTenant.getPhone()
                );
            } catch (Exception e) {
                log.warn("添加合作机构失败，发送短信失败，{}", e.getMessage());
            }
        }
        return true;
    }

    /**
     * 更新
     *
     * @param cooperateTenant CooperateTenantWrapper.CooperateTenant
     * @return Boolean
     */
    @Override
    public Boolean update(CooperateTenantWrapper.CooperateTenant cooperateTenant) {

        return this.updateById(JSON.parseObject(cooperateTenant.jsonString(), CooperateTenant.class));
    }
}