package com.casic.PgInterface.rs;

import com.casic.PgInterface.patroler.domain.PgInPgPerson;
import com.casic.PgInterface.patroler.domain.PgInPgStatus;
import com.casic.PgInterface.patroler.dto.PgInPgInfoDto;
import com.casic.PgInterface.patroler.dto.PgInPgPersonDto;
import com.casic.PgInterface.patroler.manager.PgInPgPersonManager;
import com.casic.PgInterface.patroler.manager.PgInPgStatusManager;
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.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by yxw on 2018/3/23.
 */
@Component
@Path("pgInPerson")
public class PgInPgPersonRs {

    @Resource
    private PgInPgPersonManager pgInPgPersonManager;

    @Resource
    private PgInPgStatusManager pgInPgStatusManager;

    /**
     * 派发工作证（新增入廊记录）
     */
    @POST
    @Path("addInPgPerson")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addInPgPerson(@FormParam("userName") String userName,
                                             @FormParam("userCode") String userCode,
                                             @FormParam("phoneNumber") String phoneNumber,
                                             @FormParam("personType") String personType,
                                             @FormParam("assetCode") String assetCode) throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String result = "";
        String msg = "";

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

        try {
            PgInPgPerson pgInPgPerson = pgInPgPersonManager.getPersonByUserName(userName);
            if (pgInPgPerson == null) {
                PgInPgPerson pgInPgPerson1=new PgInPgPerson();

                pgInPgPerson1.setUserName(userName);
                pgInPgPerson1.setUserCode(userCode);
                pgInPgPerson1.setPhoneNumber(phoneNumber);
                pgInPgPerson1.setPersonType(personType);
                pgInPgPerson1.setIsInPg(1);
                pgInPgPerson1.setAssetCode(assetCode);

                pgInPgPersonManager.save(pgInPgPerson1);
                String syResult=pgInPgPersonManager.synchorizedInPgInfo(assetCode,userCode,userName,personType);
                result = "true";
                msg = "人员注册保存成功"+syResult;
            }
            else {
                result = "false";
                msg = "存在人员信息";
            }
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }
        resultMap.put("result", result);
        resultMap.put("msg", msg);
        return resultMap;
    }

    /**
     * 获取人员信息列表
     */
    @POST
    @Path("getInPgPerson")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getInPgPerson() throws Exception {
        Map<String, Object> resultMap = new HashMap<>();
        String msg = "";

        List<PgInPgInfoDto> pgInPgInfoDtoList = new ArrayList<>();
        try {
            pgInPgInfoDtoList = pgInPgPersonManager.getPgInPersonList(0);
            if (pgInPgInfoDtoList == null || pgInPgInfoDtoList.size() == 0)
                msg = "没有找到入廊人员信息";
            else
                msg = "入廊人员信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "入廊人员信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgInPgInfoDtoList);
        return resultMap;
    }

    /**
     * 获取在线人员信息列表
     */
    @POST
    @Path("getInPgPersonInPg")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getInPgPersonInPg() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";

        List<PgInPgInfoDto> pgInPgInfoDtoList = new ArrayList<PgInPgInfoDto>();
        try {
            pgInPgInfoDtoList = pgInPgPersonManager.getPgInPersonList(1);
            if (pgInPgInfoDtoList == null || pgInPgInfoDtoList.size() == 0)
                msg = "没有找到入廊人员信息";
            else
                msg = "入廊人员信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "入廊人员信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgInPgInfoDtoList);
        return resultMap;
    }

    /**
     * 根据人员名称获取人员信息
     */
    @POST
    @Path("getInPgPersonByName")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getInPgPersonByName(@FormParam("userName") String userName) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";

        PgInPgPerson pgInPgPerson = null;
        PgInPgPersonDto pgInPgPersonDto=null;
        List<PgInPgPersonDto> pgInPgPersonDtoList=new ArrayList<>();
        try {
            pgInPgPerson = pgInPgPersonManager.getPersonByUserName(userName);
            if (pgInPgPerson == null)
                msg = "不存在人员信息";
            else {
                pgInPgPersonDto=new PgInPgPersonDto(pgInPgPerson);
                pgInPgPersonDtoList.add(pgInPgPersonDto);
                msg = "人员信息获取成功";
            }
        } catch (Exception e) {
            e.printStackTrace();
            msg = "人员信息获取失败";
        }

        resultMap.put("msg", msg);
        resultMap.put("result", pgInPgPersonDtoList);
        return resultMap;
    }

    /**
     * 人员出廊确认
     */
    @POST
    @Path("outPgPerson")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> outPgPerson(@FormParam("userName") String userName) throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";

        try {
            PgInPgPerson pgInPgPerson = pgInPgPersonManager.getPersonByUserName(userName);
            if (pgInPgPerson == null)
                msg = "不存在入廊人员";
            else {
                PgInPgStatus pgInPgStatus = pgInPgStatusManager.getPgInPgStatusByPerson(pgInPgPerson);
                if(pgInPgStatus==null)
                    msg="不存在入廊记录";
                else {
                    pgInPgStatus.setOutTime(new Date());
                    pgInPgPerson.setIsInPg(0);
                    pgInPgPersonManager.save(pgInPgPerson);
                    result = "true";
                    msg = "编辑成功";
                }
            }
        } catch (Exception e) {
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }

        resultMap.put("result", result);
        resultMap.put("msg", msg);
        return resultMap;
    }

    /**
     * 累计入廊人次
     */
    @POST
    @Path("getInPgNum")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> getInPgNum() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        int inPgNum=0;
        String msg = "";

        try{
            inPgNum=pgInPgStatusManager.getInPgNum();

            msg="人员入廊次数获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="人员入廊次数获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",inPgNum);

        return resultMap;
    }

    /**
     * 根据时间统计入廊人员次数
     */
    @POST
    @Path("getInPgPersonByTime")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getInPgPersonByTime(@FormParam("startTime") String startTime,
                                                   @FormParam("endTime") String endTime,
                                                   @FormParam("statisticType") String statisticType) throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";

        List<PgInPgInfoDto> pgInPgInfoDtoList = new ArrayList<PgInPgInfoDto>();
        try {
            pgInPgInfoDtoList = pgInPgPersonManager.getPgInPersonListByTime(startTime, endTime, statisticType);
            if(pgInPgInfoDtoList==null)
                msg="入廊人员统计信息获取失败";
            else
                msg="入廊人员统计信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "入廊人员统计信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgInPgInfoDtoList);
        return resultMap;
    }

}