/*
*  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 com.youchain.user.rest;

import cn.hutool.db.PageResult;
import com.youchain.annotation.Log;
import com.youchain.businessdata.inputJson.buttenJson.ElTableConfigInput;
import com.youchain.user.domain.ElTableConfig;
import com.youchain.user.repository.ElTableConfigRepository;
import com.youchain.user.service.ElTableConfigService;
import com.youchain.user.service.dto.ElTableConfigDto;
import com.youchain.user.service.dto.ElTableConfigQueryCriteria;
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.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author DengJiangKun
* @date 2024-01-25
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "elTableConfig管理")
@RequestMapping("/api/elTableConfig")
public class ElTableConfigController {

    private final ElTableConfigService elTableConfigService;
    private final ElTableConfigRepository elTableConfigRepository;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('elTableConfig:list')")
    public void exportElTableConfig(HttpServletResponse response, ElTableConfigQueryCriteria criteria) throws Exception {
        elTableConfigService.download(elTableConfigService.queryAll(criteria), response);
    }

    @GetMapping("/queryByVueUrl")
    @Log("查询配置数据")
    @ApiOperation("查询配置数据")
    @PreAuthorize("@el.check('elTable:queryByVueUrl')")
    public ResponseEntity<List<ElTableConfig>> queryElTableData(@RequestParam("vueUrl") String vueUrl){
        return new ResponseEntity<List<ElTableConfig>>(elTableConfigRepository.queryByVueUrl(vueUrl),HttpStatus.OK);
    }


    @GetMapping("/elTableConfigAllMap")
    @Log("查询展示表的配置的map")
    @ApiOperation("查询展示表的配置的map")
    @PreAuthorize("@el.check('elTableConfig:elTableConfigAllMap')")
    public ResponseEntity<Map<String, List<ElTableConfig>>> elTableConfigAllMap(ElTableConfigQueryCriteria criteria){
        Map<String, List<ElTableConfig>> stringListMap = new HashMap<>();
        List<ElTableConfig> elTableConfigDtos = elTableConfigRepository.queryAllDataS();
        //key
        String url = "";
        //list
        List<ElTableConfig> list = new ArrayList<>();
        for (ElTableConfig etc : elTableConfigDtos) {
            if (url.equals(etc.getVueUrl())){
                //向list加数据
                list.add(etc);
            }else {
                if (!list.isEmpty()){
                    //向map中存数据
                    stringListMap.put(url,list);
                }
                url = etc.getVueUrl();
                list = new ArrayList<>();
                list.add(etc);
            }
        }
        //最后一次的数据
        if (!list.isEmpty()){
            //向map中存数据
            stringListMap.put(url,list);
        }
        return new ResponseEntity<>(stringListMap,HttpStatus.OK);
    }


    @GetMapping
    @Log("查询elTableConfig")
    @ApiOperation("查询elTableConfig")
    @PreAuthorize("@el.check('elTableConfig:list')")
    public ResponseEntity<Object> queryElTableConfig(ElTableConfigQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(elTableConfigService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增elTableConfig")
    @ApiOperation("新增elTableConfig")
    @PreAuthorize("@el.check('elTableConfig:add')")
    public ResponseEntity<Object> createElTableConfig(@Validated @RequestBody ElTableConfig resources){
        return new ResponseEntity<>(elTableConfigService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改elTableConfig")
    @ApiOperation("修改elTableConfig")
    @PreAuthorize("@el.check('elTableConfig:edit')")
    public ResponseEntity<Object> updateElTableConfig(@Validated @RequestBody ElTableConfig resources){
        elTableConfigService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除elTableConfig")
    @ApiOperation("删除elTableConfig")
    @PreAuthorize("@el.check('elTableConfig:del')")
    public ResponseEntity<Object> deleteElTableConfig(@RequestBody Long[] ids) {
        elTableConfigService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PostMapping("/updateElTableConfigS")
    @Log("批量修改数据")
    @ApiOperation("批量修改数据")
    @PreAuthorize("@el.check('elTableConfig:updateElTableConfigS')")
    public ResponseEntity<Object> updateElTableConfigS(@Validated @RequestBody ElTableConfigInput resources){
        List<ElTableConfig> updateElTableConfigDtoS = resources.getUpdateElTableConfigDtoS();
        if (!updateElTableConfigDtoS.isEmpty()){
            int num = 0;
            for (ElTableConfig etc : updateElTableConfigDtoS) {
                etc.setSort(++num);
                elTableConfigService.update(etc);
            }
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

}