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.User;
import com.qxgmat.data.dao.entity.UserClass;
import com.qxgmat.data.dao.entity.UserPay;
import com.qxgmat.data.dao.entity.UserService;
import com.qxgmat.dto.admin.extend.UserClassExtendDto;
import com.qxgmat.dto.admin.extend.UserServiceExtendDto;
import com.qxgmat.dto.admin.response.UserListDto;
import com.qxgmat.service.UsersService;
import com.qxgmat.service.inline.ManagerLogService;
import com.qxgmat.service.inline.UserClassService;
import com.qxgmat.service.inline.UserPayService;
import com.qxgmat.service.inline.UserServiceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

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

@RestController("AdminUserController")
@RequestMapping("/admin/user")
@Api(tags = "用户接口", description = "用户相关操作", produces = MediaType.APPLICATION_JSON_VALUE)
public class UserController {
    @Autowired
    private ManagerLogService managerLogService;

    @Autowired
    private UsersService usersService;

    @Autowired
    private UserServiceService userServiceService;

    @Autowired
    private UserClassService userClassService;

    @Autowired
    private UserPayService userPayService;

//    @RequestMapping(value = "/add", method = RequestMethod.POST)
//    @ApiOperation(value = "添加用户信息", httpMethod = "POST")
//    public Response<User> add(@RequestBody @Validated UserDto dto, HttpServletRequest request) {
//        User entity = Transform.dtoToEntity(dto);
//        entity = userService.edit(entity);
//        managerLogService.log(request);
//        return ResponseHelp.success(Transform.convert(entity, UserDto.class));
//    }
//    @RequestMapping(value = "/edit", method = RequestMethod.PUT)
//    @ApiOperation(value = "修改用户信息", httpMethod = "PUT")
//    public Response<Boolean> edit(@RequestBody @Validated UserDto dto, HttpServletRequest request) {
//        User entity = Transform.dtoToEntity(dto);
//        entity = userService.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(userService.delete(id));
//    }
    @RequestMapping(value = "/token", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户token", httpMethod = "GET")
    public Response<String> token(@RequestParam int id, HttpSession session) {
        User entity = usersService.get(id);
        String token = usersService.getTokenByUser(entity);

        return ResponseHelp.success(token);
    }

    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ApiOperation(value = "获取用户", httpMethod = "GET")
    public Response<User> detail(@RequestParam int id, HttpSession session) {
        User entity = usersService.get(id);

        return ResponseHelp.success(Transform.convert(entity, User.class));
    }
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "用户列表", httpMethod = "GET")
    public Response<PageMessage<UserListDto>> list(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false, defaultValue = "") String keyword,
            @RequestParam(required = false) Boolean real,
            @RequestParam(required = false) String order,
            @RequestParam(required = false, defaultValue = "desc") String direction,
            HttpSession session) {
        Page<User> p = usersService.select(page, size, keyword, real, order, DirectionStatus.ValueOf(direction));
        List<UserListDto> pr = Transform.convert(p, UserListDto.class);

        Collection userIds = Transform.getIds(p, User.class, "id");
        // 绑定用户服务
        Map<Object, Collection<UserService>> serviceByUser = userServiceService.mapByUser(userIds);
        Transform.combine(pr, serviceByUser, UserListDto.class, "id", "services", UserServiceExtendDto.class);

        // 绑定用户课程
        Map<Object, Collection<UserClass>> classByUser = userClassService.mapByUser(userIds);
        Transform.combine(pr, classByUser, UserListDto.class, "id", "classes", UserClassExtendDto.class);

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

    @RequestMapping(value = "/pay/list", method = RequestMethod.GET)
    @ApiOperation(value = "用户消费列表", httpMethod = "GET")
    public Response<PageMessage<UserListDto>> listPay(
            @RequestParam(required = false, defaultValue = "1") int page,
            @RequestParam(required = false, defaultValue = "100") int size,
            @RequestParam(required = false) Integer userId,
            @RequestParam(required = false) String order,
            @RequestParam(required = false, defaultValue = "desc") String direction,
            HttpSession session) {
        Page<UserPay> p = userPayService.select(page, size);
        List<UserListDto> pr = Transform.convert(p, UserListDto.class);

        Collection userIds = Transform.getIds(p, User.class, "id");
        // 绑定用户服务
        Map<Object, Collection<UserService>> serviceByUser = userServiceService.mapByUser(userIds);
        Transform.combine(pr, serviceByUser, UserListDto.class, "id", "services", UserServiceExtendDto.class);

        // 绑定用户课程
        Map<Object, Collection<UserClass>> classByUser = userClassService.mapByUser(userIds);
        Transform.combine(pr, classByUser, UserListDto.class, "id", "classes", UserClassExtendDto.class);

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