package org.ts.ddcs.service.Impl;

import org.ts.ddcs.entity.RtuInfo;
import org.ts.ddcs.log.LogHelper;
import org.ts.ddcs.service.RtuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.stereotype.Service;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


/**
 * @author dylan
 */
@Service("RtuService")
public class RtuServiceImpl implements RtuService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void saveRtu(RtuInfo rtuInfo) {
        try {
            String sql = "SELECT * FROM wr_rtu_b WHERE RTU_CD='" + rtuInfo.getRtuCode() + "'";
            List<Map<String, Object>> records = jdbcTemplate.queryForList(sql);
            if (null != records && records.size() > 0) {
                sql = "UPDATE  wr_rtu_b SET ORG_CD=?,RTU_NAME=? where RTU_CD=?";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, rtuInfo.getOrgId());
                        preparedStatement.setString(++index, rtuInfo.getRtuName());
                        preparedStatement.setString(++index, rtuInfo.getRtuCode());
                    }
                });
            } else {
                sql = "INSERT INTO wr_rtu_b  (RTU_CD,ORG_CD,RTU_NAME) VALUES (?,?,?)";
                jdbcTemplate.update(sql, new PreparedStatementSetter() {
                    @Override
                    public void setValues(PreparedStatement preparedStatement) throws SQLException {
                        int index = 0;
                        preparedStatement.setString(++index, rtuInfo.getRtuCode());
                        preparedStatement.setString(++index, rtuInfo.getOrgId());
                        preparedStatement.setString(++index, rtuInfo.getRtuName());
                    }
                });
            }
        } catch (Exception e) {
            LogHelper.error(e);
        }
    }


    @Override
    public List<Map<String, Object>> rtuList() {
        String sql = "SELECT * FROM wr_rtu_b b WHERE b.DEL ='0'  ";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        return list;
    }

    @Override
    public void deleteRtu(String rtuCode) {
        String sql = "UPDATE  wr_rtu_b b  SET b.DEL ='1' WHERE b.RTU_CD ='" + rtuCode + "'";
        jdbcTemplate.update(sql);

    }

    @Override
    public Map<String, Object> projectInfo(String orgId) {
        String sql = "SELECT * FROM t_base_project p  WHERE  p.org_id ='" + orgId + "' ";
        List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
        if (null != list && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public void SetRtuProject(String rtuCode, String orgId, String projectId) {

        String sql = "UPDATE  wr_rtu_b b  SET b.ORG_CD ='" + orgId + "' , b.PROJECT_ID='" + projectId + "' WHERE b.RTU_CD ='" + rtuCode + "'";
        jdbcTemplate.update(sql);
    }
}
