package com.casic.PgInterface.rs;

import com.casic.PgInterface.core.util.DateUtils;
import com.casic.PgInterface.patroler.domain.PgInPgPerson;
import com.casic.PgInterface.patroler.domain.PgInPgStatus;
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.util.HashMap;
import java.util.Map;

@Component
@Path("pgInPgStatus")
public class PgInPgStatusRs {
    @Resource
    private PgInPgStatusManager pgInPgStatusManager;

    @Resource
    private PgInPgPersonManager pgInPgPersonManager;

    @POST
    @Path("addPgInTime")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addPgInTime(@FormParam("inTime") String inTime,
                                           @FormParam("assetCode") String assetCode){
        Map<String,Object> resultMap=new HashMap<String, Object>();

        String result="";
        String msg="";
        try{
            PgInPgStatus pgInPgStatus=new PgInPgStatus();

            if(inTime!=null)
            pgInPgStatus.setInTime(DateUtils.sdf4.parse(inTime));
            pgInPgStatus.setPgInPgPersonId(pgInPgPersonManager.getPgInPgPersonByAssetCode(assetCode));

            pgInPgStatusManager.save(pgInPgStatus);
            msg="入廊时间提交成功";
            result="true";
        }
        catch (Exception e){
            e.printStackTrace();
            msg="入廊时间提交失败";
            result="false";
        }

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

        return resultMap;
    }

    @POST
    @Path("addPgOutTime")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addPgOutTime(@FormParam("outTime") String outTime,
                                            @FormParam("assetCode") String assetCode){
        Map<String,Object> resultMap=new HashMap<String, Object>();

        String result="";
        String msg="";
        try{
            PgInPgPerson pgInPgPerson=pgInPgPersonManager.getPgInPgPersonByAssetCode(assetCode);

            pgInPgPerson.setIsInPg(0);
            pgInPgPersonManager.save(pgInPgPerson);

            PgInPgStatus pgInPgStatus=pgInPgStatusManager.getPgInPgStatusByPgInPgPerson(pgInPgPerson);
            if(outTime!=null)
            pgInPgStatus.setOutTime(DateUtils.sdf4.parse(outTime));

            pgInPgStatusManager.save(pgInPgStatus);

            msg="出廊时间提交成功";
            result="true";
        }
        catch (Exception e){
            e.printStackTrace();
            msg="出廊时间提交失败";
            result="false";
        }

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

        return resultMap;
    }

}