package com.huimv.admin.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.huimv.admin.common.token.TokenSign;
import com.huimv.admin.common.utils.*;
import com.huimv.admin.entity.*;
import com.huimv.admin.entity.vo.CleanAndDryVo;
import com.huimv.admin.entity.vo.CleanAndDryVo2;
import com.huimv.admin.mapper.*;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.admin.common.utils.Result;
import com.huimv.admin.common.utils.ResultCode;
import com.huimv.admin.entity.BillClean;
import com.huimv.admin.mapper.BillCleanMapper;
import com.huimv.admin.service.IBillCleanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

/**
 * <p>
 * 洗消表 服务实现类
 * </p>
 *
 * @author author
 * @since 2023-07-21
 */
@Service
public class BillCleanServiceImpl extends ServiceImpl<BillCleanMapper, BillClean> implements IBillCleanService {

    @Autowired
    private BillCleanMapper cleanMapper;
    @Autowired
    private BillIsolateMapper isolateMapper;
    @Autowired
    private BillPcrMapper pcrMapper;
    @Autowired
    private BaseProcessMapper processMapper;
    @Autowired
    private BaseLocationMapper locationMapper;
    @Autowired
    private UploadImage uploadImage;
    @Autowired
    private BillDryMapper dryMapper;
    @Autowired
    private BillPersonnelAdmissionMapper admissionMapper;

