package com.jeeplus.business.project.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.demo.annotation.DemoMode;
//import com.jeeplus.assess.utils.FreemarkerUtil;
import com.jeeplus.business.project.domain.JyProject;
import com.jeeplus.business.project.domain.JyProjectAudit;
import com.jeeplus.business.project.domain.JyProjectReportsSubmit;
import com.jeeplus.business.project.service.JyProjectAuditService;
import com.jeeplus.business.project.service.JyProjectService;
import com.jeeplus.business.project.service.dto.*;
import com.jeeplus.business.project.utils.FreemarkerUtil;
import com.jeeplus.business.project.utils.JyResponseUtil;
import com.jeeplus.business.useSeal.service.dto.SignetDTO;
import com.jeeplus.business.workContractInfo.domain.JyWorkContractInfo;
import com.jeeplus.business.workContractInfo.service.dto.JyWorkContractInfoDto;

import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(tags ="嘉溢项目登记")
@RestController
@RequestMapping(value = "/jyProject")
public class JyProjectController {

    @Resource
    private JyProjectService jyProjectService;


    /**
     * 项目登记列表
     * @param info
     * @param page
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "项目登记列表")
    @GetMapping(value = "/list")
    public ResponseEntity<IPage<JyProject>> list(JyProject info, Page<JyProject> page) throws Exception {
        IPage<JyProject> list = jyProjectService.list(page, info);
        return ResponseEntity.ok (list);
    }

    /**
     * 查询当前登录人所属项目组的项目
     * @return
     */
    @ApiOperation(value = "查询当前登录人所属项目组的项目")
    @GetMapping(value = "/getHaveProjectIds")
    public ResponseEntity<List<String>> getHaveProjectIds() {
        return jyProjectService.getHaveProjectIds();
    }

    /**
     * 合同信息列表查询
     * @param dto
     * @param page
     * @return
     */
    @ApiOperation(value = "合同信息列表查询")
    @GetMapping(value = "/findContractPageList")
    public ResponseEntity<IPage<JyWorkContractInfo>> findContractPageList(JyWorkContractInfoDto dto, Page<JyWorkContractInfo> page) {
        IPage<JyWorkContractInfo> pageList = jyProjectService.findContractPageList(dto, page);
        return ResponseEntity.ok(pageList);
    }

