package com.tykj.dev.device.allot.controller;

import com.tykj.dev.config.swagger.AutoDocument;
import com.tykj.dev.device.allot.subject.vo.AllotBillSaveVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author dengdiyi
 */
@RestController
@RequestMapping(value = "/batchAllot")
@AutoDocument
@Api(tags = "装备配发模块", description = "装备配发接口")
@Slf4j
public class BatchAllotController {

    @Autowired
    private AllotBillController allotBillController;

    @ApiOperation(value = "发起批量配发(type 1:签字配发，2:签章配发)")
    @PostMapping(value = "/createBatchAllot/{type}/{userId}")
    @Transactional(rollbackFor = Exception.class)
    public ResponseEntity createBatchAllot(@PathVariable("type") int type, @PathVariable("userId") int userId ,@RequestBody List<AllotBillSaveVo> allotBillSaveVoList){
        if (type==1){
            allotBillSaveVoList.forEach(allotBillSaveVo -> allotBillController.addAllotBill(allotBillSaveVo));
        }
        else {
            List<Integer> taskIds = new ArrayList<>();
            //批量保存草稿
            allotBillSaveVoList.forEach(allotBillSaveVo -> {
                String s = (String)allotBillController.saveAllotBill(allotBillSaveVo).getBody();
                taskIds.add(Integer.valueOf(s.substring(4)));
            });
            //批量申请签章
            taskIds.forEach(integer -> allotBillController.applySign(integer,userId));
        }
        return ResponseEntity.ok("发起成功");
    }
}
