package com.casic.PgInterface.rs;

import com.casic.PgInterface.construction.domain.PgConstruction;
import com.casic.PgInterface.construction.domain.PgConstructionType;
import com.casic.PgInterface.construction.dto.PgConstructionDto;
import com.casic.PgInterface.construction.dto.PgConstructionStiatistic;
import com.casic.PgInterface.construction.manager.PgConstructionManager;
import com.casic.PgInterface.construction.manager.PgConstructionTypeManager;
import com.casic.PgInterface.devTable.domain.PgPartition;
import com.casic.PgInterface.devTable.dto.PgDeviceDto;
import com.casic.PgInterface.devTable.manager.PgDeviceManager;
import com.casic.PgInterface.devTable.manager.PgPartitionManager;
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.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zxh on 2018/3/27.
 */

@Component
@Path("pgConstruction")
public class PgConstructionRs {

    private static Logger logger = LoggerFactory.getLogger(PgConstructionRs.class);
    @Resource
    private PgConstructionTypeManager pgConstructionTypeManager;
    @Resource
    private PgConstructionManager pgConstructionManager;
    @Resource
    private PgPartitionManager pgPartitionManager;
    @Resource
    private PgDeviceManager pgDeviceManager;

    /*
         *新建工程
         */
    @POST
    @Path("addConstruction")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addConstruction(@FormParam("cs_name")String cs_name,
                                               @FormParam("constructionType")String constructionType,
                                               @FormParam("cs_detail")String cs_detail,
                                               @FormParam("cs_beginTime")String cs_beginTime,
                                               @FormParam("cs_endTime")String cs_endTime,
                                               @FormParam("cs_charge")String cs_charge,
                                               @FormParam("cs_phone")String cs_phone,
                                               @FormParam("cs_type")String cs_type,
                                               @FormParam("parName")String parName)
    throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {

            PgConstructionType pgConstructionType = pgConstructionTypeManager.getConstructionTypeByType(constructionType);
            PgPartition pgPartition = pgPartitionManager.getPartitionByParName(parName);
            if (pgConstructionType==null) {
                result = "false";
                msg = "保存失败,不存在项目状态:"+"constructionType";
            } else if (pgPartition==null) {
                result = "false";
                msg = "保存失败，不存在施工区域:"+parName;
            } else {
                PgConstruction pgConstruction = new PgConstruction();
                pgConstruction.setCs_name(cs_name);
                pgConstruction.setCs_detail(cs_detail);
                pgConstruction.setCs_beginTime(sdf.parse(cs_beginTime));
                pgConstruction.setCs_endTime(sdf.parse(cs_endTime));
                pgConstruction.setCs_charge(cs_charge);
                pgConstruction.setCs_phone(cs_phone);
                pgConstruction.setCs_type(cs_type);

                pgConstruction.setPgPartitionId(pgPartition);

                pgConstruction.setPgConstructionTypeId(pgConstructionType);
                pgConstruction.setActive(1);

                logger.info("保存成功");
                pgConstructionManager.save(pgConstruction);
                result = "true";
                msg = "保存成功";
            }
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }
        resultMap.put("success", result);
        resultMap.put("msg", msg);
        return resultMap;
    }

    /*
     施工信息编辑
     */
    @POST
    @Path("editConstruction")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editConstruction(@FormParam("id")String id,
                                                @FormParam("cs_name")String cs_name,
                                                @FormParam("constructionType")String constructionType,
                                                @FormParam("cs_detail")String cs_detail,
                                                @FormParam("cs_beginTime")String cs_beginTime,
                                                @FormParam("cs_endTime")String cs_endTime,
                                                @FormParam("cs_charge")String cs_charge,
                                                @FormParam("cs_phone")String cs_phone,
                                                @FormParam("cs_type")String cs_type,
                                                @FormParam("parName")String parName)
    throws Exception{

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

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        try {
                if (id.equals(""))
                {
                    result = "false";
                    msg = "请输入ID";
                }else {
                    PgConstructionType pgConstructionType=pgConstructionTypeManager.getConstructionTypeByType(constructionType);

                    PgPartition pgPartition=pgPartitionManager.getPartitionByParName(parName);

                    PgConstruction pgConstruction = pgConstructionManager.get(Long.valueOf(id));
                    pgConstruction.setCs_name(cs_name);
                    pgConstruction.setCs_detail(cs_detail);
                    pgConstruction.setCs_beginTime(sdf.parse(cs_beginTime));
                    pgConstruction.setCs_endTime(sdf.parse(cs_endTime));
                    pgConstruction.setCs_charge(cs_charge);
                    pgConstruction.setCs_phone(cs_phone);
                    pgConstruction.setCs_type(cs_type);
                    pgConstruction.setPgPartitionId(pgPartition);
                    pgConstruction.setPgConstructionTypeId(pgConstructionType);
                    pgConstruction.setActive(1);

                    pgConstructionManager.save(pgConstruction);
                    result = "true";
                    msg = "编辑成功";
                }

        }catch (Exception e){
            result = "false";
            msg = "编辑失败";
            e.printStackTrace();

        }

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

    /*
     删除信息
    */
    @POST
    @Path("delConstruction")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> delConstruction(@FormParam("id")String id)
    throws Exception{
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        try {
            PgConstruction pgConstruction = pgConstructionManager.get(Long.valueOf(id));
            if (pgConstruction == null)
            {
               result = "false";
               msg = "不存在该施工信息";
            }else
            {
                pgConstruction.setActive(0);
                pgConstructionManager.save(pgConstruction);
                result = "true";
                msg = "成功删除信息";
            }
        }catch (Exception e){
            e.printStackTrace();
            result = "false";
            msg = "删除失败";
        }
        resultMap.put("result", result);
        resultMap.put("msg", msg);
        return resultMap;
    }

    /*
     查询
     */
    @POST
    @Path("searchConstruction")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> searchConstruction(@FormParam("cs_name")String cs_name,
                                                  @FormParam("cs_type")String cs_type,
                                                  @FormParam("parName")String parName,
                                                  @FormParam("startTime")String startTime,
                                                  @FormParam("endTime")String endTime)throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();
        String msg="";
        List<PgConstructionDto> pgConstructionDtoList=new ArrayList<PgConstructionDto>();
        try{
            pgConstructionDtoList=pgConstructionManager.getPgContructionDtoByNameOrType(cs_name,cs_type,parName,startTime,endTime);
            msg="信息获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="查询失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgConstructionDtoList);

        return resultMap;
    }

    /**
     * 根据时间统计施工次数
     */
    @POST
    @Path("getConstructionByTimeAndStatus")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getConstructionByTimeAndStatus(@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<PgConstructionStiatistic> pgConstructionStiatisticList=new ArrayList<PgConstructionStiatistic>();
        try {
            pgConstructionStiatisticList = pgConstructionManager.getConstructionByTimeAndStatus(startTime, endTime, statisticType);
            if(pgConstructionStiatisticList==null)
                msg="施工统计信息获取失败";
            else
                msg="施工统计信息获取成功";
        } catch (Exception e) {
            e.printStackTrace();
            msg = "施工统计信息获取失败";
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgConstructionStiatisticList);
        return resultMap;
    }

    /**
     * 工程类别统计
     */
    @POST
    @Path("getConstructionByType")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getConstructionByType(@FormParam("statisticType") String statisticType) throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();
        String msg="";
        List<PgConstructionStiatistic> pgConstructionStiatisticList=new ArrayList<PgConstructionStiatistic>();

        try{
            pgConstructionStiatisticList=statisticType.equals("0")?pgConstructionManager.getStatisticByType(statisticType):
                    pgConstructionManager.getStatisticByPar(statisticType);
            msg="统计信息获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="统计失败";
        }

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

    /**
     * 当天施工
     */
    @POST
    @Path("getConstructionToday")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getConstructionToday() throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();
        String msg="";
        List<PgConstructionDto> pgConstructionDtoList=new ArrayList<PgConstructionDto>();
        try {
            pgConstructionDtoList = pgConstructionManager.getConstructionToday();
            msg = "当天施工信息获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="获取失败";
        }

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

