/*
*  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.domain.Item;
import com.youchain.basicdata.domain.Point;
import com.youchain.basicdata.repository.BomAccountRepository;
import com.youchain.basicdata.repository.PointRepository;
import com.youchain.basicdata.repository.TableConfigRepository;
import com.youchain.basicdata.service.BomAccountLogService;
import com.youchain.basicdata.service.BomAccountService;
import com.youchain.basicdata.service.PointService;
import com.youchain.basicdata.service.dto.BomAccountDto;
import com.youchain.basicdata.service.dto.BomAccountQueryCriteria;
import com.youchain.basicdata.service.dto.TableConfigDto;
import com.youchain.basicdata.service.dto.TableConfigQueryCriteria;
import com.youchain.basicdata.service.impl.TableConfigServiceImpl;
import com.youchain.basicdata.service.mapstruct.BomAccountMapper;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.SpringContextHolder;
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.List;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author baobinglin
* @date 2024-01-02
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "bomAccount管理")
@RequestMapping("/api/bomAccount")
public class BomAccountController {

    private final BomAccountService bomAccountService;
    private final BomAccountLogService bomAccountLogService;
    private final BomAccountMapper bomAccountMapper;
    private final PointRepository pointRepository;
    private final PointService pointService;

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

    @GetMapping
    @Log("查询bomAccount")
    @ApiOperation("查询bomAccount")
    @AnonymousAccess
    public ResponseEntity<Object> queryBomAccount(BomAccountQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(bomAccountService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/queryBomAccountList")
    @Log("查询bomAccount全部")
    @ApiOperation("查询bomAccount全部")
    @AnonymousAccess
    public ResponseEntity<Object> queryBomAccountList(BomAccountQueryCriteria criteria){
        return new ResponseEntity<>(bomAccountService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping(value = "/queryBigItemByStationType/{stationType}")
    @Log("根据工位查询bomAccount")
    @ApiOperation("根据工位查询bomAccount")
    @AnonymousAccess
    public ResponseEntity<Object> queryBomAccountByStationType(@PathVariable("stationType") String stationType){
        List<BigItem> bigItems = bomAccountService.queryBigItemByType(stationType);
        return new ResponseEntity<>(bigItems,HttpStatus.OK);
    }

    @GetMapping(value = "/queryItemByArea/{areaId}")
    @Log("根据库区查询bomAccount品番")
    @ApiOperation("根据库区查询bomAccount品番")
    @AnonymousAccess
    public ResponseEntity<Object> queryItemByArea(@PathVariable("areaId") Long areaId){
        List<BomAccountDto> boms=bomAccountService.queryByArea(areaId);
        return new ResponseEntity<>(boms,HttpStatus.OK);
    }

    @PostMapping
    @Log("新增bomAccount")
    @ApiOperation("新增bomAccount")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> createBomAccount(@Validated @RequestBody BomAccount resources){
//        if(resources.getHPoint().getCode()==null){
//            resources.setHPoint(null);
//        }else{
//            Point point=pointRepository.findByCode(resources.getHPoint().getCode());
//            if (point == null) {
//                point = pointService.createPoint(resources.getHPoint().getCode(), "ZCKW", resources.getRArea(),null);
//            }
//            resources.setHPoint(point);
//        }
        if(resources.getZPoint().getCode()==null){
            resources.setZPoint(null);
        }else{
            Point point=pointRepository.findByCode(resources.getZPoint().getCode());
            if (point == null) {
                point = pointService.createPoint(resources.getZPoint().getCode(), BaseStatus.HCKW, resources.getRArea());
            }
            resources.setZPoint(point);
        }
        BomAccountDto bomAccountDto=bomAccountService.create(resources);
        bomAccountLogService.copyBomAccount(bomAccountDto.getId(),"add");
        return new ResponseEntity<>(bomAccountDto,HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改bomAccount")
    @ApiOperation("修改bomAccount")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> updateBomAccount(@Validated @RequestBody BomAccount resources){
//        if(resources.getHPoint().getCode()==null){
//            resources.setHPoint(null);
//        }else{
//            Point point=pointRepository.findByCode(resources.getHPoint().getCode());
//            if (point == null) {
//                point = pointService.createPoint(resources.getHPoint().getCode(), "ZCKW", resources.getRArea(),null);
//            }
//            resources.setHPoint(point);
//        }
        if(resources.getZPoint().getCode()==null){
            resources.setZPoint(null);
        }else{
            Point point=pointRepository.findByCode(resources.getZPoint().getCode());
            if (point == null) {
                point = pointService.createPoint(resources.getZPoint().getCode(), BaseStatus.HCKW, resources.getRArea());
            }
            resources.setZPoint(point);
        }
        bomAccountService.update(resources);
        bomAccountLogService.copyBomAccount(resources.getId(),"update");
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

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