package com.yx.face.controller.admin;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.yx.face.boot.core.BaseController;
import com.yx.face.boot.restful.RestDTO;
import com.yx.face.boot.restful.RestResponse;
import com.yx.face.boot.restful.RestResult;
import com.yx.face.boot.restful.ServiceException;
import com.yx.face.boot.uitls.ExcelListenerUtils;
import com.yx.face.model.dto.UserWhitelistDTO;
import com.yx.face.model.excel.UserWhitelistExcel;
import com.yx.face.model.search.UserWhitelistSearch;
import com.yx.face.model.vo.UserWhitelistVO;
import com.yx.face.service.UserWhitelistService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List;

/**
 * @description:
 * @ClassName UserWhitelistController
 * @Author WXG
 * @Date 2021/11/5 14:12
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "B 常客白名单 API")
@RestController
@RequestMapping("admin/userWhitelis")
public class UserWhitelistController extends BaseController {

    private final UserWhitelistService userWhitelistService;

    @ApiOperation("新增常客")
    @PostMapping("insert")
    public RestResult<Boolean> insert(@RequestBody UserWhitelistDTO userWhitelistDTO) {
        Integer userId = getUserId();
        if (null==userId){
            throw new ServiceException("请先登录");
        }
        userWhitelistDTO.setAdminId(userId);
        Integer insert = userWhitelistService.insert(userWhitelistDTO);
        if (insert == 1) return RestResponse.ok(true);
        else return RestResponse.error("增加失败");
    }

    @ApiOperation("修改常客")
    @PostMapping("update")
    public RestResult<Boolean> update(@RequestBody UserWhitelistDTO userWhitelistDTO) {
        Integer insert = userWhitelistService.update(userWhitelistDTO);
        if (insert == 1) return RestResponse.ok(true);
        else return RestResponse.error("修改失败");
    }

    @ApiOperation("删除常客")
    @GetMapping("delete")
    public RestResult<Boolean> delete(@RequestParam("UserWhitelistId") Integer id) {
        Integer insert = userWhitelistService.delete(id, getUserId());
        if (insert == 1) return RestResponse.ok(true);
        else return RestResponse.error("删除失败");
    }

    @ApiOperation("常客列表分页")
    @PostMapping("/getPageList")
    public RestResult<PageInfo<UserWhitelistVO>> getPageList(@RequestBody RestDTO<UserWhitelistSearch> dto) {
        reBuildQuery(dto);
        PageInfo<UserWhitelistVO> pageList = userWhitelistService.getPageList(dto);
        if (pageList != null) {
            return RestResponse.ok(pageList);
        }
        return RestResponse.error("获取常客列表分页失败");
    }

    private void reBuildQuery(@RequestBody RestDTO<UserWhitelistSearch> dto) {
        UserWhitelistSearch object = dto.getData();
        if (object == null) {
            object = new UserWhitelistSearch();
        }
        dto.setData(object);
    }

    @ApiOperation("常客的批量导入")
    @PostMapping("uploadUserWhitelistExcel")
    public RestResult<List<String>> uploadUserWhitelistExcel(@RequestParam("file") MultipartFile excelFile) {
        ExcelListenerUtils listener = new ExcelListenerUtils();  //ExcelListener
        try {
            EasyExcel.read(excelFile.getInputStream(), UserWhitelistExcel.class, listener).sheet(0).doReadSync();  //ExcelModel 上面创建的实体类
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Object> datas = listener.getDatas(); //把拿到的数据处理到集合里去(根据所需自行修改)Integer userId = getUserId();
        //获取登陆者id
        Integer userId = getUserId();
        if (null==userId){
            throw new ServiceException("请先登录");
        }
        return userWhitelistService.uploadUserWhitelistExcel(datas,userId);
    }

    @ApiOperation("全量白名单下发")
    @GetMapping("addSnIssued")
    public RestResult<String> addSnIssued(Integer deviceId) {
        RestResult<String> restResult = userWhitelistService.addSnIssued(deviceId);
        return restResult;
    }

    @ApiOperation("单个常客下发")
    @GetMapping("addSingle")
    public RestResult<String> addSingle(Integer id) {
        RestResult<String> restResult = userWhitelistService.addSingle(getUserId(), id);
        return restResult;
    }

}
