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

import com.slodon.b2b2c.core.constant.AdminConst;
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.enums.WebSiteConstant;
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.StoreModel;
import com.slodon.b2b2c.model.seller.StoreNameModel;
import com.slodon.b2b2c.model.seller.VendorModel;
import com.slodon.b2b2c.model.seller.VendorResourcesModel;
import com.slodon.b2b2c.model.system.AdminModel;
import com.slodon.b2b2c.model.system.SystemResourceModel;
import com.slodon.b2b2c.seller.example.StoreExample;
import com.slodon.b2b2c.seller.example.StoreNameExample;
import com.slodon.b2b2c.seller.example.VendorExample;
import com.slodon.b2b2c.seller.example.VendorResourcesExample;
import com.slodon.b2b2c.seller.pojo.Store;
import com.slodon.b2b2c.seller.pojo.StoreName;
import com.slodon.b2b2c.seller.pojo.Vendor;
import com.slodon.b2b2c.seller.pojo.VendorResources;
import com.slodon.b2b2c.system.example.AdminExample;
import com.slodon.b2b2c.system.example.SystemResourceExample;
import com.slodon.b2b2c.system.pojo.Admin;
import com.slodon.b2b2c.system.pojo.SystemResource;
import com.slodon.b2b2c.vo.business.OrderEnquiryTrackVO;
import com.slodon.b2b2c.vo.member.EnquiryVendorVO;
import com.slodon.b2b2c.vo.member.OrderEnquiryVO;
import com.slodon.b2b2c.vo.seller.StoreVO;
import com.slodon.b2b2c.vo.system.SystemRoleVO;
import io.swagger.annotations.Api;
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 = "admin-询盘管理")
@RestController
@RequestMapping("v3/seller/admin/enquiry")
@Slf4j
public class AdminEnquiryController extends BaseController {

    @Resource
    private MemberEnquiryModel memberEnquiryModel;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private VendorModel vendorModel;

    @Resource
    private MemberEnquiryAssignModel memberEnquiryAssignModel;

    @Resource
    private MemberEnquiryTrackModel memberEnquiryTrackModel;

    @Resource
    private AdminModel adminModel;

    @Resource
    private StoreModel storeModel;

    @Resource
    private StoreNameModel storeNameModel;

    @Resource
    private SystemResourceModel systemResourceModel;

    @Resource
    private VendorResourcesModel vendorResourcesModel;

