package com.huimv.receive.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huimv.receive.common.token.TokenSign;
import com.huimv.receive.common.utils.*;
import com.huimv.receive.entity.*;
import com.huimv.receive.entity.vo.*;
import com.huimv.receive.mapper.*;
import com.huimv.receive.service.IBillPersonnelAdmissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 入场申请 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@Service
public class BillPersonnelAdmissionServiceImpl extends ServiceImpl<BillPersonnelAdmissionMapper, BillPersonnelAdmission> implements IBillPersonnelAdmissionService {
    @Resource
    private BillPersonnelAdmissionMapper billPersonnelAdmissionMapper;
    @Resource
    private BaseProcessMapper baseProcessMapper;
    @Resource
    private BillPcrMapper billPcrMapper;
    @Resource
    private BaseLocationMapper baseLocationMapper;
    @Resource
    private BillIsolateMapper billIsolateMapper;
    @Resource
    private BillCleanMapper billCleanMapper;
    @Resource
    private SysAccountMultilevelMapper sysAccountMultilevelMapper;
    @Autowired
    private UploadImage uploadImage;
    @Autowired
    private BillDryMapper dryMapper;
    @Resource
    private BillGoodsInventoryMapper billGoodsInventoryMapper;
    @Resource
    private ExistMapper existMapper;
    @Autowired
    private BillGateMapper gateMapper;
    @Autowired
    private BillAccessDoorMapper doorMapper;
    @Autowired
    private DeviceListMapper deviceListMapper;
    @Autowired
    private PeopleListMapper peopleListMapper;
    @Autowired
    private BaseWashoutPointMapper baseWashoutPointMapper;
    @Autowired
    private BillSamplingMapper billSamplingMapper;
    @Autowired
    private BillCleanBeforeMapper billCleanBeforeMapper;
    @Autowired
    private FlowUtil flowUtil;
    @Autowired
    private BillGoodsInventoryMapper inventoryMapper;
    @Autowired
    private SysAccountMultilevelMapper multilevelMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private BaseFarmMapper farmMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addAdmission(HttpServletRequest httpServletRequest, String admissionType, String vistitType,
                               String userName, String phone, String workName, String papersType, String papersCode,
                               MultipartFile imgUrl, String img, String vistitDate, String vistitReson, String destId,
                               String destName, String farmId, String carNum, String source, String goods,
                               String departureId, String departureName, MultipartFile picture1, MultipartFile picture2,
                               MultipartFile picture3, Integer midId, String midName) throws IOException {
        Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        if (!PhoneNumberValidator.isValidPhoneNumber(phone)) {
            return new Result(10001, "手机号格式错误！", false);
        }
        if (admissionType.equals("1")) {
            return new Result(10001, "紧急入场申请暂未开启！请按正常入场流程申请", false);
        }
        BaseWashoutPoint baseWashoutPoint1 = baseWashoutPointMapper.selectOne(new QueryWrapper<BaseWashoutPoint>().lambda()
                .eq(BaseWashoutPoint::getLocationId, departureId).like(BaseWashoutPoint::getFarmIds, farmId)
                .eq(BaseWashoutPoint::getVisitingType, vistitType).eq(BaseWashoutPoint::getMidId, midId));
        BaseWashoutPoint baseWashoutPoint2 = baseWashoutPointMapper.selectOne(new QueryWrapper<BaseWashoutPoint>().lambda()
                .eq(BaseWashoutPoint::getLocationId, destId).like(BaseWashoutPoint::getFarmIds, farmId)
                .eq(BaseWashoutPoint::getVisitingType, vistitType).eq(BaseWashoutPoint::getMidId, midId));
        if (!departureId.equals("0")) {
            if (baseWashoutPoint1.getPointLevel() >= baseWashoutPoint2.getPointLevel()) {
                return new Result(10001, "出发地应在目的地之前!", false);
            }
        }
        String picUrl = null;

        if (ObjectUtil.isNotEmpty(picture1)) {
            String pic1 = uploadImage.getImageCom(picture1);
            if (pic1.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "物品照片1上传失败", false);
            } else {
                picUrl = pic1;
            }
        }
        if (ObjectUtil.isNotEmpty(picture2)) {
            String pic2 = uploadImage.getImageCom(picture2);
            if (pic2.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "物品照片2上传失败", false);
            } else {
                picUrl = picUrl + "," + pic2;
            }
        }
        if (ObjectUtil.isNotEmpty(picture3)) {
            String pic3 = uploadImage.getImageCom(picture3);
            if (pic3.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "物品照片3上传失败", false);
            } else {
                picUrl = picUrl + "," + pic3;
            }
        }



        SysAccountMultilevel sysAccountMultilevel1 = sysAccountMultilevelMapper.selectById(userId);
        BaseFarm baseFarm = farmMapper.selectById(farmId);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        BillPersonnelAdmission billPersonnelAdmission = new BillPersonnelAdmission();
        billPersonnelAdmission.setFarmName(baseFarm.getFarmName());
        billPersonnelAdmission.setAdmissionType(Integer.parseInt(admissionType));
        billPersonnelAdmission.setVistitType(Integer.parseInt(vistitType));
        if (vistitType.equals("0")) {
            if (ObjectUtil.isNotEmpty(picUrl)) {
                billPersonnelAdmission.setIsCarry(1);
                billPersonnelAdmission.setPicUrl(picUrl);
            } else {
                billPersonnelAdmission.setIsCarry(0);
            }
        }
        billPersonnelAdmission.setDestId(Integer.parseInt(destId));
        billPersonnelAdmission.setDestName(destName);
        billPersonnelAdmission.setDepartureName(departureName);
        billPersonnelAdmission.setDepartureId(Integer.valueOf(departureId));
        billPersonnelAdmission.setAdmissionUserId(userId);
        billPersonnelAdmission.setAdmissionUserName(userName);
        billPersonnelAdmission.setVistitDate(LocalDateTime.parse(vistitDate, dateTimeFormatter));
        billPersonnelAdmission.setSubDate(LocalDateTime.now());
        billPersonnelAdmission.setPhone(phone);
        billPersonnelAdmission.setWorkName(workName);
        billPersonnelAdmission.setPapersType(Integer.parseInt(papersType));
        billPersonnelAdmission.setPapersCode(papersCode);
        billPersonnelAdmission.setUserImgUrl(sysAccountMultilevel1.getImgUrl());
        if (ObjectUtil.isNotEmpty(carNum)) {
            billPersonnelAdmission.setCarNum(carNum);
        }
        if (ObjectUtil.isNotEmpty(imgUrl)) {
            String s = uploadImage.getImageCom(imgUrl);
            if (s.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片上传失败", false);
            }
            billPersonnelAdmission.setImgUrl(s);
        }
        if (ObjectUtil.isNotEmpty(img)) {
            billPersonnelAdmission.setImgUrl(img);
        }
        if (ObjectUtil.isNotEmpty(source)) {
            billPersonnelAdmission.setSource(source);
        }
        billPersonnelAdmission.setVistitReson(vistitReson);
        billPersonnelAdmission.setFarmId(Integer.parseInt(farmId));
        BaseProcess baseProcess = new BaseProcess();
        baseProcess.setDestName(destName);
        baseProcess.setDestId(Integer.parseInt(destId));
        baseProcess.setAdmissionUserId(userId);
        baseProcess.setAdmissionUserName(userName);
        baseProcess.setSubDate(DateTime.now());
//        baseProcess.setUpdateDate(LocalDateTime.now().format(dateTimeFormatter));
        baseProcess.setCurrentStatus("0");

        String allLocation = departureName;
//        String allLocationId = departureId;
        String allLocationId = "0";
        String allLocationStatus = "0";
        String allFlowId = "";

        List<BaseWashoutPoint> baseWashoutPoints = new ArrayList<>();
        if (ObjectUtil.isEmpty(baseWashoutPoint1)) {
            System.out.println("出发地为场外");
            baseWashoutPoints = baseWashoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>().lambda()
                    .le(BaseWashoutPoint::getPointLevel, baseWashoutPoint2.getPointLevel())
                    .eq(BaseWashoutPoint::getMidId, midId)
                    .eq(BaseWashoutPoint::getVisitingType, vistitType).like(BaseWashoutPoint::getFarmIds, farmId)
                    .orderByAsc(BaseWashoutPoint::getPointLevel));
        } else {
            System.out.println("出发地不为场外");
            baseWashoutPoints = baseWashoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>().lambda()
                    .gt(BaseWashoutPoint::getPointLevel, baseWashoutPoint1.getPointLevel())
                    .le(BaseWashoutPoint::getPointLevel, baseWashoutPoint2.getPointLevel())
                    .eq(BaseWashoutPoint::getMidId, midId)
                    .eq(BaseWashoutPoint::getVisitingType, vistitType).like(BaseWashoutPoint::getFarmIds, farmId)
                    .orderByAsc(BaseWashoutPoint::getPointLevel));
        }
        System.out.println("所有洗消点:" + baseWashoutPoints);
        for (BaseWashoutPoint baseWashoutPoint : baseWashoutPoints) {
            allLocation = allLocation + "," + baseLocationMapper.selectById(baseWashoutPoint.getLocationId()).getLocationName();
            allLocationId = allLocationId + "," + baseWashoutPoint.getLocationId();
            allLocationStatus = allLocationStatus + "," + "0";
            allFlowId = allFlowId + "0," + baseWashoutPoint.getFlowList();

            allLocation = allLocation + "," + baseWashoutPoint.getTailLocationName();
            allLocationId = allLocationId + "," + "0";
            allLocationStatus = allLocationStatus + "," + "0";
            if (!baseWashoutPoint.equals(baseWashoutPoints.get(baseWashoutPoints.size() - 1))) {
                allFlowId = allFlowId + ",";
            }
        }
        if (admissionType.equals("1")) {
            baseProcess.setAllLocation(departureName);
            baseProcess.setAllLocationId("0");
            baseProcess.setAllLocationStatus("0");
        } else {
            baseProcess.setAllLocation(allLocation);
            baseProcess.setAllLocationId(allLocationId);
            baseProcess.setAllLocationStatus(allLocationStatus);
        }

        baseProcess.setAllFlowId(allFlowId + ",0");
        baseProcess.setCurrentFlowId("0");
        baseProcess.setCurrentLocation("场外");
        baseProcess.setCurrentLocationId("0");
        baseProcess.setFarmId(Integer.parseInt(farmId));
        baseProcessMapper.insert(baseProcess);
        billPersonnelAdmission.setProcessId(baseProcess.getId());
        billPersonnelAdmission.setMidId(midId);
        billPersonnelAdmission.setMidName(midName);
        billPersonnelAdmissionMapper.insert(billPersonnelAdmission);
        ArrayList<String> phoneList = new ArrayList<>();
        List<SysAccountMultilevel> safe = sysAccountMultilevelMapper.selectList(new QueryWrapper<SysAccountMultilevel>()
                .eq("farm_ids", farmId).eq("type", 3));
        for (SysAccountMultilevel sysAccountMultilevel : safe) {
            phoneList.add(sysAccountMultilevel.getPhone());
        }
//        SendSMS.sendSMS(phoneList,TokenSign.getUserNameByJwtToken(httpServletRequest)+"入场申请测试");
        if (ObjectUtil.isNotEmpty(goods)) {
            ObjectMapper objectMapper = new ObjectMapper();
            List<BillGoodsInventory> admissionAndGoods = objectMapper.readValue(goods, new TypeReference<List<BillGoodsInventory>>() {
            });
            for (BillGoodsInventory good : admissionAndGoods) {
                good.setBillAdmission(billPersonnelAdmission.getId());
                BaseLocation baseLocation1 = baseLocationMapper.selectById(destId);
                good.setTestLocation(baseLocation1.getLocationName());
                good.setTestLocationId(baseLocation1.getId());
                good.setCreateDate(new Date());
                good.setFarmId(farmId);
                billGoodsInventoryMapper.insert(good);
            }
        }
        return Result.SUCCESS();
    }

    @Override
    public Result addAdmissionFeed(HttpServletRequest httpServletRequest, String admissionType, String vistitType,
                                   String userName, String phone, String workName, String papersType, String papersCode,
                                   MultipartFile imgUrl, String img, String vistitDate, String vistitReson, String destId, String destName,
                                   String farmId, String carNum, String source,
                                   String goods, String departureId, String departureName,
                                   String resource, MultipartFile feedImgUrl, Integer cleanId, String cleanName,
                                   String remark, Integer midId, String midName) throws IOException {
        Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        if (!PhoneNumberValidator.isValidPhoneNumber(phone)) {
            return new Result(10001, "手机号格式错误！", false);
        }
        if (admissionType.equals("1")) {
            return new Result(10001, "紧急入场申请暂未开启！请按正常入场流程申请", false);
        }
        BaseWashoutPoint baseWashoutPoint1 = baseWashoutPointMapper.selectOne(new QueryWrapper<BaseWashoutPoint>()
                .lambda().eq(BaseWashoutPoint::getLocationId, departureId).like(BaseWashoutPoint::getFarmIds, farmId)
                .eq(BaseWashoutPoint::getVisitingType, vistitType)
                .eq(BaseWashoutPoint::getMidId, midId));
        BaseWashoutPoint baseWashoutPoint2 = baseWashoutPointMapper.selectOne(new QueryWrapper<BaseWashoutPoint>()
                .lambda().eq(BaseWashoutPoint::getLocationId, destId).like(BaseWashoutPoint::getFarmIds, farmId)
                .eq(BaseWashoutPoint::getVisitingType, vistitType)
                .eq(BaseWashoutPoint::getMidId, midId));
        if (!departureId.equals("0")) {
            if (baseWashoutPoint1.getPointLevel() >= baseWashoutPoint2.getPointLevel()) {
                return new Result(10001, "出发地应在目的地之前!", false);
            }
        }
        SysAccountMultilevel sysAccountMultilevel1 = sysAccountMultilevelMapper.selectById(userId);
        BaseFarm baseFarm = farmMapper.selectById(farmId);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        BillPersonnelAdmission billPersonnelAdmission = new BillPersonnelAdmission();
        billPersonnelAdmission.setFarmName(baseFarm.getFarmName());
        billPersonnelAdmission.setAdmissionType(Integer.parseInt(admissionType));
        billPersonnelAdmission.setVistitType(Integer.parseInt(vistitType));
        billPersonnelAdmission.setDestId(Integer.parseInt(destId));
        billPersonnelAdmission.setDestName(destName);
        billPersonnelAdmission.setDepartureName(departureName);
        billPersonnelAdmission.setDepartureId(Integer.valueOf(departureId));
        billPersonnelAdmission.setAdmissionUserId(userId);
        billPersonnelAdmission.setAdmissionUserName(userName);
        billPersonnelAdmission.setVistitDate(LocalDateTime.parse(vistitDate, dateTimeFormatter));
        billPersonnelAdmission.setSubDate(LocalDateTime.now());
        billPersonnelAdmission.setPhone(phone);
        billPersonnelAdmission.setWorkName(workName);
        billPersonnelAdmission.setPapersType(Integer.parseInt(papersType));
        billPersonnelAdmission.setPapersCode(papersCode);
        billPersonnelAdmission.setUserImgUrl(sysAccountMultilevel1.getImgUrl());
        String ss=uploadImage.getImageCom(feedImgUrl);
        if (ss.equals("上传失败")) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new Result(10001, "图片上传失败", false);
        }
        billPersonnelAdmission.setFeedImgUrl(ss);
        billPersonnelAdmission.setResource(resource);
        billPersonnelAdmission.setRemark(remark);
        billPersonnelAdmission.setCleanId(cleanId);
        billPersonnelAdmission.setCleanName(cleanName);
        if (ObjectUtil.isNotEmpty(carNum)) {
            billPersonnelAdmission.setCarNum(carNum);
        }
        if (ObjectUtil.isNotEmpty(imgUrl)) {
            String s = uploadImage.getImageCom(imgUrl);
            if (s.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片上传失败", false);
            }
            billPersonnelAdmission.setImgUrl(s);
        }
        if (ObjectUtil.isNotEmpty(img)) {
            billPersonnelAdmission.setImgUrl(img);
        }
        if (ObjectUtil.isNotEmpty(source)) {
            billPersonnelAdmission.setSource(source);
        }
        billPersonnelAdmission.setVistitReson(vistitReson);
        billPersonnelAdmission.setFarmId(Integer.parseInt(farmId));
        BaseProcess baseProcess = new BaseProcess();
        baseProcess.setDestName(destName);
        baseProcess.setDestId(Integer.parseInt(destId));
        baseProcess.setAdmissionUserId(userId);
        baseProcess.setAdmissionUserName(userName);
        baseProcess.setSubDate(DateTime.now());
