package com.nmgs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.nmgs.mapper.Mapper;
import com.nmgs.service.IntegralLevelMessageService;
import com.nmgs.util.LogUtil;
import com.nmgs.util.PropertiesUtil;
import com.nmgs.util.PubTools;
import com.nmgs.util.SensitiveFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IntegralLevelMessageServiceImpl implements IntegralLevelMessageService {
    @Autowired
    public Mapper mapperI;
    private static String  DBType=PropertiesUtil.getValue("DBType");
    @Autowired
    SensitiveFilter sensitiveFilter;
    private static String sql="SELECT ilt.ID,\n" +
            "ilt.POSITION,\n" +
            "ilt.MESSAGE,\n" +
            "TO_CHAR(ilt.CREATETIME, 'YYYY-MM-DD HH24:MI:SS') AS CREATETIME,\n" +
            "TO_CHAR(ilt.REPLYTIME, 'YYYY-MM-DD HH24:MI:SS') AS REPLYTIME,\n" +
            "CASE ilt.REPLYSTATUS WHEN 'Y' THEN '已回复' ELSE '未回复' END AS REPLYSTATUSNAME,\n" +
            "ilt.REPLYSTATUS AS REPLYSTATUS,\n" +
            "NVL(ilt.REPLYCONTENT,'') AS REPLYCONTENT,\n" +
            "ilt.USERID,\n" +
            "NVL(ilt.REPLYUSERID,'') AS REPLYUSERID\n"+
            " FROM INTEGRAL_LEAVEMESSAGE_TABLE  ilt where 1=1 ";
    static{
       if("4".equals(DBType)){
           sql="SELECT ilt.ID,\n" +
                   "ilt.POSITION,\n" +
                   "ilt.MESSAGE,\n" +
                   "date_format(ilt.CREATETIME, '%Y-%m-%d %H:%i:%s') AS CREATETIME,\n" +
                   "date_format(ilt.REPLYTIME, '%Y-%m-%d %H:%i:%s') AS REPLYTIME,\n" +
                   "CASE ilt.REPLYSTATUS WHEN 'Y' THEN '已回复' ELSE '未回复' END AS REPLYSTATUSNAME,\n" +
                   "ilt.REPLYSTATUS AS REPLYSTATUS,\n" +
                   "COALESCE(ilt.REPLYCONTENT,'') AS REPLYCONTENT,\n" +
                   "ilt.USERID,\n" +
                   "COALESCE(ilt.REPLYUSERID,'') AS REPLYUSERID\n"+
                   " FROM INTEGRAL_LEAVEMESSAGE_TABLE  ilt where 1=1 ";
       }
    }
    @Override
    public List getList(String id, String userId) {
        List list=null;
        String sqlTemp=sql;
        try{
            if(!PubTools.isNull(id)){
                sqlTemp += " and ilt.ID = "+id+"";
            }
            if(!PubTools.isNull(userId)){
                sqlTemp += " and ilt.USERID =(SELECT ID FROM WEIXN_USER_TABLE WHERE OPENID='"+userId+"')";
            }
            sqlTemp +=" order by ilt.CREATETIME";
            list= this.mapperI.selectforlist(sqlTemp);
        }catch (Exception e){
            LogUtil.WriteLog_Error("查询反馈记录失败===>"+e.getMessage(),"IntegralLevelMessageServiceImpl");
            return list=null;
        }
        return list;
    }

    @Override
    public int commit(String userId, String positon, String message) {
        int ret=1;
        try{
            String currentDate=PubTools.getCurrentDate();

            message = sensitiveFilter.replaceSensitiveWord(message);

            String sql="insert into INTEGRAL_LEAVEMESSAGE_TABLE(ID,USERID,POSITION,MESSAGE,CREATETIME,REPLYSTATUS) " +
                    " values((SELECT NVL(MAX(ID),0)+1 FROM INTEGRAL_LEAVEMESSAGE_TABLE ),(SELECT ID FROM WEIXN_USER_TABLE WHERE OPENID='"+userId+"'),'"+positon+"','"+message+"',to_date('"+currentDate+"','YYYY-MM-DD HH24:MI:SS'),'N') ";
            if("4".equals(DBType)) {
                sql="insert into INTEGRAL_LEAVEMESSAGE_TABLE(ID,USERID,POSITION,MESSAGE,CREATETIME,REPLYSTATUS) " +
                        " values((SELECT COALESCE(MAX(tt.ID),0)+1 FROM INTEGRAL_LEAVEMESSAGE_TABLE tt ),(SELECT t.ID FROM WEIXN_USER_TABLE t WHERE t.OPENID='"+userId+"'),'"+positon+"','"+message+"',str_to_date('"+currentDate+"','%Y-%m-%d %H:%i:%s'),'N') ";
            }
            this.mapperI.insert(sql);
        }catch (Exception e){
            LogUtil.WriteLog_Error("插入反馈记录失败===>"+e.getMessage(),"IntegralLevelMessageServiceImpl");
           return  ret=-1;
        }
        return ret;
    }
}
