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.system.AdminEnquiryReceiveMailSettingReadMapper;
import com.slodon.b2b2c.dao.write.system.AdminEnquiryReceiveMailSettingWriteMapper;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.system.example.AdminEnquiryReceiveMailSettingExample;
import com.slodon.b2b2c.system.pojo.Admin;
import com.slodon.b2b2c.system.pojo.AdminEnquiryReceiveMailSetting;
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 AdminEnquiryReceiverMailSettingModel {

    @Resource
    private AdminEnquiryReceiveMailSettingWriteMapper adminEnquiryReceiveMailSettingWriteMapper;

    @Resource
    private AdminEnquiryReceiveMailSettingReadMapper adminEnquiryReceiveMailSettingReadMapper;

    /**
     * 添加接收询盘邮箱
     *
     * @param adminEnquiryReceiveMailSetting
     * @return
     */
    public Integer saveEnquiryAdminReceiverMail(HttpServletRequest request, AdminEnquiryReceiveMailSetting adminEnquiryReceiveMailSetting) {
        Admin admin = UserUtil.getUser(request, Admin.class);

        AdminEnquiryReceiveMailSettingExample adminEnquiryReceiveMailSettingExample = new AdminEnquiryReceiveMailSettingExample();
        adminEnquiryReceiveMailSettingExample.setWebSite(adminEnquiryReceiveMailSetting.getWebSite());
        adminEnquiryReceiveMailSettingExample.setEmail(adminEnquiryReceiveMailSetting.getEmail());
        List<AdminEnquiryReceiveMailSetting> adminEnquiryReceiveMailSettings = adminEnquiryReceiveMailSettingReadMapper.listByExample(adminEnquiryReceiveMailSettingExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(adminEnquiryReceiveMailSettings), "该邮箱已添加，无需重复添加");

        AdminEnquiryReceiveMailSettingExample adminEnquiryPhoneMailSettingExample = new AdminEnquiryReceiveMailSettingExample();
        adminEnquiryPhoneMailSettingExample.setWebSite(adminEnquiryReceiveMailSetting.getWebSite());
        adminEnquiryPhoneMailSettingExample.setPhone(adminEnquiryReceiveMailSetting.getPhone());
        List<AdminEnquiryReceiveMailSetting> adminEnquiryPhoneMailSettings = adminEnquiryReceiveMailSettingReadMapper.listByExample(adminEnquiryPhoneMailSettingExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(adminEnquiryPhoneMailSettings), "该手机号已添加，无需重复添加");

        adminEnquiryReceiveMailSetting.setOperateUid(Long.valueOf(admin.getAdminId()));
        adminEnquiryReceiveMailSetting.setOperateName(admin.getAdminName());
        adminEnquiryReceiveMailSetting.setCreateTime(new Date());
        int count = adminEnquiryReceiveMailSettingWriteMapper.insert(adminEnquiryReceiveMailSetting);
        AssertUtil.notNullOrZero(count, "询盘邮箱接收添加失败，请重试");

        return count;
    }

    /**
     * 添加接收询盘邮箱
     *
     * @param adminEnquiryReceiveMailSetting
     * @return
     */
    public Integer editEnquiryAdminReceiverMail(HttpServletRequest request, AdminEnquiryReceiveMailSetting adminEnquiryReceiveMailSetting) {
        Admin admin = UserUtil.getUser(request, Admin.class);

        AdminEnquiryReceiveMailSettingExample adminEnquiryReceiveMailSettingExample = new AdminEnquiryReceiveMailSettingExample();
        adminEnquiryReceiveMailSettingExample.setWebSite(adminEnquiryReceiveMailSetting.getWebSite());
        adminEnquiryReceiveMailSettingExample.setEmail(adminEnquiryReceiveMailSetting.getEmail());
        adminEnquiryReceiveMailSettingExample.setIdNotEqualTo(adminEnquiryReceiveMailSetting.getId());
        List<AdminEnquiryReceiveMailSetting> adminEnquiryReceiveMailSettings = adminEnquiryReceiveMailSettingReadMapper.listByExample(adminEnquiryReceiveMailSettingExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(adminEnquiryReceiveMailSettings), "该邮箱已添加，无需重复添加");

        AdminEnquiryReceiveMailSettingExample adminEnquiryPhoneMailSettingExample = new AdminEnquiryReceiveMailSettingExample();
        adminEnquiryPhoneMailSettingExample.setWebSite(adminEnquiryReceiveMailSetting.getWebSite());
        adminEnquiryPhoneMailSettingExample.setPhone(adminEnquiryReceiveMailSetting.getPhone());
        adminEnquiryPhoneMailSettingExample.setIdNotEqualTo(adminEnquiryReceiveMailSetting.getId());
        List<AdminEnquiryReceiveMailSetting> adminEnquiryPhoneMailSettings = adminEnquiryReceiveMailSettingReadMapper.listByExample(adminEnquiryPhoneMailSettingExample);
        AssertUtil.isTrue(!CollectionUtil.isEmpty(adminEnquiryPhoneMailSettings), "该手机号已添加，无需重复添加");

        adminEnquiryReceiveMailSetting.setOperateUid(Long.valueOf(admin.getAdminId()));
        adminEnquiryReceiveMailSetting.setCreateTime(new Date());
        int count = adminEnquiryReceiveMailSettingWriteMapper.updateByPrimaryKeySelective(adminEnquiryReceiveMailSetting);
        AssertUtil.notNullOrZero(count, "询盘邮箱接收编辑失败，请重试");

        return count;
    }

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

    /**
     * @param example 查询条件信息
     * @param pager   分页信息
     */
    public List<AdminEnquiryReceiveMailSetting> getEnquiryAdminReceiveMailSettings(AdminEnquiryReceiveMailSettingExample example, PagerInfo pager) {
        List<AdminEnquiryReceiveMailSetting> adminEnquiryReceiveMailSettings;
        if (pager != null) {
            pager.setRowsCount(adminEnquiryReceiveMailSettingReadMapper.countByExample(example));
            adminEnquiryReceiveMailSettings = adminEnquiryReceiveMailSettingReadMapper.listPageByExample(example, pager.getStart(), pager.getPageSize());
        } else {
            adminEnquiryReceiveMailSettings = adminEnquiryReceiveMailSettingReadMapper.listByExample(example);
        }
        return adminEnquiryReceiveMailSettings;
    }

}
