package com.zjrs.ggfw.rlzysc.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.persistens.PageResult;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.ggfw.rlzysc.bpo.SyBPO;
import com.zjrs.ggfw.rlzysc.dto.JyzxQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.SyZwxxQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.ZphglQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.ZwxxListDTO;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.SyMapQyQueryDTO;
import com.zjrs.ggfw.rlzysc.dto.grzxjlxx.ZjDTO;
import com.zjrs.ggfw.rlzysc.po.RsRlzyLbggPO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StreamUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Api("首页")
@RestController
@RequestMapping("/api/rlzysc/homepage")
public class SyController {
    private static final Logger LOG = LoggerFactory.getLogger(SyController.class);
    @Resource
    private SyBPO syBPO;

    @ApiOperation(value = "获取轮播广告列表", notes = "获取轮播广告列表")
        @GetMapping("/gglbxx/list")
    public AjaxResponse getLbggList() {
        LOG.info("获取轮播广告列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
           List<RsRlzyLbggPO> result = this.syBPO.getLbggList();

            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取轮播广告列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取职业信息列表", notes = "获取职业信息列表")
    @GetMapping("/zyxx/list")
    public AjaxResponse getZyxxList() {
        LOG.info("获取职业信息列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List result = this.syBPO.getZyxxList();
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取职业信息列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取职业信息列表", notes = "获取职业信息列表")
    @GetMapping("/zyxx/{id}")
    public AjaxResponse getZyxxList(@PathVariable(name = "id", value = "id") String id) {
        LOG.info("获取职业信息列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List result = this.syBPO.getZyxxList(id);
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取职业信息列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取职位推荐", notes = "获取职位推荐")
    @PostMapping("/zwtj/list")
    public AjaxResponse getZwtjList(@RequestBody SyZwxxQueryDTO dto) {
        LOG.info("获取职位推荐开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<ZwxxListDTO> result = this.syBPO.getZwtjList(dto);
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取职位推荐结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取企业推荐", notes = "获取企业推荐")
    @GetMapping("/qytj/list")
    public AjaxResponse getQytjList() {
        LOG.info("获取企业推荐开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List result = this.syBPO.getQytjList();
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取企业推荐结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取服务公告", notes = "获取服务公告")
    @PostMapping("/fwgg/list")
    public AjaxResponse getFwggList(@RequestBody JyzxQueryDTO dto) {
        LOG.info("获取服务公告开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        Integer pagenum = dto.getPagenum();
        Integer pagesize = dto.getPagesize();
        try {
            PageResult pageResult = this.syBPO.getFwggList(dto,pagenum,pagesize);
            ajaxResponse.setData("data", pageResult);
            ajaxResponse.setData("pagesize", pagesize);
            ajaxResponse.setData("pagenum", pagenum);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取服务公告结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "获取校园招聘会列表", notes = "获取校园招聘会列表")
    @PostMapping("/xyzpxx/list")
    public AjaxResponse getXyzpxxList(@RequestBody ZphglQueryDTO dto) {
        LOG.info("获取校园招聘会列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Integer pagenum = dto.getPagenum();
            Integer pagesize = dto.getPagesize();
            PageResult result = this.syBPO.getXyzpxxList(pagenum,pagesize);
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("获取校园招聘会列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "首页获取招聘会信息（最新4场）", notes = "首页获取招聘会信息（最新4场）")
    @PostMapping("/zphxx/list")
    public AjaxResponse getZphxxList(@RequestBody @Valid ZphglQueryDTO dto) {
        LOG.info("首页获取招聘会信息（最新4场）开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            PageResult result = this.syBPO.getZxscZphxx(dto);
            ajaxResponse.setData("data", result);
            ajaxResponse.setData("pagesize", dto.getPagesize());
            ajaxResponse.setData("pagenum",dto.getPagenum());
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("首页获取招聘会信息（最新4场）结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "首页获取招聘会日历列表", notes = "首页获取招聘会日历列表")
    @GetMapping("/zphxx/calendar/list")
    public AjaxResponse getZphCalendarList() {
        LOG.info("首页获取招聘会日历列表开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<Map<String,Object>> result = this.syBPO.getZphCalendarList();
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("首页获取招聘会日历列表结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "在线人力资源主页整合", notes = "在线人力资源主页整合")
    @PostMapping("/data/query")
    public AjaxResponse getZphxxList(@RequestBody SyZwxxQueryDTO dto) {
        LOG.info("在线人力资源主页整合开始");
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List qytj = this.syBPO.getQytjList();//企业推荐
            List<ZwxxListDTO> zwtj = this.syBPO.getZwtjList(dto);//职位推荐
            ZphglQueryDTO dto1 = new ZphglQueryDTO();
            PageResult zphlist = this.syBPO.getZxscZphxx(dto1);
            ajaxResponse.setData("qytj", qytj);
            ajaxResponse.setData("zwtj", zwtj);
            ajaxResponse.setData("zphlist", zphlist);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        LOG.info("在线人力资源主页整合结束");
        return ajaxResponse;
    }


    @ApiOperation(value = "就业e图企业职位信息", notes = "就业e图企业职位信息")
    @PostMapping("/map/getMapZw")
    public AjaxResponse getMapZw(@RequestBody SyMapQyQueryDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            ajaxResponse.setData("data", this.syBPO.getMapZw(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "就业e图企业职位信息", notes = "就业e图企业职位信息")
    @PostMapping("/map/getMapQy")
    public AjaxResponse getMapQy(@RequestBody @Valid ZjDTO dto, BindingResult bindingResult) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            String errmsg = "";
            for (int i=0;i<fieldErrors.size();i++){
                if (i == 0){
                    errmsg = errmsg + fieldErrors.get(i).getDefaultMessage();
                } else {
                    errmsg = errmsg + "；" + fieldErrors.get(i).getDefaultMessage();
                }
            }
            ajaxResponse.setErrorMsg(errmsg);
            return ajaxResponse;
        }
        try {
            ajaxResponse.setData("data", this.syBPO.getMapQy(dto));
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            LOG.error(e.getMessage());
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "下载操作手册", notes = "下载操作手册")
    @GetMapping(value = "/yhczsc")
    public void downloadTemplate(HttpServletRequest request, HttpServletResponse res) throws IOException {
        LOG.info("下载导入模板开始");
        InputStream is = SyController.class.getClassLoader()
                .getResourceAsStream("templates" + File.separator + "rlzysc" + File.separator + "zxrlzywsfbb.pdf");
        res.setContentType("application/octet-stream; charset=UTF-8");
        res.setHeader("Content-disposition",
                "attachment;filename=" + URLEncoder.encode("在线人力资源市场网上发布版.pdf", "UTF-8"));
        try {
            StreamUtils.copy(is, res.getOutputStream());
        } catch (IOException e) {
            LOG.error(e.getMessage());
        } finally {
            if (is != null) {
                is.close();
            }
            if (res.getOutputStream() != null) {
                res.getOutputStream().close();
            }
        }
        LOG.info("下载导入模板结束");
    }
}

