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

import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.ItemLis;
import com.youchain.basicdata.repository.ItemLisRepository;
import com.youchain.basicdata.repository.ItemRepository;
import com.youchain.businessdata.domain.PickKitPlan;
import com.youchain.businessdata.service.PickKitPlanService;
import com.youchain.businessdata.service.dto.PickKitPlanQueryCriteria;
import com.youchain.exception.BadRequestException;
import com.youchain.utils.BizStatus;
import com.youchain.utils.CodeUtils;
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.sql.Timestamp;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * @author DengJiangKun
 * @website https://eladmin.vip
 * @date 2024-02-28
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "pickKitPlan管理")
@RequestMapping("/api/pickKitPlan")
public class PickKitPlanController {

    private final PickKitPlanService pickKitPlanService;
    private final CodeUtils codeUtil;
    private final ItemLisRepository itemLisRepository;

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

    @GetMapping
    @Log("查询pickKitPlan")
    @ApiOperation("查询pickKitPlan")
    @AnonymousAccess
    public ResponseEntity<Object> queryPickKitPlan(PickKitPlanQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(pickKitPlanService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping("/queryPickKitPlanList")
    @Log("查询pickKitPlan")
    @ApiOperation("查询pickKitPlan")
    @AnonymousAccess
    public ResponseEntity<Object> queryPickKitPlanList(PickKitPlanQueryCriteria criteria) {
        return new ResponseEntity<>(pickKitPlanService.queryAll(criteria), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增pickKitPlan")
    @ApiOperation("新增pickKitPlan")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> createPickKitPlan(@Validated @RequestBody PickKitPlan resources) {
        if (resources.getPlanNumber() == null || resources.getPlanNumber().isEmpty()) {
            resources.setPlanNumber(codeUtil.getCode_yyMMdd("CT", 3));
        }
        resources.setPlanDate(new Timestamp(new Date().getTime()));
        String code=codeUtil.getCode_yyMMdd("CT", 3);
        Timestamp date=new Timestamp(new Date().getTime());
        List<ItemLis> itemLiss= itemLisRepository.findByBigItemCode(resources.getBigItem().getCode());
        if(itemLiss.size()<=0){
            throw new BadRequestException(resources.getBigItem().getCode()+"未找到对应的工位清单");
        }
        for(ItemLis itemLis :itemLiss){
            PickKitPlan pickKitPlan=new PickKitPlan();
            pickKitPlan.setPlanNumber(code);
            pickKitPlan.setPlanDate(date);
            pickKitPlan.setStatus(BizStatus.WCD);
            pickKitPlan.setOrderQty(resources.getOrderQty());
            pickKitPlan.setBigItem(resources.getBigItem());
            pickKitPlan.setWorkArea(itemLis.getRArea());
            pickKitPlanService.create(pickKitPlan);
        }

        return new ResponseEntity<>(HttpStatus.CREATED);
    }

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

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

    @PostMapping("/audit")
    @Log("成套计划审核")
    @ApiOperation("成套计划审核")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> audit(@RequestBody Long[] ids) {
        pickKitPlanService.audit(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
    @PostMapping("/qxAudit")
    @Log("成套计划取消审核")
    @ApiOperation("成套计划取消审核")
    @PreAuthorize("@el.check('super:man')")
    public ResponseEntity<Object> qxAudit(@RequestBody Long id) {
        pickKitPlanService.qxAudit(id);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}