package com.zd.laboratory.controller;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zd.common.core.domain.per.PerFun;
import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.web.domain.ResultData;
import com.zd.laboratory.domain.vo.LabNotifyplanExcelVO;
import com.zd.laboratory.domain.vo.LabNotifyplanVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zd.common.log.annotation.Log;
import com.zd.common.log.enums.BusinessType;
import com.zd.common.security.annotation.PreAuthorize;
import com.zd.laboratory.domain.LabNotifyplan;
import com.zd.laboratory.service.ILabNotifyplanService;
import com.zd.common.core.web.controller.BaseController;
import com.zd.common.core.web.domain.AjaxResult;
import com.zd.common.core.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.zd.common.swagger.config.Knife4jConfiguration;

import com.zd.common.core.web.page.TableDataInfo;

/**
 * 大屏通知计划Controller
 *
 * @author zd
 * @date 2022-02-16
 */
@RestController
@Api(tags = "【大屏通知计划】")
@RequestMapping("/notifyplan")
public class LabNotifyplanController extends BaseController
{
    @Autowired
    private ILabNotifyplanService labNotifyplanService;

    /**
     * 通知计划列表PC端
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NOTIFYPLAN + PerFun.LIST)
    @GetMapping("/list")
    @ApiOperation(value = "查询大屏通知计划列表")
    public TableDataInfo<LabNotifyplanVO> list(LabNotifyplanVO labNotifyplan, HttpServletRequest request)
    {
        startPage();
        List<LabNotifyplanVO> list = labNotifyplanService.selectLabNotifyplanListVO(labNotifyplan,request);
        //List<LabNotifyplan> list = labNotifyplanService.selectLabNotifyplanList(labNotifyplan);
        return getDataTable(list);
    }

    /***
     * 大屏校级-获取通知信息
     * @param labNotifyplan
     * @return
     */
    @GetMapping("/listTz")
    public TableDataInfo<LabNotifyplanVO> listTz(LabNotifyplanVO labNotifyplan, HttpServletRequest request)
    {
        startPage();
        labNotifyplan.setType(1);//工作通知
        labNotifyplan.setNotifyType(1);//大屏通知
        List<LabNotifyplanVO> list = labNotifyplanService.selectLabNotifyplanListVO(labNotifyplan,request);
        return getDataTable(list);
    }

