package org.linlinjava.jz.admin.web;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.linlinjava.jz.admin.annotation.RequiresPermissionsDesc;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.core.util.ResponseUtil;
import org.linlinjava.jz.core.validator.Order;
import org.linlinjava.jz.core.validator.Sort;
import org.linlinjava.jz.db.domain.JzDepartment;
import org.linlinjava.jz.db.domain.JzShop;
import org.linlinjava.jz.db.service.JzDepartmentService;
import org.linlinjava.jz.db.service.JzShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "Admin-Department-Controller",description = "公司部门维护")
@RestController
@RequestMapping("/admin/department")
@Validated
public class AdminDepartmentController {

    private final Log logger = LogFactory.getLog(AdminDmTicketConsultTypeController.class);

    @Autowired
    private JzDepartmentService departmentService;


    @RequiresPermissions("admin:department:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "部门配置"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,@RequestParam(defaultValue = "0") Integer shopCity,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<JzDepartment> shopList = departmentService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(shopList);
    }

    @RequiresPermissions("admin:department:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "部门配置"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzDepartment department) {
        department.setCreateUser(SecurityHelper.getAdminId());
        department.setUpdateUser(SecurityHelper.getAdminId());
        department.setDeleted(false);
        departmentService.add(department);
        return ResponseUtil.ok(department);
    }

    @RequiresPermissions("admin:department:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "部门配置"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzDepartment department) {
        department.setUpdateUser(SecurityHelper.getAdminId());
        departmentService.updateById(department);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:department:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "部门配置"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzDepartment department) {
        department.setUpdateUser(SecurityHelper.getAdminId());
        department.setDeleted(true);
        departmentService.updateById(department);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:department:all")
    @RequiresPermissionsDesc(menu = {"系统管理", "部门配置"}, button = "查询所有")
    @GetMapping("/all")
    public Object all() {
        return ResponseUtil.ok(departmentService.queryAll());
    }



}
