package com.bofeng.wx.controller;

import com.bofeng.dao.*;
import com.bofeng.entity.*;
import com.bofeng.service.HomeService;
import com.bofeng.service.QzTaskService;
import com.bofeng.service.UserOpenService;
import com.yvan.Model;
import com.yvan.platform.YvanUtil;
import com.yvan.springmvc.ResultModel;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class UserOpenController {

    @Autowired
    private HomeService homeService;
    @Autowired
    private UserOpenService userOpenService;
    @Autowired
    private UserOpenMapper userOpenMapper;
    @Autowired
    private UptownMapper uptownMapper;
    @Autowired
    private UptownHouseMapper uptownHouseMapper;
    @Autowired
    private UptownHomeMapper uptownHomeMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private QzTaskService qzTaskService;

    @GetMapping("/user/home.html")
    public ModelAndView home(ModelMap model) {
        UserOpen userOpen = homeService.getUserOpen();
        List<UserRole> list = userRoleMapper.getUserRoleByUserId(userOpen.getUserId());
//        List<UserRole> list = userRoleMapper.getUserRoleByUserId(1224239062748065800L);
        model.put("user", userOpen);
        if (list.size() == 0) {
            return new ModelAndView("/user/home.ftl", model);
        } else if (list.size()>0) {
            if (list.get(0).getRoleId() == 1) {
                return new ModelAndView("/home/home.ftl", model);
            } else if(list.get(0).getRoleId() == 2) {
                List<QzTask> taskList = qzTaskService.selectAll();
                model.put("taskList", YvanUtil.toJsonPretty(taskList));
                return new ModelAndView("/yeweihui/home.ftl", model);
            }
        }
        return null;
    }

    @GetMapping("/home/getUptownData.json")
    public List<Uptown> getUptownData(HttpServletRequest request, @RequestParam(value = "area") Integer area) {
        return uptownMapper.getAllUptown(area);
    }

    @GetMapping("/home/getUptownHouseData.json")
    public List<UptownHouse> getUptownHouseData(HttpServletRequest request, @RequestParam(value = "uptownId") Long uptownId) {
        return uptownHouseMapper.getAllUptownHouse(uptownId);
    }

    @PostMapping("/home/checkHouseId.json")
    public ResultModel checkHouseId(HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        Long houseId = Long.parseLong(request.getParameter("houseId"));
        List<UptownHome> list = uptownHomeMapper.getUptownHomeBy(houseId);
        if (list.size() > 0) {
            resultModel.put("success", false);
            resultModel.put("msg", "该门牌已有人入住！");
            return resultModel;
        }
        resultModel.put("success", true);
        resultModel.put("msg", "操作成功");
        return resultModel;
    }

    @PostMapping("/home/saveUser.json")
    public ModelAndView saveUser(ModelMap model, HttpServletRequest request) {
        Long userId = Long.parseLong(request.getParameter("userId"));
        Integer userType = Integer.parseInt(request.getParameter("userType"));
        String linkman = request.getParameter("linkman");
        String phone = request.getParameter("phone");
        Integer area = Integer.parseInt(request.getParameter("area"));
        Long uptownId = Long.parseLong(request.getParameter("uptownId"));
        Long houseId = Long.parseLong(request.getParameter("houseId"));
        userOpenService.saveUserInfo(userId,userType,linkman,phone,area,uptownId,houseId);
        UserOpen userOpen = userOpenMapper.selectByUserId(userId);
        model.put("user", userOpen);
        if (userType == 1) {
            return new ModelAndView("/home/home.ftl", model);
        } else if (userType == 2) {
            List<QzTask> taskList = qzTaskService.selectAll();
            model.put("taskList", YvanUtil.toJsonPretty(taskList));
            return new ModelAndView("/yeweihui/home.ftl", model);
        } else {
            return null;
        }

    }
}
