package com.huimv.receive.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.receive.common.utils.Result;
import com.huimv.receive.common.utils.ResultCode;
import com.huimv.receive.entity.BillPcr;
import com.huimv.receive.entity.BillSampling;
import com.huimv.receive.entity.vo.BillSamplingVo;
import com.huimv.receive.service.IBillPcrService;
import com.huimv.receive.service.IBillSamplingService;
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 java.io.IOException;
import java.text.ParseException;
import java.util.Map;

/**
 * <p>
 * 洗消表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-03-11
 */
@RestController
@RequestMapping("/bill-sampling")
@CrossOrigin
public class BillSamplingController {

    @Autowired
    private IBillSamplingService billSamplingService;
    @Autowired
    private IBillPcrService billPcrService;

    @RequestMapping("/listSampling")
    public Result listSampling(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return billSamplingService.list(httpServletRequest, paramsMap);
    }

    @RequestMapping("/listById")
    public Result listById(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return billSamplingService.listById(httpServletRequest, paramsMap);
    }

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

    @PostMapping("/getDetail")
    public Result getDetail(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap){
        String id = paramsMap.get("id");
        BillSampling billSampling = billSamplingService.getById(id);
        BillSamplingVo billSamplingVo = new BillSamplingVo();
        BeanUtil.copyProperties(billSampling,billSamplingVo);
        BillPcr billPcr = billPcrService.getOne(new QueryWrapper<BillPcr>().lambda().eq(BillPcr::getSamplingId, id));
        if (ObjectUtil.isNotEmpty(billPcr)){
            billSamplingVo.setPcrType(billPcr.getPcrType());
            billSamplingVo.setStatus(billPcr.getBillStatus());
        }
        return new Result(ResultCode.SUCCESS, billSamplingVo);
    }

    @PostMapping("/edit")
    public Result goodsCarClean(HttpServletRequest httpServletRequest, @RequestParam("id") String id,
                                @RequestParam("date") String data,
                                @RequestParam(value = "video1", required = false) String video1,
                                @RequestParam(value = "video2", required = false) String video2,
                                @RequestParam(value = "video3", required = false) String video3,
                                @RequestParam(value = "video4", required = false) String video4,
                                @RequestParam(value = "video5", required = false) String video5,
                                @RequestParam(value = "video6", required = false) String video6,
                                @RequestParam(value = "video7", required = false) String video7,
                                @RequestParam(value = "video8", required = false) String video8,
                                @RequestParam(value = "picture1", required = false) MultipartFile picture1,
                                @RequestParam(value = "picture2", required = false) MultipartFile picture2,
                                @RequestParam(value = "picture3", required = false) MultipartFile picture3,
                                @RequestParam("farmId") String farmId) throws IOException, ParseException {
        return billSamplingService.edit(httpServletRequest, farmId, id, data, video1,video2,video3,video4,video5,video6,video7,video8,picture1,picture2,picture3);
    }

    @RequestMapping("/listAll")
    public Result listAll(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return billSamplingService.listAll(httpServletRequest, paramsMap);
    }

    @PostMapping("/getVideo")
    public Result getVideo(@RequestParam(value = "video", required = true) MultipartFile video) throws IOException, ParseException {
        return billSamplingService.getVideo( video);
    }
}
