package net.vtstar.zhongtong.avi.monitoring.service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.OrderBy;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.user.search.service.SearchService;
import net.vtstar.utils.CollecUtils;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.equipment.domain.PaintshopStation;
import net.vtstar.zhongtong.avi.monitoring.domain.StationPassInfo;
import net.vtstar.zhongtong.avi.monitoring.domain.enums.ScanType;
import net.vtstar.zhongtong.avi.monitoring.mapper.StationPassInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: xiadongwei
 * @Date: 2019/12/13 15:18
 */
@Slf4j
@Service
public class StationPassInfoService {

    public static String statusR = "replenish.scan.state";
    public static String startTimeR = "replenish.scan.start";
    public static String endTimeR = "replenish.scan.end";

    private static List<String> passNames = Arrays.asList("3070-交验及防护", "3050-二遍清漆", "3130-水磨", "3120-色漆喷涂",
            "3140-图案粘贴", "3100-面漆喷涂", "3160-中涂漆及打磨", "3020-车身附件安装", "3150-纹理漆", "3030-底漆找补", "3110-腻子刮磨",
            "3170-阻尼胶喷涂", "3060-发泡喷涂", "3010-玻璃钢安装", "3090-密封", "3040-电泳");
    @Resource(name = "mesSqlServerJdbcTemplate")
    private JdbcTemplate mesSqlServerJdbcTemplate;

    @Autowired
    private StationPassInfoMapper passInfoMapper;

    @Autowired
    private SearchService searchService;

    @Resource(name = "redisTemplate")
    private ValueOperations valueOperations;

    //创建过站信息记录
    public Long createPassInfo(String workOrderNo, String busNo, String rfid, String areaNo, String stationNo) {
        StationPassInfo info = new StationPassInfo();
        info.setVIN(workOrderNo);
        info.setAreaNo(areaNo);
        if (null == workOrderNo) {
            info.setVIN("");
        }
        if (null == areaNo) {
            info.setAreaNo("");
        }
        info.setBusNo(busNo);
        info.setRfid(rfid);
        info.setStationNo(stationNo);
        info.setStationName(getPassNameByStationNo(stationNo));
        info.setInTime(new Date());
        info.setPassTime(new Date());
        info.setCreateTime(new Date());
        info.setCreateBy("RFID");
        passInfoMapper.insert(info);
        String sql = "INSERT INTO VEHICLE_PASSING_INFO  VALUES (?,?,?,?,?,?,?)";
        mesSqlServerJdbcTemplate.update(sql, new Object[]{info.getVIN(), info.getBusNo(), info.getAreaNo(),
                info.getStationNo(), DateUtils.parseDateToString(info.getPassTime(), "yyyy-MM-dd HH:mm:ss"),
                null, 1});
        return info.getId();
    }

    public void endPassInfo(PaintshopStation station) {
        StationPassInfo info = new StationPassInfo();
        info.setVIN(station.getSkid().getWorkOrderNo());
        info.setAreaNo(station.getAreaName());
        if (null == station.getSkid().getWorkOrderNo()) {
            info.setVIN("");
        }
        if (null == station.getAreaName()) {
            info.setAreaNo("");
        }
        info.setBusNo(station.getSkid().getBusNo());
        info.setRfid(station.getSkid().getRfid());
        info.setStationNo(station.getCode());
        info.setStationName(getPassNameByStationNo(station.getCode()));
        info.setOutTime(new Date());
        info.setPassTime(new Date());
        info.setCreateTime(new Date());
        info.setCreateBy("RFID");
        passInfoMapper.insert(info);
        //todo:!!!!!!!!!!!!!!!!
        String sql = "INSERT INTO VEHICLE_PASSING_INFO  VALUES (?,?,?,?,?,?,?)";
        mesSqlServerJdbcTemplate.update(sql, new Object[]{info.getVIN(), info.getBusNo(), info.getAreaNo(),
                info.getStationNo(), DateUtils.parseDateToString(info.getPassTime(), "yyyy-MM-dd HH:mm:ss"),
                null, 1});
    }

