package com.ruoyi.web.controller.Ytjback;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.WebSocketServer;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.QyInfo;
import com.ruoyi.system.service.IGtgsywService;
import com.ruoyi.system.service.IQyInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * info表Controller
 *
 * @author ruoyi
 * @date 2019-09-10
 */
@Controller
@RequestMapping("/Ytj/info")
public class QyInfoController extends BaseController {
    private String prefix = "Ytj/info";

    @Autowired
    private IQyInfoService qyInfoService;
    @Autowired
    private IGtgsywService gtgsywService;

    @RequiresPermissions("system:info:view")
    @GetMapping()
    public String info() {
        return prefix + "/info";
    }

    /**
     * 查询info表列表
     */
    @RequiresPermissions("system:info:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(QyInfo qyInfo) {
        startPage();
        List<QyInfo> list = qyInfoService.selectQyInfoList(qyInfo);
        return getDataTable(list);
    }

    /**
     * 导出info表列表
     */
    @RequiresPermissions("system:info:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(QyInfo qyInfo) {
        List<QyInfo> list = qyInfoService.selectQyInfoList(qyInfo);
        ExcelUtil<QyInfo> util = new ExcelUtil<QyInfo>(QyInfo.class);
        return util.exportExcel(list, "info");
    }

    /**
     * 新增info表
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存info表
     */
    @RequiresPermissions("system:info:add")
    @Log(title = "info表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(QyInfo qyInfo) {
        return toAjax(qyInfoService.insertQyInfo(qyInfo));
    }

    /**
     * 修改info表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        QyInfo qyInfo = qyInfoService.selectQyInfoById(id);
        mmap.put("qyInfo", qyInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存info表
     */
    @RequiresPermissions("system:info:edit")
    @Log(title = "info表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(QyInfo qyInfo) {
        return toAjax(qyInfoService.updateQyInfo(qyInfo));
    }

    /**
     * 删除info表
     */
    @RequiresPermissions("system:info:remove")
    @Log(title = "info表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(qyInfoService.deleteQyInfoByIds(ids));
    }

    @GetMapping("/selAll")
    @ResponseBody
    public AjaxResult selAll( Model model) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String end = (formatter.format(new Date()));
        String nian = end.substring(0, 5);
        String start = nian + "01-01 00:00:00";
        String yue = end.substring(0, 7);
        String yuestar = yue + "-01 00:00:00";
        String taday = end.substring(0, 10);
        String tadaystar = taday + " 00:00:00";
        List<Map>  gtgsyws = gtgsywService.selectByNumb(yuestar);
        List<Map>  gtlflg = gtgsywService.selectToflg();
        List<Map>  qyInfos = qyInfoService.selectQyInfoByNumb(yuestar);
        List<Map>  qyflg = qyInfoService.selectQyInfoToflg();
        Map map=new HashMap();
        map.put("gtgsyws",gtgsyws);
        map.put("qyInfos",qyInfos);
        map.put("gtlflg",gtlflg);
        map.put("qyflg",qyflg);
        return AjaxResult.success(map);
    }

    //推送数据接口
    @ResponseBody
    @GetMapping("/socket/push")
    public AjaxResult pushToWeb( String cid,String message) {
        try {
            WebSocketServer.sendInfo("收到一个新的申请消息!请及时查看",null);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.error(cid+"#"+e.getMessage());
        }
        return AjaxResult.success(1);
    }

}
