package com.zhangmeng.admin.manager.controller;

import com.zhangmeng.admin.manager.service.PermissionService;
import com.zhangmeng.api.service.admin_manager.PermissionControllerApi;
import com.zhangmeng.model.base.baseController.BaseController;
import com.zhangmeng.model.base.baseUtil.CommonUtil;
import com.zhangmeng.model.entity.Permission;
import com.zhangmeng.model.vo.Result;
import com.zhangmeng.model.vo.ResultTree;
import com.zhangmeng.model.vo.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Condition;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/permission")
public class PermissionController extends BaseController implements PermissionControllerApi {

    @Autowired
    private PermissionService permissionService;

    @Override
    @GetMapping("/list")
    public Result list(Integer pageNum, Integer pageSize, String title) {
        Condition condition = new Condition(Permission.class);
        Example.Criteria criteria = condition.createCriteria();
        if (CommonUtil.isNotNull(title)){
            criteria.andLike("title","%" + title + "%");
        }
        List<Permission> permissionList = this.permissionService.findByCondition(condition);
        return new Result(true, 0, "查询成功", permissionList);
    }

    @Override
    @GetMapping("/selectParent")
    public ResultTree selectParent(String parentId) {
        Condition condition = new Condition(Permission.class);
        Example.Criteria criteria = condition.createCriteria();
        if (parentId != null && !parentId.equals("")) {
            criteria.andEqualTo("parent_id", Long.parseLong(parentId));
        }
        List<Permission> list = this.permissionService.findByCondition(condition);
        Permission basePower = new Permission();
        basePower.setTitle("顶级权限");
        basePower.setId(0L);
        basePower.setParent_id(-1L);
        list.add(basePower);
        return dataTree(list);
    }

    @Override
    @PostMapping("/save")
    public Result save(@RequestParam @RequestBody Map<String, Object> map) {
        Permission permission = null;
        boolean flag = false;
        String message = null;
        String id = CommonUtil.map_get_value(map, "id");
        if (id == null || id.equals("")) {
            permission = new Permission();
            flag = true;
        } else {
            permission = this.permissionService.findById(Long.parseLong(id));
        }
        String parentId = CommonUtil.map_get_value(map, "parentId");
        if (CommonUtil.isNotNull(parentId)) {
            permission.setParent_id(Long.parseLong(parentId));
        }

        String title = CommonUtil.map_get_value(map, "title");
        if (CommonUtil.isNotNull(title)) {
            permission.setTitle(title);
        }

        String code = CommonUtil.map_get_value(map, "code");

        if (CommonUtil.isNotNull(code)) {
            permission.setCode(code);
        }

        String type = CommonUtil.map_get_value(map, "type");

        if (CommonUtil.isNotNull(type)) {
            Permission.Type permission_type = null;
            if (type.equals("CATALOG")) {
                permission_type = Permission.Type.CATALOG;
            }
            if (type.equals("MENU")) {
                permission_type = Permission.Type.MENU;
            }
            if (type.equals("BUTTON")) {
                permission_type = Permission.Type.BUTTON;
            }
            permission.setType(permission_type);
        }

        String href = CommonUtil.map_get_value(map, "href");
        if (CommonUtil.isNotNull(href)) {
            permission.setHref(href);
        }

        String openType = CommonUtil.map_get_value(map, "openType");
        if (CommonUtil.isNotNull(openType)) {
            permission.setOpenType(openType);
        }
        String icon = CommonUtil.map_get_value(map, "icon");
        if (CommonUtil.isNotNull(icon)) {
            permission.setIcon(icon);
        }

        String sort = CommonUtil.map_get_value(map, "sort");
        if (CommonUtil.isNotNull(sort)) {
            //判断是否为数字
            if (CommonUtil.isNumeric_ASCII(sort)) {
                permission.setSort(Integer.parseInt(sort));
            }
        }
        if (flag) {
            message = "保存成功";
            this.permissionService.save(permission);
        } else {
            message = "修改成功";
            this.permissionService.update(permission);
        }
        return new Result(true, StatusCode.OK, message);
    }

    ;

    @Override
    @PostMapping("/delete")
    public Result delete(String permissionId) {
        if (CommonUtil.isNotNull(permissionId)) {
            this.permissionService.deleteById(Long.parseLong(permissionId));
        }
        return new Result(true, StatusCode.OK, "删除成功");
    }

    @Override
    @PostMapping("/batchRemove")
    public Result batchRemove(String ids) {
        String id_s = CommonUtil.firstLastComma(ids);
        String[] strings = id_s.split(",");
        if (strings.length > 0) {
            for (String id : strings) {
                this.permissionService.deleteById(Long.parseLong(id));
            }
        }
        return new Result(true, 0, "删除成功");
    }

    @Override
    @GetMapping("/findByUserId")
    public List<Permission> findByUserId(Long id) {
        return this.permissionService.findByUserId(id);
    }
}
