package com.slodon.b2b2c.controller.member.admin;

import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.model.member.advich.AdminEnquiryReceiverMailSettingModel;
import com.slodon.b2b2c.system.example.AdminEnquiryReceiveMailSettingExample;
import com.slodon.b2b2c.system.pojo.Admin;
import com.slodon.b2b2c.system.pojo.AdminEnquiryReceiveMailSetting;
import com.slodon.b2b2c.vo.seller.AdminEnquiryReceiverMailVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author cyan
 */
@Api(tags = "admin-询盘管理")
@RestController
@RequestMapping("v3/seller/admin/enquiry/setting")
@Slf4j
public class AdminEnquiryReceiverMailSettingController extends BaseController {

    @Resource
    private AdminEnquiryReceiverMailSettingModel adminEnquiryReceiverMailSettingModel;

    /**
     * 询盘接收邮箱列表
     *
     * @param request
     * @param email
     * @return
     */
    @GetMapping("list")
    public JsonResult<PageVO<AdminEnquiryReceiverMailVO>> getList(HttpServletRequest request,
                                                                  @RequestParam(value = "email", required = false) String email,
                                                                  @RequestParam(value = "phone", required = false) String phone,
                                                                  @RequestParam(value = "webSite", required = false) String webSite) {
        Admin admin = UserUtil.getUser(request, Admin.class);
        PagerInfo pager = WebUtil.handlerPagerInfo(request);

        AdminEnquiryReceiveMailSettingExample example = new AdminEnquiryReceiveMailSettingExample();
        example.setWebSite(webSite);
        example.setEmailLike(email);
        example.setPhoneLike(phone);

        List<AdminEnquiryReceiveMailSetting> list = adminEnquiryReceiverMailSettingModel.getEnquiryAdminReceiveMailSettings(example, pager);
        ArrayList<AdminEnquiryReceiverMailVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(receiveMailSetting -> {
//                receiveMailSetting.setIsDel(admin.getIsSuper());
//                receiveMailSetting.setIsDel((admin.getRoleId() == 1) ? 1 : 0);
                vos.add(new AdminEnquiryReceiverMailVO(receiveMailSetting));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    /**
     * 添加接收询盘邮箱
     *
     * @param request
     * @return
     */
    @PostMapping("add")
    public JsonResult<Object> addEnquiryReceiverMail(HttpServletRequest request, AdminEnquiryReceiveMailSetting adminEnquiryReceiveMailSetting) {
        AssertUtil.notEmpty(adminEnquiryReceiveMailSetting.getEmail(), "询盘接收邮箱不能为空");
        AssertUtil.notEmpty(adminEnquiryReceiveMailSetting.getPhone(), "询盘接收手机号不能为空");
        AssertUtil.emailCheck(adminEnquiryReceiveMailSetting.getEmail());
        AssertUtil.mobileCheck(adminEnquiryReceiveMailSetting.getPhone());
        int count = adminEnquiryReceiverMailSettingModel.saveEnquiryAdminReceiverMail(request, adminEnquiryReceiveMailSetting);
        return SldResponse.success(count);
    }

    /**
     * 编辑接收询盘邮箱
     *
     * @param request
     * @return
     */
    @PostMapping("edit")
    public JsonResult<Object> editEnquiryReceiverMail(HttpServletRequest request, AdminEnquiryReceiveMailSetting adminEnquiryReceiveMailSetting) {
        AssertUtil.notEmpty(adminEnquiryReceiveMailSetting.getEmail(), "询盘接收邮箱不能为空");
        AssertUtil.notEmpty(adminEnquiryReceiveMailSetting.getPhone(), "询盘接收手机号不能为空");
        AssertUtil.emailCheck(adminEnquiryReceiveMailSetting.getEmail());
        AssertUtil.mobileCheck(adminEnquiryReceiveMailSetting.getPhone());
        int count = adminEnquiryReceiverMailSettingModel.editEnquiryAdminReceiverMail(request, adminEnquiryReceiveMailSetting);
        return SldResponse.success(count);
    }

    /**
     * 删除询盘接收邮箱
     *
     * @param request
     * @return
     */
    @PostMapping("del")
    public JsonResult<Object> delEnquiryToVendor(HttpServletRequest request, Long id) {
        int count = adminEnquiryReceiverMailSettingModel.delEnquiryAdminReceiverMail(request, id);
        return SldResponse.success(count);
    }
}