//        baseProcess.setUpdateDate(LocalDateTime.now().format(dateTimeFormatter));
        baseProcess.setCurrentStatus("0");

        String allLocation = departureName;
        String allLocationId = "0";
        String allLocationStatus = "0";
        String allFlowId = "";

        List<BaseWashoutPoint> baseWashoutPoints = new ArrayList<>();
        if (ObjectUtil.isEmpty(baseWashoutPoint1)) {
            baseWashoutPoints = baseWashoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>().lambda()
                    .le(BaseWashoutPoint::getPointLevel, baseWashoutPoint2.getPointLevel())
                    .eq(BaseWashoutPoint::getMidId, midId)
                    .eq(BaseWashoutPoint::getVisitingType, vistitType).like(BaseWashoutPoint::getFarmIds, farmId)
                    .orderByAsc(BaseWashoutPoint::getPointLevel));
        } else {
            baseWashoutPoints = baseWashoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>()
                    .lambda().gt(BaseWashoutPoint::getPointLevel, baseWashoutPoint1.getPointLevel())
                    .le(BaseWashoutPoint::getPointLevel, baseWashoutPoint2.getPointLevel())
                    .eq(BaseWashoutPoint::getMidId, midId)
                    .eq(BaseWashoutPoint::getVisitingType, vistitType).like(BaseWashoutPoint::getFarmIds, farmId)
                    .orderByAsc(BaseWashoutPoint::getPointLevel));
        }
        for (BaseWashoutPoint baseWashoutPoint : baseWashoutPoints) {
            allLocation = allLocation + "," + baseLocationMapper.selectById(baseWashoutPoint.getLocationId()).getLocationName();
            allLocationId = allLocationId + "," + baseWashoutPoint.getLocationId();
            allLocationStatus = allLocationStatus + "," + "0";
            allFlowId = allFlowId + "0," + baseWashoutPoint.getFlowList();

            allLocation = allLocation + "," + baseWashoutPoint.getTailLocationName();
            allLocationId = allLocationId + "," + "0";
            allLocationStatus = allLocationStatus + "," + "0";
            if (!baseWashoutPoint.equals(baseWashoutPoints.get(baseWashoutPoints.size() - 1))) {
                allFlowId = allFlowId + ",";
            }
        }
        baseProcess.setAllLocation(allLocation);
        baseProcess.setAllLocationId(allLocationId);
        baseProcess.setAllLocationStatus(allLocationStatus);
        baseProcess.setAllFlowId(allFlowId + ",0");
        baseProcess.setCurrentFlowId("0");
        baseProcess.setCurrentLocation("场外");
        baseProcess.setCurrentLocationId("0");
        baseProcess.setFarmId(Integer.parseInt(farmId));
        baseProcessMapper.insert(baseProcess);
        billPersonnelAdmission.setProcessId(baseProcess.getId());
        billPersonnelAdmission.setMidId(midId);
        billPersonnelAdmission.setMidName(midName);
        billPersonnelAdmissionMapper.insert(billPersonnelAdmission);
        ArrayList<String> phoneList = new ArrayList<>();
        List<SysAccountMultilevel> safe = sysAccountMultilevelMapper.selectList(new QueryWrapper<SysAccountMultilevel>()
                .eq("farm_ids", farmId).eq("type", 3));
        for (SysAccountMultilevel sysAccountMultilevel : safe) {
            phoneList.add(sysAccountMultilevel.getPhone());
        }