    /**
     * 询盘管理列表
     *
     * @param request
     * @param name
     * @param email
     * @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 = "storeId", required = false) Long storeId,
                                                      @RequestParam(value = "webSite", required = false) String webSite,
                                                      @RequestParam(value = "phone", required = false) String phone) {
        Admin admin = UserUtil.getUser(request, Admin.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        if(storeId !=null && storeId == 1){
            storeId = 0L;
        }
        List<MemberEnquiry> list = memberEnquiryModel.getEnquiryAdminListByPage(name, email, String.valueOf(admin.getIsSuper()), admin.getAdminId(),validStatus, pager,storeId,webSite,phone);
        pager.setRowsCount(memberEnquiryModel.enquiryAdminListPageCount(name, email, String.valueOf(admin.getIsSuper()), admin.getAdminId(),validStatus,storeId,webSite,phone));
        ArrayList<OrderEnquiryVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(memberEnquiry -> {
                memberEnquiry.setIsDel(String.valueOf(admin.getIsSuper()));
                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, Long storeId) {
        ArrayList<EnquiryVendorVO> vos = new ArrayList<>();
        if (storeId != null && storeId == 0) {
            AdminExample adminExample = new AdminExample();
            adminExample.setState(AdminConst.ADMIN_STATE_NORM);
            List<Admin> adminList = adminModel.getAdminList(adminExample, null);
            adminList.forEach(admin -> {
                //查询角色资源对应表,获取该角色拥有的资源列表
                SystemResourceExample resourceExample = new SystemResourceExample();
                resourceExample.setRoleId(admin.getRoleId());
                resourceExample.setGrade(AdminConst.RESOURCE_GRADE_3);
                resourceExample.setContent("询盘管理");
                resourceExample.setOrderBy("resource_id asc");
                List<SystemResource> resourceRoleList = systemResourceModel.getSystemResourceList(resourceExample, null);
                if (!CollectionUtils.isEmpty(resourceRoleList)) {
                    vos.add(new EnquiryVendorVO(admin));
                }
            });
        } else {
            VendorExample vendorExample = new VendorExample();
            vendorExample.setStoreId(storeId);
            vendorExample.setIsAllowLogin(VendorConst.IS_ALLOW_LOGIN);
            List<Vendor> vendorList = vendorModel.getVendorList(vendorExample, null);
            vendorList.forEach(vendor -> {
                //查询角色资源对应表,获取该角色拥有的资源列表
                VendorResourcesExample resourceExample = new VendorResourcesExample();
                resourceExample.setRoleId(vendor.getRolesId());
                resourceExample.setGrade(AdminConst.RESOURCE_GRADE_3);
                resourceExample.setContent("询盘管理");
                resourceExample.setOrderBy("resources_id asc");
                List<VendorResources> resourceRoleList = vendorResourcesModel.getVendorResourcesList(resourceExample, null);
                if (!CollectionUtils.isEmpty(resourceRoleList)) {
                    vos.add(new EnquiryVendorVO(vendor));
                }
            });
        }
        return SldResponse.success(new PageVO<>(vos, null));
    }


    @GetMapping("store/list")
    public JsonResult<PageVO<StoreVO>> getStoreList(HttpServletRequest request, @RequestParam(value = "webSite", required = false) String webSite) {
        StoreExample storeExample = new StoreExample();
        List<Store> storeList = storeModel.getStoreList(storeExample,null);
        if(!WebSiteConstant.MEMBER_OVERSEA.equals(webSite)){
            StoreNameExample storeNameExample = new StoreNameExample();
            storeNameExample.setWebSite(webSite);
            List<StoreName> storeNameList = storeNameModel.getStoreNameList(storeNameExample,null);
            if (!CollectionUtils.isEmpty(storeNameList)){
                storeList.forEach(store -> {
                    storeNameList.forEach(storeName -> {
                        if(store.getStoreId().equals(storeName.getStoreId())){
                            store.setStoreName(storeName.getStoreName());
                        }
                    });
                });
            }
        }

        ArrayList<StoreVO> vos = new ArrayList<>();
        storeList.forEach(store -> {
            vos.add(new StoreVO(store));
        });
        Store storeInfo = new Store();
        storeInfo.setStoreId(1L);
        storeInfo.setStoreName("平台");
        vos.add(0,new StoreVO(storeInfo));
        return SldResponse.success(new PageVO<>(vos, null));
    }


    /**
     * 询盘分配
     *
     * @param request
     * @return
     */
    @PostMapping("assign")
    public JsonResult<Object> assignEnquiryToVendor(HttpServletRequest request, MemberEnquiryAssign memberEnquiryAssign) {
        Admin admin = UserUtil.getUser(request, Admin.class);
        memberEnquiryAssign.setOperateUid(Long.valueOf(admin.getAdminId()));
        memberEnquiryAssign.setOperateName(admin.getAdminName());
        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) {
        Admin admin = UserUtil.getUser(request, Admin.class);
        enquiryTrack.setVendorId(Long.valueOf(admin.getAdminId()));
        enquiryTrack.setVendorName(admin.getAdminName());
        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) {
        Admin admin = UserUtil.getUser(request, Admin.class);
        int count = memberEnquiryTrackModel.makeEnquiryGarbage(enquiryId, Long.valueOf(admin.getAdminId()), admin.getAdminName());
        return SldResponse.success(count);
    }

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


}