    @Override
    public Result listAll(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");//展示类型 0-6
        String data = paramsMap.get("data");//状态
        String farmId = paramsMap.get("farmId");
        String pageNum = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        if ("".equals(pageNum) || null == pageNum) {
            pageNum = "1";
        }
        if ("".equals(pageSize) || null == pageSize) {
            pageSize = "20";
        }
        if ("".equals(type) || null == type) {
            type = "1";
        }
        if ("".equals(data) || null == data) {
            data = "0";
        }
        if (type.equals("0")) {

            QueryWrapper<BaseLocation> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("farm_id", farmId).eq("location_type", TokenSign.getWorkIdByJwtToken(httpServletRequest));
            List<BaseLocation> baseLocations = locationMapper.selectList(queryWrapper1);
            List<Integer> ids = new ArrayList<>();
            for (BaseLocation baseLocation : baseLocations) {
                ids.add(baseLocation.getId());
            }

            Page<BillClean> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            QueryWrapper<BillClean> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId).eq("vistit_type", type).orderByDesc("sub_date")
                    .in("test_location_id", ids);

            if ("0".equals(data)) {
                queryWrapper.in("bill_status", 0, 1, 2);
            }
            if ("1".equals(data)) {
                queryWrapper.eq("bill_status", 0);
            }
            if ("2".equals(data)) {
                queryWrapper.eq("bill_status", 1);
            }
            QueryWrapper<BillClean> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("farm_id", farmId).eq("vistit_type", type).orderByDesc("sub_date")
                    .in("test_location_id", ids).eq("bill_status",0);

            return new Result(ResultCode.SUCCESS, cleanMapper.selectPage(page, queryWrapper),cleanMapper.selectCount(queryWrapper2));
        }/* else if (type.equals("4")) {
            Page<BillClean> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            QueryWrapper<BillClean> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId).eq("vistit_type", 4).orderByDesc("sub_date")
                    .eq("test_location_id", TokenSign.getWorkIdByJwtToken(httpServletRequest));

            if ("0".equals(data)) {
                queryWrapper.in("bill_status", 0, 1, 2);
            }
            if ("1".equals(data)) {
                queryWrapper.eq("bill_status", 0);
            }
            if ("2".equals(data)) {
                queryWrapper.eq("bill_status", 1);
            }
            return new Result(ResultCode.SUCCESS, cleanMapper.selectPage(page, queryWrapper));
        }*/ else {
            Page<CleanAndDryVo2> cleanAndDryVo2Page = null;
            Page<CleanAndDryVo2> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
            QueryWrapper<BaseLocation> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("farm_id", farmId).eq("location_type", TokenSign.getWorkIdByJwtToken(httpServletRequest));
            List<BaseLocation> baseLocations = locationMapper.selectList(queryWrapper1);
            QueryWrapper<BillClean> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId).eq("vistit_type", type);
            QueryWrapper<BillDry> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("farm_id", farmId).eq("vistit_type", type);
            if ("0".equals(data)) {
                ArrayList<String> arrayList = new ArrayList<>();
                for (BaseLocation baseLocation : baseLocations) {
                    arrayList.add(baseLocation.getId().toString());
                }
                String[] s = arrayList.toArray(new String[arrayList.size()]);
                System.out.println("传入自定义sql的位置参数形式："+s);
                cleanAndDryVo2Page = cleanMapper.CleanAndDry(page, farmId, type, "0,1".split(","), s);
                queryWrapper.in("test_location_id", arrayList);
                queryWrapper2.in("test_location_id", arrayList);
            }
            if ("1".equals(data)) {
                ArrayList<String> integerArrayList = new ArrayList<>();
                for (BaseLocation baseLocation : baseLocations) {
                    integerArrayList.add(baseLocation.getId().toString());
                }
                String[] ids = integerArrayList.toArray(new String[integerArrayList.size()]);
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add("0");
                String[] s = arrayList.toArray(new String[arrayList.size()]);
                cleanAndDryVo2Page = cleanMapper.CleanAndDry(page, farmId, type, s, ids);
                queryWrapper.in("test_location_id", integerArrayList);
                queryWrapper2.in("test_location_id", integerArrayList);
            }
            if ("2".equals(data)) {
                ArrayList<String> integerArrayList = new ArrayList<>();
                for (BaseLocation baseLocation : baseLocations) {
                    integerArrayList.add(baseLocation.getId().toString());
                }
                String[] ids = integerArrayList.toArray(new String[integerArrayList.size()]);
                ArrayList<String> arrayList = new ArrayList<>();
                arrayList.add("1");
                String[] s = arrayList.toArray(new String[arrayList.size()]);
                cleanAndDryVo2Page = cleanMapper.CleanAndDry(page, farmId, type, s, ids);
                queryWrapper.in("test_location_id", integerArrayList);
                queryWrapper2.in("test_location_id", integerArrayList);
            }
            queryWrapper.eq("img_status", 0);
            queryWrapper2.eq("img_status", 0);
            Integer integer = cleanMapper.selectCount(queryWrapper);
            Integer integer1 = dryMapper.selectCount(queryWrapper2);
            return new Result(ResultCode.SUCCESS, cleanAndDryVo2Page,integer+integer1);
        }

    }

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

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

    @Override
    public Result commitClean(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) throws ParseException {
        String result = paramsMap.get("result");// 结果
        String farmId = paramsMap.get("farmId");
        String id = paramsMap.get("id");
        String date = paramsMap.get("date");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (StringUtils.isBlank(date)) {
            date = sdf.format(new Date());
        }
        BillClean billClean = cleanMapper.selectById(id);//某条洗消记录

        QueryWrapper<BaseProcess> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).eq("id", billClean.getProcessId());
        BaseProcess baseProcess = processMapper.selectOne(queryWrapper);//某条进程

        QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
        locationQueryWrapper.eq("farm_id", farmId).eq("id", billClean.getTestLocationId());
        BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前所在的位置

        if (baseProcess.getProcessType() == 2 || baseProcess.getProcessType() == 1) {
            return new Result(10001, "修改失败！该申请已提前结束或者被拒绝", false);
        } else {
            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.setCheckDate(sdf.parse(date));
                    billClean.setPassDate(sdf.parse(sdf.format(new Date())));

                    if (baseLocation.getId() == 1 && baseProcess.getDestId() > 2) {
                        //生成pcr检测申请
                        BillPcr billPcr = new BillPcr();
                        billPcr.setPcrType(0);
                        billPcr.setPhone(billClean.getPhone());
                        billPcr.setVistitType(billClean.getVistitType());
                        billPcr.setDestName(billClean.getDestName());
                        billPcr.setDestId(billClean.getDestId());
                        billPcr.setAdmissionUserName(billClean.getAdmissionUserName());
                        billPcr.setAdmissionUserId(billClean.getAdmissionUserId());
                        billPcr.setVistitDate(billClean.getVistitDate());
                        billPcr.setSubDate(LocalDateTime.now());
                        billPcr.setBillStatus(0);
                        billPcr.setProcessId(billClean.getProcessId());
                        billPcr.setFarmId(billClean.getFarmId());
                        billPcr.setTestLocation("人员二级洗消站");
                        billPcr.setTestLocationId(3);
                        pcrMapper.insert(billPcr);
                    }
                    if (baseLocation.getId() == 3 ) {
                        //生成隔离申请
                        BillIsolate isolate = new BillIsolate();
                        isolate.setPhone(billClean.getPhone());
                        isolate.setVistitType(billClean.getVistitType());
                        isolate.setDestName(billClean.getDestName());
                        isolate.setDestId(billClean.getDestId());
                        isolate.setAdmissionUserName(billClean.getAdmissionUserName());
                        isolate.setAdmissionUserId(billClean.getAdmissionUserId());
                        isolate.setVistitDate(billClean.getVistitDate());
                        isolate.setSubDate(billClean.getSubDate());
                        isolate.setFarmId(billClean.getFarmId());
                        isolate.setBillStatus(0);
                        isolate.setIsolateDayNum("");
                        isolate.setProcessId(billClean.getProcessId());
                        isolate.setIsolateLocation(baseLocation.getLocationName());
                        isolate.setIsolateLocationId(baseLocation.getId());
                        isolateMapper.insert(isolate);
                    }

                    if (baseLocation.getId() == 4) {
                        QueryWrapper<BillPcr> queryWrapper1 = new QueryWrapper<>();
                        queryWrapper1.eq("farm_id", farmId).eq("process_id", baseProcess.getId()).eq("test_location_id", 4);
                        if (ObjectUtil.isEmpty(pcrMapper.selectOne(queryWrapper1))) {
                            //生成pcr检测申请
                            BillPcr billPcr = new BillPcr();
                            billPcr.setPcrType(0);
                            billPcr.setPhone(billClean.getPhone());
                            billPcr.setVistitType(billClean.getVistitType());
                            billPcr.setDestName(billClean.getDestName());
                            billPcr.setDestId(billClean.getDestId());
                            billPcr.setAdmissionUserName(billClean.getAdmissionUserName());
                            billPcr.setAdmissionUserId(billClean.getAdmissionUserId());
                            billPcr.setVistitDate(billClean.getVistitDate());
                            billPcr.setSubDate(LocalDateTime.now());
                            billPcr.setBillStatus(0);
                            billPcr.setProcessId(billClean.getProcessId());
                            billPcr.setFarmId(billClean.getFarmId());
                            billPcr.setTestLocation("场内");
                            billPcr.setTestLocationId(4);
                            pcrMapper.insert(billPcr);
                        } else {
                            //生成隔离申请
                            BillIsolate isolate = new BillIsolate();
                            isolate.setPhone(billClean.getPhone());
                            isolate.setVistitType(billClean.getVistitType());
                            isolate.setDestName(billClean.getDestName());
                            isolate.setDestId(billClean.getDestId());
                            isolate.setAdmissionUserName(billClean.getAdmissionUserName());
                            isolate.setAdmissionUserId(billClean.getAdmissionUserId());
                            isolate.setVistitDate(billClean.getVistitDate());
                            isolate.setSubDate(LocalDateTime.now());
                            isolate.setFarmId(billClean.getFarmId());
                            isolate.setBillStatus(0);
                            isolate.setIsolateDayNum("");
                            isolate.setProcessId(billClean.getProcessId());
                            isolate.setIsolateLocation(baseLocation.getLocationName());
                            isolate.setIsolateLocationId(baseLocation.getId());
                            isolateMapper.insert(isolate);
                        }
                    }

                    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);
                } else {
                    billClean.setBillStatus(2);
                    billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                    billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                    billClean.setTestLocation(baseLocation.getLocationName());
                    billClean.setTestLocationId(baseLocation.getId());
                    billClean.setCheckDate(sdf.parse(date));
                    billClean.setPassDate(sdf.parse(sdf.format(new Date())));

                    baseProcess.setProcessType(2);
                    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");
                }
                cleanMapper.updateById(billClean);
                processMapper.updateById(baseProcess);
                return new Result(10000, "修改成功！", true);
            } else {
                //当前是目的地
                if ("1".equals(result)) {
                    billClean.setBillStatus(1);
                    baseProcess.setProcessType(1);
                } else {
                    billClean.setBillStatus(2);
                    baseProcess.setProcessType(2);
                }
                billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                billClean.setTestLocation(baseLocation.getLocationName());
                billClean.setTestLocationId(baseLocation.getId());
                billClean.setCheckDate(sdf.parse(date));
                billClean.setPassDate(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");
                processMapper.updateById(baseProcess);
                cleanMapper.updateById(billClean);
                return new Result(10000, "修改成功！", true);
            }
        }
    }

    @Resource
    private BillCleanMapper billCleanMapper;


    @Override
    public Result listPersonalClean(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        String vistitType = paramsMap.get("vistitType");
        Page<BillClean> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        QueryWrapper<BillClean> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(vistitType),"vistit_type",vistitType).eq("admission_user_id", TokenSign.getMemberIdByJwtToken(httpServletRequest)).orderByDesc("sub_date");
        return new Result(ResultCode.SUCCESS, billCleanMapper.selectPage(page, new QueryWrapper<BillClean>().eq(StringUtils.isNotBlank(vistitType),"vistit_type",vistitType).eq("admission_user_id", TokenSign.getMemberIdByJwtToken(httpServletRequest)).orderByDesc("sub_date")),billCleanMapper.selectCount(queryWrapper));
    }

    @Override
    public Result carClean(HttpServletRequest httpServletRequest, String farmId, String id, String data, MultipartFile img1, MultipartFile img2, MultipartFile img3, MultipartFile img4, MultipartFile img5, String type) throws ParseException, IOException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (StringUtils.isBlank(data)) {
            data = sdf.format(new Date());
        }
        if ("1".equals(type)) {
            //洗消上传
            BillClean billClean = cleanMapper.selectById(id);//某条洗消记录
            System.out.println("洗消记录的id:" + billClean.getTestLocationId());
            Integer testId = billClean.getTestLocationId();
            QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
            locationQueryWrapper.eq("farm_id", farmId).eq("id", testId);
            BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前所在的位置

            QueryWrapper<BaseProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId).eq("id", billClean.getProcessId());
            BaseProcess baseProcess = processMapper.selectOne(queryWrapper);//某条进程

            billClean.setBillStatus(0);
            billClean.setImgStatus(1);
            billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
            billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
            billClean.setTestLocation(baseLocation.getLocationName());
            billClean.setTestLocationId(baseLocation.getId());
            billClean.setCheckDate(sdf.parse(data));
            billClean.setPassDate(sdf.parse(sdf.format(new Date())));
            String s1 = "";
            String s2 = "";
            String s3 = "";
            String s4 = "";
            String s5 = "";
            if (ObjectUtil.isNotEmpty(img1)) {
                if (uploadImage.getImageCom(img1).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片1上传失败", false);
                }
                s1 = uploadImage.uploadImg(img1);
            }
            if (ObjectUtil.isNotEmpty(img2)) {
                if (uploadImage.getImageCom(img2).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片2上传失败", false);
                }
                s2 = "," + uploadImage.uploadImg(img2);
            }
            if (ObjectUtil.isNotEmpty(img3)) {
                if (uploadImage.getImageCom(img3).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片3上传失败", false);
                }
                s3 = "," + uploadImage.uploadImg(img3);
            }
            if (ObjectUtil.isNotEmpty(img4)) {
                if (uploadImage.getImageCom(img4).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片4上传失败", false);
                }
                s4 = "," + uploadImage.uploadImg(img4);
            }
            if (ObjectUtil.isNotEmpty(img5)) {
                if (uploadImage.getImageCom(img5).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片5上传失败", false);
                }
                s5 = "," + uploadImage.uploadImg(img5);
            }
            String s = s1 + s2 + s3 + s4 + s5;
            billClean.setImgUrl(s);
            cleanMapper.updateById(billClean);
            //物资车生成二次申请

        } else {
            BillDry billClean = dryMapper.selectById(id);//某条烘干记录
            System.out.println("洗消记录的id:" + billClean.getTestLocationId());
            Integer testId = billClean.getTestLocationId();
            QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
            locationQueryWrapper.eq("farm_id", farmId).eq("id", testId);
            BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前所在的位置

            QueryWrapper<BaseProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId).eq("id", billClean.getProcessId());
            BaseProcess baseProcess = processMapper.selectOne(queryWrapper);//某条进程

            billClean.setBillStatus(0);
            billClean.setImgStatus(1);
            billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
            billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
            billClean.setTestLocation(baseLocation.getLocationName());
            billClean.setTestLocationId(baseLocation.getId());
            billClean.setCheckDate(sdf.parse(data));
            billClean.setPassDate(sdf.parse(sdf.format(new Date())));
            String s1 = "";
            String s2 = "";
            String s3 = "";
            String s4 = "";
            String s5 = "";
            if (ObjectUtil.isNotEmpty(img1)) {
                if (uploadImage.getImageCom(img1).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片1上传失败", false);
                }
                s1 = uploadImage.uploadImg(img1);
            }
            if (ObjectUtil.isNotEmpty(img2)) {
                if (uploadImage.getImageCom(img2).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片2上传失败", false);
                }
                s2 = "," + uploadImage.uploadImg(img2);
            }
            if (ObjectUtil.isNotEmpty(img3)) {
                if (uploadImage.getImageCom(img3).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片3上传失败", false);
                }
                s3 = "," + uploadImage.uploadImg(img3);
            }
            if (ObjectUtil.isNotEmpty(img4)) {
                if (uploadImage.getImageCom(img4).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片4上传失败", false);
                }
                s4 = "," + uploadImage.uploadImg(img4);
            }
            if (ObjectUtil.isNotEmpty(img5)) {
                if (uploadImage.getImageCom(img5).equals("上传失败")) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return new Result(10001, "图片5上传失败", false);
                }
                s5 = "," + uploadImage.uploadImg(img5);
            }
            String s = s1 + s2 + s3 + s4 + s5;
            billClean.setImgUrl(s);
            dryMapper.updateById(billClean);
        }

        return new Result(10000, "提交成功！", true);
    }

    @Override
    public Result listCleanAndDry(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String type = paramsMap.get("type");
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        Page<CleanAndDryVo> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        Page<CleanAndDryVo> cleanAndDryVoPage;
        switch (type) {
            case "1":
                cleanAndDryVoPage = billCleanMapper.listCleanAndDry(page, Integer.parseInt(TokenSign.getFarmIds(httpServletRequest)), 0);
                break;
            case "2":
                cleanAndDryVoPage = billCleanMapper.listCleanAndDry(page, Integer.parseInt(TokenSign.getFarmIds(httpServletRequest)), 1);
                break;
            case "3":
                cleanAndDryVoPage = billCleanMapper.listCleanAndDry(page, Integer.parseInt(TokenSign.getFarmIds(httpServletRequest)), 2);
                break;
            default:
                cleanAndDryVoPage = billCleanMapper.listCleanAndDry(page, Integer.parseInt(TokenSign.getFarmIds(httpServletRequest)));
                break;
        }

        return new Result(ResultCode.SUCCESS, cleanAndDryVoPage,cleanAndDryVoPage.getSize());
    }

    @Override
    public Result listPersonalCleanAndDry(HttpServletRequest httpServletRequest, Map<String, String> paramsMap) {
        String pageNo = paramsMap.get("pageNum");
        String pageSize = paramsMap.get("pageSize");
        String vistitType = paramsMap.get("vistitType");
        Page<CleanAndDryVo> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        Integer userId = TokenSign.getMemberIdByJwtToken(httpServletRequest);
        Page<CleanAndDryVo> cleanAndDryVoPage = cleanMapper.listPersonalCleanAndDry(page, userId,vistitType);
        return new Result(ResultCode.SUCCESS, cleanAndDryVoPage,cleanAndDryVoPage.getSize());
    }

    @Override
    public Result goodsCarClean(HttpServletRequest httpServletRequest, String farmId, String id, String date, MultipartFile img1,
                                MultipartFile img2, MultipartFile img3, MultipartFile img4, MultipartFile img5, String type, String result
    ) throws ParseException, IOException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        if (StringUtils.isBlank(date)) {
            date = sdf.format(new Date());
        }
        String s1 = "";
        String s2 = "";
        String s3 = "";
        String s4 = "";
        String s5 = "";
        if (ObjectUtil.isNotEmpty(img1)) {
            if (uploadImage.getImageCom(img1).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片1上传失败", false);
            }
            s1 = uploadImage.uploadImg(img1);
        }
        if (ObjectUtil.isNotEmpty(img2)) {
            if (uploadImage.getImageCom(img2).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片2上传失败", false);
            }
            s2 = "," + uploadImage.uploadImg(img2);
        }
        if (ObjectUtil.isNotEmpty(img3)) {
            if (uploadImage.getImageCom(img3).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片3上传失败", false);
            }
            s3 = "," + uploadImage.uploadImg(img3);
        }
        if (ObjectUtil.isNotEmpty(img4)) {
            if (uploadImage.getImageCom(img4).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片4上传失败", false);
            }
            s4 = "," + uploadImage.uploadImg(img4);
        }
        if (ObjectUtil.isNotEmpty(img5)) {
            if (uploadImage.getImageCom(img5).equals("上传失败")) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return new Result(10001, "图片5上传失败", false);
            }
            s5 = "," + uploadImage.uploadImg(img5);
        }
        String s = s1 + s2 + s3 + s4 + s5;

        if ("1".equals(type)) {
            BillClean billClean = cleanMapper.selectById(id);//某条洗消记录
            QueryWrapper<BaseProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId).eq("id", billClean.getProcessId());
            BaseProcess baseProcess = processMapper.selectOne(queryWrapper);//某条进程
            QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
            locationQueryWrapper.eq("farm_id", farmId).eq("id", billClean.getTestLocationId());
            BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前所在的位置
            if (baseProcess.getProcessType() == 2 || baseProcess.getProcessType() == 1) {
                return new Result(10001, "修改失败！该申请已提前结束或者被拒绝", false);
            } else {
                if (billClean.getDestId() >= billClean.getTestLocationId() || billClean.getDestId() == 7) { //不是目的地

                    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.setCheckDate(sdf.parse(date));
                        billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                        billClean.setImgUrl(s);
                        billClean.setImgStatus(1);
                        cleanMapper.updateById(billClean);

                        if (billClean.getTestLocationId() == 9 || billClean.getTestLocationId() == 10) {
                            if (billClean.getDestId() > 9 || billClean.getDestId() > 10) {
                                Integer processId = billClean.getProcessId();
                                QueryWrapper<BillPersonnelAdmission> queryWrapper1 = new QueryWrapper<>();
                                queryWrapper1.eq("farm_id", farmId).eq("process_id", processId);
                                BillPersonnelAdmission admission = admissionMapper.selectOne(queryWrapper1);
                                admission.setBillStatus(1);
                                admission.setPassDate(LocalDateTime.now());
                                admission.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                                admission.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                                admissionMapper.updateById(admission);
                            }

                        }

                        //第一次通过生成洗消
                        if (billClean.getTestLocationId() == 6) {
                            BillClean clean = new BillClean();
                            clean.setVistitType(billClean.getVistitType());
                            clean.setVistitDate(billClean.getVistitDate());
                            clean.setDestName(billClean.getDestName());
                            clean.setDestId(billClean.getDestId());
                            clean.setAdmissionUserId(billClean.getAdmissionUserId());
                            clean.setAdmissionUserName(billClean.getAdmissionUserName());
                            clean.setSubDate(LocalDateTime.now());
                            clean.setBillStatus(0);
                            clean.setImgStatus(0);
                            clean.setFarmId(billClean.getFarmId());
                            clean.setCarNum(billClean.getCarNum());
                            clean.setPhone(billClean.getPhone());
                            clean.setProcessId(billClean.getProcessId());
                            clean.setTestLocationId(baseLocation.getNextId());
                            clean.setTestLocation(baseLocation.getNextLocation());
                            cleanMapper.insert(clean);
                        }

                        if (billClean.getTestLocationId() == 7) {
                            //生成烘干申请
                            BillDry billPcr = new BillDry();
                            billPcr.setPhone(billClean.getPhone());
                            billPcr.setVistitType(billClean.getVistitType());
                            billPcr.setDestName(billClean.getDestName());
                            billPcr.setDestId(billClean.getDestId());
                            billPcr.setAdmissionUserName(billClean.getAdmissionUserName());
                            billPcr.setAdmissionUserId(billClean.getAdmissionUserId());
                            billPcr.setVistitDate(billClean.getVistitDate());
                            billPcr.setSubDate(LocalDateTime.now());
                            billPcr.setBillStatus(0);
                            billPcr.setImgStatus(0);
                            billPcr.setCarNum(billClean.getCarNum());
                            billPcr.setProcessId(billClean.getProcessId());
                            billPcr.setFarmId(billClean.getFarmId());
                            billPcr.setTestLocation(baseLocation.getLocationName());
                            billPcr.setTestLocationId(baseLocation.getId());
                            dryMapper.insert(billPcr);
                        }
                        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);
                    } else {
                        billClean.setImgStatus(1);
                        billClean.setImgUrl(s);
                        billClean.setBillStatus(2);
                        billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                        billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                        billClean.setTestLocation(baseLocation.getLocationName());
                        billClean.setTestLocationId(baseLocation.getId());
                        billClean.setCheckDate(sdf.parse(date));
                        billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                        cleanMapper.updateById(billClean);

                        baseProcess.setProcessType(2);
                        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");

                        processMapper.updateById(baseProcess);
                    }
                    return new Result(10000, "修改成功！", true);
                } else if (billClean.getDestId() == 53) {
                    if ("1".equals(result)) {
                        billClean.setBillStatus(1);
                        baseProcess.setProcessType(1);
                    } else {
                        billClean.setBillStatus(2);
                        baseProcess.setProcessType(2);
                    }
                    Integer processId = billClean.getProcessId();
                    QueryWrapper<BillPersonnelAdmission> queryWrapper1 = new QueryWrapper<>();
                    queryWrapper1.eq("farm_id", farmId).eq("process_id", processId);
                    BillPersonnelAdmission admission = admissionMapper.selectOne(queryWrapper1);
                    admission.setBillStatus(1);
                    admission.setPassDate(LocalDateTime.now());
                    admission.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                    admission.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                    admissionMapper.updateById(admission);
                    billClean.setImgStatus(1);
                    billClean.setImgUrl(s);
                    billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                    billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                    billClean.setTestLocation(baseLocation.getLocationName());
                    billClean.setTestLocationId(baseLocation.getId());
                    billClean.setCheckDate(sdf.parse(date));
                    billClean.setPassDate(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");
                    processMapper.updateById(baseProcess);
                    cleanMapper.updateById(billClean);
                    return new Result(10000, "修改成功！", true);
                } else {
                    //当前是目的地
                    if ("1".equals(result)) {
                        billClean.setBillStatus(1);
                        baseProcess.setProcessType(1);
                    } else {
                        billClean.setBillStatus(2);
                        baseProcess.setProcessType(2);
                    }
                    billClean.setImgStatus(1);
                    billClean.setImgUrl(s);
                    billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                    billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                    billClean.setTestLocation(baseLocation.getLocationName());
                    billClean.setTestLocationId(baseLocation.getId());
                    billClean.setCheckDate(sdf.parse(date));
                    billClean.setPassDate(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");
                    processMapper.updateById(baseProcess);
                    cleanMapper.updateById(billClean);
                    return new Result(10000, "修改成功！", true);
                }
            }
        } else {
            BillDry billClean = dryMapper.selectById(id);//某条烘干记录
            QueryWrapper<BaseProcess> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("farm_id", farmId).eq("id", billClean.getProcessId());
            BaseProcess baseProcess = processMapper.selectOne(queryWrapper);//某条进程
            QueryWrapper<BaseLocation> locationQueryWrapper = new QueryWrapper<>();
            locationQueryWrapper.eq("farm_id", farmId).eq("id", billClean.getTestLocationId());
            BaseLocation baseLocation = locationMapper.selectOne(locationQueryWrapper);//当前所在的位置
            if (baseProcess.getProcessType() == 2 || baseProcess.getProcessType() == 1) {
                return new Result(10001, "修改失败！该申请已提前结束或者被拒绝", false);
            } else {
                if ("1".equals(result)) {
                    //烘干通过
                    billClean.setImgUrl(s);
                    billClean.setBillStatus(1);
                    billClean.setPassDate(new Date());
                    billClean.setCheckDate(new Date());
                    billClean.setPhone(billClean.getPhone());
                    billClean.setCarNum(billClean.getCarNum());
                    billClean.setImgStatus(1);
                    billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                    billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                    billClean.setTestLocation(baseLocation.getLocationName());
                    billClean.setTestLocationId(baseLocation.getId());
                    dryMapper.updateById(billClean);

                    baseProcess.setProcessType(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);
                    baseProcess.setCurrentStatus(substring + "1");
                    processMapper.updateById(baseProcess);
                } else {
                    billClean.setImgStatus(1);
                    billClean.setImgUrl(s);
                    billClean.setBillStatus(2);
                    billClean.setPassUserName(TokenSign.getUserNameByJwtToken(httpServletRequest));
                    billClean.setPassUserId(TokenSign.getMemberIdByJwtToken(httpServletRequest));
                    billClean.setTestLocation(baseLocation.getLocationName());
                    billClean.setTestLocationId(baseLocation.getId());
                    billClean.setCheckDate(sdf.parse(date));
                    billClean.setPassDate(sdf.parse(sdf.format(new Date())));
                    dryMapper.updateById(billClean);

                    baseProcess.setProcessType(2);
                    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");

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

        }


    }
}
