<%@ page import="weaver.general.*" %>
<%@page import="weaver.conn.RecordSet"%>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="org.apache.commons.lang.StringUtils" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.*" %>
<%@ page import="java.math.BigDecimal" %>
<%@ page import="weaver.formmode.customjavacode.customsearch.contractSearchService.GetTotalService" %>
<%@ page import="weaver.formmode.customjavacode.customsearch.contractSearchService.TotalMoneyDTO" %>
<%@ page import="weaver.formmode.customjavacode.customsearch.contractSearchService.TotalRequestDTO" %>
<%@ page import="java.text.DecimalFormat" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" %>

<%
    BaseBean bb = new BaseBean();
    String begindate = "" ;
    String enddate = "" ;
    int year = 0 ;
    User user = null;
    String type = Util.null2String(request.getParameter("type"));
    if("0".equals(type)){
        begindate = Util.null2String(request.getParameter("begindate"));
        enddate =  Util.null2String(request.getParameter("enddate"));
        year = Util.getIntValue(request.getParameter("year"));
        int userid = Util.getIntValue(request.getParameter("userid"));
        user = new User(userid);
    }else{
        begindate = TimeUtil.getFirstDayOfTheYear();
        enddate = TimeUtil.getCurrentDateString();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        year = calendar.get(Calendar.YEAR);
        user = HrmUserVarify.getUser(request , response) ;
    }

    String message = getSalesPerson(user,begindate,enddate,year);

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("data",message);
%>
<%=jsonObject.toString()  %>

<%!
    /***
     * 销售显示6个，签约合同金额、年度指标、指标完成率、合同收款、客户数量、商机数量；
     */
    public String getSalesPerson(User user, String begindate, String enddate,int currentYear){
        BaseBean bb = new BaseBean();

        String message = "";

        try {
            String qyhtje = getContractPerformanc(user,currentYear,begindate,enddate);
            message += "[qyhtje]:"+qyhtje;
        }catch (Exception e){

        }

        try {
            String htsk = getContractCollection(user,currentYear,begindate,enddate);
            message += "[htsk]:"+htsk;
        }catch (Exception e){

        }

        try{
            String xlkhhtje= getSignContract(user,currentYear,begindate,enddate);
            message += "[xlkhhtje]:"+xlkhhtje;
        }catch (Exception e){

        }
        return message;
    }



    /////##########################销售人员##################################//
    /***
     * 签约合同金额
     * @param user
     * @param year
     * @param startDate
     * @param endDate
     * @return
     */
    public String getSignContract(User user,int year,String startDate,String endDate){
        BaseBean bb = new BaseBean();
        bb.writeLog("getSignContract:");
        Map<String,Double> map = new HashMap<String,Double>();
        GetTotalService totalService = new GetTotalService();

        String contractTableName = "zxyjlxfb";
        String searchFlag = "1";
        String dateCondition = "5";
        String zxyjFlag = "";

        String message = "" ;

        try {
            TotalRequestDTO requestDTO = new TotalRequestDTO();
            requestDTO.setDateValue(dateCondition);
            requestDTO.setSearchFlag(searchFlag);
            requestDTO.setContractTableName(contractTableName);
            requestDTO.setUser(user);
            requestDTO.setZxyjFlag(zxyjFlag);
            TotalMoneyDTO totalMoneyDTO = totalService.getTotalInfo(requestDTO);
            bb.writeLog("totalMoneyDTO:"+totalMoneyDTO.toString());

            message = totalMoneyDTO.toString() ;
        }catch (Exception e) {
            bb.writeLog("getSignContract-e:"+e);
        }
        return message;

    }



    /***
     *  合同收款金额
     * @param user
     * @param year
     * @param startDate
     * @param endDate
     * @return
     */
    public String getContractCollection(User user,int year,String startDate,String endDate){
        BaseBean bb = new BaseBean();
        bb.writeLog("getContractCollection:");

        String message = "";

        String contractTableName = "zxhtyj";
        String searchFlag = "1";
        String dateCondition = "5";
        String zxyjFlag = "zxhtsknew";
        GetTotalService totalService = new GetTotalService();
        try {
            TotalRequestDTO requestDTO = new TotalRequestDTO();
            requestDTO.setDateValue(dateCondition);
            requestDTO.setSearchFlag(searchFlag);
            requestDTO.setContractTableName(contractTableName);
            requestDTO.setUser(user);
            requestDTO.setZxyjFlag(zxyjFlag);
            TotalMoneyDTO totalMoneyDTO = totalService.getTotalInfo(requestDTO);
            bb.writeLog("totalMoneyDTO:"+totalMoneyDTO.toString());
            message = totalMoneyDTO.toString();
        }catch (Exception e) {
            bb.writeLog("getSignContract-e:"+e);
        }
        return message;
    }


    /***
     *
     * @param user
     * @param year
     * @param startDate
     * @param endDate
     * @return
     */
    public String getContractPerformanc(User user,int year,String startDate,String endDate){
        BaseBean bb = new BaseBean();

        String message = "" ;
        bb.writeLog("getContractCollection:");
        Map<String,Double> map = new HashMap<String,Double>();
        String contractTableName = "zxhtyj";
        String searchFlag = "1";
        String dateCondition = "5";
        String zxyjFlag = "";

        GetTotalService totalService = new GetTotalService();
        try {
            TotalRequestDTO requestDTO = new TotalRequestDTO();
            requestDTO.setDateValue(dateCondition);
            requestDTO.setSearchFlag(searchFlag);
            requestDTO.setContractTableName(contractTableName);
            requestDTO.setUser(user);
            requestDTO.setZxyjFlag(zxyjFlag);
            TotalMoneyDTO totalMoneyDTO = totalService.getTotalInfo(requestDTO);
            bb.writeLog("totalMoneyDTO:"+totalMoneyDTO.toString());
            message = totalMoneyDTO.toString();
        }catch (Exception e) {
            bb.writeLog("getContractPerformanc-e:"+e);
        }

        return message;
    }


    /////##########################销售人员##################################//

    /***
     *
     * @param pkey
     * @return
     */
    public String getUfPropValue(String pkey){
        if(StringUtils.isEmpty(pkey)){
            return "";
        }
        String value = "" ;
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        String active = bb.getPropValue("developProp","active");
        String sql = " select devvalue,prodvalue from uf_hr_prop where pkey = ?" ;
        rs.executeQuery(sql,new Object[]{pkey});
        if(rs.next()){
            value = Util.null2String(rs.getString(active+"value"));
        }
        return value;
    }

    /***
     *
     * @param value
     * @return
     */
    public String formatBigDecimal(String value){
        String formatValue = "" ;
        try{
            DecimalFormat decimalFormat = new DecimalFormat("0.00");
            BigDecimal _decimal = new BigDecimal(value);
            formatValue =  decimalFormat.format(_decimal);
        }catch (Exception e){

        }
        return formatValue;
    }
%>



