package com.kiisoo.ic.store.controller;

import com.github.pagehelper.PageInfo;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.employee.entity.ImportErrorDTO;
import com.kiisoo.ic.store.entity.StoreUserDTO;
import com.kiisoo.ic.store.service.StoreEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Description: 门店号管理controller
 * @Auther: yechenhao
 * @Date: 2020/4/9 18:23
 * @Version: v1
 */
@Controller
@RequestMapping("/store/emp")
@Slf4j
public class StoreEmployeeController extends BaseController {

    @Autowired
    private StoreEmployeeService storeEmployeeService;

    /**
     *
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> listStoreUser(@RequestParam("userId")Long userId,Long storeId,Long regionId,Long companyId,
                                            @RequestParam("pageNum")Integer pageNum, @RequestParam("pageSize")Integer pageSize ,String code){
        try {
            PageInfo<StoreUserDTO> storeUserDTOS =  storeEmployeeService.listStoreUser(userId,storeId,regionId,companyId,pageNum, pageSize,code);
            return data(storeUserDTOS);
        }catch (Exception e){
            log.error("查询门店号列表失败",e);
            return fail();
        }
    }

    /**
     * 批量上传文件
     * @return
     */
    @RequestMapping(value = "import",method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> importShopEmpByExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<ImportErrorDTO> importErrorDTOS = storeEmployeeService.importShopEmpByExcel(file);
            return data(importErrorDTOS);
        } catch (Exception e) {
            log.error("添加用户失败", e);
            return fail();
        }
    }

    /**
     * 批量下载店铺二维码
     * @param response
     */
    @RequestMapping(value = "download/store/code",method = RequestMethod.POST)
    @ResponseBody
    public void downloadCode(HttpServletResponse response) {
        try {
            storeEmployeeService.downloadStoreUserCode(response);
        } catch (Exception e) {
            log.error("批量下载店铺二维码失败", e);
        }
    }

    /**
     * 批量下载店铺二维码
     * @param response
     */
    @RequestMapping(value = "download/store/code/bsd",method = RequestMethod.POST)
    @ResponseBody
    public void downloadCodeBSD(HttpServletResponse response) {
        try {
            storeEmployeeService.downloadStoreUserCodeFromBSD(response);
        } catch (Exception e) {
            log.error("批量下载店铺二维码失败", e);
        }
    }

    /**
     * 批量下载导购二维码
     * @param response
     */
    @RequestMapping(value = "download/store/seller/code",method = RequestMethod.POST)
    @ResponseBody
    public void downloadSellerCode(HttpServletResponse response) {
        try {
            storeEmployeeService.downloadStoreStaffCode(response);
        } catch (Exception e) {
            log.error("批量下载店铺二维码失败", e);
        }
    }

    /**
     * 批量下载导购二维码
     * @param response
     */
    @RequestMapping(value = "download/store/seller/code/bsd",method = RequestMethod.POST)
    @ResponseBody
    public void downloadSellerCodeBSD(HttpServletResponse response) {
        try {
            storeEmployeeService.downloadStoreStaffCodeFromBSD(response);
        } catch (Exception e) {
            log.error("批量下载店铺二维码失败", e);
        }
    }
}
