package com.huimv.receive.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.entity.BillPcr;
import com.huimv.receive.entity.Configuration;
import com.huimv.receive.service.IBillPcrService;
import com.huimv.receive.service.IConfigurationService;
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.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * pcr 表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@RestController
@RequestMapping("/bill-pcr")
@CrossOrigin
public class BillPcrController {

    @Autowired
    private IBillPcrService pcrService;
    @Autowired
    private IConfigurationService configurationService;

    @RequestMapping("/listPcr")
    public Result listPcr(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return pcrService.list(httpServletRequest, paramsMap);
    }
    @RequestMapping("/listById")
    public Result listById(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return pcrService.listById(httpServletRequest, paramsMap);
    }
    @RequestMapping("/editPcr")
    public Result editPcr(HttpServletRequest httpServletRequest,
                          @RequestParam("farmId") String farmId,
                          @RequestParam("id") String id,
                          @RequestParam("date") String date,
                          @RequestParam("result") String result,
                          @RequestParam(value = "img1",required = false) MultipartFile img1,
                          @RequestParam(value = "img2",required = false) MultipartFile img2,
                          @RequestParam(value = "img3",required = false) MultipartFile img3,
                          @RequestParam(value = "picture1",required = false) MultipartFile picture1,
                          @RequestParam(value = "picture2",required = false) MultipartFile picture2,
                          @RequestParam(value = "picture3",required = false) MultipartFile picture3) throws ParseException, IOException {
        return pcrService.edit(httpServletRequest, farmId,id,date,result,img1,img2,img3,picture1,picture2,picture3);
    }

    @RequestMapping("/editBatch")
    public Result editBatch(HttpServletRequest httpServletRequest,
                          @RequestParam("farmId") String farmId,
                          @RequestParam("ids") String ids,
                          @RequestParam("date") String date,
                          @RequestParam("result") String result,
                          @RequestParam(value = "img1",required = false) MultipartFile img1,
                          @RequestParam(value = "img2",required = false) MultipartFile img2,
                          @RequestParam(value = "img3",required = false) MultipartFile img3,
                          @RequestParam(value = "picture1",required = false) MultipartFile picture1,
                          @RequestParam(value = "picture2",required = false) MultipartFile picture2,
                          @RequestParam(value = "picture3",required = false) MultipartFile picture3) throws ParseException, IOException {
        return pcrService.editBatch(httpServletRequest,farmId,ids,date,result,img1,img2,img3,picture1,picture2,picture3);
    }
    @Autowired
    private IBillPcrService billPcrService;

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

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

    //后台
    @PostMapping("/pcrWhole")
    public Result pcrWhole(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        return billPcrService.pcrWhole(httpServletRequest, paramsMap);
    }

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

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

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

    @GetMapping("/printPcr")
    public void printPcr(HttpServletResponse response, @RequestParam(name = "ids") String ids) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("ids", ids);
        billPcrService.printPcr(response, map);
        String path = "/opt/huatong/pcr.xls";
        PdfUtil.returnPdfStream3(response, path, "pcr详情列表");
    }
    @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) {
            billPcrService.removeById(s);
        }
        return new Result(10000,"删除成功！",true);
    }
    @PostMapping("/editTime")
    public Result editTime(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        String time = paramsMap.get("time");
        String farmId = paramsMap.get("farmId");
        QueryWrapper<Configuration> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        Configuration configuration = configurationService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(configuration)) {
            Configuration configuration1 = new Configuration();
            configuration1.setClean(2);
            configuration1.setDry(2);
            configuration1.setFarmId(farmId);
            configuration1.setPcr(Integer.parseInt(time));
            configurationService.save(configuration1);
            return new Result(10000, "添加成功！", true);
        }
        configuration.setPcr(Integer.parseInt(time));
        configurationService.updateById(configuration);
        return new Result(10000,"保存成功！",true);
    }

    @PostMapping("/listConfiguration")
    public Result listConfiguration(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        return new Result(ResultCode.SUCCESS,configurationService.getOne(new QueryWrapper<Configuration>().eq("farm_id", farmId)));
    }
    @PostMapping("/pcrList")
    public Result PcrList(HttpServletRequest httpServletRequest,@RequestBody Map<String, String> paramsMap){
        return billPcrService.PcrList(httpServletRequest, paramsMap);
    }
}