//        SendSMS.sendSMS(phoneList,TokenSign.getUserNameByJwtToken(httpServletRequest)+"入场申请测试");
        if (ObjectUtil.isNotEmpty(goods)) {
            ObjectMapper objectMapper = new ObjectMapper();
            List<BillGoodsInventory> admissionAndGoods = objectMapper.readValue(goods, new TypeReference<List<BillGoodsInventory>>() {
            });
            for (BillGoodsInventory good : admissionAndGoods) {
                good.setBillAdmission(billPersonnelAdmission.getId());
                BaseLocation baseLocation1 = baseLocationMapper.selectById(destId);
                good.setTestLocation(baseLocation1.getLocationName());
                good.setTestLocationId(baseLocation1.getId());
                good.setCreateDate(new Date());
                good.setFarmId(farmId);
                billGoodsInventoryMapper.insert(good);
            }
        }
        return Result.SUCCESS();
    }

    @Override
    public Result listPersonalAdmission(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        String vistitType = paramsMap.get("vistitType");
        Page<BillPersonnelAdmissionVo> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(vistitType), "vistit_type", vistitType).eq("farm_id", farmId)
                .eq("admission_user_id", TokenSign.getMemberIdByJwtToken(httpServletRequest))
                .orderByDesc("sub_date");
        IPage<BillPersonnelAdmissionVo> voIPage = billPersonnelAdmissionMapper.listPersonAndProcess(new QueryWrapper<BillPersonnelAdmission>()
                .eq(StringUtils.isNotBlank(vistitType), "a.vistit_type", vistitType)
                .eq("a.admission_user_id", TokenSign.getMemberIdByJwtToken(httpServletRequest))
                .eq("a.farm_id", farmId)
                .orderByDesc("a.sub_date"), page);
        return new Result(ResultCode.SUCCESS, voIPage, billPersonnelAdmissionMapper.selectCount(queryWrapper));
    }

    @Override
    public Result listAdmission(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        String type = paramsMap.get("type");
        String farmId = paramsMap.get("farmId");
        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
        Integer userType = TokenSign.getUserTypeByJwtToken(httpServletRequest);
        Integer id = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        SysAccountMultilevel accountMultilevel = multilevelMapper.selectById(id);
        Integer userId = accountMultilevel.getUserId();
        SysUser sysUser = userMapper.selectById(userId);
        if (userType == 3) {
            //生物安全负责人
            queryWrapper.eq("admission_type", 0);
        } else if (userType == 2) {
            //紧急入场
            queryWrapper.ne("admission_type", 0);
        }
        switch (type) {
            case "1":
                queryWrapper.eq("bill_status", 0);
                break;
            case "2":
                queryWrapper.eq("bill_status", 1);
                break;
            case "3":
                queryWrapper.eq("bill_status", 2);
                break;
            default:
                break;
        }
        if (sysUser.getIsAllFarm() == 0) {
            queryWrapper.orderByDesc("sub_date").eq("farm_id", farmId);
        } else {
            queryWrapper.orderByDesc("sub_date");
        }

//        List<BaseLocation> baseLocations = baseLocationMapper.selectList(new QueryWrapper<BaseLocation>().eq("serial_number", 0));
//        List<Integer> locationIds = new ArrayList<>();
//        for (BaseLocation baseLocation : baseLocations) {
//            locationIds.add(baseLocation.getId());
//        }
//        if (!locationIds.contains(Integer.parseInt(TokenSign.getWorkIdByJwtToken(httpServletRequest)))){
//            queryWrapper.eq("id",0);
//        }
        Page<BillPersonnelAdmission> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        Page<BillPersonnelAdmission> billPersonnelAdmissionPage = billPersonnelAdmissionMapper.selectPage(page, queryWrapper);
        QueryWrapper<BillPersonnelAdmission> queryWrapper1 = new QueryWrapper<>();
        if (sysUser.getIsAllFarm() == 0) {
            queryWrapper1.eq("farm_id", farmId);
        } else {
        }

        if (userType == 3) {
            //生物安全负责人
            queryWrapper1.eq("admission_type", 0);
        } else if (userType == 2) {
            //紧急入场
            queryWrapper1.ne("admission_type", 0);
        }
        queryWrapper1.eq("bill_status", 0);
        return new Result(ResultCode.SUCCESS, billPersonnelAdmissionPage, billPersonnelAdmissionMapper.selectList(queryWrapper1).size());
    }

    @Override
    @Transactional
    public Result handleAdmission(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) throws IOException {
        String type = paramsMap.get("type");
        String id = paramsMap.get("id");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        BillPersonnelAdmission billPersonnelAdmission = billPersonnelAdmissionMapper.selectById(id);
        Integer userType = TokenSign.getUserTypeByJwtToken(httpServletRequest);
        billPersonnelAdmission.setPassDate(LocalDateTime.now());
        billPersonnelAdmission.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
        billPersonnelAdmission.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
        BaseProcess baseProcess = baseProcessMapper.selectById(billPersonnelAdmission.getProcessId());

        ArrayList<String> phoneList = new ArrayList<>();
        phoneList.add(billPersonnelAdmission.getPhone());
        if (StringUtils.isBlank(baseProcess.getUpdateDate())) {
            baseProcess.setUpdateDate(LocalDateTime.now().format(dateTimeFormatter));
        } else {
            baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + LocalDateTime.now().format(dateTimeFormatter));
        }
        if (type.equals("0")) {
            //拒绝
            billPersonnelAdmission.setBillStatus(2);
            baseProcess.setProcessType(2);
            baseProcess.setCurrentStatus("2");
            String status = baseProcess.getAllLocationStatus();
            String[] split1 = status.split(",");
            split1[0] = "3";
            baseProcess.setAllLocationStatus(split1[0] + status.substring(1));
        } else {
            if (billPersonnelAdmission.getBillStatus() != 0) {
                return new Result(10001, "该订单已通过，无需再次点击！", false);
            }
            //新增人员转场，不需要流程，入场审批通过后直接下发门禁，流程结束
            if (billPersonnelAdmission.getMidId()==60||billPersonnelAdmission.getMidId()==61
                    ||billPersonnelAdmission.getMidId()==67||billPersonnelAdmission.getMidId()==68) {
                DeviceList deviceList = deviceListMapper.selectOne(new QueryWrapper<DeviceList>().eq("farm_id", billPersonnelAdmission.getFarmId()));
                PeopleList peopleList = new PeopleList();
                peopleList.setFarmId(billPersonnelAdmission.getFarmId().toString());
                peopleList.setDeviceCode(deviceList.getDeviceCode());
                peopleList.setUserId(billPersonnelAdmission.getProcessId().toString());
                peopleList.setUserName(billPersonnelAdmission.getAdmissionUserName());
                peopleList.setPassword("123456");
                peopleList.setType(0);
                peopleList.setCreateTime(new Date());
                Date now = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(now);
                calendar.add(Calendar.HOUR_OF_DAY, 6);
                Date date1 = calendar.getTime();
                peopleList.setOutTime(date1);
                SysAccountMultilevel sysAccountMultilevel = multilevelMapper.selectById(billPersonnelAdmission.getAdmissionUserId());
                peopleList.setImgUrl(sysAccountMultilevel.getImgUrl());
                peopleListMapper.insert(peopleList);

                billPersonnelAdmission.setBillStatus(1);
                baseProcess.setProcessType(1);
                String status = baseProcess.getAllLocationStatus();
                String replace = status.replace("0", "2");
                baseProcess.setCurrentStatus(replace);
                baseProcess.setAllLocationStatus(replace);
                baseProcessMapper.updateById(baseProcess);
                billPersonnelAdmissionMapper.updateById(billPersonnelAdmission);
                //如果有物资，则把物资的申请单给结束
                BillGoodsInventory goodsInventory = billGoodsInventoryMapper.selectOne(new QueryWrapper<BillGoodsInventory>().eq("process_id", baseProcess.getId()));
                if (ObjectUtil.isNotEmpty(goodsInventory)) {
                    goodsInventory.setBillStatus(3);
                    billGoodsInventoryMapper.updateById(goodsInventory);
                }
                return Result.SUCCESS();
            }



            //通过
            billPersonnelAdmission.setBillStatus(1);

            //获取下一步位置
            String allLocationId = baseProcess.getAllLocationId();
            String[] strings = allLocationId.split(",");
            String nextLocationId;
            BaseLocation baseLocation = new BaseLocation();
            if (billPersonnelAdmission.getAdmissionType() == 0) {
                nextLocationId = strings[1];
                baseLocation = baseLocationMapper.selectById(nextLocationId);
            }
            //人员通过只要经过待检点就要给门禁
            if (billPersonnelAdmission.getVistitType() == 0 && baseLocation.getId() == 45) {
                DeviceList deviceList = deviceListMapper.selectOne(new QueryWrapper<DeviceList>().eq("farm_id", "1"));
                Date now = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(now);
                calendar.add(Calendar.HOUR_OF_DAY, 24);
                Date date1 = calendar.getTime();
                SysAccountMultilevel sysAccountMultilevel = multilevelMapper.selectById(billPersonnelAdmission.getAdmissionUserId());
                PeopleList peopleList1 = new PeopleList();
                peopleList1.setFarmId("1");
                peopleList1.setDeviceCode(deviceList.getDeviceCode());
                peopleList1.setUserId(billPersonnelAdmission.getProcessId().toString());
                peopleList1.setUserName(billPersonnelAdmission.getAdmissionUserName());
                peopleList1.setPassword("123456");
                peopleList1.setType(0);
                peopleList1.setCreateTime(new Date());
                peopleList1.setOutTime(date1);
                peopleList1.setImgUrl(sysAccountMultilevel.getImgUrl());
                peopleListMapper.insert(peopleList1);
            }

            if (billPersonnelAdmission.getAdmissionType() == 0) {
                String[] split = baseProcess.getCurrentStatus().split(",");
                split[split.length - 1] = "1";
                StringBuilder result = new StringBuilder();
                for (String element : split) {
                    result.append(element).append(",");
                }
                result.append("0");
                baseProcess.setCurrentStatus(result.toString());
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation() + "," + baseLocation.getLocationName());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId() + "," + baseLocation.getId());

                String status = baseProcess.getAllLocationStatus();
                String[] split1 = status.split(",");
                split1[0] = "2";
                split1[1] = "1";
                baseProcess.setAllLocationStatus(split1[0] + "," + split1[1] + status.substring(3));
                String[] split2 = baseProcess.getAllFlowId().split(",");
                String nextFlowId = split2[baseProcess.getCurrentFlowId().split(",").length];
                baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextFlowId);

                if (Integer.valueOf(nextFlowId) == 1) {
                    BillSampling billSampling = (BillSampling) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                            billPersonnelAdmission.getVistitType(),
                            billPersonnelAdmission.getDestName(),
                            billPersonnelAdmission.getDestId(),
                            billPersonnelAdmission.getAdmissionUserId(),
                            billPersonnelAdmission.getAdmissionUserName(),
                            billPersonnelAdmission.getPhone(),
                            billPersonnelAdmission.getVistitDate(),
                            baseLocation.getId(),
                            baseLocation.getLocationName(),
                            billPersonnelAdmission.getFarmId(),
                            baseProcess.getId(),
                            billPersonnelAdmission.getDepartureName(),
                            billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                            billPersonnelAdmission.getFarmName());
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                        billSampling.setCarNum(billPersonnelAdmission.getCarNum());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                        billSampling.setResource(billPersonnelAdmission.getResource());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                        billSampling.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                        billSampling.setRemark(billPersonnelAdmission.getRemark());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                        billSampling.setCleanId(billPersonnelAdmission.getCleanId());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                        billSampling.setCleanName(billPersonnelAdmission.getCleanName());
                    }
                    billSamplingMapper.insert(billSampling);
                } else if (Integer.valueOf(nextFlowId) == 2) {
                    BillPcr billPcr = (BillPcr) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                            billPersonnelAdmission.getVistitType(),
                            billPersonnelAdmission.getDestName(),
                            billPersonnelAdmission.getDestId(),
                            billPersonnelAdmission.getAdmissionUserId(),
                            billPersonnelAdmission.getAdmissionUserName(),
                            billPersonnelAdmission.getPhone(),
                            billPersonnelAdmission.getVistitDate(),
                            baseLocation.getId(),
                            baseLocation.getLocationName(),
                            billPersonnelAdmission.getFarmId(),
                            baseProcess.getId(),
                            billPersonnelAdmission.getDepartureName(),
                            billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(),
                            billPersonnelAdmission.getMidName(),
                            billPersonnelAdmission.getFarmName());
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                        billPcr.setCarNum(billPersonnelAdmission.getCarNum());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                        billPcr.setResource(billPersonnelAdmission.getResource());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                        billPcr.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                        billPcr.setRemark(billPersonnelAdmission.getRemark());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                        billPcr.setCleanId(billPersonnelAdmission.getCleanId());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                        billPcr.setCleanName(billPersonnelAdmission.getCleanName());
                    }
                    pcrMapper.insert(billPcr);
                } else if (Integer.valueOf(nextFlowId) == 3) {
                    BillClean billClean = (BillClean) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                            billPersonnelAdmission.getVistitType(),
                            billPersonnelAdmission.getDestName(),
                            billPersonnelAdmission.getDestId(),
                            billPersonnelAdmission.getAdmissionUserId(),
                            billPersonnelAdmission.getAdmissionUserName(),
                            billPersonnelAdmission.getPhone(),
                            billPersonnelAdmission.getVistitDate(),
                            baseLocation.getId(),
                            baseLocation.getLocationName(),
                            billPersonnelAdmission.getFarmId(),
                            baseProcess.getId(),
                            billPersonnelAdmission.getDepartureName(),
                            billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                            billPersonnelAdmission.getFarmName());
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                        billClean.setCarNum(billPersonnelAdmission.getCarNum());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                        billClean.setResource(billPersonnelAdmission.getResource());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                        billClean.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                        billClean.setRemark(billPersonnelAdmission.getRemark());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                        billClean.setCleanId(billPersonnelAdmission.getCleanId());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                        billClean.setCleanName(billPersonnelAdmission.getCleanName());
                    }
                    cleanMapper.insert(billClean);
                } else if (Integer.valueOf(nextFlowId) == 4) {
                    BillIsolate billIsolate = (BillIsolate) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                            billPersonnelAdmission.getVistitType(),
                            billPersonnelAdmission.getDestName(),
                            billPersonnelAdmission.getDestId(),
                            billPersonnelAdmission.getAdmissionUserId(),
                            billPersonnelAdmission.getAdmissionUserName(),
                            billPersonnelAdmission.getPhone(),
                            billPersonnelAdmission.getVistitDate(),
                            baseLocation.getId(),
                            baseLocation.getLocationName(),
                            billPersonnelAdmission.getFarmId(),
                            baseProcess.getId(),
                            billPersonnelAdmission.getDepartureName(),
                            billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                            billPersonnelAdmission.getFarmName());
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                        billIsolate.setCarNum(billPersonnelAdmission.getCarNum());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                        billIsolate.setResource(billPersonnelAdmission.getResource());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                        billIsolate.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                        billIsolate.setRemark(billPersonnelAdmission.getRemark());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                        billIsolate.setCleanId(billPersonnelAdmission.getCleanId());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                        billIsolate.setCleanName(billPersonnelAdmission.getCleanName());
                    }
                    billIsolateMapper.insert(billIsolate);
                } else if (Integer.valueOf(nextFlowId) == 5) {
                    BillDry billDry = (BillDry) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                            billPersonnelAdmission.getVistitType(),
                            billPersonnelAdmission.getDestName(),
                            billPersonnelAdmission.getDestId(),
                            billPersonnelAdmission.getAdmissionUserId(),
                            billPersonnelAdmission.getAdmissionUserName(),
                            billPersonnelAdmission.getPhone(),
                            billPersonnelAdmission.getVistitDate(),
                            baseLocation.getId(),
                            baseLocation.getLocationName(),
                            billPersonnelAdmission.getFarmId(),
                            baseProcess.getId(),
                            billPersonnelAdmission.getDepartureName(),
                            billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                            billPersonnelAdmission.getFarmName());
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                        billDry.setCarNum(billPersonnelAdmission.getCarNum());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                        billDry.setResource(billPersonnelAdmission.getResource());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                        billDry.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                        billDry.setRemark(billPersonnelAdmission.getRemark());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                        billDry.setCleanId(billPersonnelAdmission.getCleanId());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                        billDry.setCleanName(billPersonnelAdmission.getCleanName());
                    }
                    dryMapper.insert(billDry);
                } else if (Integer.valueOf(nextFlowId) == 6) {
                    BillCleanBefore billCleanBefore = (BillCleanBefore) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                            billPersonnelAdmission.getVistitType(),
                            billPersonnelAdmission.getDestName(),
                            billPersonnelAdmission.getDestId(),
                            billPersonnelAdmission.getAdmissionUserId(),
                            billPersonnelAdmission.getAdmissionUserName(),
                            billPersonnelAdmission.getPhone(),
                            billPersonnelAdmission.getVistitDate(),
                            baseLocation.getId(),
                            baseLocation.getLocationName(),
                            billPersonnelAdmission.getFarmId(),
                            baseProcess.getId(),
                            billPersonnelAdmission.getDepartureName(),
                            billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                            billPersonnelAdmission.getFarmName());
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                        billCleanBefore.setCarNum(billPersonnelAdmission.getCarNum());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                        billCleanBefore.setResource(billPersonnelAdmission.getResource());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                        billCleanBefore.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                        billCleanBefore.setRemark(billPersonnelAdmission.getRemark());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                        billCleanBefore.setCleanId(billPersonnelAdmission.getCleanId());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                        billCleanBefore.setCleanName(billPersonnelAdmission.getCleanName());
                    }
                    billCleanBeforeMapper.insert(billCleanBefore);
                } else {
                    BillGoodsInventory billCleanBefore = (BillGoodsInventory) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                            billPersonnelAdmission.getVistitType(),
                            billPersonnelAdmission.getDestName(),
                            billPersonnelAdmission.getDestId(),
                            billPersonnelAdmission.getAdmissionUserId(),
                            billPersonnelAdmission.getAdmissionUserName(),
                            billPersonnelAdmission.getPhone(),
                            billPersonnelAdmission.getVistitDate(),
                            baseLocation.getId(),
                            baseLocation.getLocationName(),
                            billPersonnelAdmission.getFarmId(),
                            baseProcess.getId(),
                            billPersonnelAdmission.getDepartureName(),
                            billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                            billPersonnelAdmission.getFarmName());
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                        billCleanBefore.setCarNum(billPersonnelAdmission.getCarNum());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                        billCleanBefore.setResource(billPersonnelAdmission.getResource());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                        billCleanBefore.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                        billCleanBefore.setRemark(billPersonnelAdmission.getRemark());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                        billCleanBefore.setCleanId(billPersonnelAdmission.getCleanId());
                    }
                    if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                        billCleanBefore.setCleanName(billPersonnelAdmission.getCleanName());
                    }
                    billGoodsInventoryMapper.insert(billCleanBefore);
                }
            } else {
                baseProcess.setCurrentStatus("1");
                baseProcess.setProcessType(1);
                baseProcess.setAllLocationStatus("2");
                //TODO 紧急入场通过先不处理
            }
        }
        baseProcessMapper.updateById(baseProcess);
        billPersonnelAdmissionMapper.updateById(billPersonnelAdmission);
        return Result.SUCCESS();
    }


    @Override
    public Result listAll(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");
        String farmId = paramsMap.get("farmId");
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
//        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("farm_id", farmId);
        Page<FactoryDirectorVo> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        IPage<FactoryDirectorVo> factoryDirectorList;
//        factoryDirectorList = billPersonnelAdmissionMapper.listAll(queryWrapper,page);
        if (type.equals("0")) {
            factoryDirectorList = billPersonnelAdmissionMapper.listAll(type, farmId, page);
        } else {
            factoryDirectorList = billPersonnelAdmissionMapper.listCarAll(type, farmId, page);
        }

        return new Result(ResultCode.SUCCESS, factoryDirectorList/*, factoryDirectorList.getSize()*/);
    }

    @Override
    public Result getDetail(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");
        String id = paramsMap.get("id");
        if (type.equals("0")) {
            BillPersonnelAdmission billPersonnelAdmission = billPersonnelAdmissionMapper.selectById(id);
            BillAdmissionAndGoods billAdmissionAndGoods = new BillAdmissionAndGoods();
            BeanUtil.copyProperties(billPersonnelAdmission, billAdmissionAndGoods);
            List<BillGoodsInventory> billGoodsInventories = billGoodsInventoryMapper.selectList(new QueryWrapper<BillGoodsInventory>().eq("bill_admission", id));
            billAdmissionAndGoods.setGoods(billGoodsInventories);
            return new Result(ResultCode.SUCCESS, billAdmissionAndGoods);
        } else if (type.equals("1")) {
            return new Result(ResultCode.SUCCESS, billPcrMapper.selectById(id));
        } else if (type.equals("2")) {
            return new Result(ResultCode.SUCCESS, billCleanMapper.selectById(id));
        } else if (type.equals("4")) {
            return new Result(ResultCode.SUCCESS, dryMapper.selectById(id));
        } else {
            return new Result(ResultCode.SUCCESS, billIsolateMapper.selectById(id));
        }
    }

    @Override
    public Result listDoor(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");//1为车辆 2为闸机
        String status = paramsMap.get("status");//0为全部 1为通过 2为不通过
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        Integer id = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "10";
        }
        if ("".equals(status) || null == status) {
            status = "0";
        }
        if ("1".equals(type)) {
            QueryWrapper<BillAccessDoor> queryWrapper = new QueryWrapper<>();
            if (null != id) {
                if (277==id) {
                    queryWrapper.orderByDesc("check_date");
                } else {
                    queryWrapper.in("farm_id", farmId,1,2).orderByDesc("check_date");
                }
            }else {
                queryWrapper.eq("farm_id", farmId).orderByDesc("check_date");
            }

            if ("1".equals(status)) {
                queryWrapper.eq("bill_status", 1);
            } else if ("2".equals(status)) {
                queryWrapper.eq("bill_status", 0);
            }
            Page<BillAccessDoor> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            return new Result(ResultCode.SUCCESS, doorMapper.selectPage(page, queryWrapper));
        } else {
            QueryWrapper<BillGate> queryWrapper = new QueryWrapper<>();
            if (null != id) {
                if (277==id) {
                    queryWrapper.orderByDesc("check_date");
                } else {
                    queryWrapper.in("farm_id", Integer.parseInt(farmId),1,2).orderByDesc("check_date");
                }
            }else {
                queryWrapper.eq("farm_id", farmId).orderByDesc("check_date");
            }

            if ("1".equals(status)) {
                queryWrapper.eq("bill_status", 1);
            } else if ("2".equals(status)) {
                queryWrapper.eq("bill_status", 0);
            }
            queryWrapper.last("limit 5");
            List<BillGate> billGates = gateMapper.selectList(queryWrapper);
            queryWrapper.eq("check_location","中转站地磅电脑（车牌识别摄像头）");
            List<BillGate> billGates1 = gateMapper.selectList(queryWrapper);
            billGates.addAll(billGates1);
            Page<BillGate> page2 = new Page<>(Integer.parseInt(pageNum), billGates.size());
             page2.setRecords(billGates);

            return new Result(ResultCode.SUCCESS, page2);
        }
    }

    @Override
    public Result countAdmission(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        if ("".equals(type) || type == null) {
            type = "1";
        }
        List<ListAdmissionVo> admissionVo = null;
        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        //默认今日
        if ("1".equals(type)) {
            Date timesmorning = DataUill.getTimesmorning();
            queryWrapper.ge("sub_date", timesmorning);
            admissionVo = billPersonnelAdmissionMapper.countAdmission(queryWrapper);
            for (ListAdmissionVo listAdmissionVo : admissionVo) {
                QueryWrapper<BillPersonnelAdmission> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("farm_id", farmId).ge("sub_date", timesmorning)
                        .eq("vistit_type", listAdmissionVo.getType()).eq("bill_status", 1);
                QueryWrapper<BillPersonnelAdmission> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("farm_id", farmId).ge("sub_date", timesmorning)
                        .eq("vistit_type", listAdmissionVo.getType()).eq("bill_status", 2);
                listAdmissionVo.setPassCount(billPersonnelAdmissionMapper.selectCount(queryWrapper1));
                listAdmissionVo.setRefuseCount(billPersonnelAdmissionMapper.selectCount(queryWrapper2));
            }
        } else if ("2".equals(type)) {
            // 本周
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
            queryWrapper.ge("sub_date", calendar.getTime());
            admissionVo = billPersonnelAdmissionMapper.countAdmission(queryWrapper);
            for (ListAdmissionVo listAdmissionVo : admissionVo) {
                QueryWrapper<BillPersonnelAdmission> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("farm_id", farmId).ge("sub_date", calendar.getTime())
                        .eq("vistit_type", listAdmissionVo.getType()).eq("bill_status", 1);
                QueryWrapper<BillPersonnelAdmission> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("farm_id", farmId).ge("sub_date", calendar.getTime())
                        .eq("vistit_type", listAdmissionVo.getType()).eq("bill_status", 2);
                listAdmissionVo.setPassCount(billPersonnelAdmissionMapper.selectCount(queryWrapper1));
                listAdmissionVo.setRefuseCount(billPersonnelAdmissionMapper.selectCount(queryWrapper2));
            }
        } else if ("3".equals(type)) {
            //本月
            Date timesMonthmorning = DataUill.getTimesMonthmorning();
            queryWrapper.ge("sub_date", timesMonthmorning);
            admissionVo = billPersonnelAdmissionMapper.countAdmission(queryWrapper);
            for (ListAdmissionVo listAdmissionVo : admissionVo) {
                QueryWrapper<BillPersonnelAdmission> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("farm_id", farmId).ge("sub_date", timesMonthmorning)
                        .eq("vistit_type", listAdmissionVo.getType()).eq("bill_status", 1);
                QueryWrapper<BillPersonnelAdmission> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.eq("farm_id", farmId).ge("sub_date", timesMonthmorning)
                        .eq("vistit_type", listAdmissionVo.getType()).eq("bill_status", 2);
                listAdmissionVo.setPassCount(billPersonnelAdmissionMapper.selectCount(queryWrapper1));
                listAdmissionVo.setRefuseCount(billPersonnelAdmissionMapper.selectCount(queryWrapper2));
            }
        }
        return new Result(ResultCode.SUCCESS, admissionVo);
    }

    @Override
    public Result listTop(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        if ("".equals(type) || type == null) {
            type = "1";
        }
        List<ListAdmissionVo> admissionVo = null;
        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        //今日
        if ("1".equals(type)) {
            Date timesmorning = DataUill.getTimesmorning();
            queryWrapper.ge("sub_date", timesmorning);
            admissionVo = billPersonnelAdmissionMapper.countAdmission(queryWrapper);
        } else if ("2".equals(type)) {
            // 本周
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
            queryWrapper.ge("sub_date", calendar.getTime());
            admissionVo = billPersonnelAdmissionMapper.countAdmission(queryWrapper);
        } else if ("3".equals(type)) {
            //本月
            Date timesMonthmorning = DataUill.getTimesMonthmorning();
            queryWrapper.ge("sub_date", timesMonthmorning);
            admissionVo = billPersonnelAdmissionMapper.countAdmission(queryWrapper);
        }
        JSONArray jsonArray = new JSONArray();
        for (ListAdmissionVo listAdmissionVo : admissionVo) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("type", listAdmissionVo.getType());
            if (listAdmissionVo.getType() == 0) {
                jsonObject.put("name", "人员");
            } else if (listAdmissionVo.getType() == 1) {
                jsonObject.put("name", "环保车");
            } else if (listAdmissionVo.getType() == 2) {
                jsonObject.put("name", "拉猪车");
            } else if (listAdmissionVo.getType() == 3) {
                jsonObject.put("name", "饲料车");
            } else if (listAdmissionVo.getType() == 4) {
                jsonObject.put("name", "送猪车");
            } else if (listAdmissionVo.getType() == 5) {
                jsonObject.put("name", "物资车");
            }
            jsonObject.put("value", listAdmissionVo.getCount());
            jsonArray.add(jsonObject);
        }
        JSONArray array = jsonArraySort(jsonArray, "value", true);
        for (int i = 0; i < array.size(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            jsonObject.put("rank", i + 1);
        }

        return new Result(ResultCode.SUCCESS, array);
    }

    @Override
    public Result admissionTail(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        if ("".equals(type) || type == null) {
            type = "3";
        }
        //今日
        List<BillAdmissionVo> admissionVos = null;
        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId);
        if ("1".equals(type)) {
            Date timesmorning = DataUill.getTimesmorning();
            queryWrapper.ge("sub_date", timesmorning);
            admissionVos = billPersonnelAdmissionMapper.admissionTail(queryWrapper);
        } else if ("2".equals(type)) {
            // 本周
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) - 7);
            queryWrapper.ge("sub_date", calendar.getTime());
            admissionVos = billPersonnelAdmissionMapper.admissionTail(queryWrapper);
        } else if ("3".equals(type)) {
            //本月
            Date timesMonthmorning = DataUill.getTimesMonthmorning();
            queryWrapper.ge("sub_date", timesMonthmorning);
            admissionVos = billPersonnelAdmissionMapper.admissionTail(queryWrapper);
        }
        for (BillAdmissionVo admissionVo : admissionVos) {
            admissionVo.setCommitCount(admissionVo.getPassCount() + admissionVo.getRefuseCount());
        }
        return new Result(ResultCode.SUCCESS, admissionVos);
    }

    @Override
    public Result selectList(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");//来访类型
        String destId = paramsMap.get("destId");//目的地
        String startTime = paramsMap.get("startTime");
        String endTime = paramsMap.get("endTime");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "10";
        }
        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotEmpty(startTime)) {
            startTime = startTime + " 00:00:00";
            endTime = endTime + " 23:59:59";
            queryWrapper.between("sub_date", startTime, endTime);
        }
        queryWrapper.lambda().orderByDesc(BillPersonnelAdmission::getId);
        queryWrapper.eq("farm_id", farmId).eq(StringUtils.isNotBlank(destId), "dest_id", destId).eq(StringUtils.isNotBlank(type), "vistit_type", type);
        Page<BillPersonnelAdmission> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        return new Result(ResultCode.SUCCESS, billPersonnelAdmissionMapper.selectPage(page, queryWrapper));
    }

    @Override
    public void printAdmission(HttpServletResponse httpServletRequest, Map<String, String> paramsMap) throws Exception {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        List<BillPersonnelAdmission> list = new ArrayList<>();
        for (String s : split) {
            BillPersonnelAdmission admission = billPersonnelAdmissionMapper.selectById(s);
            list.add(admission);
        }
        Print.printAdmission(list);
    }

    @Override
    public Result jingJiAdmission(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        QueryWrapper<BillPersonnelAdmission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).eq("admission_type", 1).orderByDesc("sub_date")
                .last(" limit 10");
        return new Result(ResultCode.SUCCESS, billPersonnelAdmissionMapper.selectList(queryWrapper));
    }

    @Override
    public Result allCount(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String farmId = paramsMap.get("farmId");
        Calendar currCal = Calendar.getInstance();
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, currCal.get(Calendar.YEAR));
        Date time = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String firstday = format.format(time);
        QueryWrapper<BillClean> cleanQueryWrapper = new QueryWrapper<>();
        QueryWrapper<BillPcr> pcrQueryWrapper = new QueryWrapper<>();
        QueryWrapper<BillGoodsInventory> goodsInventoryQueryWrapper = new QueryWrapper<>();
        if ("21".equals(farmId)) {
            cleanQueryWrapper.eq("farm_id", 26).ge("sub_date", firstday);
            pcrQueryWrapper.eq("farm_id", 26).ge("sub_date", firstday);
            goodsInventoryQueryWrapper.ge("create_date", firstday).eq("farm_id", 26);
        } else {
            cleanQueryWrapper.eq("farm_id", farmId).ge("sub_date", firstday);
            pcrQueryWrapper.eq("farm_id", farmId).ge("sub_date", firstday);
            goodsInventoryQueryWrapper.ge("create_date", firstday).eq("farm_id", farmId);
        }
        JSONObject jsonObject = new JSONObject();
        Integer integer = pcrMapper.selectCount(pcrQueryWrapper);
        Integer integer1 = cleanMapper.selectCount(cleanQueryWrapper);
        Integer integer2 = billGoodsInventoryMapper.selectCount(goodsInventoryQueryWrapper);
        jsonObject.put("pcr", integer);
        jsonObject.put("clean", integer1);
        jsonObject.put("goods", integer2);
        jsonObject.put("allCount", integer + integer1 + integer2);
        jsonObject.put("look", "22");
        return new Result(ResultCode.SUCCESS, jsonObject);
    }

    @Override
    public Result uploadImgAndAdmissin(MultipartFile imgUrl, Integer id) throws IOException {
        BillPersonnelAdmission billPersonnelAdmission = billPersonnelAdmissionMapper.selectById(id);
        String imageCom = "";
        if (ObjectUtil.isNotEmpty(imgUrl)) {
            if (uploadImage.getImageCom(imgUrl).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片上传失败", false);
            }
            imageCom = uploadImage.getImageCom(imgUrl);
        }

        billPersonnelAdmission.setUserImgUrl(imageCom);
        billPersonnelAdmissionMapper.updateById(billPersonnelAdmission);
        SysAccountMultilevel sysAccountMultilevel = new SysAccountMultilevel();

        sysAccountMultilevel.setId(billPersonnelAdmission.getAdmissionUserId());
        sysAccountMultilevel.setImgUrl(imageCom);
        sysAccountMultilevelMapper.updateById(sysAccountMultilevel);
        return new Result(10000, "修改成功", true);
    }

    @Override
    public Result helpAddAdmission(HttpServletRequest httpServletRequest, MultipartFile userImgUrl, String admissionType, String vistitType,
                                   String userName, String phone, String workName, String papersType,
                                   String papersCode, MultipartFile imgUrl, String img, String vistitDate,
                                   String vistitReson, String destId, String destName, String farmId, String carNum,
                                   String source, String goods, String departureId, String departureName,
                                   MultipartFile picture1, MultipartFile picture2, MultipartFile picture3, Integer midId, String midName) throws IOException {
        if (!PhoneNumberValidator.isValidPhoneNumber(phone)) {
            return new Result(10001, "手机号格式错误！", false);
        }
        if (admissionType.equals("1")) {
            return new Result(10001, "紧急入场申请暂未开启！请按正常入场流程申请", false);
        }
        String picUrl = null;

        if (ObjectUtil.isNotEmpty(picture1)) {
            String pic1 = uploadImage.getImageCom(picture1);
            if (pic1.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "物品照片1上传失败", false);
            } else {
                picUrl = pic1;
            }
        }
        if (ObjectUtil.isNotEmpty(picture2)) {
            String pic2 = uploadImage.getImageCom(picture2);
            if (pic2.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "物品照片2上传失败", false);
            } else {
                picUrl = picUrl + "," + pic2;
            }
        }
        if (ObjectUtil.isNotEmpty(picture3)) {
            String pic3 = uploadImage.getImageCom(picture3);
            if (pic3.equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "物品照片3上传失败", false);
            } else {
                picUrl = picUrl + "," + pic3;
            }
        }


        Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        SysAccountMultilevel sysAccountMultilevel1;
        //先进行账号注册
        QueryWrapper<SysAccountMultilevel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", 7).and(i -> i.eq("phone", phone).or().eq("papers_code", papersCode));
        SysAccountMultilevel data = sysAccountMultilevelMapper.selectOne(queryWrapper);
        if (ObjectUtil.isEmpty(data)) {
            SysAccountMultilevel sysAccountMultilevel = new SysAccountMultilevel();
            sysAccountMultilevel.setUserName(userName);
            sysAccountMultilevel.setPapersCode(papersCode);
            sysAccountMultilevel.setPhone(phone);
            sysAccountMultilevel.setAccount(phone);
            sysAccountMultilevel.setPid(0);
            sysAccountMultilevel.setPids("0");
            sysAccountMultilevel.setPassword("123456");
            sysAccountMultilevel.setWorkName(workName);
            sysAccountMultilevel.setAccountStatus(1);
            sysAccountMultilevel.setFarmIds(farmId);
            sysAccountMultilevel.setLastFarmId(Integer.parseInt(farmId));
            sysAccountMultilevel.setPapersType(papersType);
            sysAccountMultilevel.setType(7);
            String imageCom = uploadImage.getImageCom(userImgUrl);
            if ("上传失败".equals(imageCom)) {
                return new Result(10001, "图片上传失败！", false);
            }
            sysAccountMultilevel.setImgUrl(imageCom);
            sysAccountMultilevelMapper.insert(sysAccountMultilevel);
            sysAccountMultilevel1 = sysAccountMultilevelMapper.selectOne(new QueryWrapper<SysAccountMultilevel>().eq("papers_code", papersCode));
        } else {
            sysAccountMultilevel1 = data;
        }


        BaseWashoutPoint baseWashoutPoint1 = baseWashoutPointMapper.selectOne(new QueryWrapper<BaseWashoutPoint>().lambda()
                .eq(BaseWashoutPoint::getLocationId, departureId).like(BaseWashoutPoint::getFarmIds, farmId)
                .eq(BaseWashoutPoint::getMidId, midId)
                .eq(BaseWashoutPoint::getVisitingType, vistitType));
        BaseWashoutPoint baseWashoutPoint2 = baseWashoutPointMapper.selectOne(new QueryWrapper<BaseWashoutPoint>().lambda()
                .eq(BaseWashoutPoint::getLocationId, destId).like(BaseWashoutPoint::getFarmIds, farmId)
                .eq(BaseWashoutPoint::getMidId, midId)
                .eq(BaseWashoutPoint::getVisitingType, vistitType));
        if (!departureId.equals("0")) {
            if (baseWashoutPoint1.getPointLevel() >= baseWashoutPoint2.getPointLevel()) {
                return new Result(10001, "出发地应在目的地之前!", false);
            }
        }

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        BaseFarm baseFarm = farmMapper.selectById(farmId);
        BillPersonnelAdmission billPersonnelAdmission = new BillPersonnelAdmission();
        billPersonnelAdmission.setFarmName(baseFarm.getFarmName());
        billPersonnelAdmission.setAdmissionType(Integer.parseInt(admissionType));
        billPersonnelAdmission.setVistitType(Integer.parseInt(vistitType));
        billPersonnelAdmission.setDestId(Integer.parseInt(destId));
        billPersonnelAdmission.setDestName(destName);
        billPersonnelAdmission.setDepartureName(departureName);
        billPersonnelAdmission.setDepartureId(Integer.valueOf(departureId));
        billPersonnelAdmission.setAdmissionUserId(sysAccountMultilevel1.getId());
        billPersonnelAdmission.setAdmissionUserName(userName);
        billPersonnelAdmission.setVistitDate(LocalDateTime.parse(vistitDate, dateTimeFormatter));
        billPersonnelAdmission.setSubDate(LocalDateTime.now());
        billPersonnelAdmission.setPhone(phone);
        billPersonnelAdmission.setWorkName(workName);
        billPersonnelAdmission.setPapersType(Integer.parseInt(papersType));
        billPersonnelAdmission.setPapersCode(papersCode);
        billPersonnelAdmission.setUserImgUrl(sysAccountMultilevel1.getImgUrl());
        billPersonnelAdmission.setPicUrl(picUrl);
        if (!"".equals(picUrl) || null != picUrl) {
            billPersonnelAdmission.setIsCarry(1);
        }
        if (ObjectUtil.isNotEmpty(carNum)) {
            billPersonnelAdmission.setCarNum(carNum);
        }
