package com.huimv.receive.controller;


import com.huimv.receive.common.utils.PdfUtil;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.service.IBillLuggageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-07-24
 */
@RestController
@RequestMapping("/bill-luggage")
@CrossOrigin
public class BillLuggageController {
    @Autowired
    private IBillLuggageService billLuggageService;

    @PostMapping("/addLuggage")
    public Result addLuggage(HttpServletRequest httpServletRequest,
                             @RequestParam("farmId") String farmId,
                             @RequestParam("userName") String userName,
                             @RequestParam("phone") String phone,
                             @RequestParam("luggageDate") String luggageDate,
                             @RequestParam("luggageLocation") String luggageLocation,
                             @RequestParam("luggageLocationId") String luggageLocationId,
                             @RequestParam("luggageNum") Integer luggageNum,
                             @RequestParam(value = "img1",required = false) MultipartFile img,
                             @RequestParam(value = "img2",required = false) MultipartFile img2,
                             @RequestParam(value = "img3",required = false) MultipartFile img3,
                             @RequestParam(value = "img4",required = false) MultipartFile img4,
                             @RequestParam(value = "img5",required = false) MultipartFile img5) throws IOException {
        return billLuggageService.addLuggage(httpServletRequest,farmId,userName,phone,luggageDate,luggageLocation,luggageLocationId,luggageNum,img,img2,img3,img4,img5);
    }

    @PostMapping("/listPersonalLuggage")
    public Result listPersonalLuggage(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap){
        return billLuggageService.listPersonalLuggage(httpServletRequest,paramsMap);
    }

    @PostMapping("/getDetail")
    public Result getDetail(@RequestBody Map<String, String> paramsMap){
        return new Result(ResultCode.SUCCESS,billLuggageService.getById(paramsMap.get("id")));
    }

    @PostMapping("/listLuggage")
    public Result listLuggage(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        return billLuggageService.listLuggage(httpServletRequest, paramsMap);
    }

    @PostMapping("/selectLuggage")
    public Result selectLuggage(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        return billLuggageService.selectLuggage(httpServletRequest, paramsMap);
    }
    @PostMapping("/delete")
    public Result delete(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        for (String s : split) {
            billLuggageService.removeById(s);
        }
        return new Result(10000,"删除成功！",true);
    }
    @GetMapping("/printLuggage")
    public void printLuggage(HttpServletResponse response, @RequestParam(name = "ids") String ids) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("ids", ids);
        billLuggageService.printLuggage(response, map);
        String path = "/opt/huatong/luggage.xls";
        PdfUtil.returnPdfStream3(response, path, "行李详情列表");
    }
}
