package com.kiisoo.ic.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.service.EmployeeService;
import com.kiisoo.ic.store.entity.PoiStoreStaff;
import com.kiisoo.ic.store.entity.PoiStoreStaffVO;
import com.kiisoo.ic.store.service.PoiStoreStaffService;
import com.kiisoo.ic.system.bean.ImportAccountErrorBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * @Description: 门店人员信息
 * @Auther: yinliujiang
 * @Date: 2020/4/9
 * @Version: v1
 */
@Controller
@RequestMapping("/store/staff")
@Slf4j
public class StoreStaffController extends BaseController {

    @Autowired
    private PoiStoreStaffService poiStoreStaffService;


    /**
     * 查询店铺人员列表
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> staffList(int pageNum, int pageSize, Long shopId) {
        try {
            IPage<PoiStoreStaffVO> storeStaffIs = poiStoreStaffService.getStaffByUserAndDate( pageNum,  pageSize, shopId);
            return data(storeStaffIs);
        } catch (Exception e) {
            log.error("获取店铺人员", e);
            return fail();
        }
    }

    /**
     * 查询店铺人员列表
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "/list/userId", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> staffListByUserId(Long userId) {
        try {
            Map<String, Object> result = poiStoreStaffService.getStaffByUserId(userId);
            return data(result);
        } catch (Exception e) {
            log.error("获取店铺人员", e);
            return fail();
        }
    }

    /**
     * 查询店铺人员列表
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addStaff(PoiStoreStaffVO poiStoreStaffVO) {
        try {
             poiStoreStaffService.addStaff(poiStoreStaffVO);
            return success();
        } catch (Exception e) {
            log.error("获取店铺人员", e);
            return fail();
        }
    }

    /**
     * 批量上传推广人员
     * @param files excel
     * @return 错误List
     */
    @ResponseBody
    @RequestMapping(value = "/upload" , method = RequestMethod.POST, produces = "application/json;charset=utf8")
    public Map<String,Object> uploadGoods(@RequestParam("file") MultipartFile[] files){
        try {
            List<ImportAccountErrorBean> result = poiStoreStaffService.uploadStaff(files);
            return data(result);
        }catch (Exception e){
            log.error("批量上传商品错误", e);
            return fail();
        }
    }

    /**
     * 查询店铺人员列表
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> editStaff(PoiStoreStaffVO poiStoreStaffVO) {
        try {
            poiStoreStaffService.ediStaff(poiStoreStaffVO);
            return success();
        } catch (Exception e) {
            log.error("获取店铺人员", e);
            return fail();
        }
    }


    /**
     * 查询店铺人员列表
     *
     * @param
     * @return
     * @throws
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> staffList(long id) {
        try {
            PoiStoreStaffVO storeStaff = poiStoreStaffService.getStaffDetail(id);
            return data(storeStaff);
        } catch (Exception e) {
            log.error("获取店铺人员", e);
            return fail();
        }
    }

    /**
     * 删除店铺人员
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteStaff(long staffId) {
        try {
            boolean del = poiStoreStaffService.deleteStaff(staffId);
            return data(del);
        } catch (Exception e) {
            log.error("删除店铺人员", e);
            return fail();
        }
    }
}
