package com.yinet.web.controller.wsyy;

import com.alibaba.fastjson.JSON;
import com.yinet.common.core.controller.BaseController;
import com.yinet.wsyy.domain.Wsyy;
import com.yinet.wsyy.domain.WsyySetting;
import com.yinet.wsyy.service.IWsyyQuantumService;
import com.yinet.wsyy.service.IWsyyService;
import com.yinet.wsyy.service.IWsyySettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 网上预约列表Controller
 *
 * @author yinet
 * @date 2019-09-02
 */
@Controller
@RequestMapping("/wsyy/web")
public class WsyyBaseController extends BaseController {
    private String prefix = "wsyy/";
    @Autowired
    private IWsyyService wsyyService;
    @Autowired
    private IWsyySettingService wsyySettingService;

    @Autowired
    private IWsyyQuantumService wsyyQuantumService;

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

    @GetMapping("/entbuis")
    public String entBuis() {
        return prefix + "entbuis";
    }

    @GetMapping("/yy/{type}")
    public String entSet(@PathVariable("type") int type, ModelMap mmp) {
        WsyySetting wsyySetting = wsyySettingService.selectWsyySetting();
        mmp.addAttribute("yytype", type);
        switch (type) {
            case 1:
                mmp.addAttribute("title", "企业设立预约");
                break;
            case 2:
                mmp.addAttribute("title", "企业变更预约");
                break;
            case 3:
                mmp.addAttribute("title", "个体工商户设立预约");
                break;

        }
        mmp.addAttribute("wsyySetting", wsyySetting);
        return prefix + "yy";
    }

    @GetMapping("/getnum")
    @ResponseBody
    public String getEntNum() {
        Wsyy wsyy = new Wsyy();
        wsyy.setType(1L);
        int sls = wsyyService.selectWsyyList(wsyy).size();//企业设立
        wsyy.setType(2L);
        int bgs = wsyyService.selectWsyyList(wsyy).size();

        return "{\"sls\":" + sls + ",\"bgs\":" + bgs + "}";
    }

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

    @PostMapping("/saveYy")
    @ResponseBody
    public String saveYy(Wsyy wsyy) {
        int i = wsyyService.insertWsyy(wsyy);
        return String.valueOf(i);
    }
}
