package com.yx.face.controller.admin;


import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
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.UserControlEarlyWarningDTO;
import com.yx.face.model.dto.UserWhitelistDTO;
import com.yx.face.model.excel.UserControlEarlyWarningExcel;
import com.yx.face.model.excel.UserWhitelistExcel;
import com.yx.face.model.search.UserControlEarlyWarningDelete;
import com.yx.face.model.search.UserControlEarlyWarningSearch;
import com.yx.face.model.search.UserWhitelistSearch;
import com.yx.face.model.vo.AdminVO;
import com.yx.face.model.vo.UserControlEarlyWarningVO;
import com.yx.face.model.vo.UserWhitelistVO;
import com.yx.face.service.AdminService;
import com.yx.face.service.UserControlEarlyWarningListService;
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.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 预警人员 前端控制器
 * </p>
 *
 * @author wxg
 * @since 2022-01-24
 */
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "B 预警人员 API")
@RestController
@RequestMapping("admin/userControlEarlyWarningList")
public class UserControlEarlyWarningListController {

    private final UserControlEarlyWarningListService userControlEarlyWarningListService;
    private final AdminService adminService;


    @ApiOperation("新增或修改预警人员")
    @PostMapping("addOrUpdate")
    public RestResult<Boolean> addOrUpdate(@RequestBody @Valid UserControlEarlyWarningDTO userControlEarlyWarningDTO) {
        if(userControlEarlyWarningDTO.getAdminId() == null) throw new ServiceException("账户id不能为空");

        AdminVO info = adminService.getInfo(userControlEarlyWarningDTO.getAdminId());
        if(info == null)throw new ServiceException("没有查询到账号信息");
        if(info.getType() != 5)throw new ServiceException("预警人员只能添加到区级账号下");
        Integer insert = userControlEarlyWarningListService.addOrUpdate(userControlEarlyWarningDTO);
        if (insert == 1) return RestResponse.ok(true);
        else return RestResponse.error("操作失败");
    }

    @ApiOperation("预警人员的批量导入")
    @PostMapping("userControlEarlyWarningListExcel")
    public RestResult<List<String>> userControlEarlyWarningListExcel(@RequestParam("file") MultipartFile excelFile , @RequestParam("adminId")Integer adminId ) {
        AdminVO info = adminService.getInfo(adminId);
        if(info == null)throw new ServiceException("没有查询到账号信息");
        if(info.getType() != 5)throw new ServiceException("预警人员只能添加到区级账号下");

        ExcelListenerUtils listener = new ExcelListenerUtils();  //ExcelListener
        try {
            EasyExcel.read(excelFile.getInputStream(), UserControlEarlyWarningExcel.class, listener).sheet(0).doReadSync();  //ExcelModel 上面创建的实体类
        } catch (IOException e) {
            e.printStackTrace();
        }
        List<Object> datas = listener.getDatas(); //把拿到的数据处理到集合里去(根据所需自行修改)
        return userControlEarlyWarningListService.userControlEarlyWarningListExcel(datas,adminId);
    }

    @ApiOperation("删除预警人员")
    @GetMapping("delete")
    public RestResult<Boolean> delete(@RequestParam("controlEarlyWarningId") Integer controlEarlyWarningId) {
        Integer insert = userControlEarlyWarningListService.delete(controlEarlyWarningId);
        if (insert == 1) return RestResponse.ok(true);
        else return RestResponse.error("删除失败");
    }

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

    @ApiOperation("获取布控单位列表(只有布控人单位)")
    @GetMapping("getControlUnitList")
    public RestResult<List<UserControlEarlyWarningVO>> getControlUnitList() {
        List<UserControlEarlyWarningVO> list = userControlEarlyWarningListService.getControlUnitList();
        return RestResponse.ok(list);
    }
    @ApiOperation("批量删除预警人员")
    @PostMapping("batchDelete")
    public RestResult<Boolean> batchDelete(@RequestBody UserControlEarlyWarningDelete userControlEarlyWarningDelete) {
        Integer insert = userControlEarlyWarningListService.batchDelete(userControlEarlyWarningDelete);
        if (insert >= 1) return RestResponse.ok(true);
        else return RestResponse.error("该条件删除的数据量为0");
    }
    private void reBuildQuery(@RequestBody RestDTO<UserControlEarlyWarningSearch> dto) {
        UserControlEarlyWarningSearch object = dto.getData();
        if (object == null) {
            object = new UserControlEarlyWarningSearch();
        }
        dto.setData(object);
    }
}
