package com.huimv.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.common.utils.PdfUtil;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.BillGoodsInventory;
import com.huimv.admin.entity.BillIsolate;
import com.huimv.admin.entity.vo.BillAdmissionAndGoods;
import com.huimv.admin.mapper.BillGoodsInventoryMapper;
import com.huimv.admin.service.IBillPersonnelAdmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.entity.BillPersonnelAdmission;
import com.huimv.admin.service.IBillPersonnelAdmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.Map;

/**
 * <p>
 * 入场申请 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@RestController
@CrossOrigin
@RequestMapping("/bill-personnel-admission")
public class BillPersonnelAdmissionController {
    @Autowired
    private IBillPersonnelAdmissionService billPersonnelAdmissionService;
    @Resource
    private BillGoodsInventoryMapper billGoodsInventoryMapper;

    @PostMapping("/addAdmission")
    public Result addAdmission(HttpServletRequest httpServletRequest, @RequestParam("admissionType") String admissionType,
                               @RequestParam("vistitType") String vistitType,
                               @RequestParam("userName") String userName,
                               @RequestParam("phone") String phone,
                               @RequestParam("workName") String workName,
                               @RequestParam("papersType") String papersType,
                               @RequestParam("papersCode") String papersCode,
                               @RequestParam(value = "imgUrl",required = false) MultipartFile imgUrl,
                               @RequestParam(value = "img",required = false) String img,
                               @RequestParam("vistitDate") String vistitDate,
                               @RequestParam("vistitReson") String vistitReson,
                               @RequestParam("destId") String destId,
                               @RequestParam("destName") String destName,
                               @RequestParam(value = "carNum",required = false) String carNum,
                               @RequestParam(value = "source",required = false) String source,
                               @RequestParam(value = "goods",required = false) String goods,
                               @RequestParam("farmId") String farmId) throws IOException {
        return billPersonnelAdmissionService.addAdmission(httpServletRequest,admissionType,vistitType,userName,phone,workName,papersType,papersCode,imgUrl,img,vistitDate,vistitReson,destId,destName,farmId,carNum,source,goods);
    }

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

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

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

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

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

    @PostMapping("/getById")
    public Result getById(@RequestBody Map<String, String> paramsMap){
        BillPersonnelAdmission billPersonnelAdmission = billPersonnelAdmissionService.getById(paramsMap.get("id"));
        BillAdmissionAndGoods billAdmissionAndGoods = new BillAdmissionAndGoods();
        BeanUtil.copyProperties(billPersonnelAdmission,billAdmissionAndGoods);
        List<BillGoodsInventory> billGoodsInventories = billGoodsInventoryMapper.selectList(new QueryWrapper<BillGoodsInventory>().eq("bill_admission", paramsMap.get("id")));
        billAdmissionAndGoods.setGoods(billGoodsInventories);
        return new Result(ResultCode.SUCCESS,billAdmissionAndGoods);
    }

    @Autowired
    private IBillPersonnelAdmissionService admissionService;

    @RequestMapping("/listPrc")
    public Result listAll2(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return admissionService.listPrc(httpServletRequest, paramsMap);
    }
    @RequestMapping("/listById")
    public Result listById(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return admissionService.listById(httpServletRequest, paramsMap);
    }
    @RequestMapping("/editStatus")
    public Result editStatus(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return admissionService.editStatus(httpServletRequest, paramsMap);
    }
    @RequestMapping("/listIsolate")
    public Result listIsolate(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return admissionService.listIsolate(httpServletRequest, paramsMap);
    }
    @RequestMapping("/IsolateById")
    public Result isolateById(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return admissionService.isolateById(httpServletRequest, paramsMap);
    }

    @RequestMapping("/commitIsolate")
    public Result listAll(HttpServletRequest httpServletRequest, @RequestBody BillIsolate billIsolate) {
        return admissionService.commitIsolate(httpServletRequest, billIsolate);
    }
    @RequestMapping("/editIsolate")
    public Result editIsolate(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return admissionService.editIsolate(httpServletRequest, paramsMap);
    }
    @RequestMapping("/listClean")
    public Result listClean(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return admissionService.listClean(httpServletRequest, paramsMap);
    }

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

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

    //后台
    @RequestMapping("/countAdmission")
    public Result countAdmission(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return admissionService.countAdmission(httpServletRequest, paramsMap);
    }
    @RequestMapping("/listTop")
    public Result listTop(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return admissionService.listTop(httpServletRequest, paramsMap);
    }
    @RequestMapping("/admissionTail")
    public Result admissionTail(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return admissionService.admissionTail(httpServletRequest, paramsMap);
    }
    @RequestMapping("/selectList")
    public Result selectList(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        return admissionService.selectList(httpServletRequest, paramsMap);
    }
    @RequestMapping("/delete")
    public Result delete(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws ParseException {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        for (String s : split) {
            admissionService.removeById(s);
        }
        return new Result(10000, "删除成功！", true);
    }
    @GetMapping("/printAdmission")
    public void printAdmission(HttpServletResponse response, @RequestParam(name = "ids") String ids) throws Exception {
        Map<String, String> map = new HashMap<>();
        map.put("ids", ids);
        admissionService.printAdmission(response, map);
        String path = "G://admission.xls";
        PdfUtil.returnPdfStream3(response, path, "人员管理列表");
    }

}
