package com.kiisoo.ic.store.controller;

import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.store.entity.PoiStore;
import com.kiisoo.ic.store.service.PoiStoreService;
import com.kiisoo.ic.utils.DataImportUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 门店
 */
@Controller
@RequestMapping("/poi/store")
@Slf4j
public class PoiStoreController extends BaseController {

    @Autowired
    private PoiStoreService poiStoreService;



    /**
     * 根据用户id，区域id获取店铺
     * @return
     */
    @RequestMapping(value = "/user/shop", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUserShop(@RequestParam("userId") long userId, @RequestParam(value = "customerIds", required = false) String customerIds) {
        try {
            List<Long> customers = null;
            if (StringUtils.isNotBlank(customerIds)) {
                String[] split = customerIds.replace("[", "").replace("]", "").split(",");
                List<String> strings = Arrays.asList(split);
                if (CollectionUtils.isNotEmpty(strings)) {
                    customers = new ArrayList<>();
                    for (String customerId : strings) {
                        if (StringUtils.isNotBlank(customerId)) {
                            customers.add(Long.parseLong(customerId));
                        }
                    }
                }
            }
            return data(poiStoreService.getRegionShop(userId, customers));
        } catch (Exception e) {
            log.error("获取用户店铺失败", e);
            return fail();
        }

    }

    @RequestMapping(value = "/user/shopDTO", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getUserShopDTO(@RequestParam("userId") long userId, @RequestParam(value = "customerIds", required = false) String customerIds, @RequestParam("scheduleId") Long scheduleId) {
        try {
            List<Long> customers = null;
            if (StringUtils.isNotBlank(customerIds)) {
                String[] split = customerIds.replace("[", "").replace("]", "").split(",");
                List<String> strings = Arrays.asList(split);
                if (CollectionUtils.isNotEmpty(strings)) {
                    customers = new ArrayList<>();
                    for (String customerId : strings) {
                        if (StringUtils.isNotBlank(customerId)) {
                            customers.add(Long.parseLong(customerId));
                        }
                    }
                }
            }
            return data(poiStoreService.getRegionShopDTO(userId, customers, scheduleId));
        } catch (Exception e) {
            log.error("获取用户店铺失败", e);
            return fail();
        }

    }
    /**
     * 查询所有店铺
     *
     * @return 店铺信息
     */
    @RequestMapping(value = "/all/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> listAllShop() {
        try {
            List<PoiStore> shops = poiStoreService.listAllShop();
            return data(shops);
        } catch (Exception e) {
            log.error("查询渠道店铺出错", e);
            return fail();
        }
    }


    /**
     * 通过公司查询店铺
     *
     * @return 店铺信息
     */
    @RequestMapping(value = "/org/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> orgList(@RequestParam("departmentIdList") String departmentIdList) {

        try {
            List<PoiStore> shops = poiStoreService.organizationalList(departmentIdList);
            return data(shops);
        } catch (Exception e) {
            log.error("查询店铺出错", e);
            return fail();
        }
    }


}