/**
 * 获取关联设备《摄像机》
 */
    @POST
    @Path("getConstructionDev")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getConstructionDev(@FormParam("id") String id) throws Exception{
        Map<String,Object> resultMap=new HashMap<String, Object>();
        String msg="";

        List<PgDeviceDto> pgDeviceDtoList =new ArrayList<PgDeviceDto>();
        try{

            PgConstruction pgConstruction=pgConstructionManager.get(Long.valueOf(id));

            pgDeviceDtoList=pgDeviceManager.getPgDeviceDtoListByPartition(pgConstruction.getPgPartitionId().getParName());

            if(pgDeviceDtoList==null)
                msg="信息获取失败";
            else
                msg="信息获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="设备信息获取成功";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgDeviceDtoList);

        return resultMap;
    }

    /*
  * 获取维护信息
   */
    @POST
    @Path("getConstruction")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getConstruction() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        List<PgConstructionDto> pgConstructionDtoList=new ArrayList<PgConstructionDto>();

        try{
            pgConstructionDtoList=pgConstructionManager.getPgConstructionList();
            if(pgConstructionDtoList==null||pgConstructionDtoList.size()==0)
                msg="不存在施工记录";
            else
                msg="施工信息获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="施工信息获取失败";
            result="false";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgConstructionDtoList);

        return resultMap;
    }


}
