package com.youchain.businessdata.rest;

import com.youchain.RequestData.*;
import com.youchain.annotation.AnonymousAccess;
import com.youchain.annotation.Log;
import com.youchain.basicdata.domain.BigBom;
import com.youchain.basicdata.domain.BigItem;
import com.youchain.basicdata.domain.Item;
import com.youchain.basicdata.repository.BigBomRepository;
import com.youchain.basicdata.repository.BigItemRepository;
import com.youchain.basicdata.repository.ItemRepository;
import com.youchain.basicdata.service.BigBomService;
import com.youchain.businessdata.domain.Gd;
import com.youchain.businessdata.domain.GdDetail;
import com.youchain.businessdata.service.GdDetailService;
import com.youchain.businessdata.service.GdService;
import com.youchain.businessdata.service.dto.TaskQueryCriteria;
import com.youchain.exception.BadRequestException;
import com.youchain.exception.handler.ApiResult;
import com.youchain.modules.system.service.DeptService;
import com.youchain.utils.BaseStatus;
import com.youchain.utils.BizStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequiredArgsConstructor
@Api(tags = "mes接口管理")
@RequestMapping("/api/mes")
@Slf4j
public class MesController {
    private final GdService gdService;
    private final GdDetailService gdDetailService;
    private final DeptService deptService;
    private final BigBomService bigBomService;
    private final ItemRepository itemRepository;
    private final BigBomRepository bigBomRepository;
    private final BigItemRepository bigItemRepository;
    @PostMapping("/yclbl")
    @Log("mes-原材料备料")
    @ApiOperation("mes-原材料备料")
    @AnonymousAccess
    public ResponseEntity<Object> yclbl(@Validated @RequestBody Yclbl yclbl){
        /* 添加工单单头*/
        Gd gd = gdService.save(yclbl.getOrderNo(),yclbl.getTaskCode(),yclbl.getStation());
        if (yclbl.getBlzc()!=null){
            String orderType = BaseStatus.GD_TYPE_CT;
            Set<ZcData> blzc = yclbl.getBlzc();
            for (ZcData zcData : blzc) {
                /* 验证成套 物料*/
                BigItem bigItem=verifiedBigItem(zcData.getCompleteCode(),zcData.getCompleteName());
                Set<ItemDate> itemDates=zcData.getBlzcmx();
                for (ItemDate itemDate : itemDates) {
                    /* 验证单品 物料*/
                    Item item=verifiedItem(itemDate.getItemCode(),itemDate.getItemName());
                    /* 验证BOM*/
                    BigBom bigBom=verifiedBigBom(bigItem,item,itemDate.getItemQty());
                    /* 添加工单明细*/
                    GdDetail gdDetail = gdDetailService.save(gd,orderType,bigItem,zcData.getCompleteQty(),item,itemDate.getItemQty());
                }
            }
        }
        if (yclbl.getBlzcmx()!=null){
            String orderType = BaseStatus.GD_TYPE_DP;
            Set<ItemDate> itemDateList=yclbl.getBlzcmx();
            for (ItemDate itemDate:itemDateList){
                /* 验证单品物料*/
                Item item=verifiedItem(itemDate.getItemCode(),itemDate.getItemName());
                /* 添加工单明细*/
                GdDetail gdDetail = gdDetailService.save(gd,orderType,null,null,item,itemDate.getItemQty());
            }
        }
        /* 生成 出库明细*/
        gdService.addPickDetail(gd);
        ApiResult apiResult=ApiResult.result(200,"成功",null);
        return new ResponseEntity<>(apiResult, HttpStatus.valueOf(apiResult.getStatus()));
    }


    @PostMapping("/sjBom")
    @Log("mes-散件BOM同步")
    @ApiOperation("mes-散件BOM同步")
    @AnonymousAccess
    public ResponseEntity<Object> sjBom(@Validated @RequestBody SjBom sjBom){

        return new ResponseEntity<>(ApiResult.result(200,"成功",null), HttpStatus.OK);
    }
    @PostMapping("/zpjlZj")
    @Log("mes-其他整件叫料")
    @ApiOperation("mes-其他整件叫料")
    @AnonymousAccess
    public ResponseEntity<Object> zpjlZj(@Validated @RequestBody ZpjlZj zpjlZj){

        return new ResponseEntity<>(ApiResult.result(200,"成功",null), HttpStatus.OK);
    }
    @PostMapping("/zpjlCt")
    @Log("mes-成套叫料")
    @ApiOperation("mes-成套叫料")
    @AnonymousAccess
    public ResponseEntity<Object> zpjlCt(@Validated @RequestBody ZpjlCt zpjlCt){

        return new ResponseEntity<>(ApiResult.result(200,"成功",null), HttpStatus.OK);
    }
    @PostMapping("/zpjlJj")
    @Log("mes-紧急叫料")
    @ApiOperation("mes-紧急叫料")
    @AnonymousAccess
    public ResponseEntity<Object> zpjlJj(@Validated @RequestBody ZpjlJj zpjlJj){

        return new ResponseEntity<>(ApiResult.result(200,"成功",null), HttpStatus.OK);
    }
    @PostMapping("/transTask")
    @Log("mes-搬运任务")
    @ApiOperation("mes-搬运任务")
    @AnonymousAccess
    public ResponseEntity<Object> transTask(@Validated @RequestBody TransTask transTask){

        return new ResponseEntity<>(ApiResult.result(200,"成功",null), HttpStatus.OK);
    }

    /** 验证成套物料*/
    private BigItem verifiedBigItem(String completeCode, String completeName) {
        List<BigItem> bigItemList = bigItemRepository.findRepeat(completeCode,completeName);
        if (bigItemList.size()>0){
            return bigItemList.get(0);
        }else {
            throw new BadRequestException(HttpStatus.INTERNAL_SERVER_ERROR, "未维护成套物料"+completeCode);
        }
    }
    private Item verifiedItem(String itemCode, String itemName) {
        List<Item> itemList = itemRepository.findRepeat(itemCode,itemName);
        if (itemList.size()>0){
            return itemList.get(0);
        }else {
            throw new BadRequestException(HttpStatus.INTERNAL_SERVER_ERROR, "未维护单品物料"+itemCode);
        }
    }

    private BigBom verifiedBigBom(BigItem bigItem, Item item, Double itemQty) {
        List<BigBom> bigBomList = bigBomRepository.findRepeat(bigItem.getId(),item.getId());
        if (bigBomList.size()>0){
            return bigBomList.get(0);
        }else {
            // 添加BOM
            BigBom bigBom= new BigBom();
            bigBom.setBigItem(bigItem);
            bigBom.setItem(item);
            bigBom.setQuantity(itemQty);
            bigBom.setDept(deptService.isDept());
            bigBomRepository.save(bigBom);
            return bigBom;
        }
    }
}
