package com.casic.PgInterface.rs;

import com.casic.PgInterface.devTable.domain.PgEquipment;
import com.casic.PgInterface.devTable.dto.PgDeviceTypeStatisticDto;
import com.casic.PgInterface.devTable.dto.PgEquipmentDto;
import com.casic.PgInterface.devTable.manager.PgEquipmentManager;
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 yxw on 2018/9/3.
 */
@Component
@Path("pgEquipment")
public class PgEquipmentRs {
    @Resource
    PgEquipmentManager pgEquipmentManager;

    /**
     * 新增工器具
     */
    @POST
    @Path("addEquipment")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addEquipment(@FormParam("eqName") String eqName,
                                            @FormParam("eqCode") String eqCode,
                                            @FormParam("eqType") String eqType,
                                            @FormParam("ownerUnit") String ownerUnit,
                                            @FormParam("enterDate") String enterDate,
                                            @FormParam("curUser") String curUser)
            throws Exception {

        Map<String, Object> resultMap = new HashMap<String, Object>();
        String result = "";
        String msg = "";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        try {
            if (pgEquipmentManager.getEquipmentByCode(eqCode)!=null) {
                result = "false";
                msg = "已存在编号";
            } else {
                PgEquipment pgEquipment=new PgEquipment();

                pgEquipment.setEqName(eqName);
                pgEquipment.setEqType(eqType);
                pgEquipment.setEqCode(eqCode);
                pgEquipment.setCurUser(curUser);
                pgEquipment.setOwnerUnit(ownerUnit);
                pgEquipment.setEnterDate(sdf.parse(enterDate));
                pgEquipment.setActive(1);

                pgEquipmentManager.save(pgEquipment);
                result = "true";
                msg = "保存成功";
            }
        } catch (Exception e) {
            result = "false";
            msg = "保存失败";
            e.printStackTrace();
        }
        resultMap.put("result", result);
        resultMap.put("msg", msg);

        return resultMap;
    }

    /**
     * 维护工器具
     */
    @POST
    @Path("editEquipment")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> addEquipment(@FormParam("id") String id,
                                            @FormParam("eqName") String eqName,
                                            @FormParam("eqCode") String eqCode,
                                            @FormParam("eqType") String eqType,
                                            @FormParam("ownerUnit") String ownerUnit,
                                            @FormParam("enterDate") String enterDate,
                                            @FormParam("curUser") String curUser) {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        String result = "";
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");

        try {
            PgEquipment pgEquipment=pgEquipmentManager.get(Long.valueOf(id));
            if(pgEquipment==null)
            {
                msg="不存在工器具";
                result="false";
            }
            else
            {
                pgEquipment.setEqName(eqName);
                pgEquipment.setEqType(eqType);
                pgEquipment.setEqCode(eqCode);
                pgEquipment.setCurUser(curUser);
                pgEquipment.setOwnerUnit(ownerUnit);
                pgEquipment.setEnterDate(sdf.parse(enterDate));

                pgEquipmentManager.save(pgEquipment);
                msg="编辑成功";
                result="true";
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = "false";
            msg = "编辑失败";
        }
        resultMap.put("result", result);
        resultMap.put("msg", msg);

        return resultMap;
    }

    /*
     * 获取工器具信息
      */
    @POST
    @Path("getEquipment")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getEquipment() throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgEquipmentDto> pgEquipmentDtoList=new ArrayList<PgEquipmentDto>();

        try{
            pgEquipmentDtoList=pgEquipmentManager.getEquipmentDtiList();
            if(pgEquipmentDtoList==null||pgEquipmentDtoList.size()==0)
                msg="不存在工器具记录";
            else
                msg="维获取成功";
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="维护记录获取失败";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",pgEquipmentDtoList);

        return resultMap;
    }

    /**
     * 删除
     */
    @POST
    @Path("deleteEquipment")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> deleteEquipment(@FormParam("id") String id)throws Exception {
        HashMap<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        String result = "";

        try {
            PgEquipment pgEquipment=pgEquipmentManager.get(Long.valueOf(id));
            if(pgEquipment==null)
            {
                msg="不存在工器具";
                result="false";
            }
            else
            {
                pgEquipment.setActive(0);
                msg="删除成功";
                result="true";
            }
        }catch(Exception e){
            e.printStackTrace();
            msg="设备信息获取失败";
            result="false";
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);
        return resultMap;
    }

    /*
     * 按照设备类型统计设备数量
     */
    @POST
    @Path("getEquipmentStatistic")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> getEquipmentStatistic(@FormParam("eqType") String eqType)
            throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        String msg = "";
        List<PgDeviceTypeStatisticDto> pgDeviceTypeStatisticDtoList=new ArrayList<PgDeviceTypeStatisticDto>();

        try {
            pgDeviceTypeStatisticDtoList=pgEquipmentManager.getDeviceStatisticByDevType(eqType);

            msg = "获取成功";
        } catch (Exception e) {
            msg = "获取失败";
            e.printStackTrace();
        }
        resultMap.put("msg", msg);
        resultMap.put("result", pgDeviceTypeStatisticDtoList);
        return resultMap;
    }

}