    public List<StationPassInfo> findPassInfoByNoFind() {
        return passInfoMapper.findList(new WhereFilter() {{
            addFilter("pass_time", Operation.IS, "null");
            addFilter("station_name", Operation.IS_NOT, "null");
            addOrderBy("create_time", OrderBy.DESC);
        }}, StationPassInfo.class);
    }

    public List<StationPassInfo> findPassInfoBySearchCode(JSONObject pageFilter) {
        PageHelper.startPage(pageFilter.getIntValue("pageNum"), pageFilter.getIntValue("pageSize"));
        if (ScanType.NULL.name().equals(pageFilter.get("scanType"))){
            return passInfoMapper.findList(searchService.pageFilter2Filter(pageFilter, new WhereFilter(){{
                addFilter("pass_time", Operation.IS, "null");
                addFilter("station_name", Operation.IS_NOT, "null");
            }}), StationPassInfo.class);
        }
        return passInfoMapper.findList(searchService.pageFilter2Filter(pageFilter), StationPassInfo.class);
    }

    public void savePassInfo(StationPassInfo info) {
        StationPassInfo byId = passInfoMapper.getById(info.getId(), StationPassInfo.class);
        byId.setPassTime(info.getPassTime());
        byId.setStationNo(info.getStationNo());
        byId.setCreateBy(info.getCreateBy());
        byId.setCreateTime(new Date());
        passInfoMapper.update(byId);
        String sql = "INSERT INTO VEHICLE_PASSING_INFO  VALUES (?,?,?,?,?,?,?)";
        mesSqlServerJdbcTemplate.update(sql, new Object[]{info.getVIN(), byId.getBusNo(), byId.getAreaNo(),
                info.getStationNo(), DateUtils.parseDateToString(info.getPassTime(), "yyyy-MM-dd HH:mm:ss"),
                null, 1});
    }

    public void startReplenishScan(Integer beginTimeInt, Integer endTimeInt) {
        valueOperations.set(statusR, true);
        valueOperations.set(startTimeR, beginTimeInt);
        valueOperations.set(endTimeR, endTimeInt);
    }

    public void stopReplenishScan() {
        valueOperations.set(statusR, false);
    }

    public void checkPassInfo(String workOrderNo, String busNo, String rfid) {
        Boolean status = (Boolean) valueOperations.get(statusR);
        Integer startInt = (Integer) valueOperations.get(startTimeR);
        Integer endInt = (Integer) valueOperations.get(endTimeR);
        if (null == startInt || null == endInt){
            startInt = 0;
            endInt = 10;
        }
        if (null == status || !status){
            return;
        }
        List<StationPassInfo> list = passInfoMapper.findList(new WhereFilter() {{
            addFilter("VIN", Operation.EQUAL, workOrderNo);
            addFilter("rfid", Operation.EQUAL, rfid);
        }}, StationPassInfo.class);
        Map<String, List<StationPassInfo>> listMap = list.stream().filter(s -> s.getStationName() != null).collect(Collectors.groupingBy(StationPassInfo::getStationName));
        for (int i = 0; i < passNames.size(); i++){
            String passName = passNames.get(i);
            if (null != listMap.get(passName)){
                continue;
            }
            if (i == 0){
                StationPassInfo info = new StationPassInfo();
                info.setVIN(workOrderNo);
                info.setAreaNo(getAreaNoByName(passName));
                info.setStationNo(getStationNoByName(passName)[0]);
                info.setBusNo(busNo);
                info.setRfid(rfid);
                info.setStationName(passName);
                info.setPassTime(new Date());
                info.setCreateTime(new Date());
                info.setCreateBy("rfid");
                passInfoMapper.insert(info);
                continue;
            }
            String lastPassName = passNames.get(i - 1);
            List<StationPassInfo> lastInfo = passInfoMapper.findList(new WhereFilter() {{
                addFilter("VIN", Operation.EQUAL, workOrderNo);
                addFilter("rfid", Operation.EQUAL, rfid);
                addFilter("station_name", Operation.EQUAL, lastPassName);
            }}, StationPassInfo.class);
            if (!CollecUtils.isEmpty(lastInfo)){
                int time = (int)(Math.random() * (endInt - startInt) + startInt);
                StationPassInfo stationPassInfo = lastInfo.get(0);
                StationPassInfo info = new StationPassInfo();
                info.setVIN(workOrderNo);
                info.setAreaNo(getAreaNoByName(passName));
                info.setStationNo(getStationNoByName(passName)[0]);
                info.setBusNo(busNo);
                info.setRfid(rfid);
                info.setStationName(passName);
                info.setPassTime(new Date(stationPassInfo.getCreateTime().getTime() - (time * 60 * 1000)));
                info.setCreateTime(new Date());
                info.setCreateBy("rfid");
                passInfoMapper.insert(info);
            }else{
                log.error("自动补扫失败, 没有找到其下一工序的信息");
            }
        }
    }