    /**
     * 流程保存
     */
    @ApiOperation(value = "流程保存")
    @PostMapping(value = "/saveForm")
    public ResponseEntity saveForm(@RequestBody JyProjectDTO jyProjectDTO) throws Exception{
        String id = jyProjectService.save(jyProjectDTO);
        return ResponseUtil.newInstance().add("businessTable", "jy_project_record").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/findById")
    public ResponseEntity<JyProjectDTO> save(@RequestParam String id){
        JyProjectDTO dto = jyProjectService.findById(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 根据id修改状态status
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "updateStatusById")
    public void updateStatusById(@RequestBody JyProjectDTO jyProjectDTO) {
        jyProjectService.updateStatusById(jyProjectDTO);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id删除")
    @GetMapping(value = "/deleteById")
    public ResponseEntity<String> deleteById(@RequestParam String id){
        String s = jyProjectService.deleteById(id);
        return ResponseEntity.ok(s);
    }


    /**
     * 批量删除字典
     */
    @DemoMode
    @ApiLog("批量删除项目信息")
    @PreAuthorize("hasAuthority('jy_project:info:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        String idArray[] = ids.split ( "," );
        for(String id: idArray){
            jyProjectService.deleteById ( id );
        }
        return ResponseEntity.ok ( "删除信息成功！" );
    }

    /**
     * 根据当前登录人获取登记的项目
     */
    @ApiOperation(value = "根据当前登录人获取登记的项目")
    @GetMapping(value = "/getByCreate")
    public ResponseEntity<IPage<JyProject>> getByCreate(JyProject info, Page<JyProject> page) throws Exception {
        IPage<JyProject> list = jyProjectService.getByCreate(page, info);
        return ResponseEntity.ok (list);
    }

    /**
     * 根据当前登录人获取外审通过的项目
     */
    @ApiOperation(value = "根据当前登录人获取外审通过的项目")
    @GetMapping(value = "/getByCreateOut")
    public ResponseEntity<IPage<JyProject>> getByCreateOut(JyProject info, Page<JyProject> page) throws Exception {
        IPage<JyProject> list = jyProjectService.getByCreateOut(page, info);
        return ResponseEntity.ok (list);
    }


    /**
     * 根据id获取三级校审信息
     */
    @ApiOperation(value = "根据id获取三级校审信息")
    @GetMapping(value = "/findByIdAudit")
    public ResponseEntity<JyProjectAuditDTO> findByIdAudit(@RequestParam String id){
        JyProjectAuditDTO dto = jyProjectService.findByIdAudit(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 新增/修改三级校审
     */
    @ApiOperation(value = "新增/修改三级校审")
    @PostMapping(value = "/saveFormThree")
    public ResponseEntity saveFormThree(@RequestBody JyProjectAuditDTO jyProjectAuditDTO) throws Exception{
        String id = jyProjectService.saveFormThree(jyProjectAuditDTO);
        return ResponseUtil.newInstance().add("businessTable", "jy_project_audit").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id修改三级校审状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改三级校审状态值status")
    @PostMapping(value = "/updateStatusByAuditId")
    public ResponseEntity<String> updateStatusByAuditId(@RequestBody JyProjectAuditDTO dto) {
        String s = jyProjectService.updateStatusByAuditId(dto);
        return ResponseEntity.ok(s);
    }

    /**
     * 查询项目一级校审中未发起逾期的项目
     */
    @ApiOperation(value = "查询项目一级校审中未发起逾期的项目")
    @GetMapping(value = "/selectAudit")
    public String selectAudit() {
        List<JyProjectAudit> list = jyProjectService.selectAudit();
        return JSON.toJSONString(list);
    }

    /**
     * 查询项目一级校审中未发起未逾期的项目
     */
    @ApiOperation(value = "查询项目一级校审中未发起未逾期的项目")
    @GetMapping(value = "/selectAuditInTime")
    public String selectAuditInTime() {
        List<JyProjectAudit> list = jyProjectService.selectAuditInTime();
        return JSON.toJSONString(list);
    }

    /**
     * 修改一级校审逾期状态
     */
    @ApiOperation(value = "修改一级校审逾期状态")
    @GetMapping(value = "/updateStatus")
    public void updateStatus(@RequestParam String id,@RequestParam String status,@RequestParam String exceedTimeStatus) {
        jyProjectService.updateStatus(id,status,exceedTimeStatus);

    }

    /**
     * 下载一级校审表
     * @param response
     * @param info
     */
    @RequestMapping(value="downloadFirstAuditTpl")
    @ResponseBody
    public void downloadFirstAuditTpl(HttpServletResponse response, JyProject info)  {
        Map data = jyProjectService.getAuditInfoMap(info);


        //模板对象
        Template template=null;
        //下载文件地址
        String filePath = null;
        if(System.getProperty("os.name").toLowerCase().contains("win")){
            filePath = this.getClass().getResource("/").getPath()+"/freemarker";
        }else{
            filePath = "/mnt/project/cloud/freemarker";
        }
        //freemaker模板路径
        File path = new File(filePath);

        Configuration cfg = new Configuration();
        try {
            cfg.setDirectoryForTemplateLoading(path);
            //选择对应的ftl文件
            template = cfg.getTemplate("firstAudit.ftl","UTF-8");

            File docFile = new File("嘉溢一级校审.doc");
            FreemarkerUtil.generateFile(data,template,docFile);
            JyResponseUtil.docResponse("嘉溢一级校审.doc",docFile,response);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {

            if(System.getProperty("os.name").toLowerCase().contains("win")){
                //获取tomcat的路径
                String tomcatFilePath=System.getProperty("catalina.home");
                //删除tomcat目录下的处理后的文件信息
                File tomcatFile = new File(tomcatFilePath+"/bin/"+"嘉溢一级校审.doc");
                if (tomcatFile.isFile()) {
                    tomcatFile.delete();
                }
            }else{
                //删除目录下的处理后的文件信息
                File tomcatFile = new File("/mnt/project/cloud/"+"嘉溢一级校审.doc");
                if (tomcatFile.isFile()) {
                    tomcatFile.delete();
                }
            }
        }

    }

    /**
     * 下载二级校审表
     * @param response
     * @param info
     */
    @RequestMapping(value="downloadSecondAuditTpl")
    @ResponseBody
    public void downloadSecondAuditTpl(HttpServletResponse response, JyProject info)  {
        Map data = jyProjectService.getAuditInfoMap(info);


        //模板对象
        Template template=null;
        //下载文件地址
        String filePath = null;
        if(System.getProperty("os.name").toLowerCase().contains("win")){
            filePath = this.getClass().getResource("/").getPath()+"/freemarker";
        }else{
            filePath = "/mnt/project/cloud/freemarker";
        }
        //freemaker模板路径
        File path = new File(filePath);
        Configuration cfg = new Configuration();
        try {
            cfg.setDirectoryForTemplateLoading(path);
            //选择对应的ftl文件
            template = cfg.getTemplate("secondAudit.ftl","UTF-8");

            File docFile = new File("嘉溢二级校审.doc");
            FreemarkerUtil.generateFile(data,template,docFile);
            JyResponseUtil.docResponse("嘉溢二级校审.doc",docFile,response);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(System.getProperty("os.name").toLowerCase().contains("win")){
                //获取tomcat的路径
                String tomcatFilePath=System.getProperty("catalina.home");
                //删除tomcat目录下的处理后的文件信息
                File tomcatFile = new File(tomcatFilePath+"/bin/"+"嘉溢二级校审.doc");
                if (tomcatFile.isFile()) {
                    tomcatFile.delete();
                }
            }else{
                //删除目录下的处理后的文件信息
                File tomcatFile = new File("/mnt/project/cloud/"+"嘉溢二级校审.doc");
                if (tomcatFile.isFile()) {
                    tomcatFile.delete();
                }
            }
        }

    }

    /**
     * 下载三级校审表
     * @param response
     * @param info
     */
    @RequestMapping(value="downloadThirdlyAuditTpl")
    @ResponseBody
    public void downloadThirdlyAuditTpl(HttpServletResponse response, JyProject info)  {
        Map data = jyProjectService.getAuditInfoMap(info);


        //模板对象
        Template template=null;
        //下载文件地址
        String filePath = null;
        if(System.getProperty("os.name").toLowerCase().contains("win")){
            filePath = this.getClass().getResource("/").getPath()+"/freemarker";
        }else{
            filePath = "/mnt/project/cloud/freemarker";
        }
        //freemaker模板路径
        File path = new File(filePath);
        Configuration cfg = new Configuration();
        try {
            cfg.setDirectoryForTemplateLoading(path);
            //选择对应的ftl文件
            template = cfg.getTemplate("thirdAudit.ftl","UTF-8");

            File docFile = new File("嘉溢三级校审.doc");
            FreemarkerUtil.generateFile(data,template,docFile);
            JyResponseUtil.docResponse("嘉溢三级校审.doc",docFile,response);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(System.getProperty("os.name").toLowerCase().contains("win")){
                //获取tomcat的路径
                String tomcatFilePath=System.getProperty("catalina.home");
                //删除tomcat目录下的处理后的文件信息
                File tomcatFile = new File(tomcatFilePath+"/bin/"+"嘉溢三级校审.doc");
                if (tomcatFile.isFile()) {
                    tomcatFile.delete();
                }
            }else{
                //删除目录下的处理后的文件信息
                File tomcatFile = new File("/mnt/project/cloud/"+"嘉溢三级校审.doc");
                if (tomcatFile.isFile()) {
                    tomcatFile.delete();
                }
            }
        }

    }

    /**
     * 根据id查询外审信息
     */
    @ApiOperation(value = "根据id查询外审信息")
    @GetMapping(value = "/findByIdOut")
    public ResponseEntity<JyProjectOutinstanceDTO> findByIdOut(@RequestParam String id){
        JyProjectOutinstanceDTO dto = jyProjectService.findByIdOut(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 新增/修改外审
     */
    @ApiOperation(value = "新增/修改外审")
    @PostMapping(value = "/saveFormOut")
    public ResponseEntity saveFormOut(@RequestBody JyProjectOutinstanceDTO jyProjectOutinstanceDTO) throws Exception{
        String id = jyProjectService.saveFormOut(jyProjectOutinstanceDTO);
        return ResponseUtil.newInstance().add("businessTable", "jy_project_outinstance").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id修改外审状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改外审状态值status")
    @PostMapping(value = "/updateStatusByOutId")
    public ResponseEntity<String> updateStatusByOutId(@RequestBody JyProjectOutinstanceDTO dto) {
        String s = jyProjectService.updateStatusByOutId(dto);
        return ResponseEntity.ok(s);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = "/queryReportsById")
    public ResponseEntity<JyProjectReportsSubmitDTO> queryReportsById(@RequestParam String id){
        JyProjectReportsSubmitDTO dto = jyProjectService.queryReportsById(id);
        return ResponseEntity.ok(dto);
    }


    /**
     * 保存或修改报批签发信息
     */
    @ApiOperation(value = "保存或修改报批签发信息")
    @PostMapping("/saveReportsSubmit")
    public ResponseEntity saveReportsSubmit(@RequestBody JyProjectReportsSubmitDTO dto) throws Exception{
        String id = jyProjectService.saveReportsSubmit(dto);
        return ResponseUtil.newInstance().add("businessTable", "jy_project_reports_submit").add("businessId", id).ok ("操作成功");
    }


    /**
     * 根据id修改状态status
     * @param id
     * @param status
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "/updateReportsSubmitById")
    public void updateReportsSubmitById(String id,String status) {
        jyProjectService.updateReportsSubmitById(id,status);
    }


    /**
     * 查找报批签发逾期的数据
     */
    @ApiOperation(value = "查找报批签发逾期的数据")
    @GetMapping(value = "/findReportsSubmitListExccedTime")
    public String findReportsSubmitListExccedTime(){
        List<JyProjectReportsSubmit> jyProjectReportsSubmits=jyProjectService.findReportsSubmitListExccedTime();
        return JSON.toJSONString(jyProjectReportsSubmits);
    }

    /**
     * 查找报批签发未逾期的数据
     */
    @ApiOperation(value = "查找报批签发未逾期的数据")
    @GetMapping(value = "/findReportsSubmitListInTime")
    public String findReportsSubmitListInTime(){
        List<JyProjectReportsSubmit> jyProjectReportsSubmits=jyProjectService.findReportsSubmitListInTime();
        return JSON.toJSONString(jyProjectReportsSubmits);
    }


    /**
     * 修改报批签发逾期的状态
     */
    @ApiOperation(value = "修改报批签发逾期的状态")
    @GetMapping(value = "/updateExceedTimeStatus")
    public void updateExceedTimeStatus(@RequestParam(value = "id")String id,@RequestParam(value = "status")String status,@RequestParam(value = "exceedTimeStatus")String exceedTimeStatus){
        jyProjectService.updateExceedTimeStatus(id,status,exceedTimeStatus);
    }

    /**
     * 根据id查询归档信息
     */
    @ApiOperation(value = "根据id查询归档信息")
    @GetMapping(value = "/findByIdArchive")
    public ResponseEntity<JyProjectArchiveDTO> findByIdArchive(@RequestParam String id){
        JyProjectArchiveDTO dto = jyProjectService.findByIdArchive(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 新增/修改归档
     */
    @ApiOperation(value = "新增/修改归档")
    @PostMapping(value = "/saveFormArchive")
    public ResponseEntity saveFormArchive(@RequestBody JyProjectArchiveDTO jyProjectArchiveDTO) throws Exception{
        String id = jyProjectService.saveFormArchive(jyProjectArchiveDTO);
        return ResponseUtil.newInstance().add("businessTable", "jy_project_archive").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id修改归档状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改归档状态值status")
    @PostMapping(value = "/updateStatusByArchiveId")
    public ResponseEntity<String> updateStatusByArchiveId(@RequestBody JyProjectArchiveDTO dto) {
        String s = jyProjectService.updateStatusByArchiveId(dto);
        return ResponseEntity.ok(s);
    }

    /**
     * 根据id查询环评资质信息
     */
    @ApiOperation(value = "根据id查询环评资质信息")
    @GetMapping(value = "/findByIdEia")
    public ResponseEntity<JyProjectEiaQualificationDTO> findByIdEia(@RequestParam String id){
        JyProjectEiaQualificationDTO dto = jyProjectService.findByIdEia(id);
        return ResponseEntity.ok(dto);
    }

    /**
     * 新增/修改环评资质
     */
    @ApiOperation(value = "新增/修改环评资质")
    @PostMapping(value = "/saveFormEia")
    public ResponseEntity saveFormEia(@RequestBody JyProjectEiaQualificationDTO jyProjectEiaQualificationDTO) throws Exception{
        String id = jyProjectService.saveFormEia(jyProjectEiaQualificationDTO);
        return ResponseUtil.newInstance().add("businessTable", "jy_project_eia_qualification").add("businessId", id).ok ("操作成功");
    }

    /**
     * 根据id修改环评状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改环评状态值status")
    @PostMapping(value = "/updateStatusByEiaId")
    public ResponseEntity<String> updateStatusByEiaId(@RequestBody JyProjectEiaQualificationDTO dto) {
        String s = jyProjectService.updateStatusByEiaId(dto);
        return ResponseEntity.ok(s);
    }



















}
