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.service.AdminSkillGroupService;
import org.linlinjava.jz.admin.util.SecurityHelper;
import org.linlinjava.jz.admin.vo.SkillGroupVo;
import org.linlinjava.jz.core.util.JacksonUtil;
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.JzSkillGroup;
import org.linlinjava.jz.db.domain.SysUser;
import org.linlinjava.jz.db.service.JzSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 坐席技能组
 * 2020-12-14
 */

@Api(value = "Admin-Skill-Group-Controller",description = "技能组维护")
@RestController
@RequestMapping("/admin/skill")
@Validated
public class AdminSkillGroupController {

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

    @Autowired
    private AdminSkillGroupService skillGroupService;

    @Autowired
    private JzSysUserService userService;

    @RequiresPermissions("admin:skill:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "技能组"}, button = "查询")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {

        List<JzSkillGroup> channelList = skillGroupService.querySelective( page, limit, sort, order);
        return ResponseUtil.okList(channelList);
    }

    @RequiresPermissions("admin:skill:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "技能组"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody JzSkillGroup group) {
        group.setCreateUser(SecurityHelper.getAdminId());
        group.setDeleted(false);
        skillGroupService.add(group);
        return ResponseUtil.ok(group);
    }

    @RequiresPermissions("admin:skill:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "技能组"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody JzSkillGroup source) {
        skillGroupService.update(source);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:skill:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "技能组"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody JzSkillGroup group) {
        group.setDeleted(true);
        skillGroupService.update(group);
        return ResponseUtil.ok();
    }

    @RequiresPermissions("admin:skill:binding")
    @RequiresPermissionsDesc(menu = {"系统管理", "技能组"}, button = "分组")
    @PostMapping("/binding")
    public Object binding(@RequestBody String body) {
        Integer groupId = JacksonUtil.parseInteger(body, "groupId");
        List<String> admins = JacksonUtil.parseStringList(body, "admins");
        if (groupId == null || admins == null) {
            return ResponseUtil.badArgument();
        }
        skillGroupService.binding(admins, groupId);
        return ResponseUtil.ok();
    }

    /**
     * 已分配的成员
     *
     */
    @RequiresPermissions("admin:skill:admin:get")
    @RequiresPermissionsDesc(menu = {"系统管理", "技能组"}, button = "成员详情")
    @GetMapping("/users")
    public Object getUsers(Integer groupId) {

        List<SkillGroupVo>  allUsers= skillGroupService.getSysUser();
        Set<String> assignedPermissions = skillGroupService.getSkillGroupUser(groupId);

        Map<String, Object> data = new HashMap<>();
        data.put("allUsers", allUsers);
        data.put("assignedUsers", assignedPermissions);
        return ResponseUtil.ok(data);
    }


}
