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

import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import net.vtstar.scada.base.equipmgt.service.EquipDataService;
import net.vtstar.user.mybatis.provider.Operation;
import net.vtstar.user.mybatis.provider.OrderBy;
import net.vtstar.user.mybatis.provider.WhereFilter;
import net.vtstar.utils.DateUtils;
import net.vtstar.zhongtong.avi.equipment.domain.EquipStatusRecord;
import net.vtstar.zhongtong.avi.equipment.mapper.EquipStatusRecordMapper;
import net.vtstar.zhongtong.avi.equipment.mapper.EquipmentDataMapper;
import net.vtstar.zhongtong.avi.gateway.painting.domain.DryingRoomTempMachine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author: xiadongwei
 * @Date: 2019/9/20 9:31
 */
@Slf4j
@Service
public class DryingRoomTempService {

    @Autowired
    private EquipDataService equipDataService;

    @Autowired
    private EquipmentDataMapper dataMapper;

    @Autowired
    private EquipStatusRecordMapper statusRecordMapper;

    /**
     * 查询温度状态列表
     */
    public List<EquipStatusRecord> findPageEquip(Integer pageNum, Integer pageSize, String equipCode, Date beginTime) {
        PageHelper.startPage(pageNum, pageSize);
        if (null == beginTime){
            beginTime = DateUtils.getStartOfDay(1);
        }
        String startOfDay = DateUtils.parseDateToString(DateUtils.getStartOfDay(-1, beginTime), "yyyy-MM-dd HH:mm:ss");
        String endOfDay = DateUtils.parseDateToString(DateUtils.getEndOfDay(-1, beginTime), "yyyy-MM-dd HH:mm:ss");
        if (StringUtils.isEmpty(equipCode)){
            return statusRecordMapper.findList(new WhereFilter(){{
                addFilter("equip_type", Operation.EQUAL, "DRYING_ROOM_TEMP");
                addFilter("begin_time", Operation.GREATER_THAN, startOfDay);
                addFilter("end_time", Operation.LESS_THAN, endOfDay);
                addFilter("equip_status", Operation.EQUAL, "RUN");
                addOrderBy(EquipStatusRecord.class, "begin_time", OrderBy.DESC);
            }}, EquipStatusRecord.class);
        }
        return statusRecordMapper.findList(new WhereFilter(){{
            addFilter("equip_code", Operation.EQUAL, equipCode);
            addFilter("begin_time", Operation.GREATER_THAN, startOfDay);
            addFilter("end_time", Operation.LESS_THAN, endOfDay);
            addFilter("equip_status", Operation.EQUAL, "RUN");
            addOrderBy(EquipStatusRecord.class, "begin_time", OrderBy.DESC);
        }}, EquipStatusRecord.class);
    }


    public List<DryingRoomTempMachine> getTempByStateId(String equipCode, String beginTime, String endTime) {
        return dataMapper.findJoin(new WhereFilter() {{
            addFilter("eqp_code", Operation.EQUAL, equipCode);
            addFilter("create_time", Operation.EQUAL_GREATER_THAN, beginTime);
            addFilter("create_time", Operation.EQUAL_LESS_THAN, endTime);
        }}, DryingRoomTempMachine.class);
    }
}
