package com.yinet.web.controller.yyqh;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yinet.common.core.controller.BaseController;
import com.yinet.system.domain.Holiday;
import com.yinet.system.service.IHolidayService;
import com.yinet.wsyy.domain.WsyySetting;
import com.yinet.wsyy.service.IWsyySettingService;
import com.yinet.yyqh.domain.Yyqh;
import com.yinet.yyqh.domain.YyqhGuidance;
import com.yinet.yyqh.domain.YyqhReturn;
import com.yinet.yyqh.service.YyqhGuidanceService;
import com.yinet.yyqh.service.YyqhService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 网上预约列表Controller
 *
 * @author yinet
 * @date 2019-09-02
 */
@Controller
@RequestMapping("/yyqh/web")
public class YyqhBaseController extends BaseController {
    private String prefix = "yyqh/";
    @Autowired
    private YyqhService yyqhService;
    @Autowired
    private IWsyySettingService wsyySettingService;
    @Autowired
    private YyqhGuidanceService yyqhGuidanceService;
    @Autowired
    private IHolidayService holidayService;

    @GetMapping("/index")
    public String entSet(ModelMap mmp) {
        WsyySetting yyqhSetting = wsyySettingService.selectWsyySetting();
        List<YyqhGuidance> yyqhGuidances = yyqhGuidanceService.selectGuidanceByParentId(0L);
        List<Holiday> holidays = holidayService.selecAllHoliday();
        mmp.addAttribute("yyqhSetting", yyqhSetting);
        mmp.addAttribute("yyqhGuidances", yyqhGuidances);
        mmp.addAttribute("holidays", new Gson().toJson(holidays));
        return prefix + "yyqhweb";
    }

    @PostMapping("/getTimeQuantumNum")
    @ResponseBody
    public String getTimeQuantumNum(String bookedDay, Long guidanceParent) throws ParseException {
        Date bookDay = new SimpleDateFormat("yyyy-MM-dd").parse(bookedDay);
        LinkedHashMap<String, List<Integer>> map = yyqhService.selectTimeNum(bookDay, guidanceParent);
        return JSON.toJSONString(map);
    }

    @PostMapping("/saveYyqh")
    @ResponseBody
    public int saveYy(Yyqh yyqh) {
        return yyqhService.insertYyqh(yyqh);
    }

    @PostMapping("/cancel")
    @ResponseBody
    public String cancelYy(String id) {
        YyqhReturn yyqhReturn = yyqhService.cancelYy(id);
        return JSON.toJSONString(yyqhReturn);
    }

    @GetMapping("/yycx")
    public String getInfoView() {
        return prefix + "yycx";
    }

    @GetMapping("/getInfo")
    public String getInfo(String card, ModelMap mmap) {
        Yyqh yyqh = new Yyqh();
        yyqh.setStatus("1");
        yyqh.setIdNum(card);
        List<Yyqh> yyqhs = yyqhService.selectYyqhListByCard(yyqh);
        mmap.addAttribute("yyqhs", yyqhs);
        return prefix + "yycxresult";
    }

    @GetMapping("/getGuidance")
    @ResponseBody
    public String getGuidance(Long id) {
        YyqhGuidance yyqhGuidance = yyqhGuidanceService.selectGuidanceById(id);
        return JSON.toJSONString(yyqhGuidance);
    }

    @PostMapping("/getYyqhGuidance")
    @ResponseBody
    public String getYyqhGuidance(Long guidanceParentId) {
        List<YyqhGuidance> yyqhGuidances = yyqhGuidanceService.selectGuidanceByParentId(guidanceParentId);
        return JSON.toJSONString(yyqhGuidances);
    }

    @GetMapping("/GetCardForm")
    @ResponseBody
    public String GetCardForm(String Card) {
        YyqhReturn yyqhReturn = yyqhService.getCardForm(Card);
        return JSON.toJSONString(yyqhReturn);
    }

    @GetMapping("/changeStatus")
    @ResponseBody
    public String changeStatus(String id, String tickNum) {
        YyqhReturn yyqhReturn = yyqhService.changeStatus(id, tickNum);
        return JSON.toJSONString(yyqhReturn);
    }
}