    public String[] getStationNoByName(String stationName) {
        String[] str;
        switch (stationName) {
            case "3040-电泳":
                str = new String[]{"HDG2-2"};
                break;
            case "3090-密封":
                str = new String[]{"HDG1-57", "HDG1-58", "HDG1-75"};
                break;
            case "3010-玻璃钢安装":
                str = new String[]{"HDG1-70", "HDG1-74"};
                break;
            case "3060-发泡喷涂":
                str = new String[]{"HDG1-89", "HDG1-94"};
                break;
            case "3170-阻尼胶喷涂":
                str = new String[]{"HDG1-85", "HDG1-90"};
                break;
            case "3110-腻子刮磨":
                str = new String[]{"HDG1-109", "HDG1-114"};
                break;
            case "3030-底漆找补":
                str = new String[]{"HDG1-109", "HDG1-114"};
                break;
            case "3150-纹理漆":
                str = new String[]{"HDG1-109", "HDG1-114"};
                break;
            case "3020-车身附件安装":
                str = new String[]{"HDG1-105", "HDG1-110"};
                break;
            case "3160-中涂漆及打磨":
                str = new String[]{"HDG1-119", "HDG1-129"};
                break;
            case "3100-面漆喷涂":
                str = new String[]{"HDG1-168", "HDG1-170", "HDG1-171", "HDG1-196"};
                break;
            case "3140-图案粘贴":
                str = new String[]{"HDG1-150", "HDG1-151", "HDG1-152", "HDG1-156", "HDG1-157"};
                break;
            case "3120-色漆喷涂":
                str = new String[]{"HDG1-181", "HDG1-184", "HDG1-190", "HDG1-195"};
                break;
            case "3130-水磨":
                str = new String[]{"HDG5-24", "HDG5-25"};
                break;
            case "3050-二遍清漆":
                str = new String[]{"HDG1-168", "HDG1-170", "HDG1-171", "HDG1-196"};
                break;
            case "3070-交验及防护":
                str = new String[]{"HDG1-153", "HDG1-154", "HDG1-155", "HDG1-158", "HDG1-159", "HDG1-160"};
                break;
            default:
                str = new String[]{};
                break;
        }
        return str;
    }

