package com.qxgmat.controller.admin;

import com.github.pagehelper.Page;
import com.nuliji.tools.PageMessage;
import com.nuliji.tools.Response;
import com.nuliji.tools.ResponseHelp;
import com.nuliji.tools.Transform;
import com.qxgmat.data.constants.enums.status.DirectionStatus;
import com.qxgmat.data.dao.entity.Manager;
import com.qxgmat.data.dao.entity.ManagerRole;
import com.qxgmat.data.relation.entity.ManagerRelation;
import com.qxgmat.dto.admin.request.ManagerDto;
import com.qxgmat.dto.admin.request.ManagerRoleDto;
import com.qxgmat.service.ManagerService;
import com.qxgmat.service.inline.ManagerLogService;
import com.qxgmat.service.inline.ManagerRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.List;

@RestController("AdminManagerController")
@RequestMapping("/admin/manager")
public class ManagerController {

    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private ManagerService managerService;

    @Autowired
    private ManagerRoleService managerRoleService;

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加管理账号", httpMethod = "POST")
    public Response<Manager> add(@RequestBody @Validated ManagerDto dto, HttpServletRequest request) {
        Manager entity = Transform.dtoToEntity(dto);
        entity = managerService.add(entity);
        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }
    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改管理账号", httpMethod = "PUT")
    public Response<Boolean> edit(@RequestBody @Validated ManagerDto dto, HttpServletRequest request) {
        Manager entity = Transform.dtoToEntity(dto);
        entity = managerService.edit(entity);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除管理账号", httpMethod = "DELETE")
    public Response<Boolean> delete(@RequestParam int id, HttpServletRequest request) {
        managerLogService.log(request);
        return ResponseHelp.success(managerService.delete(id));
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取管理账号", httpMethod = "GET")
    public Response<ManagerRelation> detail(@RequestParam int id, HttpSession session) {
        Manager entity = managerService.get(id);
        ManagerRelation relation = Transform.convert(entity, ManagerRelation.class);
        relation.setRole(managerRoleService.get(relation.getRoleId()));
        return ResponseHelp.success(relation);
    }
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "管理账号列表", httpMethod = "GET")
    public Response<PageMessage<ManagerRelation>> list(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false, defaultValue = "id") String order,
            @RequestParam(required = false, defaultValue = "desc") String direction,
            HttpSession session) {
        Page<Manager> p = managerService.select(page, size, order, DirectionStatus.ValueOf(direction));
        List<ManagerRelation> pr = Transform.convert(p, ManagerRelation.class);

        // 绑定角色
        Collection ids = Transform.getIds(p, Manager.class, "roleId");
        List<ManagerRole> roleList = managerRoleService.select(ids);
        Transform.combine(pr, roleList, ManagerRelation.class, "roleId", "role", ManagerRole.class, "id");

        return ResponseHelp.success(pr, page, size, p.getTotal());
    }

    @RequestMapping(value = "/role/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加管理角色", httpMethod = "POST")
    public Response<ManagerRole> addRole(@RequestBody @Validated ManagerRoleDto dto, HttpServletRequest request) {
        ManagerRole entity = Transform.dtoToEntity(dto);
        entity = managerRoleService.add(entity);
        managerLogService.log(request);
        return ResponseHelp.success(entity);
    }
    @RequestMapping(value = "/role/edit", method = RequestMethod.PUT)
    @ApiOperation(value = "修改管理角色", httpMethod = "PUT")
    public Response<Boolean> editRole(@RequestBody @Validated ManagerRoleDto dto, HttpServletRequest request) {
        ManagerRole entity = Transform.dtoToEntity(dto);
        entity = managerRoleService.edit(entity);
        managerLogService.log(request);
        return ResponseHelp.success(true);
    }

    @RequestMapping(value = "/role/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除管理角色", httpMethod = "DELETE")
    public Response<Boolean> deleteRole(@RequestParam int id, HttpServletRequest request) {
        managerLogService.log(request);
        return ResponseHelp.success(managerRoleService.delete(id));
    }

    @RequestMapping(value = "/role/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取管理角色", httpMethod = "GET")
    public Response<ManagerRole> detailRole(@RequestParam int id, HttpSession session) {
        ManagerRole entity = managerRoleService.get(id);
        return ResponseHelp.success(entity);
    }
    @RequestMapping(value = "/role/list", method = RequestMethod.GET)
    @ApiOperation(value = "管理角色列表", httpMethod = "GET")
    public Response<PageMessage<ManagerRole>> listRole(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false, defaultValue = "id") String order,
            @RequestParam(required = false, defaultValue = "desc") String direction,
            HttpSession session) {
        Page<ManagerRole> p = managerRoleService.select(page, size, order, DirectionStatus.ValueOf(direction));
        return ResponseHelp.success(p, page, size, p.getTotal());
    }
}
