package com.app.admin.framework.system.organization.controller;

import com.app.admin.framework.system.administrator.model.SysAdministrator;
import com.app.admin.framework.system.administrator.service.SysAdministratorService;
import com.app.admin.framework.system.organization.model.SysOrganization;
import com.app.admin.framework.system.organization.service.SysOrgPowerService;
import com.app.admin.framework.system.organization.service.SysOrganizationService;
import com.app.admin.framework.system.organization.view.SysOrganizationView;
import com.app.admin.framework.system.permission.authorization.model.SysAdministratorRole;
import com.app.framework.core.support.auth.LoginUser;
import com.app.framework.core.support.controller.CrudBaseControllerSupport;
import com.app.framework.core.support.enums.common.StateEnum;
import com.app.framework.core.support.view.form.AjaxResult;
import com.app.framework.core.support.view.form.ModelEditForm;
import com.app.framework.core.support.view.grid.Paging;
import com.app.framework.core.support.view.tree.ExTreeNode;
import com.app.framework.core.utils.request.SessionUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 组织机构管理
 */
@Controller
@RequestMapping("/sys/organization")
public class SysOrganizationController extends CrudBaseControllerSupport<SysOrganizationView> {

    @Resource
    SysOrganizationService sysOrganizationService;

    @Resource
    SysAdministratorService sysAdministratorService;

    @Resource
    SysOrgPowerService sysOrgPowerService;

    /**
     * 查询列表的具体实现
     * 请求：list
     *
     * @param request HttpServletRequest
     * @param view    查询条件
     * @param paging  分页条件
     * @return 查询结果
     */
    @Override
    public List<SysOrganizationView> list(HttpServletRequest request, SysOrganizationView view, Paging paging) {
        view.setParentId(0L);
        List<SysOrganization> list = sysOrganizationService.getPageList(view, paging);
        List<SysOrganizationView> viewList = new ArrayList<>(list.size());
        list.forEach(organization -> {
            SysAdministrator administrator = sysAdministratorService.getOrgAdministrator(organization.getId());
            SysOrganizationView v = new SysOrganizationView();
            BeanUtils.copyProperties(organization, v);
            if (administrator != null) {
                v.setAdministratorRealName(administrator.getRealName());
                v.setAdministratorUsername(administrator.getUsername());
            }
            viewList.add(v);
        });
        return viewList;
    }

    /**
     * 保存记录的具体实现
     * 请求：saveEntity
     *
     * @param request HttpServletRequest
     * @param view    实体
     * @return 影响结果数
     */
    @Override
    public AjaxResult saveEntity(HttpServletRequest request, SysOrganizationView view) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        sysOrgPowerService.saveOrg(view, loginUser);
        return AjaxResult.wrap(true);
    }

    /**
     * 删除记录,根据Id 具体实现
     * 请求：deleteById 参数oid
     * 请求：batchDelete 参数oid数组
     *
     * @param request HttpServletRequest
     * @param oids    实体
     * @return 影响结果数
     */
    @Override
    public AjaxResult batchDelete(HttpServletRequest request, Long oids[]) {
        for (Long id : oids) {
            SysOrganization update = new SysOrganization();
            update.setId(id);
            update.setState(StateEnum.DELETE.getValue());
            sysOrganizationService.updateById(update);
        }
        return AjaxResult.wrap(true);
    }

    /**
     * 跳转到列表页面
     */
    @RequestMapping(value = "/golist")
    public ModelAndView goList(HttpServletRequest request) {
        return new ModelAndView("admin/framework/system/organization/list.html");
    }

    /**
     * 跳转到编辑页面
     */
    @RequestMapping(value = "/edit")
    public ModelAndView edit(HttpServletRequest request, SysOrganization entity, ModelEditForm editForm) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        SysAdministrator administrator = null;
        if (entity.getId() != null) {
            entity = sysOrganizationService.getById(entity.getId());
            //机构管理员
            administrator = sysAdministratorService.getOrgAdministrator(entity.getId());
        }
        map.put("entity", entity);
        map.put("administrator", administrator);
        map.put("editForm", editForm);
        return new ModelAndView("admin/framework/system/organization/edit.html", map);
    }

    /**
     * 跳转到权限管理页面
     */
    @RequestMapping(value = "/power")
    public ModelAndView power(SysOrganization entity) {
        SysAdministratorRole orgAdminRole = sysOrgPowerService.getOrgPower(entity.getId());
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId", orgAdminRole.getUserId());
        map.put("roleId", orgAdminRole.getRoleId());
        return new ModelAndView("admin/framework/system/authorization/bind_module.html", map);
    }

    /**
     * 查询机构树
     *
     * @return
     */
    @RequestMapping(value = "/treeList")
    @ResponseBody
    public List<ExTreeNode> treeList(HttpServletRequest request) {
        LoginUser loginUser = SessionUtil.getCurrentUser(request);
        return sysOrganizationService.listTree(loginUser.getOrganizationId());
    }

}