package com.yinet.web.controller.touchscreen;

import com.yinet.bjtj.service.IFormBjtjService;
import com.yinet.common.config.Global;
import com.yinet.common.config.ServerConfig;
import com.yinet.common.core.controller.BaseController;
import com.yinet.common.core.page.TableDataInfo;
import com.yinet.common.utils.QRCodeUtils;
import com.yinet.common.utils.ServletUtils;
import com.yinet.common.utils.StringUtils;
import com.yinet.system.domain.GuidanceFile;
import com.yinet.system.domain.TouchScreenGuidance;
import com.yinet.system.domain.TouchScreenNotice;
import com.yinet.system.service.*;
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.util.List;
import java.util.Map;

/**
 * @author ruoyi
 */
@Controller
@RequestMapping("/touchscreen")
public class BaseWebController extends BaseController {
    static String touchIndex = "touchscreen/web/";
    @Autowired
    private TouchScreenHelpInfoService touchScreenHelpInfoService;
    @Autowired
    private TouchScreenNoticeService touchScreenNoticeService;
    @Autowired
    private TouchScreenGuidanceService touchScreenGuidanceService;
    @Autowired
    private IGuidanceFileService guidanceFileService;
    @Autowired
    private BjcxService bjcxService;
    @Autowired
    private TouchScreenHaoService touchScreenHaoService;
    @Autowired
    private IFormBjtjService formBjtjService;

    /**
     * 首页
     *
     * @return
     */
    @GetMapping(value = "/index")
    public String Index() {
        return touchIndex + "index";
    }

    /**
     * 中心介绍
     *
     * @return
     */
    @GetMapping(value = "/jieshao")
    public String GetInfo(Model model) {
        model.addAttribute("helpInfo", touchScreenHelpInfoService.selectHelpInfoLimit(1));
        return touchIndex + "jieshao";
    }

    /**
     * 办件查询
     *
     * @return
     */
    @GetMapping(value = "/search")
    public String Search() {
        return touchIndex + "search";
    }


    /**
     * 办事指南
     * touchScreenGuidanceService
     *
     * @return
     */
    @GetMapping(value = "/nan")
    public String GetHelp(Model model) {

        return touchIndex + "zhengding";
    }

    @GetMapping(value = "/zhengding/{id}")
    public String GetHelp1(@PathVariable("id") String id, Model model) {
        //List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectParentGuidanceList();
        //    model.addAttribute("guidanceList", guidanceList);
        if (id.equals("zdxq")) {
            List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectGuidanceListByParentId("6577bf17d2844d468065c8f366a3cacd");
            model.addAttribute("guidanceList", guidanceList);
            return touchIndex + "list";
        } else {
            String parentId = "0";
            List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectGuidanceListByZNameFindAll(id, parentId);
            model.addAttribute("guidanceList", guidanceList);
            return touchIndex + "nan";
        }
    }

    /**
     * 公示公告
     *
     * @return
     */

    @GetMapping(value = "/notice")
    public String GetNotice() {
        return touchIndex + "notice";
    }

    /**
     * 公示公告列表
     *
     * @return
     */
    @PostMapping("/notice/list")
    @ResponseBody
    public TableDataInfo GetNoticeList() {
        startPage();
        List<TouchScreenNotice> touchScreenNotices = touchScreenNoticeService.selectNoticeList();
        return getDataTable(touchScreenNotices);
    }

    /**
     * 公示公告详细信息
     *
     * @return
     */
    @GetMapping(value = "/notice/{id}")
    public String GetNoticeInfo(@PathVariable("id") String id, Model model) {
        TouchScreenNotice touchScreenNotice = touchScreenNoticeService.selectNoticeById(id);
        model.addAttribute("noticeinfo", touchScreenNotice);
        return touchIndex + "noticeinfo";
    }

