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

import com.slodon.b2b2c.core.constant.VendorConst;
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.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.member.example.MemberEnquiryExample;
import com.slodon.b2b2c.member.example.MemberEnquiryTrackExample;
import com.slodon.b2b2c.member.pojo.MemberEnquiry;
import com.slodon.b2b2c.member.pojo.MemberEnquiryAssign;
import com.slodon.b2b2c.member.pojo.MemberEnquiryTrack;
import com.slodon.b2b2c.model.member.advich.MemberEnquiryAssignModel;
import com.slodon.b2b2c.model.member.advich.MemberEnquiryModel;
import com.slodon.b2b2c.model.member.advich.MemberEnquiryTrackModel;
import com.slodon.b2b2c.model.seller.VendorModel;
import com.slodon.b2b2c.seller.example.VendorExample;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.vo.business.OrderEnquiryTrackVO;
import com.slodon.b2b2c.vo.member.EnquiryVendorVO;
import com.slodon.b2b2c.vo.member.OrderEnquiryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
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")
@Slf4j
public class SellerEnquiryController extends BaseController {

    @Resource
    private MemberEnquiryModel memberEnquiryModel;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private VendorModel vendorModel;

    @Resource
    private MemberEnquiryAssignModel memberEnquiryAssignModel;

    @Resource
    private MemberEnquiryTrackModel memberEnquiryTrackModel;

    /**
     * 询盘管理列表
     *
     * @param request
     * @param name
     * @param email
     * @param validStatus
     * @param webSite
     * @return
     */
    @GetMapping("list")
    public JsonResult<PageVO<OrderEnquiryVO>> getList(HttpServletRequest request, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "email", required = false) String email, @RequestParam(value = "validStatus", required = false) Integer validStatus, @RequestParam(value = "webSite", required = false,defaultValue = "1") String webSite,@RequestParam(value = "phone", required = false) String phone ) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        List<MemberEnquiry> list = memberEnquiryModel.getEnquiryListByPage(name, email, vendor.getStoreId(), vendor.getIsStoreAdmin(), vendor.getVendorId(), validStatus, webSite, phone, pager);
        pager.setRowsCount(memberEnquiryModel.enquiryListPageCount(name, email, vendor.getStoreId(), vendor.getIsStoreAdmin(), vendor.getVendorId(), validStatus, webSite, phone));
        ArrayList<OrderEnquiryVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(memberEnquiry -> {
                memberEnquiry.setIsDel(vendor.getIsStoreAdmin());
                vos.add(new OrderEnquiryVO(memberEnquiry));
            });
        }
        return SldResponse.success(new PageVO<>(vos, pager));

    }

    /**
     * 账户列表
     *
     * @param request
     * @return
     */
    @GetMapping("sub/list")
    public JsonResult<PageVO<EnquiryVendorVO>> getSubAdminList(HttpServletRequest request) {
        Vendor vendorInfo = UserUtil.getUser(request, Vendor.class);
        VendorExample example = new VendorExample();
        example.setIsAllowLoginNotEquals(VendorConst.LOGOUT_ALLOW_LOGIN);
        example.setStoreId(vendorInfo.getStoreId());
        example.setOrderBy("vendor_id asc");
        List<Vendor> vendorList = vendorModel.getVendorList(example, null);
        ArrayList<EnquiryVendorVO> vos = new ArrayList<>();
        vendorList.forEach(vendor -> {
            vos.add(new EnquiryVendorVO(vendor));
        });
        return SldResponse.success(new PageVO<>(vos, example.getPager()));
    }

    /**
     * 询盘分配
     *
     * @param request
     * @return
     */
    @PostMapping("assign")
    public JsonResult<Object> assignEnquiryToVendor(HttpServletRequest request, MemberEnquiryAssign memberEnquiryAssign) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        memberEnquiryAssign.setOperateUid(vendor.getVendorId());
        memberEnquiryAssign.setOperateName(vendor.getVendorName());
        int count = memberEnquiryAssignModel.saveEnquiryAssign(memberEnquiryAssign);
        return SldResponse.success(count);
    }

    /**
     * 询盘删除
     *
     * @param request
     * @return
     */
    @PostMapping("del")
    public JsonResult<Object> delEnquiryToVendor(HttpServletRequest request, Long enquiryId) {
        int count = memberEnquiryModel.delMemberEnquiry(request, enquiryId);
        return SldResponse.success(count);
    }

    /**
     * 询盘跟踪
     *
     * @param request
     * @return
     */
    @PostMapping("track")
    public JsonResult<Object> trackEnquiryToVendor(HttpServletRequest request, MemberEnquiryTrack enquiryTrack) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        enquiryTrack.setVendorId(vendor.getVendorId());
        enquiryTrack.setVendorName(vendor.getVendorName());
        int count = memberEnquiryTrackModel.saveEnquiryTrack(enquiryTrack);
        return SldResponse.success(count);
    }

    /**
     * 询盘跟踪记录
     *
     * @param request
     * @param message
     * @return
     */
    @GetMapping("track/record")
    public JsonResult<PageVO<OrderEnquiryTrackVO>> getTrackRecordList(HttpServletRequest request, Long enquiryId,
                                                                      @RequestParam(value = "message", required = false) String message,
                                                                      @RequestParam(value = "vendorId", required = false) Long vendorId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        MemberEnquiryTrackExample example = new MemberEnquiryTrackExample();
        example.setEnquiryId(enquiryId);
        example.setMessageLike(message);
        example.setVendorId(vendorId);
        example.setPager(pager);
        List<MemberEnquiryTrack> list = memberEnquiryTrackModel.getTrackList(example, pager);
        ArrayList<OrderEnquiryTrackVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(memberEnquiryTrack -> {
                vos.add(new OrderEnquiryTrackVO(memberEnquiryTrack));
            });
        }
        return SldResponse.success(new PageVO<>(vos, example.getPager()));
    }

    /**
     * 标记为垃圾询盘
     *
     * @param request
     * @return
     */
    @PostMapping("garbage")
    public JsonResult<Object> makeEnquiryGarbage(HttpServletRequest request, Long enquiryId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        int count = memberEnquiryTrackModel.makeEnquiryGarbage(enquiryId, vendor.getVendorId(), vendor.getVendorName());
        return SldResponse.success(count);
    }

    /**
     * 取消标记为垃圾询盘
     *
     * @param request
     * @return
     */
    @PostMapping("cancle/garbage")
    public JsonResult<Object> cancleMakeEnquiryGarbage(HttpServletRequest request, Long enquiryId) {
        Vendor vendor = UserUtil.getUser(request, Vendor.class);
        int count = memberEnquiryTrackModel.cancleMakeEnquiryGarbage(enquiryId, vendor.getVendorId(), vendor.getVendorName());
        return SldResponse.success(count);
    }
}
