/*
*  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.dmAccessTraffic.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dmAccessTraffic.domain.DmAccessTraffic;
import me.zhengjie.modules.dmAccessTraffic.service.DmAccessTrafficService;
import me.zhengjie.modules.dmAccessTraffic.service.dto.DmAccessTrafficDto;
import me.zhengjie.modules.dmAccessTraffic.service.dto.DmAccessTrafficQueryCriteria;
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.utils.SecurityUtils;
import me.zhengjie.ykt.domain.DmRoomDevice;
import me.zhengjie.ykt.service.DmRoomDeviceService;
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.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author zhirong
* @date 2021-11-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "api/dmAccessTraffic管理")
@RequestMapping("/api/dmAccessTraffic")
public class DmAccessTrafficController {

    private final DmAccessTrafficService dmAccessTrafficService;
    private final DmRoomDeviceService dmRoomDeviceService;
    private final UserService userService;

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

    @GetMapping
    @Log("查询api/dmAccessTraffic")
    @ApiOperation("查询api/dmAccessTraffic")
    @PreAuthorize("@el.check('dmAccessTraffic:list')")
    public ResponseEntity<Object> query(DmAccessTrafficQueryCriteria 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.setUserNumber(userDto.getId());
        }

        Map<String,Object> map = dmAccessTrafficService.queryAll(criteria,pageable);
        List<DmAccessTrafficDto> array = JSONArray.parseArray(JSONArray.toJSONString(map.get("content")),DmAccessTrafficDto.class);
        List<DmAccessTrafficDto> newArray = new ArrayList<>();
        for(DmAccessTrafficDto dmAccessTrafficDto : array){
            String roomDevice = dmAccessTrafficDto.getRoomDevice();
            DmRoomDevice dmRoomDevice = dmRoomDeviceService.findByCode(roomDevice).get(0);
            dmAccessTrafficDto.setDmRoomDevice(dmRoomDevice);
            newArray.add(dmAccessTrafficDto);
        }
        map.put("content",newArray);

        return new ResponseEntity<>(map,HttpStatus.OK);
    }

    @GetMapping("/getAll")
    @Log("查询api/dmAccessTraffic")
    @ApiOperation("查询api/dmAccessTraffic")
    @PreAuthorize("@el.check('dmAccessTraffic:list')")
    public ResponseEntity<Object> queryAll(DmAccessTrafficQueryCriteria criteria){
        return new ResponseEntity<>(dmAccessTrafficService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping(value = "/homePageYkt")
    @Log("首页一卡通使用情况")
    @ApiOperation("首页一卡通使用情况")
    public ResponseEntity<Object> homePageYkt(){
        return new ResponseEntity<>(dmAccessTrafficService.homePageYkt(),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增api/dmAccessTraffic")
    @ApiOperation("新增api/dmAccessTraffic")
    @PreAuthorize("@el.check('dmAccessTraffic:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DmAccessTraffic resources) throws IOException, ParseException {
        return new ResponseEntity<>(dmAccessTrafficService.create(resources),HttpStatus.CREATED);
    }

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

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