package com.yihu.jw.base.endpoint.role;

import com.alibaba.fastjson.JSONArray;
import com.yihu.jw.base.service.role.RoleService;
import com.yihu.jw.entity.base.role.RoleDO;
import com.yihu.jw.entity.base.role.RoleMenuDO;
import com.yihu.jw.entity.base.wx.WxWechatDO;
import com.yihu.jw.entity.base.wx.WxWechatSaasDO;
import com.yihu.jw.restmodel.base.role.RoleVO;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ListEnvelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
import com.yihu.jw.restmodel.web.PageEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.base.BaseRequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Endpoint - 角色
 * Created by progr1mmer on 2018/8/16.
 */
@RestController
@RequestMapping(value = BaseRequestMapping.Role.PREFIX)
@Api(value = "角色管理", description = "角色管理服务接口", tags = {"wlyy基础服务 - 角色管理服务接口"})
public class RoleEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private RoleService roleService;

    @PostMapping(value = BaseRequestMapping.Module.CREATE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ApiOperation(value = "创建")
    public Envelop create (
            @ApiParam(name = "json_data", value = "Json数据", required = true)
            @RequestBody String jsonData) throws Exception {
        RoleDO roleDO = toEntity(jsonData, RoleDO.class);
        if(roleDO.getName().length() > 50){
            return failed("角色名称必填，但是请不要超过50个字符！");
        }
        if(roleDO.getId()!=null){
            roleDO.setUpdateUser(getUID());
            roleDO.setUpdateUserName(getUNAME());
        }else{
            roleDO.setUpdateUser(getUID());
            roleDO.setUpdateUserName(getUNAME());
            roleDO.setCreateUser(getUID());
            roleDO.setCreateUserName(getUNAME());
        }
        roleDO = roleService.save(roleDO);
        return success(roleDO, RoleVO.class);
    }

    public RoleService getRoleService() {
        return roleService;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    @PostMapping(value = BaseRequestMapping.Module.DELETE)
    @ApiOperation(value = "删除")
    public Envelop delete(
            @ApiParam(name = "ids", value = "id串，中间用,分隔", required = true)
            @RequestParam(value = "ids") String ids) {
        roleService.delete(ids.split(","));
        return success("删除成功");

    }

    @PostMapping(value = BaseRequestMapping.Module.UPDATE)
    @ApiOperation(value = "更新")
    public Envelop update (
            @ApiParam(name = "jsonData", value = "实体json", required = true)
            @RequestParam(value = "jsonData") String jsonData) throws Exception {
        RoleDO roleDO = toEntity(jsonData, RoleDO.class);
        if (null == roleDO.getId()) {
            return failed("ID不能为空", Envelop.class);
        }else{
            roleDO.setUpdateUser(getUID());
            roleDO.setUpdateUserName(getUNAME());
        }
        roleDO = roleService.save(roleDO);
        return success(roleDO);
    }

    @GetMapping(value = BaseRequestMapping.Module.PAGE)
    @ApiOperation(value = "获取分页")
    public PageEnvelop<RoleVO> page (
            @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters,
            @ApiParam(name = "sorts", value = "排序，规则参见说明文档")
            @RequestParam(value = "sorts", required = false) String sorts,
            @ApiParam(name = "page", value = "分页大小", required = true, defaultValue = "1")
            @RequestParam(value = "page") int page,
            @ApiParam(name = "size", value = "页码", required = true, defaultValue = "15")
            @RequestParam(value = "size") int size) throws Exception {
        List<RoleDO> roleDOS = roleService.search(fields, filters, "-status,-updateTime", page, size);
        int count = (int)roleService.getCount(filters);
        return success(roleDOS, count, page, size, RoleVO.class);
    }

    @GetMapping(value = BaseRequestMapping.Module.LIST)
    @ApiOperation(value = "获取列表")
    public ListEnvelop<RoleVO> list (
            @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
            @RequestParam(value = "fields", required = false) String fields,
            @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
            @RequestParam(value = "filters", required = false) String filters) throws Exception {
        List<RoleDO> roleDOS = roleService.search(fields, filters, "-status,-createTime");
        return success(roleDOS, RoleVO.class);
    }

    @PostMapping(value = BaseRequestMapping.Role.saveRole)
    @ApiOperation(value = "保存角色与菜单")
    public Envelop saveRole(@ApiParam(name = "roleJson", value = "角色json")
                            @RequestParam(value = "roleJson", required = false)String roleJson,
                            @ApiParam(name = "roleMenuJosn", value = "角色菜单json")
                            @RequestParam(value = "roleMenuJosn", required = false)String roleMenuJosn)throws Exception {
        RoleDO roleDO = toEntity(roleJson, RoleDO.class);
        List<RoleMenuDO> list = (List<RoleMenuDO>) JSONArray.parseArray(roleMenuJosn, RoleMenuDO.class);

        return roleService.saveRole(roleDO,list);
    }

    @GetMapping(value = BaseRequestMapping.Role.findRoleNameExist)
    @ApiOperation(value = "判断角色是否存在")
    public ObjEnvelop<Boolean> findRoleNameExist(@ApiParam(name = "name", value = "角色名称")
                                                 @RequestParam(value = "name", required = true)String name,
                                                 @ApiParam(name = "saasId", value = "租户id")
                                                 @RequestParam(value = "saasId", required = false)String saasId) {

        return success(BaseRequestMapping.Role.api_success,roleService.findRoleNameExist(name,saasId));
    }
}
