package com.engine.htsc.mhjc.service.impl;

import com.engine.core.impl.Service;
import com.engine.htsc.mhjc.dao.HtscPortalDao;
import com.engine.htsc.mhjc.service.HtscPortalService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.GCONST;
import weaver.general.Util;
import weaver.hrm.User;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class HtscPortalServiceImpl extends Service implements HtscPortalService {

    @Override
    public Map<String, Object> getKanyueTotalForPortal(Map<String, Object> params) {

        Map<String, Object> appdate = new HashMap<String, Object>();
        HtscPortalDao htscPortalDao = new HtscPortalDao();
        RecordSet rs = new RecordSet();
        int total = 0;
        try{
            String workcode = Util.null2String(params.get("workcode"));
            String type = Util.null2String(params.get("type"));
            if(StringUtils.isNotEmpty(workcode))
            {
                StringBuffer queryBuffer = new StringBuffer();
                queryBuffer.append( " select count(1) as cou from uf_gwky p where (p.gwgg = 0 or p.gwgg is null) " ) ;

                String queryWhere = htscPortalDao.getQueryDataSql(workcode);
                queryBuffer.append(queryWhere);

                String dt5Where  = htscPortalDao.getKanyueUserWhere(workcode);
                if("0".equals(type))
                {
                    queryBuffer.append(" and exists(select 1 from uf_gwky_dt5 l where 1=1 "+dt5Where+" and l.mainid = p.id) " ) ;

                }else{
                    queryBuffer.append(" and not exists(select 1 from uf_gwky_dt5 l where 1=1 "+dt5Where+" and l.mainid = p.id) " )  ;
                }

                System.out.println(queryBuffer.toString());
                log.info(queryBuffer.toString());
                rs.executeQuery(queryBuffer.toString());
                if(rs.next()){
                    total = Util.getIntValue(Util.null2String(rs.getString("cou")));
                }
            }
            appdate.put("total",total);
        }catch (Exception e){
            log.info("e-1:"+e);
            appdate.put("total",total);
            appdate.put("api_status", false);
            appdate.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return appdate;
    }

    @Override
    public Map<String, Object> getGonggaoTotalForDocCenter(Map<String, Object> params, User user) {

        Map<String, Object> appdate = new HashMap<String, Object>();
        HtscPortalDao htscPortalDao = new HtscPortalDao();
        RecordSet rs = new RecordSet();
        int total = 0;
        try{
            String workcode = htscPortalDao.getUserCodeByUserid(user.getUID());
            if(StringUtils.isNotEmpty(workcode))
            {
                StringBuffer queryBuffer = new StringBuffer();
                queryBuffer.append( " select count(1) as cou from uf_gwky p where p.gwgg = 1 " ) ;

                String queryWhere = htscPortalDao.getQueryDataSql(workcode);
                queryBuffer.append(queryWhere);
                queryBuffer.append(" and ((to_char(sysdate,'yyyy-mm-dd') <=  p.yxqx and  p.yxqx is not null) or p.yxqx is null) "  );
                log.info(queryBuffer.toString());
                System.out.println(queryBuffer.toString());
                rs.executeQuery(queryBuffer.toString());
                if(rs.next()){
                    total = Util.getIntValue(Util.null2String(rs.getString("cou")));
                }
            }
            appdate.put("total",total);
        }catch (Exception e){
            log.info("e-1:"+e);
            appdate.put("total",total);
            appdate.put("api_status", false);
            appdate.put("api_errormsg", "catch exception : " + e.getMessage());
        }

        return appdate;
    }

    public static void main(String[] args) {
        GCONST.setRootPath("WEB-INF/");
        GCONST.setServerName("ecology");
        Map<String, Object> params=new HashMap<>();
        params.put("workcode","012901");
        params.put("type","1");
        User user = new User(11260);
        HtscPortalServiceImpl htscPortalServiceImpl = new HtscPortalServiceImpl();
        //htscPortalServiceImpl.getKanyueTotalForPortal(params);
        htscPortalServiceImpl.getGonggaoTotalForDocCenter(params,user);
    }

}
