package com.zcloud.primeport.plan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jjb.saas.framework.job.Job;
import com.jjb.saas.framework.job.annotation.JobRegister;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import com.zcloud.gbscommon.utils.DateUtil;
import com.zcloud.primeport.persistence.dataobject.ClosedAreaCarApplyDO;
import com.zcloud.primeport.persistence.dataobject.VehicleApplyDO;
import com.zcloud.primeport.persistence.repository.ClosedAreaCarApplyRepository;
import com.zcloud.primeport.persistence.repository.ClosedAreaRepository;
import com.zcloud.primeport.persistence.repository.VehicleApplyRepository;
import com.zcloud.primeport.plan.mjDevice.dto.CarEnum;
import com.zcloud.primeport.plan.mjDevice.dto.OneLevelCarRemoveDto;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class VehicleExpiredExpirationXxlJob implements Job {

    @Resource
    private VehicleApplyRepository vehicleApplyRepository;
    @Resource
    private ClosedAreaCarApplyRepository closedAreaCarApplyRepository;
    @Resource
    private ClosedAreaRepository closedAreaRepository;

    // 每天晚上12，10分执行，把过期的车刷新状态，过期车移除门禁系统。不处理临时车、相关方车。
    @Override
    @JobRegister(cron = "0 10 0 * * ?", jobDesc = "口门定时任务，进行车辆有效期过期判定", triggerStatus = 1)
    @XxlJob("com.zcloud.plan.VehicleExpiredExpirationXxlJob")
    public ReturnT<String> execute(String param) {
        QueryWrapper<VehicleApplyDO> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("expired_expiration", 2);
        objectQueryWrapper.in("audit_flag", 2, 4);
        objectQueryWrapper.lt("visit_end_time", DateUtil.date2Str(new Date(), "yyyy-MM-dd"));
        List<VehicleApplyDO> list = vehicleApplyRepository.list(objectQueryWrapper);
//        ArrayList<OneLevelCarRemoveDto> oneLevelCarRemoveDtos = new ArrayList<>();
        // 临时车
//        ArrayList<OneLevelCarRemoveDto> tmpOneLevelCarRemoveDtos = new ArrayList<>();
        if (!list.isEmpty()) {
            List<Long> idList = list.stream()
                    .map(VehicleApplyDO::getId)
                    .collect(Collectors.toList());
            UpdateWrapper<VehicleApplyDO> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("expired_expiration", 1);
            updateWrapper.set("mkmj_permission", 2);
            updateWrapper.in("id", idList);
            vehicleApplyRepository.update(updateWrapper);
//            list.forEach(item -> {
//                OneLevelCarRemoveDto oneLevelCarRemoveDto = new OneLevelCarRemoveDto();
//                oneLevelCarRemoveDto.setTruckNo(item.getLicenceNo());
//                oneLevelCarRemoveDto.setTruckCardColor(CarEnum.getMenuKeyByPath(item.getLicenceTypeName()));
//                oneLevelCarRemoveDtos.add(oneLevelCarRemoveDto);
//            });
        }
//        QueryWrapper<ClosedAreaCarApplyDO> closetCarQueryWrapper = new QueryWrapper<>();
//        closetCarQueryWrapper.eq("expired_expiration", 2);
//        closetCarQueryWrapper.eq("audit_flag", 2);
//        closetCarQueryWrapper.lt("visit_end_time", DateUtil.date2Str(new Date(), "yyyy-MM-dd"));
//        List<ClosedAreaCarApplyDO> closedAreaCarList = closedAreaCarApplyRepository.list(closetCarQueryWrapper);
//        if (!closedAreaCarList.isEmpty()) {
//            List<Long> idList = closedAreaCarList.stream()
//                    .map(ClosedAreaCarApplyDO::getId)
//                    .collect(Collectors.toList());
//            UpdateWrapper<ClosedAreaCarApplyDO> updateWrapper = new UpdateWrapper<>();
//            updateWrapper.set("expired_expiration", 1);
//            updateWrapper.in("id", idList);
//            closedAreaCarApplyRepository.update(updateWrapper);
//            closedAreaCarList.forEach(item -> {
//                // 一级口门车辆删除
//                OneLevelCarRemoveDto oneLevelCarRemoveDto = new OneLevelCarRemoveDto();
//                oneLevelCarRemoveDto.setTruckNo(item.getLicenceNo());
//                oneLevelCarRemoveDto.setTruckCardColor(CarEnum.getMenuKeyByPath(item.getLicenceTypeName()));
//                oneLevelCarRemoveDtos.add(oneLevelCarRemoveDto);
//                // 二级口门车辆删除
//            });
//        }
        return ReturnT.SUCCESS;
    }
}
