package org.linlinjava.jz.admin.web;


import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.JzDmLabelProfile;
import org.linlinjava.jz.db.service.JzDmLabelProfileService;
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/labelProfile")
@Validated
public class AdminDmLabelProfileController {
    private final Log logger = LogFactory.getLog(AdminDmLabelProfileController.class);

    @Autowired
    private JzDmLabelProfileService labelProfileService;

    @RequiresPermissions("admin:labelProfile: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<JzDmLabelProfile> profiles = labelProfileService.querySelective(name, page, limit, sort, order);
        return ResponseUtil.okList(profiles);
    }

    @RequiresPermissions("admin:labelProfile:create")
    @RequiresPermissionsDesc(menu = {"会员营销", "画像管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzDmLabelProfile profile) {
        profile.setCreateUser(SecurityHelper.getAdminId());
        profile.setUpdateUser(SecurityHelper.getAdminId());
        profile.setDeleted(false);
        labelProfileService.add(profile);
        return ResponseUtil.ok(profile);
    }

    @RequiresPermissions("admin:labelProfile:update")
    @RequiresPermissionsDesc(menu = {"会员营销", "画像管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzDmLabelProfile profile) {
        profile.setUpdateUser(SecurityHelper.getAdminId());
        labelProfileService.add(profile);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:labelProfile:delete")
    @RequiresPermissionsDesc(menu = {"会员营销", "画像管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzDmLabelProfile profile) {
        profile.setUpdateUser(SecurityHelper.getAdminId());
        profile.setDeleted(true);
        labelProfileService.update(profile);
        return ResponseUtil.ok(profile);
    }

}
