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

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.BigItem;
import com.youchain.basicdata.domain.BomAccount;
import com.youchain.basicdata.repository.BomAccountRepository;
import com.youchain.basicdata.service.BigItemService;
import com.youchain.basicdata.service.BomAccountService;
import com.youchain.basicdata.service.dto.BigItemDto;
import com.youchain.basicdata.service.dto.BigItemQueryCriteria;
import com.youchain.basicdata.service.dto.BomAccountDto;
import com.youchain.basicdata.service.dto.BomAccountQueryCriteria;
import com.youchain.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author baobinglin
* @date 2023-12-29
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "bigItem管理")
@Slf4j
@RequestMapping("/api/bigItem")
public class BigItemController {

    private final BigItemService bigItemService;
    private final BomAccountService bomAccountService;
    private final BomAccountRepository bomAccountRepository;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportBigItem(HttpServletResponse response, BigItemQueryCriteria criteria) throws Exception {
        bigItemService.download(bigItemService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询bigItem")
    @ApiOperation("查询bigItem")
    @AnonymousAccess
    public ResponseEntity<Object> queryBigItem(BigItemQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(bigItemService.queryAll(criteria,pageable),HttpStatus.OK);
    }



    @GetMapping("/queryBigItemAll")
    @Log("查询bigItem全部")
    @ApiOperation("查询bigItem全部")
    @AnonymousAccess
    public ResponseEntity<Object> queryBigItemAll(BigItemQueryCriteria criteria){
        return new ResponseEntity<>(bigItemService.queryAll(criteria),HttpStatus.OK);
    }



    @GetMapping("/queryBigItemAllInBomAccount")
    @Log("查询在BomAccount里的BigItem数据并去重")
    @ApiOperation("查询在BomAccount里的BigItem数据并去重")
    @AnonymousAccess
    public ResponseEntity<Object> queryBigItemAllInBomAccount(){
        //BOM工位清单
        List<BomAccount> bomAccounts = bomAccountRepository.queryBomAccountsBIidOne();
        log.info("size:"+bomAccounts.size());
        //获取所有对象的BigItem
        List<BigItem> collect = bomAccounts.stream().map(BomAccount::getBigItem).collect(Collectors.toList());
//        List<BigItem> collect = bomAccountRepository.queryBigItemALl();
        return new ResponseEntity<>(collect,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增bigItem")
    @ApiOperation("新增bigItem")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> createBigItem(@Validated @RequestBody BigItem resources){
        resources.setDeptId(UserUtils.getDept().getId());
        return new ResponseEntity<>(bigItemService.create(resources),HttpStatus.CREATED);
    }
    @PostMapping("/getBigItem")
    @Log("加载BigItem下拉框")
    @AnonymousAccess
    public ResponseEntity<Object> getBigItem(@RequestBody(required=false)  String type){
        return new ResponseEntity<>(bigItemService.getBigItem("1"),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改bigItem")
    @ApiOperation("修改bigItem")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> updateBigItem(@Validated @RequestBody BigItem resources){
        bigItemService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除bigItem")
    @ApiOperation("删除bigItem")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> deleteBigItem(@RequestBody Long[] ids) {
        bigItemService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}