package com.yunzhi.marketing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.common.SMSUtils;
import com.yunzhi.marketing.common.StringUtils;
import com.yunzhi.marketing.entity.TaOfficeContact;
import com.yunzhi.marketing.mapper.TaOfficeContactMapper;
import com.yunzhi.marketing.sample.entity.TaContact;
import com.yunzhi.marketing.sample.mapper.TaContactMapper;
import com.yunzhi.marketing.service.ITaOfficeContactService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 联系咨询  服务实现类
 * </p>
 *
 * @author yansen
 * @since 2020-06-11
 */
@Service
public class TaOfficeContactServiceImpl extends ServiceImpl<TaOfficeContactMapper, TaOfficeContact> implements ITaOfficeContactService {

    @Autowired
    public SMSUtils smsUtils;

    @Autowired
    TaContactMapper taContactMapper;

    @Override
    public void sendNotice(TaOfficeContact taOfficeContact) {
        List<String> params = new ArrayList<String>(){{
            add(taOfficeContact.getName());
            add(taOfficeContact.getPhone());
            add(taOfficeContact.getCompany());
        }};

        QueryWrapper<TaContact> query = new QueryWrapper<>();
        query.eq("contact_type", CommConstant.CONTACT_PRESALE);
        query.eq("status", CommConstant.STATUS_NORMAL);

        List<TaContact> taContacts = taContactMapper.selectList(query);
        for (TaContact taContact : taContacts) {
            String phone = taContact.getPhone();
            if (!StringUtils.isEmpty(phone)) {
                try {
                    smsUtils.sendMessage(phone, params, CommConstant.MESSAGE_CONTENT_OF_OFFICE_PROMOTION);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
