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.baomidou.mybatisplus.core.toolkit.StringUtils;
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.BillClean;
import com.huimv.receive.entity.BillDry;
import com.huimv.receive.entity.BillGoodsInventory;
import com.huimv.receive.entity.Configuration;
import com.huimv.receive.entity.vo.BillCleanVo;
import com.huimv.receive.entity.vo.BillDryVo;
import com.huimv.receive.entity.vo.GoodsVo;
import com.huimv.receive.service.IBillCleanService;
import com.huimv.receive.service.IBillDryService;
import com.huimv.receive.service.IBillGoodsInventoryService;
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.Map;

/**
 * <p>
 * 洗消表 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@RestController
@RequestMapping("/bill-clean")
@CrossOrigin
public class BillCleanController {
    @Autowired
    private IBillCleanService billCleanService;
    @Autowired
    private IBillDryService billDryService;
    @Autowired
    private IBillGoodsInventoryService goodsInventoryService;

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

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

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

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

    @PostMapping("/getCleanAndDryDetail")
    public Result getCleanAndDryDetail(@RequestBody Map<String, String> paramsMap) {
        String type = paramsMap.get("type");
        if (StringUtils.isBlank(type)) {
            BillClean billClean = billCleanService.getById(paramsMap.get("id"));
            if (ObjectUtil.isEmpty(billClean.getImgUrl())) {
                billClean.setImgUrl("");
            }
            BillCleanVo billCleanVo = new BillCleanVo();
            BeanUtil.copyProperties(billClean, billCleanVo);
            billCleanVo.setType("");
            return new Result(ResultCode.SUCCESS, billCleanVo);
        }
        if ("1".equals(type)) {
            BillClean billClean = billCleanService.getById(paramsMap.get("id"));
            if (ObjectUtil.isEmpty(billClean.getImgUrl())) {
                billClean.setImgUrl("");
            }
            BillCleanVo billCleanVo = new BillCleanVo();
            BeanUtil.copyProperties(billClean, billCleanVo);
            billCleanVo.setType(type);
            return new Result(ResultCode.SUCCESS, billCleanVo);
        } else if ("2".equals(type)) {
            BillDry billDry = billDryService.getById(paramsMap.get("id"));
            if (ObjectUtil.isEmpty(billDry.getImgUrl())) {
                billDry.setImgUrl("");
            }
            BillDryVo billCleanVo = new BillDryVo();
            BeanUtil.copyProperties(billDry, billCleanVo);
            billCleanVo.setType(type);
            return new Result(ResultCode.SUCCESS, billCleanVo);
        } else {
            BillGoodsInventory goodsInventory = goodsInventoryService.getById(paramsMap.get("id"));
            if (ObjectUtil.isEmpty(goodsInventory.getImgUrl())) {
                goodsInventory.setImgUrl("");
            }
            GoodsVo billCleanVo = new GoodsVo();
            BeanUtil.copyProperties(goodsInventory, billCleanVo);
            billCleanVo.setType(type);
            return new Result(ResultCode.SUCCESS, billCleanVo);
        }

    }

    @Autowired
    private IBillCleanService cleanService;
    @Autowired
    private IConfigurationService configurationService;

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

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

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

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

//    @RequestMapping("/editClean")
//    public Result editClean(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
//    ) throws ParseException, IOException {
//        return cleanService.commitClean(httpServletRequest, farmId, id, date, result, img1,img2,img3);
//    }
//
//    @PostMapping("/carCommit")
//    public Result addAdmission(HttpServletRequest httpServletRequest, @RequestParam("id") String id,
//                               @RequestParam("data") String data,
//                               @RequestParam(value = "img1", required = false) MultipartFile img1,
//                               @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,
//                               @RequestParam("farmId") String farmId,
//                               @RequestParam("type") String type) throws IOException, ParseException {
//        return cleanService.carClean(httpServletRequest, farmId, id, data, img1, img2, img3, img4, img5, type);
//    }

    @PostMapping("/goodsCarClean")
    public Result goodsCarClean(HttpServletRequest httpServletRequest, @RequestParam("id") String id,
                                @RequestParam("date") String date,
                                @RequestParam(value = "img1", required = false) MultipartFile img1,
                                @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,
                                @RequestParam("farmId") String farmId,
                                @RequestParam("type") String type,
                                @RequestParam("result") String result) throws IOException, ParseException {
        return cleanService.goodsCarClean(httpServletRequest, farmId, id, date, img1, img2, img3, img4, img5, type, result);
    }


    //
    @RequestMapping("/listCount")
    public Result listCount(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return cleanService.listCount(httpServletRequest, paramsMap);
    }

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

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

    @GetMapping("/printClean")
    public void printClean(HttpServletResponse response, @RequestParam(name = "ids") String ids) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("ids", ids);
        billCleanService.printClean(response, map);
        String path = "/opt/huatong/clean.xls";
        PdfUtil.returnPdfStream3(response, path, "洗消详情列表");
    }

    @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) {
            billCleanService.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");
        String hum = paramsMap.get("hum");
        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(Integer.parseInt(time));
            configuration1.setDry(Integer.parseInt(hum));
            configuration1.setFarmId(farmId);
            configuration1.setPcr(2);
            configurationService.save(configuration1);
            return new Result(10000, "添加成功！", true);
        }
        configuration.setClean(Integer.parseInt(time));
        configuration.setDry(Integer.parseInt(hum));
        configurationService.updateById(configuration);
        return new Result(10000, "保存成功！", true);
    }

    @RequestMapping("/listScreen")
    public Result listScreen(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return cleanService.listScreen(httpServletRequest, paramsMap);
    }
}
