package com.zheng.upms.admin.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.upms.dao.model.UpmsOrganization;
import com.zheng.upms.dao.model.UpmsOrganizationExample;
import com.zheng.upms.dao.model.UpmsSystem;
import com.zheng.upms.rpc.api.UpmsOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 组织controller
 * Created by shuzheng on 2017/2/6.
 */
@Controller
@Api(value = "组织管理", description = "组织管理")
@RequestMapping("/manage/organization")
public class UpmsOrganizationController extends BaseController {

    private static Logger _log = LoggerFactory.getLogger(UpmsOrganizationController.class);

    @Autowired
    private UpmsOrganizationService upmsOrganizationService;

    @ApiOperation(value = "组织首页")
    @RequiresPermissions("upms:organization:read")
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() {
        return "/manage/organization/index";
    }

    @ApiOperation(value = "组织列表")
    @RequiresPermissions("upms:organization:read")
    @RequestMapping("/list")
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order) {
        // 数据列表
        UpmsOrganizationExample upmsupmsOrganizationExample = new UpmsOrganizationExample();
        upmsupmsOrganizationExample.setOffset(offset);
        upmsupmsOrganizationExample.setLimit(limit);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            upmsupmsOrganizationExample.setOrderByClause(sort + " " + order);
        }
        List<UpmsOrganization> organizations = upmsOrganizationService.selectByExample(upmsupmsOrganizationExample);
        return organizations;
    }

    @ApiOperation(value = "新增组织")
    @RequiresPermissions("upms:organization:create")
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add() {
        return "/manage/organization/add";
    }

    @ApiOperation(value = "新增组织")
    @RequiresPermissions("cms:organization:create")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public String add(UpmsOrganization upmsOrganization, ModelMap modelMap) {
        long time = System.currentTimeMillis();
        upmsOrganization.setCtime(time);
        int count = upmsOrganizationService.insertSelective(upmsOrganization);
        modelMap.put("count", count);
        _log.info("新增记录id为：{}", upmsOrganization.getOrganizationId());
        return "redirect:/manage/organization/list";
    }

    @ApiOperation(value = "删除组织")
    @RequiresPermissions("cms:organization:delete")
    @RequestMapping(value = "/delete/{ids}",method = RequestMethod.GET)
    public String delete(@PathVariable("ids") String ids, ModelMap modelMap) {
        int count = upmsOrganizationService.deleteByPrimaryKeys(ids);
        modelMap.put("count", count);
        return "redirect:/manage/organization/list";
    }

    @ApiOperation(value = "修改组织")
    @RequiresPermissions("cms:organization:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.GET)
    public String update(@PathVariable("id") int id, ModelMap modelMap) {
        UpmsOrganization organization = upmsOrganizationService.selectByPrimaryKey(id);
        modelMap.put("organization", organization);
        return "/manage/organization/update";
    }

    @ApiOperation(value = "修改组织")
    @RequiresPermissions("cms:organization:update")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    public String update(@PathVariable("id") int id, UpmsOrganization upmsOrganization, ModelMap modelMap) {
        int count = upmsOrganizationService.updateByPrimaryKeySelective(upmsOrganization);
        modelMap.put("count", count);
        modelMap.put("id", id);
        return "redirect:/manage/organization/list";
    }

}
