package com.huimv.receive.controller;


import cn.hutool.core.bean.BeanUtil;
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.BaseProcess;
import com.huimv.receive.entity.BillGoodsInventory;
import com.huimv.receive.entity.BillIsolate;
import com.huimv.receive.entity.BillPersonnelAdmission;
import com.huimv.receive.entity.vo.BillAdmissionAndGoods;
import com.huimv.receive.entity.vo.BillPersonnelAdmissionVo;
import com.huimv.receive.mapper.BillGoodsInventoryMapper;
import com.huimv.receive.service.IBaseProcessService;
import com.huimv.receive.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 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>
 * 入场申请 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@RestController
@CrossOrigin
@RequestMapping("/bill-personnel-admission")
public class BillPersonnelAdmissionController {
    @Autowired
    private IBillPersonnelAdmissionService billPersonnelAdmissionService;
    @Resource
    private BillGoodsInventoryMapper billGoodsInventoryMapper;
    @Autowired
    private IBaseProcessService processService;

    @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("departureId") String departureId,
                               @RequestParam("departureName") String departureName,
                               @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(value = "picture1",required = false) MultipartFile picture1,
                               @RequestParam(value = "picture2",required = false) MultipartFile picture2,
                               @RequestParam(value = "picture3",required = false) MultipartFile picture3,
                               @RequestParam("farmId") String farmId,
                               @RequestParam("midId") Integer midId,
                               @RequestParam("midName") String midName) throws IOException {
        return billPersonnelAdmissionService.addAdmission(httpServletRequest, admissionType, vistitType, userName, phone,
                workName, papersType, papersCode, imgUrl, img, vistitDate, vistitReson, destId, destName, farmId, carNum,
                source, goods, departureId, departureName, picture1, picture2, picture3,
                midId,midName);
    }

    @PostMapping("/addAdmissionFeed")
    public Result addAdmissionFeed(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("departureId") String departureId,
                                   @RequestParam("departureName") String departureName,
                                   @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,
                                   @RequestParam("resource") String resource,
                                   @RequestParam("feedImgUrl") MultipartFile feedImgUrl,
                                   @RequestParam(value = "cleanId", required = false) Integer cleanId,
                                   @RequestParam(value = "cleanName", required = false) String cleanName,
                                   @RequestParam("remark") String remark,
                                   @RequestParam("midId") Integer midId,
                                   @RequestParam("midName") String midName) throws IOException {
        return billPersonnelAdmissionService.addAdmissionFeed(httpServletRequest, admissionType,
                vistitType, userName, phone, workName, papersType, papersCode, imgUrl, img, vistitDate, vistitReson, destId
                , destName, farmId, carNum, source, goods, departureId, departureName, resource, feedImgUrl, cleanId, cleanName, remark,midId,midName);
    }

    @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("/uploadImgAndAdmissin")
    public Result uploadImgAndAdmissin(@RequestParam(value = "imgUrl") MultipartFile imgUrl,
                                       @RequestParam(value = "id") Integer id) throws IOException {
        return billPersonnelAdmissionService.uploadImgAndAdmissin(imgUrl, id);
    }

    @PostMapping("/handleAdmission")
    public Result handleAdmission(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) throws IOException {
        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"));
        QueryWrapper<BaseProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", billPersonnelAdmission.getProcessId());
        BaseProcess process = processService.getOne(queryWrapper);

        BillPersonnelAdmissionVo admissionVo = new BillPersonnelAdmissionVo();
        BeanUtil.copyProperties(billPersonnelAdmission, admissionVo);
        admissionVo.setProcessType(process.getProcessType());
        BillAdmissionAndGoods billAdmissionAndGoods = new BillAdmissionAndGoods();
        BeanUtil.copyProperties(admissionVo, billAdmissionAndGoods);
        List<BillGoodsInventory> billGoodsInventories = billGoodsInventoryMapper.selectList(new QueryWrapper<BillGoodsInventory>().eq("bill_admission", paramsMap.get("id")));
        billAdmissionAndGoods.setGoods(billGoodsInventories);
        return new Result(ResultCode.SUCCESS, billAdmissionAndGoods);
    }

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


    @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) throws IOException {
        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) {
            BillPersonnelAdmission admission = admissionService.getById(s);
            if (admission.getBillStatus() == 0 || admission.getBillStatus() == 3) {
                return new Result(10001, "删除失败！入场申请未审批", true);
            }
            BaseProcess baseProcess = processService.getById(admission.getProcessId());
            admissionService.removeById(s);
            processService.removeById(baseProcess);

        }
        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 = "/opt/huatong/admission.xls";
        PdfUtil.returnPdfStream3(response, path, "人员管理列表");
    }

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

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

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

    //
    @PostMapping("/helpAddAdmission")
    public Result helpAddAdmission(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 = "userImgUrl", required = false) MultipartFile userImgUrl,
                                   @RequestParam(value = "imgUrl", required = false) MultipartFile imgUrl,
                                   @RequestParam(value = "img", required = false) String img,
                                   @RequestParam("vistitDate") String vistitDate,
                                   @RequestParam("vistitReson") String vistitReson,
                                   @RequestParam("departureId") String departureId,
                                   @RequestParam("departureName") String departureName,
                                   @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,
                                   @RequestParam(value = "picture1",required = false) MultipartFile picture1,
                                   @RequestParam(value = "picture2",required = false) MultipartFile picture2,
                                   @RequestParam(value = "picture3",required = false) MultipartFile picture3,
                                   @RequestParam("midId") Integer midId,
                                   @RequestParam("midName") String midName) throws IOException {
        return billPersonnelAdmissionService.helpAddAdmission(httpServletRequest, userImgUrl, admissionType,
                vistitType, userName, phone, workName, papersType, papersCode, imgUrl, img, vistitDate, vistitReson,
                destId, destName, farmId, carNum, source, goods, departureId, departureName,picture1,picture2,picture3, midId,midName);
    }


    //入场申请撤回
    @RequestMapping("/removeAdmission")
    public Result removeAdmission(HttpServletRequest httpServletRequest, @RequestBody Map<String, String> paramsMap) {
        return admissionService.removeAdmission(httpServletRequest, paramsMap);
    }
}
