package com.slodon.b2b2c.model.member.advich;

import cn.hutool.core.collection.CollectionUtil;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.dao.read.seller.EnquiryReceiveMailSettingReadMapper;
import com.slodon.b2b2c.dao.write.seller.EnquiryReceiveMailSettingWriteMapper;
import com.slodon.b2b2c.seller.example.EnquiryReceiveMailSettingExample;
import com.slodon.b2b2c.seller.pojo.EnquiryReceiveMailSetting;
import com.slodon.b2b2c.seller.pojo.Vendor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author cyan
 */
@Component
@Slf4j
public class EnquiryReceiverMailSettingModel {

    @Resource
    private EnquiryReceiveMailSettingWriteMapper enquiryReceiveMailSettingWriteMapper;

    @Resource
    private EnquiryReceiveMailSettingReadMapper enquiryReceiveMailSettingReadMapper;

    /**
     * 添加接收询盘邮箱
     *
     * @param enquiryReceiveMailSetting
     * @return
     */
    public Integer saveEnquiryReceiverMail(HttpServletRequest request, EnquiryReceiveMailSetting enquiryReceiveMailSetting) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        EnquiryReceiveMailSettingExample receiveMailSettingExample = new EnquiryReceiveMailSettingExample();
        receiveMailSettingExample.setWebSite(enquiryReceiveMailSetting.getWebSite());
        receiveMailSettingExample.setEmail(enquiryReceiveMailSetting.getEmail());
        receiveMailSettingExample.setStoreId(enquiryReceiveMailSetting.getStoreId());
        List<EnquiryReceiveMailSetting> enquiryReceiveMailSettings = enquiryReceiveMailSettingReadMapper.listByExample(receiveMailSettingExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(enquiryReceiveMailSettings), "该邮箱已添加，无需重复添加");

        EnquiryReceiveMailSettingExample receiveMailSettingPhoneExample = new EnquiryReceiveMailSettingExample();
        receiveMailSettingPhoneExample.setWebSite(enquiryReceiveMailSetting.getWebSite());
        receiveMailSettingPhoneExample.setPhone(enquiryReceiveMailSetting.getPhone());
        receiveMailSettingPhoneExample.setStoreId(enquiryReceiveMailSetting.getStoreId());
        List<EnquiryReceiveMailSetting> enquiryReceivePhoneSettings = enquiryReceiveMailSettingReadMapper.listByExample(receiveMailSettingPhoneExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(enquiryReceivePhoneSettings), "该手机号已添加，无需重复添加");

        enquiryReceiveMailSetting.setStoreId(vendor.getStoreId());
        enquiryReceiveMailSetting.setOperateUid(vendor.getVendorId());
        enquiryReceiveMailSetting.setOperateName(vendor.getVendorName());
        enquiryReceiveMailSetting.setCreateTime(new Date());
        int count = enquiryReceiveMailSettingWriteMapper.insert(enquiryReceiveMailSetting);
        AssertUtil.notNullOrZero(count, "询盘邮箱接收添加失败，请重试");

        return count;
    }

    /**
     * 编辑接收询盘邮箱
     * @param request
     * @param enquiryReceiveMailSetting
     * @return
     */
    public Integer editEnquiryAdminReceiverMail(HttpServletRequest request, EnquiryReceiveMailSetting enquiryReceiveMailSetting) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        EnquiryReceiveMailSettingExample receiveMailSettingMailExample = new EnquiryReceiveMailSettingExample();
        receiveMailSettingMailExample.setWebSite(enquiryReceiveMailSetting.getWebSite());
        receiveMailSettingMailExample.setEmail(enquiryReceiveMailSetting.getEmail());
        receiveMailSettingMailExample.setStoreId(vendor.getStoreId());
        receiveMailSettingMailExample.setIdNotEqualTo(enquiryReceiveMailSetting.getId());
        List<EnquiryReceiveMailSetting> enquiryReceiveMailSettings = enquiryReceiveMailSettingReadMapper.listByExample(receiveMailSettingMailExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(enquiryReceiveMailSettings), "该邮箱已添加，无需重复添加");

        EnquiryReceiveMailSettingExample receiveMailSettingPhoneExample = new EnquiryReceiveMailSettingExample();
        receiveMailSettingPhoneExample.setWebSite(enquiryReceiveMailSetting.getWebSite());
        receiveMailSettingPhoneExample.setPhone(enquiryReceiveMailSetting.getPhone());
        receiveMailSettingPhoneExample.setStoreId(vendor.getStoreId());
        receiveMailSettingPhoneExample.setIdNotEqualTo(enquiryReceiveMailSetting.getId());
        List<EnquiryReceiveMailSetting> enquiryReceivePhoneSettings = enquiryReceiveMailSettingReadMapper.listByExample(receiveMailSettingPhoneExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(enquiryReceivePhoneSettings), "该手机号已添加，无需重复添加");

        enquiryReceiveMailSetting.setOperateUid(vendor.getVendorId());
        enquiryReceiveMailSetting.setCreateTime(new Date());
        int count = enquiryReceiveMailSettingWriteMapper.updateByPrimaryKeySelective(enquiryReceiveMailSetting);
        AssertUtil.notNullOrZero(count, "询盘邮箱接收编辑失败，请重试");

        return count;
    }

    /**
     * 删除询盘接收邮箱
     *
     * @param id
     * @return
     */
    public Integer delEnquiryReceiverMail(HttpServletRequest request, Long id) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        int count = enquiryReceiveMailSettingWriteMapper.deleteByPrimaryKey(id);
        AssertUtil.notNullOrZero(count, "删除询盘接收邮箱失败，请重试");
        return count;
    }

    /**
     * @param example 查询条件信息
     * @param pager   分页信息
     */
    public List<EnquiryReceiveMailSetting> getEnquiryReceiveMailSettings(EnquiryReceiveMailSettingExample example, PagerInfo pager) {
        List<EnquiryReceiveMailSetting> enquiryReceiveMailSettings;
        if (pager != null) {
            pager.setRowsCount(enquiryReceiveMailSettingReadMapper.countByExample(example));
            enquiryReceiveMailSettings = enquiryReceiveMailSettingReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            enquiryReceiveMailSettings = enquiryReceiveMailSettingReadMapper.listByExample(example);
        }
        return enquiryReceiveMailSettings;
    }

}
