package org.linlinjava.jz.admin.web;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.SecurityUtils;
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.JzDmLabel;
import org.linlinjava.jz.db.service.JzDmLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/dmLabel")
@Validated
public class AdminDmLabelController {
    private final Log logger = LogFactory.getLog(AdminDmLabelController.class);

    @Autowired
    private JzDmLabelService labelSettingService;

    @RequiresPermissions("admin:dmLabel:list")
    @RequiresPermissionsDesc(menu = {"会员营销", "标签管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        List<JzDmLabel> labelSettings = labelSettingService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(labelSettings);
    }

    @RequiresPermissions("admin:dmLabel:create")
    @RequiresPermissionsDesc(menu = {"会员营销", "标签管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzDmLabel labelSetting) {
        labelSetting.setCreateUser(SecurityHelper.getAdminId());
        labelSetting.setUpdateUser(SecurityHelper.getAdminId());
        labelSetting.setDeleted(false);
        labelSettingService.add(labelSetting);
        return ResponseUtil.ok(labelSetting);
    }

    @RequiresPermissions("admin:dmLabel:update")
    @RequiresPermissionsDesc(menu = {"会员营销", "标签管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzDmLabel labelSetting) {
        labelSetting.setUpdateUser(SecurityHelper.getAdminId());
        labelSettingService.add(labelSetting);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:dmLabel:delete")
    @RequiresPermissionsDesc(menu = {"会员营销", "标签管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzDmLabel labelSetting) {
        labelSetting.setUpdateUser(SecurityHelper.getAdminId());
        labelSetting.setDeleted(true);
        labelSettingService.update(labelSetting);
        return ResponseUtil.ok(labelSetting);
    }

    @RequiresPermissions("admin:dmLabel:listAll")
    @RequiresPermissionsDesc(menu = {"会员营销", "标签管理"}, button = "所有标签")
    @GetMapping("/listAll")
    public Object all() {
        return ResponseUtil.ok(labelSettingService.all());
    }

}
