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

import com.alibaba.fastjson.JSONArray;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.dm.dmAttendanceRecord.domain.DmAttendanceRecord;
import me.zhengjie.modules.dm.dmAttendanceRecord.repository.DmAttendanceRecordRepository;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.DmAttendanceRecordService;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.dto.DmAttendanceRecordDto;
import me.zhengjie.modules.dm.dmAttendanceRecord.service.dto.DmAttendanceRecordQueryCriteria;
import me.zhengjie.modules.dmAccessTraffic.service.dto.DmAccessTrafficDto;
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.PageUtil;
import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.ykt.domain.DmRoomDevice;
import me.zhengjie.ykt.service.DmRoomDeviceService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.data.jpa.provider.HibernateUtils;
import org.springframework.data.jpa.repository.Query;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
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.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletResponse;

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

    private final DmAttendanceRecordService dmAttendanceRecordService;
    private final DmAttendanceRecordRepository dmAttendanceRecordRepository;
    private final DmRoomDeviceService dmRoomDeviceService;
    private final UserService userService;

    @Autowired
    JdbcTemplate jdbcTemplate;

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

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

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

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

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

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

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