    private String getPassNameByStationNo(String stationNo) {
        String passName;
        switch (stationNo) {
            case "HDG2-2":
                passName = "3040-电泳";
                break;
            case "HDG1-57":
                passName = "3090-密封";
                break;
            case "HDG1-58":
                passName = "3090-密封";
                break;
            case "HDG1-75":
                passName = "3090-密封";
                break;
            case "HDG1-70":
                passName = "3010-玻璃钢安装";
                break;
            case "HDG1-74":
                passName = "3010-玻璃钢安装";
                break;
            case "HDG1-89":
                passName = "3060-发泡喷涂";
                break;
            case "HDG1-94":
                passName = "3060-发泡喷涂";
                break;
            case "HDG1-85":
                passName = "3170-阻尼胶喷涂";
                break;
            case "HDG1-90":
                passName = "3170-阻尼胶喷涂";
                break;
            case "HDG1-109":
                passName = "3110-腻子刮磨";
                break;
            case "HDG1-114":
                passName = "3110-腻子刮磨";
                break;
            case "HDG1-105":
                passName = "3020-车身附件安装";
                break;
            case "HDG1-110":
                passName = "3020-车身附件安装";
                break;
            case "HDG1-119":
                passName = "3160-中涂漆及打磨";
                break;
            case "HDG1-129":
                passName = "3160-中涂漆及打磨";
                break;
            case "HDG1-168":
                passName = "3100-面漆喷涂";
                break;
            case "HDG1-170":
                passName = "3100-面漆喷涂";
                break;
            case "HDG1-171":
                passName = "3100-面漆喷涂";
                break;
            case "HDG1-196":
                passName = "3100-面漆喷涂";
                break;
            case "HDG1-150":
                passName = "3140-图案粘贴";
                break;
            case "HDG1-151":
                passName = "3140-图案粘贴";
                break;
            case "HDG1-152":
                passName = "3140-图案粘贴";
                break;
            case "HDG1-156":
                passName = "3140-图案粘贴";
                break;
            case "HDG1-157":
                passName = "3140-图案粘贴";
                break;
            case "HDG1-181":
                passName = "3120-色漆喷涂";
                break;
            case "HDG1-184":
                passName = "3120-色漆喷涂";
                break;
            case "HDG1-190":
                passName = "3120-色漆喷涂";
                break;
            case "HDG1-195":
                passName = "3120-色漆喷涂";
                break;
            case "HDG5-24":
                passName = "3130-水磨";
                break;
            case "HDG5-25":
                passName = "3130-水磨";
                break;
            case "HDG1-153":
                passName = "3070-交验及防护";
                break;
            case "HDG1-154":
                passName = "3070-交验及防护";
                break;
            case "HDG1-155":
                passName = "3070-交验及防护";
                break;
            case "HDG1-158":
                passName = "3070-交验及防护";
                break;
            case "HDG1-159":
                passName = "3070-交验及防护";
                break;
            case "HDG1-160":
                passName = "3070-交验及防护";
                break;
            default:
                passName = "";
                break;
        }
        return passName;
    }

    public String getAreaNoByName(String stationName) {
        String str;
        switch (stationName) {
            case "3040-电泳":
                str = "电泳";
                break;
            case "3090-密封":
                str = "密封防腐";
                break;
            case "3010-玻璃钢安装":
                str = "密封防腐";
                break;
            case "3060-发泡喷涂":
                str = "密封防腐";
                break;
            case "3170-阻尼胶喷涂":
                str = "密封防腐";
                break;
            case "3110-腻子刮磨":
                str = "腻子";
                break;
            case "3030-底漆找补":
                str = "腻子";
                break;
            case "3150-纹理漆":
                str = "腻子";
                break;
            case "3020-车身附件安装":
                str = "腻子";
                break;
            case "3160-中涂漆及打磨":
                str = "面/色漆";
                break;
            case "3100-面漆喷涂":
                str = "面/色漆";
                break;
            case "3140-图案粘贴":
                str = "面/色漆";
                break;
            case "3120-色漆喷涂":
                str = "面/色漆";
                break;
            case "3130-水磨":
                str = "面/色漆";
                break;
            case "3050-二遍清漆":
                str = "面/色漆";
                break;
            case "3070-交验及防护":
                str = "成品";
                break;
            default:
                str = "";
                break;
        }
        return str;
    }

}
