/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.dm.dmVisitorsUser.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dm.dmVisitorsUser.domain.DmVisitorsUser;
import me.zhengjie.modules.dm.dmVisitorsUser.service.DmVisitorsUserService;
import me.zhengjie.modules.dm.dmVisitorsUser.service.dto.DmVisitorsUserQueryCriteria;
import me.zhengjie.modules.dmuser.domain.DmUser;
import me.zhengjie.modules.dmuser.service.mapstruct.DmUserMapper;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.modules.system.service.dto.RoleSmallDto;
import me.zhengjie.modules.system.service.dto.UserDto;
import me.zhengjie.modules.system.service.mapstruct.UserMapper;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author juan
* @date 2021-11-19
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "api/dmVisitorsUser管理")
@RequestMapping("/api/dmVisitorsUser")
public class DmVisitorsUserController {

    private final DmVisitorsUserService dmVisitorsUserService;
    private final UserService userService;
    private final DmUserMapper userMapper;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dmVisitorsUser:list')")
    public void download(HttpServletResponse response, DmVisitorsUserQueryCriteria criteria) throws IOException {
        dmVisitorsUserService.download(dmVisitorsUserService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询api/dmVisitorsUser")
    @ApiOperation("查询api/dmVisitorsUser")
    @PreAuthorize("@el.check('dmVisitorsUser:list')")
    public ResponseEntity<Object> query(DmVisitorsUserQueryCriteria criteria, Pageable pageable){
        UserDto userDto = userService.findByName(SecurityUtils.getCurrentUsername());
        List<Integer> levels = new ArrayList<>();
        for(RoleSmallDto roleSmallDto : userDto.getRoles()){
            levels.add(roleSmallDto.getLevel());
        }
        if(!levels.contains(1)){
            criteria.setDmUser(userMapper.toEntity(userDto.getDmUser()));
        }
        return new ResponseEntity<>(dmVisitorsUserService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增api/dmVisitorsUser")
    @ApiOperation("新增api/dmVisitorsUser")
    @PreAuthorize("@el.check('dmVisitorsUser:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmVisitorsUser resources){
        return new ResponseEntity<>(dmVisitorsUserService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改api/dmVisitorsUser")
    @ApiOperation("修改api/dmVisitorsUser")
    @PreAuthorize("@el.check('dmVisitorsUser:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DmVisitorsUser resources){
        dmVisitorsUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除api/dmVisitorsUser")
    @ApiOperation("删除api/dmVisitorsUser")
    @PreAuthorize("@el.check('dmVisitorsUser:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) {
        dmVisitorsUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}