package com.yihu.hos.system.controller;

import com.yihu.hos.core.datatype.StringUtil;
import com.yihu.hos.system.model.SystemOrganization;
import com.yihu.hos.system.service.OrganizationManager;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 机构管理
 * Created by hzp on 2016/1/12.
 */
@RequestMapping("/org")
@Controller
public class OrgController extends BaseController {

    @Resource(name = OrganizationManager.BEAN_ID)
    private OrganizationManager organization;

    /**
     * 组织管理界面
     * @param model
     * @return
     */
    @RequestMapping("initial")
    public String organization(Model model) {
        model.addAttribute("contentPage","system/org/organization");
        return "partView";
    }


    /**
     * 组织编辑界面
     */
    @RequestMapping("editorOrganization")
    public String editorOrganization(Model model,String orgId) {
        try {
            //是否编辑
            if (orgId != null && orgId.length() > 0) {
                //获取机构信息
                SystemOrganization org = organization.getOrgById(orgId);
                model.addAttribute("model", org);
            }
            model.addAttribute("contentPage", "system/org/editorOrganization");
            return "pageView";
        } catch (Exception ex) {
            model.addAttribute("contentPage", "organization/editorOrganization");
            return "pageView";
        }
    }


    /**
     * 获取组织列表
     */
    @RequestMapping("getOrgList")
    @ResponseBody
    public Result getOrgList(String pid,String name,String activityFlag,Integer page, Integer rows) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("pid", pid);
            map.put("name", name);
            map.put("activityFlag", activityFlag);

            return organization.getOrgList(map, page, rows);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 新增组织
     */
    @RequestMapping("addOrg")
    @ResponseBody
    public Result addOrg(HttpServletRequest request) {
        try {
            SystemOrganization obj = new SystemOrganization();

            BeanUtils.populate(obj, request.getParameterMap());
            obj.setActivityFlag("1");
            return organization.addOrg(obj);

            }
            catch(Exception ex)
            {
                return Result.error(ex.getMessage());
            }

    }


    /**
     * 修改组织
     */

    @RequestMapping("updateOrg")
    @ResponseBody
    public Result updateOrg(HttpServletRequest request) {
        try {

            SystemOrganization obj = new SystemOrganization();
            BeanUtils.populate(obj, request.getParameterMap());
            if(!StringUtil.isEmpty(obj.getSettledWay()))
            {
                obj.setSettled("1");
            }
            else{
                obj.setSettled("0");
            }
            return organization.updateOrg(obj);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 删除组织
     */
    @RequestMapping("deleteOrg")
    @ResponseBody
    public Result deleteOrg(String orgId) {
        try {
            return organization.deleteOrg(orgId);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }

    /**
     * 修改组织状态
     *//*
    @RequestMapping("activityOrg")
    @ResponseBody
    public Result activityOrg(String orgId,String activityFlag) {
        try {
            return organization.activityOrg(orgId,activityFlag);
        }
        catch (Exception ex)
        {
            return Result.error(ex.getMessage());
        }
    }*/
}
