package com.casic.PgInterface.rs;

import com.casic.PgInterface.patroler.domain.PgLog;
import com.casic.PgInterface.patroler.domain.PgUser;
import com.casic.PgInterface.patroler.dto.PgLogDto;
import com.casic.PgInterface.patroler.manager.PgLogManager;
import com.casic.PgInterface.patroler.manager.PgUserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 * Created by yxw on 2017/11/4.
 */
@Component
@Path("pgLog")
public class PgLogRs {

    private static Logger logger = LoggerFactory.getLogger(PgLogRs.class);

    @Resource
    private PgLogManager pgLogManager;
    @Resource
    private PgUserManager pgPatrolerManager;

    /**
     * 新增日志
     */
    @POST
    @Path("addLog")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addLog(@FormParam("userName") String userName,
                                      @FormParam("operate") String operate)
            throws Exception {

        //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        PgLog pgLog=new PgLog();
        try {
            PgUser pgUser = new PgUser();
            pgUser = pgPatrolerManager.getPatrolerByuserName(userName);

            pgLog.setUserId(pgUser);
            pgLog.setOperate(operate);
            pgLog.setOperateTime(new Date());
            pgLog.setActive(1);

            pgLogManager.save(pgLog);
            result = "true";
            msg = "保存成功";

        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }
        resultMap.put("result", result);
        resultMap.put("msg", msg);
        return resultMap;
    }

    /**
     * 显示设备列表
     */
    @POST
    @Path("getPgLog")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getLogList(@FormParam("userName") String userName,
                                          @FormParam("startTime") String startTime,
                                          @FormParam("endTime") String endTime)
            throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgLogDto> pgLogDtoList = new ArrayList<PgLogDto>();
        try {
            pgLogDtoList = pgLogManager.getAllLogs(userName, startTime, endTime);
            if (pgLogDtoList == null || pgLogDtoList.size() == 0)
                msg = "没有找到日志信息";
            else
                msg = "日志信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "日志信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgLogDtoList);
        return resultMap;
    }


}