package com.kiisoo.ic.store.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.kiisoo.ic.common.BaseController;
import com.kiisoo.ic.common.KiisooException;
import com.kiisoo.ic.employee.entity.EmployeeDO;
import com.kiisoo.ic.employee.entity.PoiStoreStaffDO;
import com.kiisoo.ic.employee.entity.PrivilageCpUserDO;
import com.kiisoo.ic.employee.service.EmployeeService;
import com.kiisoo.ic.store.bean.PoiStoreVO;
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.store.service.StoreEmployeeService;
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;

    @Autowired
    private StoreEmployeeService storeEmployeeService;

    /**
     * 查询店铺人员列表
     *
     * @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 {
            String code = poiStoreStaffService.addStaff(poiStoreStaffVO);
            return data(code);
        } 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();
        }
    }

    /**
     * 保存导购头像
     */
    @RequestMapping(value = "/save/avatarUrl",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveAvatarUrl(Long userId, String avatarUrl) {
        try {
            int update = poiStoreStaffService.saveAvatarUrl(userId,avatarUrl);
            return data(update);
        } catch (Exception e) {
            log.error("保存导购头像", e);
            return fail();
        }
    }

    /**
     * 作废二维码
     * @author dexiang.jiang
     * @date 2020/05/19 19:24
     */
    @RequestMapping(value = "/cancel/qrcode",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> cancelQrCode(@RequestParam("cpUserId") String cpUserId, @RequestParam("state") String state) {
        try {
            boolean b = storeEmployeeService.cancelQrCodeFromBSD(cpUserId,state);
            return data(b);
        } catch (Exception e) {
            log.error("作废二维码", e);
            return fail();
        }
    }

    /**
     * 添加组织
     * @author dexiang.jiang
     * @date 2020/05/20 14:52
     */
    @RequestMapping(value = "/region/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addRegion(@RequestParam("name") String name, @RequestParam("code") String code) {
        try {
            boolean b = storeEmployeeService.addOrganizationalByRegion(name,code);
            return data(b);
        } catch (KiisooException e) {
            log.error("添加组织", e);
            return fail(e.getCode(),e.getMsg());
        }
    }

    /**
     * 添加组织
     * @author dexiang.jiang
     * @date 2020/05/20 14:52
     */
    @RequestMapping(value = "/company/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> companyAdd(@RequestParam("name") String name, @RequestParam("code") String code, @RequestParam("parentId") Long parentId, @RequestParam("level") Long level) {
        try {
            boolean b = storeEmployeeService.addOrganizational(name,code, parentId, level);
            return data(b);
        } catch (KiisooException e) {
            log.error("添加组织", e);
            return fail(e.getCode(),e.getMsg());
        }
    }

    /**
     * 添加组织
     * @author dexiang.jiang
     * @date 2020/05/20 14:52
     */
    @RequestMapping(value = "/company/add/code",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> companyAddCode(@RequestParam("name") String name, @RequestParam("code") String code, @RequestParam("parentCode") String parentCode, @RequestParam("level") Long level) {
        try {
            boolean b = storeEmployeeService.addOrganizationalByCode(name,code, parentCode, level);
            return data(b);
        } catch (KiisooException e) {
            log.error("添加组织", e);
            return fail(e.getCode(),e.getMsg());
        }
    }

    /**
     * 添加组织
     * @author dexiang.jiang
     * @date 2020/05/20 14:52
     */
    @RequestMapping(value = "/cpuser/add/code",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> cpUserAddCode(@RequestParam("name") String name,@RequestParam("alias") String alias, @RequestParam("cpUserId") String cpUserId, @RequestParam("storeCode") String storeCode) {
        try {
            boolean b = storeEmployeeService.addCpUserIdByCode(name,alias, cpUserId, storeCode);
            return data(b);
        } catch (KiisooException e) {
            log.error("添加组织", e);
            return fail(e.getCode(),e.getMsg());
        }
    }

    /**
     * 添加店铺
     * @author dexiang.jiang
     * @date 2020/05/20 13:55
     */
    @RequestMapping(value = "/store/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addStore(PoiStoreVO parentId) {
        try {
            boolean b = storeEmployeeService.addStore(parentId);
            return data(b);
        } catch (KiisooException e) {
            log.error("添加店铺", e);
            return fail(e.getCode(),e.getMsg());
        }
    }

    /**
     * 添加店铺人员
     * @author dexiang.jiang
     * @date 2020/05/20 22:56
     */
    @RequestMapping(value = "/store/staff/add",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> addStoreStaff(com.kiisoo.ic.store.bean.PoiStoreStaffVO poiStoreStaffVO) {
        try {
            boolean b = storeEmployeeService.addStoreStaff(poiStoreStaffVO);
            return data(b);
        } catch (KiisooException e) {
            log.error("添加店铺人员", e);
            return fail(e.getCode(),e.getMsg());
        }
    }

}