    /***
     * 大屏校级-获取计划信息
     * @param labNotifyplan
     * @return
     */
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WORKNOTIFYPLAN + PerFun.LIST)
    @GetMapping("/listJh")
    public TableDataInfo<LabNotifyplanVO> listJh(LabNotifyplanVO labNotifyplan, HttpServletRequest request)
    {
        startPage();
        labNotifyplan.setType(2);//计划类型
        List<LabNotifyplanVO> list = labNotifyplanService.selectLabWorkNotifyplanListVO(labNotifyplan,request);
        return getDataTable(list);
    }

    /***
     * 大屏院级-获取校院通知信息
     * @param labNotifyplan
     * @return
     */
    @GetMapping("/listCollegeTz")
    public TableDataInfo<LabNotifyplanVO> listCollegeTz(LabNotifyplanVO labNotifyplan, HttpServletRequest request)
    {
        startPage();
//        labNotifyplan.setType(1);//1大屏通知
//        labNotifyplan.setNotifyType(2);//2院校通知
        List<LabNotifyplanVO> list = labNotifyplanService.selectLabNotifyplanListVO(labNotifyplan,request);
        return getDataTable(list);
    }

    /***
     * 大屏院级-获取计划信息
     * @param labNotifyplan
     * @return
     */
    @GetMapping("/listCollegeJh")
    public AjaxResult listCollegeJh(LabNotifyplanVO labNotifyplan, HttpServletRequest request)
    {
        startPage();
        Map<String, Object> map =new HashMap<String, Object>();
        labNotifyplan.setType(2);//计划类型
        labNotifyplan.setRemark("1");//解析xecel
        List<LabNotifyplanVO> list = labNotifyplanService.selectLabNotifyplanListVO(labNotifyplan,request);
        map.put("list",list.size()>0?list.get(0):list);
        return AjaxResult.success(map);
    }

    /***
     * 大屏 校级 计划列表
     * @param labNotifyplan
     * @return
     */
    @GetMapping("/listIndexJh")
    public AjaxResult listIndexJh(LabNotifyplanVO labNotifyplan, HttpServletRequest request)
    {
        startPage();
        Map<String, Object> map =new HashMap<String, Object>();
        labNotifyplan.setType(2);//计划类型
        labNotifyplan.setRemark("1");//解析xecel
        List<LabNotifyplanVO> list = labNotifyplanService.selectLabNotifyplanListVO(labNotifyplan,request);
        map.put("list",list.size()>0?list.get(0):list);
        return AjaxResult.success(map);
    }

    /****
     * 首页工作计划详情
     * @param labNotifyplan
     * @return
     */
    @GetMapping("/listIndexJhDetals")
    public AjaxResult listIndexJhDetals(LabNotifyplanVO labNotifyplan, HttpServletRequest request)
    {
        Map<String, Object> map =new HashMap<String, Object>();
        labNotifyplan.setType(2);//计划类型
        labNotifyplan.setRemark("1");//解析xecel
        List<LabNotifyplanVO> list = labNotifyplanService.listIndexJhDetals(labNotifyplan,request);
        map.put("list",list.size()>0?list.get(0):list);
        return AjaxResult.success(map);
    }

   /* public static InputStream byteByUrl(String urlOrPath) throws IOException {
        InputStream in = null;
        byte[] bytes;
        if (urlOrPath.toLowerCase().startsWith("https")) {
            bytes = HttpsUtils.doGet(urlOrPath);
        } else if (urlOrPath.toLowerCase().startsWith("http")) {
            URL url = new URL(urlOrPath);
            return url.openStream();
        } else {
            File file = new File(urlOrPath);
            if (!file.isFile() || !file.exists() || !file.canRead()) {
                return null;
            }
            return new FileInputStream(file);
        }
        return new ByteArrayInputStream(bytes);
    }*/

    /**
     * 导出大屏通知计划列表
     */
    @ApiOperation(value = "导出大屏通知计划列表")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NOTIFYPLAN + PerFun.EXPORT)
    @Log(title = "大屏通知计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LabNotifyplan labNotifyplan) throws IOException
    {
        List<LabNotifyplan> list = labNotifyplanService.selectLabNotifyplanList(labNotifyplan);
        ExcelUtil<LabNotifyplan> util = new ExcelUtil<LabNotifyplan>(LabNotifyplan.class);
        util.exportExcel(response, list, "大屏通知计划数据");
    }

    /**
     * 获取大屏通知计划详细信息
     */
    @ApiOperation(value = "获取大屏通知计划详细信息")
//    @PreAuthorize(hasPermi = "laboratory:notifyplan:query")
    @GetMapping(value = "/{id}")
    public ResultData<LabNotifyplanVO> getInfo(@PathVariable("id") Long id)
    {
        return ResultData.success(labNotifyplanService.selectLabNotifyplanById(id));
    }

    /**
     * 新增大屏通知计划
     */
    @ApiOperation(value = "新增大屏通知计划")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NOTIFYPLAN + PerFun.ADD)
    @Log(title = "大屏通知计划", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody LabNotifyplan labNotifyplan,HttpServletRequest request)
    {
        //大屏通知发布
        /* `title`   '通知标题',
        `notify_type` '通知类型 1工作通知 2校院通知  ',
        `type` '1大屏通知  ',
        `content`   '内容',
        `company`   '发布单位',*/

        //大屏工作计划发布
        /* `title`   '标题',
        `type` '2工作计划  ',
        `content`   '内容',
        `company`   '发布单位',
        `content_name`   '工作计划文件名称',
        `content_url`   '附件地址',
        */
        return ResultData.result(labNotifyplanService.insertLabNotifyplan(labNotifyplan,request));
    }


    /**
     * 新增工作计划
     */
    @ApiOperation(value = "新增工作计划")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WORKNOTIFYPLAN + PerFun.ADD)
    @Log(title = "新增工作计划", businessType = BusinessType.INSERT)
    @PostMapping("/addWorkPlan")
    public ResultData addWorkNotifyplan(@RequestBody LabNotifyplan labNotifyplan,HttpServletRequest request)
    {
        //大屏通知发布
        /* `title`   '通知标题',
        `notify_type` '通知类型 1工作通知 2校院通知  ',
        `type` '1大屏通知  ',
        `content`   '内容',
        `company`   '发布单位',*/

        //大屏工作计划发布
        /* `title`   '标题',
        `type` '2工作计划  ',
        `content`   '内容',
        `company`   '发布单位',
        `content_name`   '工作计划文件名称',
        `content_url`   '附件地址',
        */
        return ResultData.result(labNotifyplanService.insertLabNotifyplan(labNotifyplan,request));
    }

    /**
     * 修改大屏通知计划
     */
    @ApiOperation(value = "修改大屏通知计划")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NOTIFYPLAN + PerFun.EDIT)
    @Log(title = "大屏通知计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResultData edit(@RequestBody LabNotifyplan labNotifyplan)
    {
        return  ResultData.result(labNotifyplanService.updateLabNotifyplan(labNotifyplan));
    }

    /**
     * 删除大屏通知计划
     */
    @ApiOperation(value = "删除大屏通知计划")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_NOTIFYPLAN + PerFun.REMOVE)
    @Log(title = "大屏通知计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable Long[] ids)
    {
        return ResultData.result(labNotifyplanService.deleteLabNotifyplanByIds(ids));
    }

    /**
     * 删除工作计划
     */
    @ApiOperation(value = "删除工作计划")
    @PreAuthorize(hasPermi = PerPrefix.LABORATORY_WORKNOTIFYPLAN + PerFun.REMOVE)
    @Log(title = "删除工作计划", businessType = BusinessType.DELETE)
    @DeleteMapping("/workPlan/{ids}")
    public ResultData workPlanRemove(@PathVariable Long[] ids)
    {
        return ResultData.result(labNotifyplanService.deleteLabNotifyplanByIds(ids));
    }

    /***
     * 模板下载
     * @param response
     * @throws IOException
     */
    @Log(title = "大屏通知计划", businessType = BusinessType.EXPORT)
    @PostMapping("/exportModel")
    public void exportModel(HttpServletResponse response) throws IOException
    {
        List<LabNotifyplanExcelVO> list = new ArrayList<>();
        ExcelUtil<LabNotifyplanExcelVO> util = new ExcelUtil<LabNotifyplanExcelVO>(LabNotifyplanExcelVO.class);
        util.exportExcel(response, list, "工作计划模板");
    }
}