//        if (ObjectUtil.isNotEmpty(imgUrl)) {
//            String s = uploadImage.getImageCom(imgUrl);
//            if (s.equals("上传失败")) {
//                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//                return new Result(10001, "图片上传失败", false);
//            }
//            billPersonnelAdmission.setImgUrl(s);
//        }
        if (ObjectUtil.isNotEmpty(img)) {

        }
        billPersonnelAdmission.setImgUrl(sysAccountMultilevel1.getImgUrl());
        if (ObjectUtil.isNotEmpty(source)) {
            billPersonnelAdmission.setSource(source);
        }
        billPersonnelAdmission.setVistitReson(vistitReson);
        billPersonnelAdmission.setFarmId(Integer.parseInt(farmId));
        BaseProcess baseProcess = new BaseProcess();
        baseProcess.setDestName(destName);
        baseProcess.setDestId(Integer.parseInt(destId));
        baseProcess.setAdmissionUserId(userId);
        baseProcess.setAdmissionUserName(userName);
        baseProcess.setSubDate(DateTime.now());
        baseProcess.setCurrentStatus("0");

        String allLocation = departureName;
        String allLocationId = "0";
        String allLocationStatus = "0";
        String allFlowId = "";

        List<BaseWashoutPoint> baseWashoutPoints = new ArrayList<>();
        if (ObjectUtil.isEmpty(baseWashoutPoint1)) {
            System.out.println("出发地为场外");
            baseWashoutPoints = baseWashoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>().lambda()
                    .le(BaseWashoutPoint::getPointLevel, baseWashoutPoint2.getPointLevel())
                    .eq(BaseWashoutPoint::getMidId, midId)
                    .eq(BaseWashoutPoint::getVisitingType, vistitType).like(BaseWashoutPoint::getFarmIds, farmId)
                    .orderByAsc(BaseWashoutPoint::getPointLevel));
        } else {
            System.out.println("出发地不为场外");
            baseWashoutPoints = baseWashoutPointMapper.selectList(new QueryWrapper<BaseWashoutPoint>().lambda()
                    .gt(BaseWashoutPoint::getPointLevel, baseWashoutPoint1.getPointLevel())
                    .le(BaseWashoutPoint::getPointLevel, baseWashoutPoint2.getPointLevel())
                    .eq(BaseWashoutPoint::getMidId, midId)
                    .eq(BaseWashoutPoint::getVisitingType, vistitType).like(BaseWashoutPoint::getFarmIds, farmId)
                    .orderByAsc(BaseWashoutPoint::getPointLevel));
        }
        System.out.println("所有洗消点:" + baseWashoutPoints);
        for (BaseWashoutPoint baseWashoutPoint : baseWashoutPoints) {
            allLocation = allLocation + "," + baseLocationMapper.selectById(baseWashoutPoint.getLocationId()).getLocationName();
            allLocationId = allLocationId + "," + baseWashoutPoint.getLocationId();
            allLocationStatus = allLocationStatus + "," + "0";
            allFlowId = allFlowId + "0," + baseWashoutPoint.getFlowList();

            allLocation = allLocation + "," + baseWashoutPoint.getTailLocationName();
            allLocationId = allLocationId + "," + "0";
            allLocationStatus = allLocationStatus + "," + "0";
            if (!baseWashoutPoint.equals(baseWashoutPoints.get(baseWashoutPoints.size() - 1))) {
                allFlowId = allFlowId + ",";
            }
        }
        if (admissionType.equals("1")) {
            baseProcess.setAllLocation(departureName);
            baseProcess.setAllLocationId("0");
            baseProcess.setAllLocationStatus("0");
        } else {
            baseProcess.setAllLocation(allLocation);
            baseProcess.setAllLocationId(allLocationId);
            baseProcess.setAllLocationStatus(allLocationStatus);
        }

        baseProcess.setAllFlowId(allFlowId + ",0");
        baseProcess.setCurrentFlowId("0");
        baseProcess.setCurrentLocation("场外");
        baseProcess.setCurrentLocationId("0");
        baseProcess.setFarmId(Integer.parseInt(farmId));
        baseProcessMapper.insert(baseProcess);
        billPersonnelAdmission.setProcessId(baseProcess.getId());
        billPersonnelAdmission.setMidId(midId);
        billPersonnelAdmission.setMidName(midName);
        billPersonnelAdmissionMapper.insert(billPersonnelAdmission);
        ArrayList<String> phoneList = new ArrayList<>();
        List<SysAccountMultilevel> safe = sysAccountMultilevelMapper.selectList(new QueryWrapper<SysAccountMultilevel>().eq("farm_ids", farmId).eq("type", 3));
        for (SysAccountMultilevel sysAccountMultilevel : safe) {
            phoneList.add(sysAccountMultilevel.getPhone());
        }
        if (ObjectUtil.isNotEmpty(goods)) {
            ObjectMapper objectMapper = new ObjectMapper();
            List<BillGoodsInventory> admissionAndGoods = objectMapper.readValue(goods, new TypeReference<List<BillGoodsInventory>>() {
            });
            for (BillGoodsInventory good : admissionAndGoods) {
                good.setBillAdmission(billPersonnelAdmission.getId());
                BaseLocation baseLocation1 = baseLocationMapper.selectById(destId);
                good.setTestLocation(baseLocation1.getLocationName());
                good.setTestLocationId(baseLocation1.getId());
                good.setCreateDate(new Date());
                good.setFarmId(farmId);
                billGoodsInventoryMapper.insert(good);
            }
        }
        return Result.SUCCESS();

    }

    @Override
    public Result handleAdmissionBatch(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) throws IOException {
        String ids = paramsMap.get("ids");
        String type = paramsMap.get("type");
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
        String[] strings1 = ids.split(",");
        for (String s : strings1) {
            BillPersonnelAdmission billPersonnelAdmission = billPersonnelAdmissionMapper.selectById(s);
            billPersonnelAdmission.setPassDate(LocalDateTime.now());
            billPersonnelAdmission.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
            billPersonnelAdmission.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
            BaseProcess baseProcess = baseProcessMapper.selectById(billPersonnelAdmission.getProcessId());

            ArrayList<String> phoneList = new ArrayList<>();
            phoneList.add(billPersonnelAdmission.getPhone());
            if (StringUtils.isBlank(baseProcess.getUpdateDate())) {
                baseProcess.setUpdateDate(LocalDateTime.now().format(dateTimeFormatter));
            } else {
                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + LocalDateTime.now().format(dateTimeFormatter));
            }
            if (type.equals("0")) {
                //拒绝
                billPersonnelAdmission.setBillStatus(2);
                baseProcess.setProcessType(2);
                baseProcess.setCurrentStatus("2");
                String status = baseProcess.getAllLocationStatus();
                String[] split1 = status.split(",");
                split1[0] = "3";
                baseProcess.setAllLocationStatus(split1[0] + status.substring(1));
            } else {
                if (billPersonnelAdmission.getBillStatus() != 0) {
                    return new Result(10001, "该订单已通过，无需再次点击！", false);
                }


                //新增人员转场，不需要流程，入场审批通过后直接下发门禁，流程结束
                if (billPersonnelAdmission.getMidId()==60||billPersonnelAdmission.getMidId()==61
                        ||billPersonnelAdmission.getMidId()==67||billPersonnelAdmission.getMidId()==68) {
                    DeviceList deviceList = deviceListMapper.selectOne(new QueryWrapper<DeviceList>().eq("farm_id", billPersonnelAdmission.getFarmId()));
                    PeopleList peopleList = new PeopleList();
                    peopleList.setFarmId(billPersonnelAdmission.getFarmId().toString());
                    peopleList.setDeviceCode(deviceList.getDeviceCode());
                    peopleList.setUserId(billPersonnelAdmission.getProcessId().toString());
                    peopleList.setUserName(billPersonnelAdmission.getAdmissionUserName());
                    peopleList.setPassword("123456");
                    peopleList.setType(0);
                    peopleList.setCreateTime(new Date());
                    Date now = new Date();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(now);
                    calendar.add(Calendar.HOUR_OF_DAY, 6);
                    Date date1 = calendar.getTime();
                    peopleList.setOutTime(date1);
                    SysAccountMultilevel sysAccountMultilevel = multilevelMapper.selectById(billPersonnelAdmission.getAdmissionUserId());
                    peopleList.setImgUrl(sysAccountMultilevel.getImgUrl());
                    peopleListMapper.insert(peopleList);

                    billPersonnelAdmission.setBillStatus(1);
                    baseProcess.setProcessType(1);
                    String status = baseProcess.getAllLocationStatus();
                    String replace = status.replace("0", "2");
                    baseProcess.setCurrentStatus(replace);
                    baseProcess.setAllLocationStatus(replace);
                    baseProcessMapper.updateById(baseProcess);
                    billPersonnelAdmissionMapper.updateById(billPersonnelAdmission);
                    //如果有物资，则把物资的申请单给结束
                    BillGoodsInventory goodsInventory = billGoodsInventoryMapper.selectOne(new QueryWrapper<BillGoodsInventory>().eq("process_id", baseProcess.getId()));
                    if (ObjectUtil.isNotEmpty(goodsInventory)) {
                        goodsInventory.setBillStatus(3);
                        billGoodsInventoryMapper.updateById(goodsInventory);
                    }
                    return Result.SUCCESS();
                }
                //通过
                billPersonnelAdmission.setBillStatus(1);

                //获取下一步位置
                String allLocationId = baseProcess.getAllLocationId();
                String[] strings = allLocationId.split(",");
                String nextLocationId;
                BaseLocation baseLocation = new BaseLocation();
                if (billPersonnelAdmission.getAdmissionType() == 0) {
                    nextLocationId = strings[1];
                    baseLocation = baseLocationMapper.selectById(nextLocationId);
                }

                //人员通过只要经过待检点就要给门禁
                if (billPersonnelAdmission.getVistitType() == 0 && baseLocation.getId() == 45) {
                    DeviceList deviceList = deviceListMapper.selectOne(new QueryWrapper<DeviceList>().eq("farm_id", "1"));
                    Date now = new Date();
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(now);
                    calendar.add(Calendar.HOUR_OF_DAY, 24);
                    Date date1 = calendar.getTime();
                    SysAccountMultilevel sysAccountMultilevel = multilevelMapper.selectById(billPersonnelAdmission.getAdmissionUserId());
                    PeopleList peopleList1 = new PeopleList();
                    peopleList1.setFarmId("1");
                    peopleList1.setDeviceCode(deviceList.getDeviceCode());
                    peopleList1.setUserId(billPersonnelAdmission.getProcessId().toString());
                    peopleList1.setUserName(billPersonnelAdmission.getAdmissionUserName());
                    peopleList1.setPassword("123456");
                    peopleList1.setType(0);
                    peopleList1.setCreateTime(new Date());
                    peopleList1.setOutTime(date1);
                    peopleList1.setImgUrl(sysAccountMultilevel.getImgUrl());
                    peopleListMapper.insert(peopleList1);
                }

                if (billPersonnelAdmission.getAdmissionType() == 0) {
                    String[] split = baseProcess.getCurrentStatus().split(",");
                    split[split.length - 1] = "1";
                    StringBuilder result = new StringBuilder();
                    for (String element : split) {
                        result.append(element).append(",");
                    }
                    result.append("0");
                    baseProcess.setCurrentStatus(result.toString());
                    baseProcess.setCurrentLocation(baseProcess.getCurrentLocation() + "," + baseLocation.getLocationName());
                    baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId() + "," + baseLocation.getId());

                    String status = baseProcess.getAllLocationStatus();
                    String[] split1 = status.split(",");
                    split1[0] = "2";
                    split1[1] = "1";
                    baseProcess.setAllLocationStatus(split1[0] + "," + split1[1] + status.substring(3));
                    String[] split2 = baseProcess.getAllFlowId().split(",");
                    String nextFlowId = split2[baseProcess.getCurrentFlowId().split(",").length];
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextFlowId);

                    if (Integer.valueOf(nextFlowId) == 1) {
                        BillSampling billSampling = (BillSampling) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                                billPersonnelAdmission.getVistitType(),
                                billPersonnelAdmission.getDestName(),
                                billPersonnelAdmission.getDestId(),
                                billPersonnelAdmission.getAdmissionUserId(),
                                billPersonnelAdmission.getAdmissionUserName(),
                                billPersonnelAdmission.getPhone(),
                                billPersonnelAdmission.getVistitDate(),
                                baseLocation.getId(),
                                baseLocation.getLocationName(),
                                billPersonnelAdmission.getFarmId(),
                                baseProcess.getId(),
                                billPersonnelAdmission.getDepartureName(),
                                billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                                billPersonnelAdmission.getFarmName());
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                            billSampling.setCarNum(billPersonnelAdmission.getCarNum());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                            billSampling.setResource(billPersonnelAdmission.getResource());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                            billSampling.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                            billSampling.setRemark(billPersonnelAdmission.getRemark());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                            billSampling.setCleanId(billPersonnelAdmission.getCleanId());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                            billSampling.setCleanName(billPersonnelAdmission.getCleanName());
                        }
                        billSamplingMapper.insert(billSampling);
                    } else if (Integer.valueOf(nextFlowId) == 2) {
                        BillPcr billPcr = (BillPcr) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                                billPersonnelAdmission.getVistitType(),
                                billPersonnelAdmission.getDestName(),
                                billPersonnelAdmission.getDestId(),
                                billPersonnelAdmission.getAdmissionUserId(),
                                billPersonnelAdmission.getAdmissionUserName(),
                                billPersonnelAdmission.getPhone(),
                                billPersonnelAdmission.getVistitDate(),
                                baseLocation.getId(),
                                baseLocation.getLocationName(),
                                billPersonnelAdmission.getFarmId(),
                                baseProcess.getId(),
                                billPersonnelAdmission.getDepartureName(),
                                billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                                billPersonnelAdmission.getFarmName());
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                            billPcr.setCarNum(billPersonnelAdmission.getCarNum());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                            billPcr.setResource(billPersonnelAdmission.getResource());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                            billPcr.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                            billPcr.setRemark(billPersonnelAdmission.getRemark());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                            billPcr.setCleanId(billPersonnelAdmission.getCleanId());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                            billPcr.setCleanName(billPersonnelAdmission.getCleanName());
                        }
                        pcrMapper.insert(billPcr);
                    } else if (Integer.valueOf(nextFlowId) == 3) {
                        BillClean billClean = (BillClean) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                                billPersonnelAdmission.getVistitType(),
                                billPersonnelAdmission.getDestName(),
                                billPersonnelAdmission.getDestId(),
                                billPersonnelAdmission.getAdmissionUserId(),
                                billPersonnelAdmission.getAdmissionUserName(),
                                billPersonnelAdmission.getPhone(),
                                billPersonnelAdmission.getVistitDate(),
                                baseLocation.getId(),
                                baseLocation.getLocationName(),
                                billPersonnelAdmission.getFarmId(),
                                baseProcess.getId(),
                                billPersonnelAdmission.getDepartureName(),
                                billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                                billPersonnelAdmission.getFarmName());
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                            billClean.setCarNum(billPersonnelAdmission.getCarNum());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                            billClean.setResource(billPersonnelAdmission.getResource());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                            billClean.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                            billClean.setRemark(billPersonnelAdmission.getRemark());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                            billClean.setCleanId(billPersonnelAdmission.getCleanId());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                            billClean.setCleanName(billPersonnelAdmission.getCleanName());
                        }
                        cleanMapper.insert(billClean);
                    } else if (Integer.valueOf(nextFlowId) == 4) {
                        BillIsolate billIsolate = (BillIsolate) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                                billPersonnelAdmission.getVistitType(),
                                billPersonnelAdmission.getDestName(),
                                billPersonnelAdmission.getDestId(),
                                billPersonnelAdmission.getAdmissionUserId(),
                                billPersonnelAdmission.getAdmissionUserName(),
                                billPersonnelAdmission.getPhone(),
                                billPersonnelAdmission.getVistitDate(),
                                baseLocation.getId(),
                                baseLocation.getLocationName(),
                                billPersonnelAdmission.getFarmId(),
                                baseProcess.getId(),
                                billPersonnelAdmission.getDepartureName(),
                                billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                                billPersonnelAdmission.getFarmName());
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                            billIsolate.setCarNum(billPersonnelAdmission.getCarNum());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                            billIsolate.setResource(billPersonnelAdmission.getResource());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                            billIsolate.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                            billIsolate.setRemark(billPersonnelAdmission.getRemark());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                            billIsolate.setCleanId(billPersonnelAdmission.getCleanId());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                            billIsolate.setCleanName(billPersonnelAdmission.getCleanName());
                        }
                        billIsolateMapper.insert(billIsolate);
                    } else if (Integer.valueOf(nextFlowId) == 5) {
                        BillDry billDry = (BillDry) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                                billPersonnelAdmission.getVistitType(),
                                billPersonnelAdmission.getDestName(),
                                billPersonnelAdmission.getDestId(),
                                billPersonnelAdmission.getAdmissionUserId(),
                                billPersonnelAdmission.getAdmissionUserName(),
                                billPersonnelAdmission.getPhone(),
                                billPersonnelAdmission.getVistitDate(),
                                baseLocation.getId(),
                                baseLocation.getLocationName(),
                                billPersonnelAdmission.getFarmId(),
                                baseProcess.getId(),
                                billPersonnelAdmission.getDepartureName(),
                                billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                                billPersonnelAdmission.getFarmName());
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                            billDry.setCarNum(billPersonnelAdmission.getCarNum());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                            billDry.setResource(billPersonnelAdmission.getResource());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                            billDry.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                            billDry.setRemark(billPersonnelAdmission.getRemark());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                            billDry.setCleanId(billPersonnelAdmission.getCleanId());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                            billDry.setCleanName(billPersonnelAdmission.getCleanName());
                        }
                        dryMapper.insert(billDry);
                    } else if (Integer.valueOf(nextFlowId) == 6) {
                        BillCleanBefore billCleanBefore = (BillCleanBefore) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                                billPersonnelAdmission.getVistitType(),
                                billPersonnelAdmission.getDestName(),
                                billPersonnelAdmission.getDestId(),
                                billPersonnelAdmission.getAdmissionUserId(),
                                billPersonnelAdmission.getAdmissionUserName(),
                                billPersonnelAdmission.getPhone(),
                                billPersonnelAdmission.getVistitDate(),
                                baseLocation.getId(),
                                baseLocation.getLocationName(),
                                billPersonnelAdmission.getFarmId(),
                                baseProcess.getId(),
                                billPersonnelAdmission.getDepartureName(),
                                billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                                billPersonnelAdmission.getFarmName());
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                            billCleanBefore.setCarNum(billPersonnelAdmission.getCarNum());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                            billCleanBefore.setResource(billPersonnelAdmission.getResource());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                            billCleanBefore.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                            billCleanBefore.setRemark(billPersonnelAdmission.getRemark());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                            billCleanBefore.setCleanId(billPersonnelAdmission.getCleanId());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                            billCleanBefore.setCleanName(billPersonnelAdmission.getCleanName());
                        }
                        billCleanBeforeMapper.insert(billCleanBefore);
                    } else {
                        BillGoodsInventory billCleanBefore = (BillGoodsInventory) flowUtil.getEntity(Integer.valueOf(nextFlowId),
                                billPersonnelAdmission.getVistitType(),
                                billPersonnelAdmission.getDestName(),
                                billPersonnelAdmission.getDestId(),
                                billPersonnelAdmission.getAdmissionUserId(),
                                billPersonnelAdmission.getAdmissionUserName(),
                                billPersonnelAdmission.getPhone(),
                                billPersonnelAdmission.getVistitDate(),
                                baseLocation.getId(),
                                baseLocation.getLocationName(),
                                billPersonnelAdmission.getFarmId(),
                                baseProcess.getId(),
                                billPersonnelAdmission.getDepartureName(),
                                billPersonnelAdmission.getDepartureId(), billPersonnelAdmission.getMidId(), billPersonnelAdmission.getMidName(),
                                billPersonnelAdmission.getFarmName());
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCarNum())) {
                            billCleanBefore.setCarNum(billPersonnelAdmission.getCarNum());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getResource())) {
                            billCleanBefore.setResource(billPersonnelAdmission.getResource());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getFeedImgUrl())) {
                            billCleanBefore.setFeedImgUrl(billPersonnelAdmission.getFeedImgUrl());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getRemark())) {
                            billCleanBefore.setRemark(billPersonnelAdmission.getRemark());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanId())) {
                            billCleanBefore.setCleanId(billPersonnelAdmission.getCleanId());
                        }
                        if (ObjectUtil.isNotEmpty(billPersonnelAdmission.getCleanName())) {
                            billCleanBefore.setCleanName(billPersonnelAdmission.getCleanName());
                        }
                        billGoodsInventoryMapper.insert(billCleanBefore);
                    }
                } else {
                    baseProcess.setCurrentStatus("1");
                    baseProcess.setProcessType(1);
                    baseProcess.setAllLocationStatus("2");
                    //TODO 紧急入场通过先不处理
                }

            }
            baseProcessMapper.updateById(baseProcess);
            billPersonnelAdmissionMapper.updateById(billPersonnelAdmission);
        }

        return Result.SUCCESS();
    }

    @Override
    public Result removeAdmission(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        BillPersonnelAdmission billPersonnelAdmission = billPersonnelAdmissionMapper.selectById(id);
        if (billPersonnelAdmission.getBillStatus() != 0) {
            return new Result(10001, "当前记录已审批 不支持撤回", false);
        }
        Integer processId = billPersonnelAdmission.getProcessId();
        pcrMapper.delete(new QueryWrapper<BillPcr>().eq("process_id", processId));
        cleanMapper.delete(new QueryWrapper<BillClean>().eq("process_id", processId));
        dryMapper.delete(new QueryWrapper<BillDry>().eq("process_id", processId));
        billGoodsInventoryMapper.delete(new QueryWrapper<BillGoodsInventory>().eq("process_id", processId));
        samplingMapper.delete(new QueryWrapper<BillSampling>().eq("process_id", processId));
        billPersonnelAdmissionMapper.deleteById(id);
        processMapper.deleteById(processId);
        return Result.SUCCESS();
    }

    /*数组排序*/
    public static JSONArray jsonArraySort(JSONArray jsonArr, String sortKey, boolean is_desc) {
        //存放排序结果json数组
        JSONArray sortedJsonArray = new JSONArray();
        //用于排序的list
        List<JSONObject> jsonValues = new ArrayList<JSONObject>();
        //将参数json数组每一项取出，放入list

        for (int i = 0; i < jsonArr.size(); i++) {
            jsonValues.add(jsonArr.getJSONObject(i));
        }
        //快速排序，重写compare方法，完成按指定字段比较，完成排序
        Collections.sort(jsonValues, new Comparator<JSONObject>() {
            //排序字段
            private final String KEY_NAME = sortKey;

            //重写compare方法
            @Override
            public int compare(JSONObject a, JSONObject b) {
                //如果用String接会导致一位数和两位数混合比对的时候不能准确比出来，要用int类型接
                //String valA = new String();
                //String valB = new String();
                Float valA = 0.00f;
                Float valB = 0.00f;
                try {
                    valA = Float.parseFloat(a.getString(KEY_NAME));
                    valB = Float.parseFloat(b.getString(KEY_NAME));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                //是升序还是降序
                if (is_desc) {
                    return -valA.compareTo(valB);
                } else {
                    return -valB.compareTo(valA);
                }

            }
        });
        //将排序后结果放入结果jsonArray
        for (int i = 0; i < jsonArr.size(); i++) {
            sortedJsonArray.add(jsonValues.get(i));
        }

        return sortedJsonArray;
    }

    @Autowired
    private BillPcrMapper pcrMapper;
    @Autowired
    private BaseProcessMapper processMapper;
    @Autowired
    private BaseLocationMapper locationMapper;
    @Autowired
    private BillCleanMapper cleanMapper;
    @Autowired
    private BillIsolateMapper isolateMapper;
    @Autowired
    private BillSamplingMapper samplingMapper;
    @Autowired
    private BillCleanBeforeMapper beforeMapper;
    @Autowired
    private BaseWashoutPointMapper washoutPointMapper;


    @Override
    public Result listPrc(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");//类型
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "20";
        }
        Page<BillPcr> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        if ("".equals(type) || null == type) {
            type = "0";
        }
        String farmId = paramsMap.get("farmId");
        QueryWrapper<BillPcr> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("farm_id", farmId).orderByDesc("sub_date");
        if ("0".equals(type)) {
            queryWrapper.in("pcr_type", 1, 2, 3);
        }
        if ("1".equals(type)) {
            queryWrapper.eq("pcr_type", 1);
        }
        if ("2".equals(type)) {
            queryWrapper.eq("pcr_type", 2);
        }
        if ("3".equals(type)) {
            queryWrapper.eq("pcr_type", 3);
        }
        QueryWrapper<BillPcr> queryWrapper1 = new QueryWrapper<>();

        queryWrapper1.eq("farm_id", farmId).orderByDesc("sub_date").eq("pcr_type", 1);
        return new Result(ResultCode.SUCCESS, pcrMapper.selectPage(page, queryWrapper), pcrMapper.selectCount(queryWrapper1));
    }

    @Override
    public Result listById(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return new Result(ResultCode.SUCCESS, pcrMapper.selectById(id));
    }

    @Override
    public Result editStatus(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) throws ParseException {
        String farmId = paramsMap.get("farmId");
        String type = paramsMap.get("type");
        String id = paramsMap.get("id");
        String data = paramsMap.get("data");

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (StringUtils.isBlank(data)) {
            data = sdf.format(new Date());
        }
        BillPcr billPcr = pcrMapper.selectById(id);

        if (billPcr.getPcrType() != 1) {
            return new Result(10001, "pcr已通过或拒绝，无需再次复审", false);
        }

        QueryWrapper<BaseProcess> processQueryWrapper = new QueryWrapper<>();
        processQueryWrapper.eq("farm_id", farmId).eq("id", billPcr.getProcessId());
        BaseProcess baseProcess = processMapper.selectOne(processQueryWrapper);//进程

        QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq("id", billPcr.getTestLocationId());
        BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前pcr检测所在的位置

        String allLocationId = baseProcess.getAllLocationId();//所有的位置id
        String allFlowId = baseProcess.getAllFlowId();//所有的流程id
        String currentFlowId = baseProcess.getCurrentFlowId();//当前已经完成的流程id
        String substring1 = allFlowId.substring(currentFlowId.length() + 1, allFlowId.length());
        String[] split = substring1.split(",");
        Integer nextId = Integer.parseInt(split[0]);
        if (baseProcess.getProcessType() == 2 || baseProcess.getProcessType() == 1) {
            return new Result(10001, "修改失败！该申请已提前结束或者被拒绝", false);
        } else {
            //说明当前不是目的地
            if ("1".equals(type)) {

                Integer pcrTime = washoutPointMapper.selectOne(new QueryWrapper<BaseWashoutPoint>().eq("farm_ids", farmId)
                        .eq("visiting_type", billPcr.getVistitType()).eq("mid_id", billPcr.getMidId()).eq("location_id", baseLocation.getId())).getPcrTime();
                billPcr.setQualifiedDate(pcrTime);
                billPcr.setBillStatus(1);
                billPcr.setPcrType(2);
                billPcr.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billPcr.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billPcr.setTestLocation(baseLocation.getLocationName());
                billPcr.setTestLocationId(baseLocation.getId());
                billPcr.setPassDate(sdf.parse(sdf.format(new Date())));
                billPcr.setCheckDate(sdf.parse(data));

                Object newObject = BillPcrServiceImpl.getNewObject(nextId, billPcr.getDestName(), billPcr.getDestId(), billPcr.getAdmissionUserName(),
                        billPcr.getAdmissionUserId(), billPcr.getVistitDate(), billPcr.getSubDate(),
                        billPcr.getProcessId(), billPcr.getFarmId(), billPcr.getTestLocation(), billPcr.getTestLocationId(), billPcr.getVistitType(),
                        billPcr.getPhone(), billPcr.getDepartureName(), billPcr.getDepartureId(), billPcr.getMidId(), billPcr.getMidName(),billPcr.getFarmName());
                if (nextId == 0) {
                    if (split.length == 1) {
                        baseProcess.setProcessType(1);
                        String[] strings = allLocationId.split(",");
                        String ss2 = "";
                        for (int i = 0; i < strings.length; i++) {
                            strings[i].replaceAll(strings[i], "2");
                            if (i == strings.length - 1) {
                                ss2 = ss2 + strings[i].replaceAll(strings[i], "2");
                            } else {
                                ss2 = strings[i].replaceAll(strings[i], "2") + "," + ss2;
                            }
                        }
                        baseProcess.setAllLocationStatus(ss2);
                    } else {
                        String[] strings = allLocationId.split(",");
                        Integer index = 0;
                        for (int i = 0; i < strings.length; i++) {
                            if ((baseLocation.getId().toString()).equals(strings[i])) {
                                index = i;
                            }
                        }
                        QueryWrapper<BaseLocation> queryWrapper1 = new QueryWrapper();
                        queryWrapper1.eq("id", strings[index + 2]);
                        BaseLocation location = locationMapper.selectOne(queryWrapper1);
                        FlowUtil flowUtil = new FlowUtil();
                        Object entity = flowUtil.getEntity(Integer.parseInt(split[1]), billPcr.getVistitType(), billPcr.getDestName()
                                , billPcr.getDestId(), billPcr.getAdmissionUserId(), billPcr.getAdmissionUserName(), billPcr.getPhone(),
                                billPcr.getVistitDate(), location.getId(), location.getLocationName(), billPcr.getFarmId()
                                , baseProcess.getId(), billPcr.getDepartureName(), billPcr.getDepartureId()
                                , billPcr.getMidId(), billPcr.getMidName(),billPcr.getFarmName());
                        if (Integer.parseInt(split[1]) == 1) {
                            //新增采样
                            BillSampling billSampling = (BillSampling) entity;
                            if (billPcr.getVistitType() != 0) {
                                billSampling.setCarNum(billPcr.getCarNum());
                            }
                            if (billPcr.getVistitType() == 5) {
                                billSampling.setResource(billPcr.getResource());
                                billSampling.setFeedImgUrl(billPcr.getFeedImgUrl());
                                billSampling.setRemark(billPcr.getRemark());
                            }
                            billSamplingMapper.insert(billSampling);
                        } else if (Integer.parseInt(split[1]) == 2) {
                            //新增pcr
                            BillPcr billClean = (BillPcr) entity;
                            if (billPcr.getVistitType() != 0) {
                                billClean.setCarNum(billPcr.getCarNum());
                            }
                            if (billPcr.getVistitType() == 5) {
                                billClean.setResource(billPcr.getResource());
                                billClean.setFeedImgUrl(billPcr.getFeedImgUrl());
                                billClean.setRemark(billPcr.getRemark());
                            }
                            billClean.setSamplingId(Integer.parseInt(id));
                            pcrMapper.insert(billClean);
                        } else if (Integer.parseInt(split[1]) == 3) {
                            //新增洗澡
                            BillClean billClean = (BillClean) entity;
                            if (billPcr.getVistitType() != 0) {
                                billClean.setCarNum(billPcr.getCarNum());
                            }
                            if (billPcr.getVistitType() == 5) {
                                billClean.setResource(billPcr.getResource());
                                billClean.setFeedImgUrl(billPcr.getFeedImgUrl());
                                billClean.setRemark(billPcr.getRemark());
                            }
                            cleanMapper.insert(billClean);
                        } else if (Integer.parseInt(split[1]) == 4) {
                            //新增隔离
                            BillIsolate pcr = (BillIsolate) entity;
                            if (billPcr.getVistitType() != 0) {
                                pcr.setCarNum(billPcr.getCarNum());
                            }
                            if (billPcr.getVistitType() == 5) {
                                pcr.setResource(billPcr.getResource());
                                pcr.setFeedImgUrl(billPcr.getFeedImgUrl());
                                pcr.setRemark(billPcr.getRemark());
                            }
                            isolateMapper.insert(pcr);
                        } else if (Integer.parseInt(split[1]) == 5) {
                            BillDry billClean = (BillDry) entity;
                            if (billPcr.getVistitType() != 0) {
                                billClean.setCarNum(billPcr.getCarNum());
                            }
                            if (billPcr.getVistitType() == 5) {
                                billClean.setResource(billPcr.getResource());
                                billClean.setFeedImgUrl(billPcr.getFeedImgUrl());
                                billClean.setRemark(billPcr.getRemark());
                            }
                            dryMapper.insert(billClean);
                            //新增烘干
                        } else if (Integer.parseInt(split[1]) == 6) {
                            //新增洗澡前拍照
                            BillCleanBefore before = (BillCleanBefore) entity;
                            if (billPcr.getVistitType() != 0) {
                                before.setCarNum(billPcr.getCarNum());
                            }
                            if (billPcr.getVistitType() == 5) {
                                before.setResource(billPcr.getResource());
                                before.setFeedImgUrl(billPcr.getFeedImgUrl());
                                before.setRemark(billPcr.getRemark());
                            }
                            billCleanBeforeMapper.insert(before);
                        } else if (Integer.parseInt(split[1]) == 7) {
                            //新增洗澡前拍照
                            BillGoodsInventory before = (BillGoodsInventory) entity;
                            if (billPcr.getVistitType() != 0) {
                                before.setCarNum(billPcr.getCarNum());
                            }
                            if (billPcr.getVistitType() == 5) {
                                before.setResource(billPcr.getResource());
                                before.setFeedImgUrl(billPcr.getFeedImgUrl());
                                before.setRemark(billPcr.getRemark());
                            }
                            inventoryMapper.insert(before);
                        }
                        baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId + "," + split[1]);
                        Integer locationId = billPcr.getTestLocationId();
                        String[] split2 = allLocationId.split(",");
                        int a = 0;
                        for (int i = 0; i < split2.length; i++) {
                            if (locationId.toString().equals(split2[i])) {
                                a = i + 1;
                            }
                        }
                        int indexOf = 2 * (a - 2);//当前位置在所有流程位置中的下标

                        String substring = baseProcess.getAllLocationStatus().substring(0, indexOf + 3);//截取已经过的位置状态
                        String[] split1 = substring.split(",");
                        String sb2 = "";
                        for (int i = 0; i < split1.length; i++) {
                            if (i == split1.length - 1) {
                                sb2 = sb2 + split1[i].replaceAll(split1[i], "2");
                            } else {
                                sb2 = split1[i].replaceAll(split1[i], "2") + "," + sb2;
                            }
                        }
                        sb2 = sb2 + baseProcess.getAllLocationStatus().substring(indexOf + 3);
                        StringBuilder sb = new StringBuilder(sb2);
                        sb.replace(indexOf + 4, indexOf + 5, "2");
                        sb.replace(indexOf + 6, indexOf + 7, "1");
                        baseProcess.setAllLocationStatus(sb.toString());
                    }
                } else if (nextId == 1) {
                    //新增采样
                    BillSampling billSampling = (BillSampling) newObject;
                    if (billPcr.getVistitType() != 0) {
                        billSampling.setCarNum(billPcr.getCarNum());
                    }
                    if (billPcr.getVistitType() == 5) {
                        billSampling.setResource(billPcr.getResource());
                        billSampling.setFeedImgUrl(billPcr.getFeedImgUrl());
                        billSampling.setRemark(billPcr.getRemark());
                    }
                    billSamplingMapper.insert(billSampling);
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
                } else if (nextId == 2) {
                    //新增pcr
                    BillPcr billClean = (BillPcr) newObject;
                    if (billPcr.getVistitType() != 0) {
                        billClean.setCarNum(billPcr.getCarNum());
                    }
                    if (billPcr.getVistitType() == 5) {
                        billClean.setResource(billPcr.getResource());
                        billClean.setFeedImgUrl(billPcr.getFeedImgUrl());
                        billClean.setRemark(billPcr.getRemark());
                    }
                    billClean.setSamplingId(Integer.parseInt(id));
                    pcrMapper.insert(billClean);
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
                } else if (nextId == 3) {
                    //新增洗澡
                    BillClean billClean = (BillClean) newObject;
                    if (billPcr.getVistitType() != 0) {
                        billClean.setCarNum(billPcr.getCarNum());
                    }
                    if (billPcr.getVistitType() == 5) {
                        billClean.setResource(billPcr.getResource());
                        billClean.setFeedImgUrl(billPcr.getFeedImgUrl());
                        billClean.setRemark(billPcr.getRemark());
                    }
                    cleanMapper.insert(billClean);
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
                } else if (nextId == 4) {
                    //新增隔离
                    BillIsolate pcr = (BillIsolate) newObject;
                    if (billPcr.getVistitType() != 0) {
                        pcr.setCarNum(billPcr.getCarNum());
                    }
                    if (billPcr.getVistitType() == 5) {
                        pcr.setResource(billPcr.getResource());
                        pcr.setFeedImgUrl(billPcr.getFeedImgUrl());
                        pcr.setRemark(billPcr.getRemark());
                    }
                    isolateMapper.insert(pcr);
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
                } else if (nextId == 5) {
                    BillDry billClean = (BillDry) newObject;
                    if (billPcr.getVistitType() != 0) {
                        billClean.setCarNum(billPcr.getCarNum());
                    }
                    if (billPcr.getVistitType() == 5) {
                        billClean.setResource(billPcr.getResource());
                        billClean.setFeedImgUrl(billPcr.getFeedImgUrl());
                        billClean.setRemark(billPcr.getRemark());
                    }
                    dryMapper.insert(billClean);
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
                    //新增烘干
                } else if (nextId == 6) {
                    //新增洗澡前拍照
                    BillCleanBefore before = (BillCleanBefore) newObject;
                    if (billPcr.getVistitType() != 0) {
                        before.setCarNum(billPcr.getCarNum());
                    }
                    if (billPcr.getVistitType() == 5) {
                        before.setResource(billPcr.getResource());
                        before.setFeedImgUrl(billPcr.getFeedImgUrl());
                        before.setRemark(billPcr.getRemark());
                    }
                    billCleanBeforeMapper.insert(before);
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
                } else if (nextId == 7) {
                    //新增洗澡前拍照
                    BillGoodsInventory before = (BillGoodsInventory) newObject;
                    if (billPcr.getVistitType() != 0) {
                        before.setCarNum(billPcr.getCarNum());
                    }
                    if (billPcr.getVistitType() == 5) {
                        before.setResource(billPcr.getResource());
                        before.setFeedImgUrl(billPcr.getFeedImgUrl());
                        before.setRemark(billPcr.getRemark());
                    }
                    inventoryMapper.insert(before);
                    baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
                }
                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation() + "," + baseLocation.getLocationName());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId() + "," + baseLocation.getId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);

                baseProcess.setCurrentStatus(substring + "1," + 0);

                processMapper.updateById(baseProcess);
                pcrMapper.updateById(billPcr);
                return new Result(10000, "修改成功！", true);
            } else {
                billPcr.setBillStatus(2);
                billPcr.setPcrType(3);
                billPcr.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billPcr.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billPcr.setTestLocation(baseLocation.getLocationName());
                billPcr.setTestLocationId(baseLocation.getId());
                billPcr.setPassDate(sdf.parse(sdf.format(new Date())));
                billPcr.setCheckDate(sdf.parse(data));

                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);

                baseProcess.setCurrentStatus(substring + "2");
                baseProcess.setProcessType(2);


                System.out.println("这里是pcr复审拒绝的当前位置id：" + billPcr.getTestLocationId());
                int i = allLocationId.indexOf(billPcr.getTestLocationId().toString());
                String status1 = baseProcess.getAllLocationStatus();
                StringBuilder sb = new StringBuilder(status1);
                sb.setCharAt(i, '3');
                baseProcess.setAllLocationStatus(sb.toString());

                processMapper.updateById(baseProcess);
                pcrMapper.updateById(billPcr);
                return new Result(10000, "修改成功！", true);
            }
        }

    }

    @Override
    public Result listIsolate(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");//类型
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "20";
        }
        Page<BillIsolate> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        if ("".equals(type) || null == type) {
            type = "0";
        }
        String farmId = paramsMap.get("farmId");
        QueryWrapper<BillIsolate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).orderByDesc("sub_date");
        if ("0".equals(type)) {
            queryWrapper.in("bill_status", 0, 1, 2, 3);
        }
        if ("1".equals(type)) {
            queryWrapper.eq("bill_status", 0);
        }
        if ("2".equals(type)) {
            queryWrapper.in("bill_status", 1, 2);
        }
        if ("3".equals(type)) {
            queryWrapper.eq("bill_status", 3);
        }
        QueryWrapper<BillIsolate> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", farmId).eq("bill_status", 0);
        return new Result(ResultCode.SUCCESS, isolateMapper.selectPage(page, queryWrapper), isolateMapper.selectCount(queryWrapper1));
    }

    @Override
    public Result isolateById(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return new Result(ResultCode.SUCCESS, isolateMapper.selectById(id));
    }

    @Override
    public Result commitIsolate(HttpServletRequest httpServletRequest, BillIsolate billIsolate) throws IOException {
        BillIsolate isolate = isolateMapper.selectById(billIsolate.getId());
        Date startDate = billIsolate.getIsolateStartDate();
        String dayNum = billIsolate.getIsolateDayNum();//隔离天数
        Date endDate = billIsolate.getIsolateEndDate();
        isolate.setIsolateStartDate(startDate);
        isolate.setIsolateEndDate(endDate);
        isolate.setIsolateDayNum(dayNum);
        isolate.setBillStatus(1);
        isolateMapper.updateById(isolate);

        /*这个地方是人员下发进入生产区洗澡前的一个人脸门禁，可以直接写死 等后期生产区划分后再修改*/
/*        //通过生成门禁白名单
        QueryWrapper<DeviceList> deviceListQueryWrapper = new QueryWrapper<>();
        deviceListQueryWrapper.eq("farm_id", isolate.getFarmId()).eq("location_id", 5);
        List<DeviceList> deviceLists = deviceListMapper.selectList(deviceListQueryWrapper);
        //找到对应的入场申请表
        QueryWrapper<BillPersonnelAdmission> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", isolate.getFarmId()).eq("process_id", isolate.getProcessId());
        BillPersonnelAdmission billPersonnelAdmission = billPersonnelAdmissionMapper.selectOne(queryWrapper1);
        if (deviceLists.size() > 0) {
            for (DeviceList deviceList : deviceLists) {
                //通过生成门禁白名单
                PeopleList peopleList = new PeopleList();
                peopleList.setFarmId(isolate.getFarmId().toString());
                peopleList.setDeviceCode(deviceList.getDeviceCode());
                peopleList.setUserId(isolate.getAdmissionUserId());
                peopleList.setUserName(isolate.getAdmissionUserName());
                peopleList.setPassword("123456");
                peopleList.setType(0);
                peopleList.setCreateTime(new Date());
                Date date2 = new Date();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date2);
                calendar.add(Calendar.HOUR_OF_DAY, 2);
                Date date1 = calendar.getTime();
                peopleList.setOutTime(date1);
                peopleListMapper.insert(peopleList);

                HashMap<String, Object> jsonObject = new HashMap<>();
                jsonObject.put("seq", deviceList.getDeviceCode());
//                    String savePath = "D:\\works\\pig_projects\\甜妹.jpg";
                String savePath = "/opt/huatong/test.jpg";
                URL url = new URL(billPersonnelAdmission.getUserImgUrl());
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                int resCode = connection.getResponseCode();
                if (resCode == HttpURLConnection.HTTP_OK) {
                    InputStream inputStream = connection.getInputStream();
                    byte[] buffer = new byte[4096];
                    int length;
                    //读取数据并写入到文件中
                    try (FileOutputStream outStream = new FileOutputStream(savePath)) {
                        while ((length = inputStream.read(buffer)) != -1) {
                            outStream.write(buffer, 0, length);
                        }
                        outStream.flush();
                    } finally {
                        inputStream.close();
                    }
                } else {
                    System.out.println("文件下载错误码为" + resCode);
                }

//                    jsonObject.put("face", FileUtil.file("D:\\works\\pig_projects\\甜妹.jpg"));
                jsonObject.put("face", FileUtil.file(savePath));
                jsonObject.put("name", billPersonnelAdmission.getAdmissionUserName());
                jsonObject.put("password", "123456");
                jsonObject.put("timestamp", date1.getTime());
                jsonObject.put("uid", billPersonnelAdmission.getAdmissionUserId());
                String post = HttpRequest.post("http://39.173.90.137:8040/addDeviceUser")
                        .header(Header.CONTENT_TYPE, "multipart/form-data")
                        .form(jsonObject)
                        .timeout(30 * 1000).execute().body();
                System.out.println(post);
            }
        }*/
        return new Result(10000, "提交成功！", true);
    }

    @Override
    @Transactional
    public Result editIsolate(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) throws ParseException {
        String id = paramsMap.get("id");
        String farmId = paramsMap.get("farmId");
        BillIsolate billIsolate = isolateMapper.selectById(id);
        if (billIsolate.getBillStatus() == 2 || billIsolate.getBillStatus() == 3) {
            return new Result(10001, "隔离已完成或提前解除，无需再次修改！", false);
        }
        BaseProcess baseProcess = processMapper.selectById(billIsolate.getProcessId());
        QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq("id", billIsolate.getIsolateLocationId());
        BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前隔离所在的位置

        String allLocationId = baseProcess.getAllLocationId();//所有的位置id
        String allFlowId = baseProcess.getAllFlowId();//所有的流程id
        String currentFlowId = baseProcess.getCurrentFlowId();//当前已经完成的流程id
        String substring1 = allFlowId.substring(currentFlowId.length() + 1, allFlowId.length());
        String[] splits = substring1.split(",");
        Integer nextId = Integer.parseInt(splits[0]);
        Object newObject = BillPcrServiceImpl.getNewObject(nextId, billIsolate.getDestName(), billIsolate.getDestId(), billIsolate.getAdmissionUserName(),
                billIsolate.getAdmissionUserId(), billIsolate.getVistitDate(), billIsolate.getSubDate(),
                billIsolate.getProcessId(), billIsolate.getFarmId(), billIsolate.getIsolateLocation(), billIsolate.getIsolateLocationId(),
                billIsolate.getVistitType(), billIsolate.getPhone(), billIsolate.getDepartureName(), billIsolate.getDepartureId(),
                billIsolate.getMidId(), billIsolate.getMidName(),billIsolate.getFarmName());
        if (nextId == 0) {
            if (splits.length == 1) {
                baseProcess.setProcessType(1);
                String[] strings = allLocationId.split(",");
                String ss2 = "";
                for (int i = 0; i < strings.length; i++) {
                    strings[i].replaceAll(strings[i], "2");
                    if (i == strings.length - 1) {
                        ss2 = ss2 + strings[i].replaceAll(strings[i], "2");
                    } else {
                        ss2 = strings[i].replaceAll(strings[i], "2") + "," + ss2;
                    }
                }
                baseProcess.setAllLocationStatus(ss2);
            } else {
                String[] strings = allLocationId.split(",");
                Integer index = 0;
                for (int i = 0; i < strings.length; i++) {
                    if ((baseLocation.getId().toString()).equals(strings[i])) {
                        index = i;
                    }
                }
                QueryWrapper<BaseLocation> queryWrapper1 = new QueryWrapper();
                queryWrapper1.eq("id", strings[index + 2]);
                BaseLocation location = locationMapper.selectOne(queryWrapper1);
                FlowUtil flowUtil = new FlowUtil();
                Object entity = flowUtil.getEntity(Integer.parseInt(splits[1]), billIsolate.getVistitType(), billIsolate.getDestName()
                        , billIsolate.getDestId(), billIsolate.getAdmissionUserId(), billIsolate.getAdmissionUserName(), billIsolate.getPhone(),
                        billIsolate.getVistitDate(), location.getId(), location.getLocationName(), Integer.parseInt(farmId)
                        , baseProcess.getId(), billIsolate.getDepartureName(), billIsolate.getDepartureId(), billIsolate.getMidId(),
                        billIsolate.getMidName(),billIsolate.getFarmName());
                if (Integer.parseInt(splits[1]) == 1) {
                    //新增采样
                    BillSampling billSampling = (BillSampling) entity;
                    if (billIsolate.getVistitType() != 0) {
                        billSampling.setCarNum(billIsolate.getCarNum());
                    }
                    if (billIsolate.getVistitType() == 5) {
                        billSampling.setResource(billIsolate.getResource());
                        billSampling.setFeedImgUrl(billIsolate.getFeedImgUrl());
                        billSampling.setRemark(billIsolate.getRemark());
                    }
                    samplingMapper.insert(billSampling);
                } else if (Integer.parseInt(splits[1]) == 2) {
                    //新增pcr
                    BillPcr billClean = (BillPcr) entity;
                    if (billIsolate.getVistitType() != 0) {
                        billClean.setCarNum(billIsolate.getCarNum());
                    }
                    if (billIsolate.getVistitType() == 5) {
                        billClean.setResource(billIsolate.getResource());
                        billClean.setFeedImgUrl(billIsolate.getFeedImgUrl());
                        billClean.setRemark(billIsolate.getRemark());
                    }
                    pcrMapper.insert(billClean);
                } else if (Integer.parseInt(splits[1]) == 3) {
                    //新增洗澡
                    BillClean billClean = (BillClean) entity;
                    if (billIsolate.getVistitType() != 0) {
                        billClean.setCarNum(billIsolate.getCarNum());
                    }
                    if (billIsolate.getVistitType() == 5) {
                        billClean.setResource(billIsolate.getResource());
                        billClean.setFeedImgUrl(billIsolate.getFeedImgUrl());
                        billClean.setRemark(billIsolate.getRemark());
                    }
                    cleanMapper.insert(billClean);
                } else if (Integer.parseInt(splits[1]) == 4) {
                    //新增隔离
                    BillIsolate pcr = (BillIsolate) entity;
                    if (billIsolate.getVistitType() != 0) {
                        pcr.setCarNum(billIsolate.getCarNum());
                    }
                    if (billIsolate.getVistitType() == 5) {
                        pcr.setResource(billIsolate.getResource());
                        pcr.setFeedImgUrl(billIsolate.getFeedImgUrl());
                        pcr.setRemark(billIsolate.getRemark());
                    }
                    isolateMapper.insert(pcr);
                } else if (Integer.parseInt(splits[1]) == 5) {
                    BillDry billClean = (BillDry) entity;
                    if (billIsolate.getVistitType() != 0) {
                        billClean.setCarNum(billIsolate.getCarNum());
                    }
                    if (billIsolate.getVistitType() == 5) {
                        billClean.setResource(billIsolate.getResource());
                        billClean.setFeedImgUrl(billIsolate.getFeedImgUrl());
                        billClean.setRemark(billIsolate.getRemark());
                    }
                    dryMapper.insert(billClean);
                    //新增烘干
                } else if (Integer.parseInt(splits[1]) == 6) {
                    //新增洗澡前拍照
                    BillCleanBefore before = (BillCleanBefore) entity;
                    if (billIsolate.getVistitType() != 0) {
                        before.setCarNum(billIsolate.getCarNum());
                    }
                    if (billIsolate.getVistitType() == 5) {
                        before.setResource(billIsolate.getResource());
                        before.setFeedImgUrl(billIsolate.getFeedImgUrl());
                        before.setRemark(billIsolate.getRemark());
                    }
                    beforeMapper.insert(before);
                } else if (Integer.parseInt(splits[1]) == 7) {
                    //新增洗澡前拍照
                    BillGoodsInventory before = (BillGoodsInventory) entity;
                    if (billIsolate.getVistitType() != 0) {
                        before.setCarNum(billIsolate.getCarNum());
                    }
                    if (billIsolate.getVistitType() == 5) {
                        before.setResource(billIsolate.getResource());
                        before.setFeedImgUrl(billIsolate.getFeedImgUrl());
                        before.setRemark(billIsolate.getRemark());
                    }
                    inventoryMapper.insert(before);
                }
                baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId + "," + splits[1]);
                Integer locationId = billIsolate.getIsolateLocationId();
                int indexOf = allLocationId.indexOf(locationId.toString());//当前位置在所有流程位置中的下标
                String substring = baseProcess.getAllLocationStatus().substring(0, indexOf + 3);//截取已经过的位置状态
                String[] split1 = substring.split(",");
                String sb2 = "";
                for (int i = 0; i < split1.length; i++) {
                    if (i == split1.length - 1) {
                        sb2 = sb2 + split1[i].replaceAll(split1[i], "2");
                    } else {
                        sb2 = split1[i].replaceAll(split1[i], "2") + "," + sb2;
                    }
                }
                sb2 = sb2 + baseProcess.getAllLocationStatus().substring(indexOf + 3);
                baseProcess.setAllLocationStatus(sb2);
            }
        } else if (nextId == 1) {
            //新增采样
            BillSampling billSampling = (BillSampling) newObject;
            if (billIsolate.getVistitType() != 0) {
                billSampling.setCarNum(billIsolate.getCarNum());
            }
            if (billIsolate.getVistitType() == 5) {
                billSampling.setResource(billIsolate.getResource());
                billSampling.setFeedImgUrl(billIsolate.getFeedImgUrl());
                billSampling.setRemark(billIsolate.getRemark());
            }
            samplingMapper.insert(billSampling);
            baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
        } else if (nextId == 2) {
            //新增pcr
            BillPcr pcr = (BillPcr) newObject;
            if (billIsolate.getVistitType() != 0) {
                pcr.setCarNum(billIsolate.getCarNum());
            }
            if (billIsolate.getVistitType() == 5) {
                pcr.setResource(billIsolate.getResource());
                pcr.setFeedImgUrl(billIsolate.getFeedImgUrl());
                pcr.setRemark(billIsolate.getRemark());
            }
            pcrMapper.insert(pcr);
            baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
        } else if (nextId == 3) {
            //新增洗澡
            BillClean pcr = (BillClean) newObject;
            if (billIsolate.getVistitType() != 0) {
                pcr.setCarNum(billIsolate.getCarNum());
            }
            if (billIsolate.getVistitType() == 5) {
                pcr.setResource(billIsolate.getResource());
                pcr.setFeedImgUrl(billIsolate.getFeedImgUrl());
                pcr.setRemark(billIsolate.getRemark());
            }
            cleanMapper.insert(pcr);
            baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
        } else if (nextId == 4) {
            //新增隔离
            BillIsolate pcr = (BillIsolate) newObject;
            if (billIsolate.getVistitType() != 0) {
                pcr.setCarNum(billIsolate.getCarNum());
            }
            if (billIsolate.getVistitType() == 5) {
                pcr.setResource(billIsolate.getResource());
                pcr.setFeedImgUrl(billIsolate.getFeedImgUrl());
                pcr.setRemark(billIsolate.getRemark());
            }
            isolateMapper.insert(pcr);
            baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
        } else if (nextId == 5) {
            BillDry pcr = (BillDry) newObject;
            if (billIsolate.getVistitType() != 0) {
                pcr.setCarNum(billIsolate.getCarNum());
            }
            if (billIsolate.getVistitType() == 5) {
                pcr.setResource(billIsolate.getResource());
                pcr.setFeedImgUrl(billIsolate.getFeedImgUrl());
                pcr.setRemark(billIsolate.getRemark());
            }
            dryMapper.insert(pcr);
            baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
            //新增烘干
        } else if (nextId == 6) {
            //新增洗澡前拍照
            BillCleanBefore before = (BillCleanBefore) newObject;
            if (billIsolate.getVistitType() != 0) {
                before.setCarNum(billIsolate.getCarNum());
            }
            if (billIsolate.getVistitType() == 5) {
                before.setResource(billIsolate.getResource());
                before.setFeedImgUrl(billIsolate.getFeedImgUrl());
                before.setRemark(billIsolate.getRemark());
            }
            beforeMapper.insert(before);
            baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
        } else if (nextId == 7) {
            //新增洗澡前拍照
            BillGoodsInventory before = (BillGoodsInventory) newObject;
            if (billIsolate.getVistitType() != 0) {
                before.setCarNum(billIsolate.getCarNum());
            }
            if (billIsolate.getVistitType() == 5) {
                before.setResource(billIsolate.getResource());
                before.setFeedImgUrl(billIsolate.getFeedImgUrl());
                before.setRemark(billIsolate.getRemark());
            }
            inventoryMapper.insert(before);
            baseProcess.setCurrentFlowId(baseProcess.getCurrentFlowId() + "," + nextId);
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String end = sdf.format(billIsolate.getIsolateEndDate());
        String start = sdf.format(billIsolate.getIsolateStartDate());
        String now = sdf.format(new Date());


        //提前结束隔离
        baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
        baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
        billIsolate.setIsolateRealEndDate(new Date());
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal1 = Calendar.getInstance();
        Calendar cal2 = Calendar.getInstance();
        Date startDate = sdf1.parse(start);
        Date endDate = sdf1.parse(now);
        cal1.setTime(startDate);
        cal2.setTime(endDate);
        long days = (startDate.getTime() - endDate.getTime()) / (1000 * 3600);
        Integer l1 = (Integer.parseInt(String.valueOf(days / 24)));
        long l = l1 % 24;
        String num = l1.toString() + "天" + l + "小时";
        billIsolate.setIsolateRealDayNum(num);
        billIsolate.setBillStatus(3);
        billIsolate.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
        billIsolate.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));

        baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + now);

        String status = baseProcess.getCurrentStatus();
        String substring = status.substring(0, status.length() - 1);
        baseProcess.setCurrentStatus(substring + "1," + 0);


        isolateMapper.updateById(billIsolate);
        processMapper.updateById(baseProcess);
        return new Result(10000, "提前解除隔离成功！", true);

    }

    @Override
    public Result listClean(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");//类型
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "20";
        }
        Page<CleanAndDryVo2> cleanAndDryVo2Page = null;
        Page<CleanAndDryVo2> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        if ("".equals(type) || null == type) {
            type = "0";
        }
        String farmId = paramsMap.get("farmId");
        QueryWrapper<BillClean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).ne("vistit_type", 0).orderByDesc("sub_date").eq("img_status", 1);
        QueryWrapper<BillDry> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("farm_id", farmId).ne("vistit_type", 0).orderByDesc("sub_date").eq("img_status", 1);
        if ("0".equals(type)) {
            cleanAndDryVo2Page = cleanMapper.CleanAndDry2(page, farmId, "0,1,2".split(","));
        }
        ArrayList<String> arrayList = new ArrayList<>();
        if ("1".equals(type)) {
            arrayList.add("0");
            String[] s = arrayList.toArray(new String[arrayList.size()]);
            cleanAndDryVo2Page = cleanMapper.CleanAndDry2(page, farmId, s);
        }
        if ("2".equals(type)) {
            arrayList.add("1");
            String[] s = arrayList.toArray(new String[arrayList.size()]);
            cleanAndDryVo2Page = cleanMapper.CleanAndDry2(page, farmId, s);
        }
        if ("3".equals(type)) {
            arrayList.add("2");
            String[] s = arrayList.toArray(new String[arrayList.size()]);
            cleanAndDryVo2Page = cleanMapper.CleanAndDry2(page, farmId, s);
        }
        queryWrapper.eq("bill_status", 0);
        queryWrapper1.eq("bill_status", 0);
        Integer integer = cleanMapper.selectCount(queryWrapper);
        Integer integer1 = dryMapper.selectCount(queryWrapper1);
        return new Result(ResultCode.SUCCESS, cleanAndDryVo2Page, integer + integer1);

    }

    @Override
    public Result listByIdClean(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String id = paramsMap.get("id");
        return new Result(ResultCode.SUCCESS, cleanMapper.selectById(id));
    }

    //清洗和洗消的检测
    @Override
    @Transactional
    public Result editClean(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) throws ParseException {
        String result = paramsMap.get("result");
        String id = paramsMap.get("id");
        String farmId = paramsMap.get("farmId");
        BillClean billClean = cleanMapper.selectById(id);
        QueryWrapper<BaseProcess> processQueryWrapper = new QueryWrapper<>();
        processQueryWrapper.eq("farm_id", farmId).eq("id", billClean.getProcessId());
        BaseProcess baseProcess = processMapper.selectOne(processQueryWrapper);//进程

        QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq("id", billClean.getTestLocationId());
        BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前洗消检测所在的位置
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        if (billClean.getBillStatus() != 0) {
            return new Result(10001, "该订单已通过，无需再次提交！", false);
        }
        if (billClean.getDestId() >= billClean.getTestLocationId()) { //说明当前不是目的地
            if ("1".equals(result)) {
                billClean.setBillStatus(1);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));

                //拉猪车结束
                if (billClean.getTestLocationId() == 15) {
                    baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                    baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                    baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                    String status = baseProcess.getCurrentStatus();
                    String substring = status.substring(0, status.length() - 1);
                    baseProcess.setCurrentStatus(substring + "1");
                    baseProcess.setProcessType(1);
                    baseProcess.setAllLocationStatus("2,2,2");

                } else {
                    baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
//                    baseProcess.setCurrentLocation(baseProcess.getCurrentLocation() + "," + baseLocation.getNextLocation());
//                    baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId() + "," + baseLocation.getNextId());
                    String status = baseProcess.getCurrentStatus();
                    String substring = status.substring(0, status.length() - 1);
                    baseProcess.setCurrentStatus(substring + "1," + 0);
                }


                //环保车洗消合格生成烘干申请
                if (billClean.getTestLocationId() == 12 || billClean.getTestLocationId() == 13) {
                    BillDry billDry = new BillDry();
                    billDry.setVistitType(billClean.getVistitType());
                    billDry.setDestName(billClean.getDestName());
                    billDry.setDestId(billClean.getDestId());
                    billDry.setAdmissionUserName(billClean.getAdmissionUserName());
                    billDry.setAdmissionUserId(billClean.getAdmissionUserId());
                    billDry.setVistitDate(billClean.getVistitDate());
                    billDry.setSubDate(LocalDateTime.now());
                    billDry.setFarmId(billClean.getFarmId());
                    billDry.setBillStatus(0);
                    billDry.setProcessId(billClean.getProcessId());
                    billDry.setTestLocation(baseLocation.getLocationName());
                    billDry.setTestLocationId(baseLocation.getId());
                    billDry.setPhone(billClean.getPhone());
                    billDry.setCarNum(billClean.getCarNum());
                    billDry.setImgStatus(0);
                    dryMapper.insert(billDry);
                }
                //拉猪车洗消合格生成烘干申请

                if (billClean.getTestLocationId() == 15) {
                    QueryWrapper<BillClean> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("farm_id", farmId).eq("process_id", baseProcess.getId());
                    List<BillClean> billCleans = cleanMapper.selectList(queryWrapper);
                    if (billCleans.size() == 1) {
                        BillDry billDry = new BillDry();
                        billDry.setVistitType(billClean.getVistitType());
                        billDry.setDestName(billClean.getDestName());
                        billDry.setDestId(billClean.getDestId());
                        billDry.setAdmissionUserName(billClean.getAdmissionUserName());
                        billDry.setAdmissionUserId(billClean.getAdmissionUserId());
                        billDry.setVistitDate(billClean.getVistitDate());
                        billDry.setSubDate(LocalDateTime.now());
                        billDry.setFarmId(billClean.getFarmId());
                        billDry.setBillStatus(0);
                        billDry.setProcessId(billClean.getProcessId());
                        billDry.setTestLocation(baseLocation.getLocationName());
                        billDry.setTestLocationId(baseLocation.getId());
                        billDry.setPhone(billClean.getPhone());
                        billDry.setCarNum(billClean.getCarNum());
                        billDry.setImgStatus(0);
                        baseProcess.setAllLocationStatus("2,1,0");
                        dryMapper.insert(billDry);
                    }
                }

                //送猪车洗消合格生成烘干申请
                if (billClean.getTestLocationId() == 17) {
                    BillDry billDry = new BillDry();
                    billDry.setVistitType(billClean.getVistitType());
                    billDry.setDestName(billClean.getDestName());
                    billDry.setDestId(billClean.getDestId());
                    billDry.setAdmissionUserName(billClean.getAdmissionUserName());
                    billDry.setAdmissionUserId(billClean.getAdmissionUserId());
                    billDry.setVistitDate(billClean.getVistitDate());
                    billDry.setSubDate(LocalDateTime.now());
                    billDry.setFarmId(billClean.getFarmId());
                    billDry.setBillStatus(0);
                    billDry.setProcessId(billClean.getProcessId());
                    billDry.setTestLocation(baseLocation.getLocationName());
                    billDry.setTestLocationId(baseLocation.getId());
                    billDry.setPhone(billClean.getPhone());
                    billDry.setCarNum(billClean.getCarNum());
                    billDry.setImgStatus(0);
                    String status1 = baseProcess.getAllLocationStatus();
                    String[] split = status1.split(",");
                    if (billClean.getTestLocationId() == 17) {
                        split[0] = "2";
                        split[1] = "1";
                        baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                    }
                    dryMapper.insert(billDry);
                }
                processMapper.updateById(baseProcess);
                cleanMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            } else {
                billClean.setBillStatus(2);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));
                billClean.setImgStatus(1);

                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);

                String status1 = baseProcess.getAllLocationStatus();
                String[] split = status1.split(",");
                if (billClean.getTestLocationId() == 12) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }
                if (billClean.getTestLocationId() == 13) {
                    split[0] = "2";
                    split[1] = "2";
                    split[2] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + "," + split[2] + status1.substring(5));
                }

                if (billClean.getTestLocationId() == 15 || billClean.getTestLocationId() == 16) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }

                if (billClean.getTestLocationId() == 17) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }

                baseProcess.setCurrentStatus(substring + "2");
                baseProcess.setProcessType(2);

                processMapper.updateById(baseProcess);
                cleanMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            }
        } else {
            if ("1".equals(result)) {
                billClean.setBillStatus(1);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));

                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);
                baseProcess.setCurrentStatus(substring + "1");
                baseProcess.setProcessType(1);
                if (billClean.getTestLocationId() == 15 || billClean.getDestId() == 16) {
                    baseProcess.setAllLocationStatus("2,2,0");
                }
                if (billClean.getTestLocationId() == 17) {
                    baseProcess.setAllLocationStatus("2,1");
                }
                processMapper.updateById(baseProcess);
                cleanMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            } else {
                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);
                baseProcess.setCurrentStatus(substring + "2");
                baseProcess.setProcessType(2);
                billClean.setBillStatus(2);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));
                billClean.setImgStatus(1);

                String status1 = baseProcess.getAllLocationStatus();
                String[] split = status1.split(",");
                if (billClean.getTestLocationId() == 12) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }
                if (billClean.getTestLocationId() == 13) {
                    split[0] = "2";
                    split[1] = "2";
                    split[2] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + "," + split[2] + status1.substring(5));
                }
                if (billClean.getTestLocationId() == 17) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }
                if (billClean.getTestLocationId() == 15) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }

                processMapper.updateById(baseProcess);
                cleanMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            }

        }

    }

    //烘干检测
    @Override
    public Result editDry(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) throws ParseException {
        String result = paramsMap.get("result");
        String id = paramsMap.get("id");
        String farmId = paramsMap.get("farmId");
        BillDry billClean = dryMapper.selectById(id);
        QueryWrapper<BaseProcess> processQueryWrapper = new QueryWrapper<>();
        processQueryWrapper.eq("farm_id", farmId).eq("id", billClean.getProcessId());
        BaseProcess baseProcess = processMapper.selectOne(processQueryWrapper);//进程

        QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq("id", billClean.getTestLocationId());
        BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前洗消检测所在的位置
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (billClean.getBillStatus() != 0) {
            return new Result(10001, "该订单已通过，无需再次提交！", false);
        }
        if (billClean.getDestId() >= billClean.getTestLocationId()) { //说明当前不是目的地
            if ("1".equals(result)) {
                billClean.setBillStatus(1);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));

                //最终结束
                if (billClean.getTestLocationId() == 13 || billClean.getTestLocationId() == 17 || billClean.getTestLocationId() == 43) {
                    baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                    baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                    baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                    String status = baseProcess.getCurrentStatus();
                    String substring = status.substring(0, status.length() - 1);
                    baseProcess.setCurrentStatus(substring + "1");
                    baseProcess.setProcessType(1);
                    if (billClean.getTestLocationId() == 17) {
                        baseProcess.setAllLocationStatus("2,2,2");
                    }
                    if (billClean.getTestLocationId() == 13) {
                        baseProcess.setAllLocationStatus("2,2,2,2");
                    }
                } else {
                    baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
//                    baseProcess.setCurrentLocation(baseProcess.getCurrentLocation() + "," + baseLocation.getNextLocation());
//                    baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId() + "," + baseLocation.getNextId());
                    String status = baseProcess.getCurrentStatus();
                    String substring = status.substring(0, status.length() - 1);
                    baseProcess.setCurrentStatus(substring + "1," + 0);
                }

                //环保车烘干合格生成洗消申请
                if (billClean.getTestLocationId() == 12 && baseProcess.getDestId() > 12) {

                    BillClean clean = new BillClean();
                    clean.setVistitType(billClean.getVistitType());
                    clean.setDestName(billClean.getDestName());
                    clean.setDestId(billClean.getDestId());
                    clean.setAdmissionUserName(billClean.getAdmissionUserName());
                    clean.setAdmissionUserId(billClean.getAdmissionUserId());
                    clean.setVistitDate(billClean.getVistitDate());
                    clean.setSubDate(LocalDateTime.now());
                    clean.setFarmId(billClean.getFarmId());
                    clean.setBillStatus(0);
                    clean.setProcessId(billClean.getProcessId());
//                    clean.setTestLocationId(baseLocation.getNextId());
//                    clean.setTestLocation(baseLocation.getNextLocation());
                    clean.setPhone(billClean.getPhone());
                    clean.setCarNum(billClean.getCarNum());
                    clean.setImgStatus(0);
                    cleanMapper.insert(clean);
                    if (billClean.getDestId() == 13) {
                        baseProcess.setAllLocationStatus("2,2,1");
                    } else {
                        baseProcess.setAllLocationStatus("2,2,1,0");
                    }

                }

                if (billClean.getTestLocationId() == 12 && baseProcess.getDestId() == 12) {
                    baseProcess.setAllLocationStatus("2,2");
                }

                //拉猪车烘干合格生成洗消申请
                if (billClean.getTestLocationId() == 15) {
                    BillClean clean = new BillClean();
                    clean.setVistitType(billClean.getVistitType());
                    clean.setDestName(billClean.getDestName());
                    clean.setDestId(billClean.getDestId());
                    clean.setAdmissionUserName(billClean.getAdmissionUserName());
                    clean.setAdmissionUserId(billClean.getAdmissionUserId());
                    clean.setVistitDate(billClean.getVistitDate());
                    clean.setSubDate(LocalDateTime.now());
                    clean.setFarmId(billClean.getFarmId());
                    clean.setBillStatus(0);
                    clean.setProcessId(billClean.getProcessId());
                    clean.setTestLocationId(baseLocation.getId());
                    clean.setTestLocation(baseLocation.getLocationName());
                    clean.setPhone(billClean.getPhone());
                    clean.setCarNum(billClean.getCarNum());
                    clean.setImgStatus(0);
                    baseProcess.setAllLocationStatus("2,1,0");
                    cleanMapper.insert(clean);
                }

                processMapper.updateById(baseProcess);
                dryMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            } else {
                billClean.setBillStatus(2);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));
                billClean.setImgStatus(1);

                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);

                String status1 = baseProcess.getAllLocationStatus();
                String[] split = status1.split(",");
                if (billClean.getTestLocationId() == 12) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }
                if (billClean.getTestLocationId() == 13) {
                    split[0] = "2";
                    split[1] = "2";
                    split[2] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + "," + split[2] + status1.substring(5));
                }

                if (billClean.getTestLocationId() == 15) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }
                if (billClean.getTestLocationId() == 17) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }

                baseProcess.setCurrentStatus(substring + "2");
                baseProcess.setProcessType(2);
                processMapper.updateById(baseProcess);
                dryMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            }
        } else {
            if ("1".equals(result)) {
                billClean.setBillStatus(1);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));

                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);
                baseProcess.setCurrentStatus(substring + "1");
                baseProcess.setProcessType(1);

                if (billClean.getTestLocationId() == 15) {
                    baseProcess.setAllLocationStatus("2,1,0");
                }

                if (billClean.getTestLocationId() == 12) {
                    baseProcess.setAllLocationStatus("2,2");
                }
                if (billClean.getTestLocationId() == 13) {
                    baseProcess.setAllLocationStatus("2,2,2");
                }
                if (billClean.getTestLocationId() == 17) {
                    baseProcess.setAllLocationStatus("2,1,0");
                }
                processMapper.updateById(baseProcess);
                dryMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            } else {
                baseProcess.setUpdateDate(baseProcess.getUpdateDate() + "," + sdf.format(new Date()));
                baseProcess.setCurrentLocation(baseProcess.getCurrentLocation());
                baseProcess.setCurrentLocationId(baseProcess.getCurrentLocationId());
                String status = baseProcess.getCurrentStatus();
                String substring = status.substring(0, status.length() - 1);
                baseProcess.setCurrentStatus(substring + "2");
                baseProcess.setProcessType(2);
                billClean.setBillStatus(2);
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                billClean.setCheckDate(sdf.parse(sdf.format(new Date())));
                billClean.setImgStatus(1);

                String status1 = baseProcess.getAllLocationStatus();
                String[] split = status1.split(",");
                if (billClean.getTestLocationId() == 12) {
                    split[0] = "2";

                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }
                if (billClean.getTestLocationId() == 13) {
                    split[0] = "2";
                    split[1] = "2";
                    split[2] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + "," + split[2] + status1.substring(5));
                }
                if (billClean.getTestLocationId() == 15) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }

                if (billClean.getTestLocationId() == 17) {
                    split[0] = "2";
                    split[1] = "3";
                    baseProcess.setAllLocationStatus(split[0] + "," + split[1] + status1.substring(3));
                }

                processMapper.updateById(baseProcess);
                dryMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            }


        }
    }

}
