package com.palmnest.dao.lingling.impl;

import com.palmnest.application.CodeConstants;
import com.palmnest.application.core.util.DateHelper;
import com.palmnest.application.dto.output.lingling.VisitorPassRecordVo;
import com.palmnest.dao.hibernate.GenericDaoHibernate;
import com.palmnest.dao.lingling.LingLingCurrentsetDao;
import com.palmnest.model.lingling.LingLingCurrentset;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: lmz
 * @create: 2022/2/14 14:11
 **/
@Slf4j
@Repository(value = "lingLingCurrentsetDao")
public class LingLingCurrentsetDaoImpl extends GenericDaoHibernate<LingLingCurrentset, Integer> implements LingLingCurrentsetDao {
    public LingLingCurrentsetDaoImpl() {
        super(LingLingCurrentset.class);
    }

    @Override
    public LingLingCurrentset getByOrgId(Integer orgId) {
        String               hql  = "from LingLingCurrentset where orgId=? and isEnabled = 1 ";
        List<LingLingCurrentset> list = getSession().createQuery(hql).setInteger(0, orgId).list();
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<VisitorPassRecordVo> getVisitorPassInfo(Integer orderId,int type) {
        List<VisitorPassRecordVo> resList = new ArrayList<>();
        StringBuilder sb=new StringBuilder(" SELECT o.visitorQrCode, o.visitorQrCodeId, o.linglingId, l.openTime, l.deviceId, IFNULL(d.deviceName,'已删除设备') deviceName  ");
        sb.append("   FROM tb_visitororder o  ");
        if(CodeConstants.PASS_LINGLINGID == type){
            sb.append("   INNER JOIN tb_lingling_log l ON l.lingLingId = o.linglingId  ");
        }else{
            sb.append("   INNER JOIN tb_lingling_log l ON l.qrcodeKey = o.visitorQrCode  ");
        }
        sb.append("   LEFT JOIN tb_lingling_device d ON d.deviceId = l.deviceId  ");
        sb.append("   WHERE o.visitorOrderId =   "+orderId);
        sb.append("   ORDER BY l.openTime DESC  ");
        log.info("getVisitorPassInfo-sql:{}",sb.toString());
        List<Map<String, Object>> list = getJdbcTemplate().queryForList(sb.toString());
        if(CollectionUtils.isEmpty(list)){
            return resList;
        }

        for(Map map : list){
            if(null == map || !map.containsKey("openTime") || null == map.get("openTime") || StringUtils.isEmpty(map.get("openTime").toString())){
                continue;
            }
            if(null == map || !map.containsKey("deviceName") || null == map.get("deviceName") || StringUtils.isEmpty(map.get("deviceName").toString())){
                continue;
            }
            VisitorPassRecordVo vo = new VisitorPassRecordVo();
            String openTime = map.get("openTime").toString();
            if(StringUtils.isNotEmpty(openTime)){
                openTime=DateHelper.formatDate(DateHelper.parseToDate(openTime,DateHelper.DATE_TIME_STRING),DateHelper.DATE_TIME_STRING);
            }
            String deviceName = map.get("deviceName").toString();
            vo.setDeviceName(deviceName);
            vo.setPassTime(openTime);
            resList.add(vo);
        }
        return resList;
    }
}
