package cn.pluss.platform.controller.userOrgan;

import cn.pluss.platform.controller.BaseNoModelController;
import cn.pluss.platform.entitiy.ShiroUser;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.entity.UserOrgan;
import cn.pluss.platform.userApp.UserAppService;
import cn.pluss.platform.userOrgan.UserOrganService;
import cn.pluss.platform.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("userOrgan")
public class UserOrganController extends BaseNoModelController {

    private Integer PAGE_SIZE = 12;

    @Autowired
    private UserOrganService userOrganService;

    @Autowired
    private UserAppService userAppService;

    /**
     * 引导页
     *
     * @return
     */
    @GetMapping(value = "/index")
    public String index(HttpServletRequest request) {
        return "userOrgan/userOrganList";
    }

    /**
     * 分页查询
     */
    @RequestMapping(value = "/queryUserOrganByPage", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryUserOrganByPage(@RequestParam(defaultValue = "1") Integer currPage) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("pageSize", PAGE_SIZE);
        map.put("offset", (currPage - 1) * PAGE_SIZE);
        ShiroUser user = queryCurrentShiroUser();
        Boolean bol = verifyUserRole();
        UserApp userApp = new UserApp();
        userApp.setUserId(user.getId());
        List<UserApp> queryUserAppList = userAppService.queryUserAppList(userApp);

        if (!bol) {
            map.put("userId", user.getId());
            //可以得到用户的类型
            if (queryUserAppList != null && queryUserAppList.size() > 0) {
                result.put("userType", queryUserAppList.get(0).getUserType());
            }
        }
        if (bol) {
            result.put("admin", "admin");
        }
        List<UserOrgan> userOrganList = userOrganService.queryUserOrganPage(map);
        Integer count = userOrganService.queryUserOrganPageCount(map);
        result.put("userOrganList", userOrganList);
        result.put("count", count);
        result.put("pageCount", StringUtil.getPageCount(count, PAGE_SIZE));
        result.put("currPage", currPage);
        return result;
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryUserOrganDetails", method = RequestMethod.GET)
    public String queryUserOrganDetails(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            UserOrgan userOrgan = new UserOrgan();
            userOrgan.setId(id);
            userOrgan = userOrganService.queryUserOrgan(userOrgan);
            request.setAttribute("userOrgan", userOrgan);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "userOrgan/userOrganDetails";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryUserOrganBaseInfo", method = RequestMethod.GET)
    public String queryUserOrganBaseInfo(HttpServletRequest request, Integer id, Integer pageIndex) {

        if (id != null && id != 0) {
            UserOrgan userOrgan = new UserOrgan();
            userOrgan.setId(id);
            userOrgan = userOrganService.queryUserOrgan(userOrgan);
            request.setAttribute("userOrgan", userOrgan);
            request.setAttribute("pageIndex", pageIndex);
        }
        return "userOrgan/userOrganBaseInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryUserOrganExtendsInfo", method = RequestMethod.GET)
    public String queryUserOrganExtendsInfo() {

        return "userOrgan/userOrganExtendsInfo";
    }

    /**
     * 查询详情
     */
    @RequestMapping(value = "/queryUserOrganFunctionsInfo", method = RequestMethod.GET)
    public String queryUserOrganFunctionsInfo() {

        return "userOrgan/userOrganFunctionsInfo";
    }

    /**
     * 保存对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/saveUserOrgan", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> saveUserOrgan(UserOrgan userOrgan) {

        if (userOrgan.getId() != null && userOrgan.getId() != 0) {
            userOrganService.updateUserOrgan(userOrgan);
        } else {
            userOrganService.saveUserOrgan(userOrgan);
        }
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 修改对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/updateUserOrgan", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateUserOrgan(Integer id) {

        UserOrgan userOrgan = new UserOrgan();
        userOrgan.setId(id);
        userOrganService.updateUserOrgan(userOrgan);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

    /**
     * 删除对象
     *
     * @throws IOException
     */
    @RequestMapping(value = "/deleteUserOrgan", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> deleteUserOrgan(Integer id) {
        UserOrgan userOrgan = new UserOrgan();
        userOrgan.setId(id);
        userOrganService.deleteUserOrgan(userOrgan);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("resultCode", "1");
        result.put("resultMessage", "保存成功!");
        return result;
    }

}


