package org.ts.ddcs.business.rtu.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.ts.ddcs.business.rtu.entity.RtuBaseInfo;
import org.ts.ddcs.business.rtu.service.RtuService;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


@Service("RtuService")
@Transactional
public class RtuServiceImpl implements RtuService {

    @Autowired
    public JdbcTemplate jdbcTemplate;

    @Override
    public List<RtuBaseInfo> rtuDataListByProjectId(String projectId) {
        try {
            String sql = "SELECT b.RTU_CD as rtuCode,b.RTU_NAME as rtuName,b.RTU_UUID as uuid,b.SIM_CD as phone,b.NT as remark ,b.PROJECT_ID as projectId ,b.ORG_CD as orgId ,p.project_name as projectName, o.ORG_NAME as orgName  FROM t_base_rtu_info b LEFT JOIN t_au_org o on o.ID = b.ORG_CD  LEFT JOIN t_base_project p on p.id = b.PROJECT_ID WHERE b.DEL='0' ";
            if (null != projectId && projectId.length() > 0) {
                sql += " AND b.PROJECT_ID ='" + projectId + "'";
            }
            sql += " ORDER BY  b.RTU_CD  ";
            List<RtuBaseInfo> list = jdbcTemplate.query(sql, new RowMapper<RtuBaseInfo>() {
                @Override
                public RtuBaseInfo mapRow(ResultSet resultSet, int i) throws SQLException {
                    RtuBaseInfo rtuBaseInfo = new RtuBaseInfo();
                    rtuBaseInfo.setRtuCode(resultSet.getString(1));
                    rtuBaseInfo.setRtuName(resultSet.getString(2));
                    rtuBaseInfo.setUuid(resultSet.getString(3));
                    rtuBaseInfo.setPhone(resultSet.getString(4));
                    rtuBaseInfo.setRemark(resultSet.getString(5));
                    rtuBaseInfo.setProjectId(resultSet.getString(6));
                    rtuBaseInfo.setOrgId(resultSet.getString(7));
                    rtuBaseInfo.setProjectName(resultSet.getString(8));
                    rtuBaseInfo.setOrgName(resultSet.getString(9));
                    return rtuBaseInfo;
                }
            });
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