    /**
     * 办事指南列表 //正定
     */
    @GetMapping(value = "/guidanceList1/{id}")
    public String guidanceList1(@PathVariable("id") String id, Model model) {
        List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectGuidanceListByParentId(id);
        model.addAttribute("guidanceList", guidanceList);
        return touchIndex + "list";
    }
    /**
     * 办事指南列表
     */
   /*@GetMapping(value = "/guidanceList/{id}")
    public String guidanceList(@PathVariable("id") String id, Model model) {
        List<TouchScreenGuidance> guidanceList = touchScreenGuidanceService.selectGuidanceListByParentId(id);
        model.addAttribute("guidanceList", guidanceList);
        return touchIndex + "list";
    }*/

    /**
     * 查看文档
     * selectGuidanceNotParentId
     *
     * @return
     */
    @GetMapping(value = "/guidanceList/viewdoc/{id}")
    public String ViewDoc(@PathVariable("id") String id, Model model) throws IOException {
        GuidanceFile guidanceFile = guidanceFileService.selectGuidanceFileById(id);
        TouchScreenGuidance touchScreenGuidance = touchScreenGuidanceService.selectGuidanceById(guidanceFile.getGuidanceId());
        String domain = ServerConfig.getDomain(ServletUtils.getRequest());
        String qrUrl = domain + guidanceFile.getFileUrl();
        String s = QRCodeUtils.generalQRCode(qrUrl);
        guidanceFile.setQrCode(s);
        model.addAttribute("guidance", touchScreenGuidance);
        model.addAttribute("guidanceFile", guidanceFile);
        String url = Global.getKKFileView();
        model.addAttribute("kkview", url);
        return touchIndex + "viewdoc";
    }

    /**
     * 查看文档
     *
     * @return
     */
    @GetMapping(value = "/guidanceList/viewdoc/{type}/{id}")
    public String viewDocByType(@PathVariable("id") String id, @PathVariable("type") String type, Model model) throws IOException {
        GuidanceFile temp = new GuidanceFile();
        temp.setFileType(type);
        temp.setGuidanceId(id);
        List<GuidanceFile> guidanceFiles = guidanceFileService.selectGuidanceFileList(temp);
        if (guidanceFiles.size() == 1) {
            return ViewDoc(guidanceFiles.get(0).getFileId(), model);
        } else {
            model.addAttribute("guidanceFiles", guidanceFiles);
            return touchIndex + "list2";
        }
    }


    /**
     * 页头
     *
     * @return
     */
    @GetMapping(value = "/header")
    public String GetHeader() {
        return touchIndex + "tsheader";
    }

    /**
     * 页脚
     *
     * @return
     */
    @GetMapping(value = "/bottom")
    public String GetFooter() {
        return touchIndex + "bottom";
    }

    @PostMapping("/bjcx")
    @ResponseBody
    public String bjcx(String sbh, String password) {
        return bjcxService.bjcx(sbh, password);
    }

    /**
     * 实时叫号
     */
    @GetMapping("/hao")
    public String viewRTC(ModelMap mmap) {
        Map<String, Integer> pieNum = touchScreenHaoService.selectPieNum();
        Map<String, Integer> callNum = touchScreenHaoService.selectCallNum();
        Integer haoCount = touchScreenHaoService.selectCount();
        mmap.addAttribute("pieNum", pieNum);
        if (StringUtils.isNotEmpty(callNum)) {
            Integer today = callNum.get("today");
            Integer blz = callNum.get("blz");
            Integer deng = callNum.get("deng");
            Integer yb = today - blz - deng;
            mmap.addAttribute("today", today);
            mmap.addAttribute("blz", blz);
            mmap.addAttribute("deng", deng);
            mmap.addAttribute("yb", yb < 0 ? 0 : yb);
        } else {
            mmap.addAttribute("today", 0);
            mmap.addAttribute("blz", 0);
            mmap.addAttribute("deng", 0);
            mmap.addAttribute("yb", 0);
        }
        mmap.addAttribute("zong", haoCount);
        return touchIndex + "hao";
    }

    /**
     * 办件统计
     *
     * @return
     */
    @GetMapping(value = "/tong")
    public String tongjiList(Model model) {
        Map tongjiList = formBjtjService.selectTongjiList();
        model.addAttribute("tjList", tongjiList);
        return touchIndex + "tong";
    }
}
