package com.bofeng.wx.controller;

import com.bofeng.dao.*;
import com.bofeng.entity.*;
import com.bofeng.excel.ExcelUtils;
import com.bofeng.service.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.yvan.platform.JsonWapper;
import com.yvan.platform.YvanUtil;
import com.yvan.springmvc.ResultModel;
import io.swagger.annotations.*;
import lombok.SneakyThrows;
import org.apache.shiro.authz.annotation.RequiresPermissions;
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 javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api("注册登陆")
@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 UptownUnitMapper uptownUnitMapper;
    @Autowired
    private QzTaskService qzTaskService;
    @Autowired
    private RbService rbService;
    @Autowired
    private MsReportService msReportService;

    @ApiOperation("登陆首页")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID", required = false, dataType = "Long",paramType="query"),
            @ApiImplicitParam(name = "userType", value = "用户类型", required = false, dataType = "Integer",paramType="query")
    })
    @GetMapping("/user/home.html")
    public ModelAndView home(ModelMap model, @RequestParam(value = "userId", required = false,defaultValue = "0") Long userId,
                             @RequestParam(value = "userType", required = false,defaultValue = "0") Integer userType) throws JsonProcessingException {
        List<UserRole> list = new ArrayList<>();
        if (userId == 0) {
//            UserOpen userOpen = homeService.getUserOpen();
//            list = userRoleMapper.getUserRoleByUserId(userOpen.getUserId());
            list = userRoleMapper.getUserRoleByUserId(1225325493987995649L);
            UserOpen userOpen = userOpenMapper.selectByUserId(1225325493987995649L);
            userId = userOpen.getUserId();
            model.put("user", userOpen);
            model.put("user_id", "\""+userOpen.getUserId()+"\"");
            if (list.size() == 0) {
                return new ModelAndView("/user/home.ftl", model);
            }
            if (list.size() == 1) {
                MsReport msReport =msReportService.getReportByDateNow(userId);
                model.put("msReport", new JsonWapper(msReport));
                return new ModelAndView("/home/home.ftl", model);
            }
            if (list.size() == 2) {
                return new ModelAndView("/user/changeRole.ftl", model);
            }
        } else {
            model.put("user", userOpenMapper.selectByUserId(userId));
            model.put("user_id", "\""+userId+"\"");
            if (userType == 1) {
                MsReport msReport =msReportService.getReportByDateNow(userId);
                model.put("msReport", new JsonWapper(msReport));
                return new ModelAndView("/home/home.ftl", model);
            } else if (userType == 2) {
                return new ModelAndView("/user/changeRole.ftl", model);
            }
        }
        return null;
    }

    @GetMapping("/user/homeIndex.html")
    public ModelAndView homeIndex(ModelMap model, @RequestParam(value = "userId", required = false,defaultValue = "0") Long userId,
                             @RequestParam(value = "userType", required = false,defaultValue = "0") Integer userType) throws JsonProcessingException {
        if (userId != 0 && userType != 0) {
            model.put("user_id", "\"" + userId + "\"");
            if (userType == 1) {
                MsReport msReport = msReportService.getReportByDateNow(userId);
                model.put("msReport", new JsonWapper(msReport));
                return new ModelAndView("/home/home.ftl", model);
            } else if (userType == 2) {
                List<QzTask> taskList = qzTaskService.selectAll(userId);
                model.put("taskList", YvanUtil.toJsonPretty(taskList));
                List<sysUptownUnit> rbList = rbService.selectAll(userId);
                model.put("rbList", YvanUtil.toJsonPretty(rbList));
                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/getUptownUnitData.json")
    public List<UptownUnit> getUptownUnitData(HttpServletRequest request, @RequestParam(value = "uptownId") Long uptownId) {
        return uptownUnitMapper.getUptownUnitData(uptownId);
    }

    @PostMapping("/home/checkHouseId.json")
    public ResultModel checkHouseId(HttpServletRequest request) {
        ResultModel resultModel = new ResultModel();
        Long unitId = Long.parseLong(request.getParameter("unitId"));
        String doorplate = request.getParameter("doorplate");
        List<UptownHome> list = uptownHouseMapper.getUptownHouseBy(unitId, doorplate);
        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 void 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 unitId = Long.parseLong(request.getParameter("unitId"));
        String doorplate = request.getParameter("doorplate");
        userOpenService.saveUserInfo(userId,userType,linkman,phone,area,uptownId,unitId,doorplate);
    }

    @ApiOperation("客户档案信息错误数据导出")
    @GetMapping("/user/importExcelUserOpen")
    @SneakyThrows
    public void errorCustInfo(HttpServletResponse resp) {
        List<UserOpen> list = userOpenMapper.getAllUser();
        ExcelUtils.writeSheet(UserOpen.class, list).export(resp);
    }
}
