package com.steering.message.server;

import CoreFS.SA08.CoreXmlSqlParsersFactory;
import com.steering.comm.converter.EntityTool;
import com.steering.message.model.AppMessageEntity;

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

/**
 * Created by 朱少波 on 2019/12/17.
 */
public class messageHelper {
    public static void addOrUpdateEntrustMessage(Connection connection,String subNo,String status,String key ,

                                               String title,String content,  String userId, String userName) throws SQLException {


        String sqlQuery = CoreXmlSqlParsersFactory.getSql("message.selectByKey");// 新增可疑从表实绩
        PreparedStatement pstQuery = connection.prepareStatement(sqlQuery);

        pstQuery.setObject(1,subNo);
        pstQuery.setObject(2,"entrust");
        ResultSet rs = pstQuery.executeQuery();
        List<AppMessageEntity> appMessages = EntityTool.getEntitysByRs(rs,
                AppMessageEntity.class);
        rs.close();
        pstQuery.close();

        if(appMessages==null || appMessages.size()<=0){

            AppMessageEntity appMessageEntity = new AppMessageEntity();

          //  appMessageEntity.setMessageTitle("[" + ("0".equals(httEntrustTotal.getSubcontractTarget())?"委外":"委内")  + "] "+ httEntrustTotal.getPlineName());
           appMessageEntity.setMessageTitle(title);
            appMessageEntity.setMessageLevelCode("90040101");
            appMessageEntity.setMessageClassCode("90040202");
            /*appMessageEntity.setMessageContent(
                    httEntrustTotal.getHttEntrusts()
                            .stream().filter(p-> StringUtil.isNotEmpty(p.getJudgeStoveNo())).map(HttEntrust::getJudgeStoveNo).distinct().collect(Collectors.joining(","))
                            + " " + httEntrustTotal.getHttEntrusts()
                            .stream().filter(p-> StringUtil.isNotEmpty(p.getJudgeStoveNo())).flatMap(p->p.getHttEntrustProjects().stream())
                            .map(HttEntrustProject::getProjectName).distinct().collect(Collectors.joining(","))
            );*/
            appMessageEntity.setMessageContent(content);
            appMessageEntity.setReleaseUserid(userId);
            appMessageEntity.setReleaseName(userName);
            appMessageEntity.setCreateUserid(userId);
            appMessageEntity.setCreateName(userName);
            appMessageEntity.setValidflag("20");
            appMessageEntity.setReferKey(subNo);
            appMessageEntity.setApprovalNode(status);
            appMessageEntity.setWindowKey("entrust");
            appMessageEntity.setEventKey(key);
            appMessageEntity.setNeedGroup("1");

            String ID = "";
            String sqlloading = CoreXmlSqlParsersFactory
                    .getSql("message.getMaxId");//
            PreparedStatement psta = connection
                    .prepareStatement(sqlloading);
            ResultSet rs1 = psta.executeQuery();
            if (rs1.next()) {
                ID = rs1.getString("MESSAGE_NO");
            }
            rs1.close();
            psta.close();

            appMessageEntity.setMessageNo(ID);
            String inserSql = CoreXmlSqlParsersFactory.getSql("message.insert");
            inserSql = EntityTool.fillSqlByEntity(inserSql, appMessageEntity);

             psta = connection.prepareStatement(inserSql);
            psta.addBatch();
            psta.executeBatch();
            psta.close();

            String inserUserSql = CoreXmlSqlParsersFactory.getSql("message.insertUser");
             psta = connection.prepareStatement(inserUserSql);
             psta.setObject(1,ID);
             psta.setObject(2,subNo);
            psta.addBatch();
            psta.executeBatch();
            psta.close();

        }else {
            String inserSql = CoreXmlSqlParsersFactory.getSql("message.update");
            PreparedStatement psta = connection.prepareStatement(inserSql);

            for (AppMessageEntity appMessage : appMessages) {
                psta.setObject(1,status);
                psta.setObject(2,key);
                psta.setObject(3,appMessage.getMessageNo());
                psta.addBatch();
            }
            psta.executeBatch();
            psta.close();
        }
    }

}
