package com.slodon.b2b2c.controller.member.seller.advich;

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.EnquiryReceiverMailSettingModel;
import com.slodon.b2b2c.seller.example.EnquiryReceiveMailSettingExample;
import com.slodon.b2b2c.seller.pojo.EnquiryReceiveMailSetting;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.business.OrderEnquiryReceiverMailVO;
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 = "seller-询盘管理")
@RestController
@RequestMapping("v3/seller/seller/enquiry/setting")
@Slf4j
public class SellerEnquiryReceiverMailSettingController extends BaseController {

    @Resource
    private EnquiryReceiverMailSettingModel enquiryReceiverMailSettingModel;

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

        EnquiryReceiveMailSettingExample example = new EnquiryReceiveMailSettingExample();
        example.setStoreId(vendor.getStoreId());
        example.setEmailLike(email);
        example.setPhoneLike(phone);
        example.setWebSite(webSite);

        List<EnquiryReceiveMailSetting> list = enquiryReceiverMailSettingModel.getEnquiryReceiveMailSettings(example, pager);
        ArrayList<OrderEnquiryReceiverMailVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(receiveMailSetting -> {
                receiveMailSetting.setIsDel(vendor.getIsStoreAdmin());
                vos.add(new OrderEnquiryReceiverMailVO(receiveMailSetting));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    /**
     * 添加接收询盘邮箱
     *
     * @param request
     * @return
     */
    @PostMapping("add")
    public JsonResult<Object> addEnquiryReceiverMail(HttpServletRequest request, EnquiryReceiveMailSetting enquiryReceiveMailSetting) {
        Vendor vendor = UserUtil.getUser(request,Vendor.class);
        AssertUtil.notEmpty(enquiryReceiveMailSetting.getWebSite(), "询盘渠道不能为空");
        AssertUtil.notEmpty(enquiryReceiveMailSetting.getEmail(), "询盘接收邮箱不能为空");
        AssertUtil.notEmpty(enquiryReceiveMailSetting.getPhone(), "询盘手机号不能为空");
        AssertUtil.emailCheck(enquiryReceiveMailSetting.getEmail());
        AssertUtil.mobileCheck(enquiryReceiveMailSetting.getPhone());
        enquiryReceiveMailSetting.setStoreId(vendor.getStoreId());
        int count = enquiryReceiverMailSettingModel.saveEnquiryReceiverMail(request, enquiryReceiveMailSetting);
        return SldResponse.success(count);
    }

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

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