package com.casic.PgInterface.rs;

import com.casic.PgInterface.construction.domain.PgConstruction;
import com.casic.PgInterface.construction.domain.PgCsDocument;
import com.casic.PgInterface.construction.dto.PgCsDocumentDto;
import com.casic.PgInterface.construction.manager.PgConstructionManager;
import com.casic.PgInterface.construction.manager.PgCsDocumentManager;
import com.casic.PgInterface.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
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/28.
 */
@Component
@Path("pgCsDocument")
public class PgCsDocumentRs {

    private static Logger logger = LoggerFactory.getLogger(pgStatisticRs.class);
    @Resource
    private PgCsDocumentManager pgCsDocumentManager;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    @Resource
    private PgConstructionManager pgConstructionManager;

    /*
        新增&上传
     */
    @POST
    @Path("addConstructDoc")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String,Object> addConstructDoc(
            @FormParam("list_name") String list_name,
            @FormParam("doc_name") String doc_name,
            @FormParam("construct_doc") String construct_doc,
            @FormParam("cs_name") String cs_name)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        PgCsDocument pgCsDocument = new PgCsDocument();

        String result = "";
        String msg = "";

        try {
            if (StringUtils.isBlank(construct_doc)||StringUtils.isBlank(doc_name))
                pgCsDocument.setConstruct_doc("");
            else {
                pgCsDocument.setConstruct_doc(pgCsDocumentManager.saveFiles(construct_doc, doc_name, list_name));
            }

            PgConstruction pgConstruction=pgConstructionManager.getPgContructionByName(cs_name);
            pgCsDocument.setList_name(list_name);
            pgCsDocument.setDoc_name(doc_name);
            pgCsDocument.setPgConstructionId(pgConstruction);
            pgCsDocument.setActive(1);
            pgCsDocumentManager.save(pgCsDocument);

            result = "true";
            msg = "保存成功";

        } catch (Exception ex) {
            result = "false";
            msg = "保存失败";
            ex.printStackTrace();
        }

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

    //编辑施工文档
    @POST
    @Path("editConstructDoc")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> editReservePlan(@FormParam("id") String id,
                                               @FormParam("list_name") String List_name,
                                               @FormParam("doc_name") String doc_name,
                                               @FormParam("cs_name") String cs_name) throws Exception {

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

        try {
            PgCsDocument pgCsDocument = pgCsDocumentManager.get(Long.valueOf(id));
            if (pgCsDocument == null) {
                result = "false";
                msg = "不存在该预案信息";
            } else {
                PgConstruction pgConstruction=pgConstructionManager.getPgContructionByName(cs_name);
                pgCsDocument.setDoc_name(doc_name);
                pgCsDocument.setPgConstructionId(pgConstruction);
                pgCsDocument.setActive(1);

                pgCsDocumentManager.save(pgCsDocument);

                result = "true";
                msg = "编辑成功";
            }
        } catch (Exception e) {

            result = "false";
            msg = "编辑失败";
            e.printStackTrace();
        }
        resultMap.put("msg",msg);
        resultMap.put("result",result);
        return resultMap;
    }

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

        try{

            PgCsDocument pgCsDocument = pgCsDocumentManager.get(Long.valueOf(id));
            if(pgCsDocument==null)
            {
                result="false";
                msg="不存在该预案信息";
            }
            else{
                pgCsDocument.setActive(0);
                pgCsDocumentManager.save(pgCsDocument);
                result="true";
                msg="删除成功";
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            msg="预案删除失败";
            result="false";
        }

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

    }

    /*
        按工程名称、工程类别、施工时间查询
     */
    @POST
    @Path("queryConstructDoc")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> queryConstructDoc(@FormParam("cs_name")String cs_name,
                                                 @FormParam("cs_type")String cs_type,
                                                 @FormParam("startTime")String startTime,
                                                 @FormParam("endTime")String endTime)
    throws  Exception{

        Map<String, Object> resultMap = new HashMap<String, Object>();
        List<PgCsDocumentDto> pgCsDocumentDtoList = new ArrayList<PgCsDocumentDto>();

        String msg="";
        String result="";

        try {
            pgCsDocumentDtoList = pgCsDocumentManager.getAllDocData(cs_name, cs_type, startTime,endTime);
            if (pgCsDocumentDtoList != null)
            {
                result = "true";
                msg = "获取数据成功";
            }else
            {
                result = "false";
                msg = "没有满足条件的数据";
            }
        }catch (Exception e){
            e.printStackTrace();
            result = "false";
            msg = "获取失败";
        }

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

    @POST
    @Path("fileDownload")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, Object> downloadFile(@FormParam("id") String id)throws Exception {
        Map<String, Object> resultMap = new HashMap<String, Object>();

        String msg = "";
        ResponseEntity<byte[]> path = null;
        try {
            PgCsDocument pgCsDocument = pgCsDocumentManager.get(Long.valueOf(id));
            path = pgCsDocumentManager.downloadFile(pgCsDocument);
            if (path == null)
                msg = "不存在该文档";
            else
                msg = "下载成功";

        } catch (Exception e) {
            e.printStackTrace();
            msg = "下载失败";
        }

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

}
