package com.securitycontrol.background.controller;

import com.securitycontrol.background.service.HumanService;
import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.web.controller.BaseController;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.core.web.page.TableDataInfo;
import com.securitycontrol.common.log.annotation.Log;
import com.securitycontrol.common.log.enums.OperationType;
import com.securitycontrol.entity.background.dto.ParamDto;
import com.securitycontrol.entity.background.vo.HumanManageVo;
import com.securitycontrol.entity.background.vo.UserAccessVo;
import com.securitycontrol.entity.system.base.dto.ProDto;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 人员管理
 *
 * @author jsk
 */
@RequestMapping(value = "/back/personnel/")
@RestController
public class HumanManageController extends BaseController {

    @Resource(name = "HumanService")
    private HumanService service;

    @ApiOperation(value = "获取人员列表")
    @GetMapping("getHumanLists")
    @Log(title = "人员管理", menu = "人员管理->人员管理", grade = OperationType.QUERY_BUSINESS, details = "查询人员", type = "业务日志")
    public TableDataInfo getHumanLists(ParamDto dto) {
        startPage();
        List<HumanManageVo> list = new ArrayList<>();
        list = service.getHumanLists(dto);
        return getDataTable(list);
    }

    @ApiOperation(value = "新增人员")
    @PostMapping("addPersonnel")
    @Log(title = "人员管理", menu = "人车管理->人员管理", grade = OperationType.ADD_BUSINESS, details = "新增人员", type = "业务日志")
    public AjaxResult addPersonnel(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file, String params) {
        return service.addOrUpdatePersonnel(file, params);
    }

    @ApiOperation(value = "修改人员")
    @PostMapping("editPersonnel")
    @Log(title = "人员管理", menu = "人车管理->人员管理", grade = OperationType.UPDATE_BUSINESS, details = "修改人员", type = "业务日志")
    public AjaxResult editPersonnel(HttpServletRequest request, @RequestParam(value = "file", required = false) MultipartFile file, String params) {
        return service.addOrUpdatePersonnel(file, params);
    }

    @ApiOperation(value = "人员详情")
    @GetMapping("getPersonnelById")
    public AjaxResult getPersonnelById(ParamDto dto) {
        return service.getPersonnelById(dto);
    }

    @ApiOperation(value = "删除人员")
    @PostMapping("delPersonnel")
    @Log(title = "人员管理", menu = "人车管理->人员管理", grade = OperationType.DELETE_BUSINESS, details = "删除人员", type = "业务日志")
    public AjaxResult delPersonnelById(@RequestBody ParamDto dto) {
        return service.delPersonnelById(dto);
    }

    @ApiOperation(value = "人员照片预览")
    @GetMapping("viewPersonnelFile")
    public AjaxResult viewPersonnelFile(ParamDto dto) {
        return service.viewPersonnelFile(dto);
    }

    @ApiOperation(value = "获取人员出入记录")
    @GetMapping("getPersonnelAccessLists")
    @Log(title = "人员管理", menu = "人员管理->人员管理", grade = OperationType.QUERY_BUSINESS, details = "查询人员出入记录", type = "业务日志")
    public TableDataInfo getPersonnelAccessLists(ParamDto dto) {
        if(StringUtils.isNotEmpty(dto.getTime())){
            String[] timeArr = dto.getTime().split(" - ");
            dto.setStartTime(timeArr[0]);
            dto.setEndTime(timeArr[1]);
        }else {
            dto.setStartTime(DateTimeHelper.getNowDate());
            dto.setEndTime(DateTimeHelper.getNowDate());
        }
        startPage();
        List<UserAccessVo> list = new ArrayList<>();
        list = service.getPersonnelAccessLists(dto);
        return getDataTable(list);
    }

    @ApiOperation(value = "绑定安全帽")
    @PostMapping("bindDev")
    @Log(title = "人员管理", menu = "人车管理->人员管理", grade = OperationType.ADD_BUSINESS, details = "绑定安全膜", type = "业务日志")
    public AjaxResult bindDev(@RequestBody ParamDto dto) {
        return service.bindDev(dto);
    }

    @ApiOperation(value = "查看绑定安全帽")
    @GetMapping("getBindDetail")
    public AjaxResult getBindDetail(ParamDto dto) {
        return service.getBindDetail(dto);
    }

    @ApiOperation(value = "")
    @GetMapping("delBind")
    @Log(title = "delBind", menu = "人车管理->人员管理", grade = OperationType.ADD_BUSINESS, details = "绑定安全膜", type = "业务日志")
    public AjaxResult delBind(ParamDto dto) {
        return service.delBind(dto);
    }

}
