package cn.pluss.platform.controller.home;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.enums.UserRoleEnum;
import cn.pluss.platform.userApp.UserAppService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 *
 *用户相关controller
 *@author: bzg
 *@time: 2022/2/16 10:14
 */
@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Resource
    private UserAppService userAppService;

    private final String ERROR_PATH = "merchant/errorInfo";

    private final String ERROR_NAME = "error";

    @GetMapping(value = "/agent/children")
    public ModelAndView bstAgentChildrenList(ModelAndView view,String token,HttpServletRequest request){
        view.setViewName(ERROR_PATH);
        try {
            userAppService.checkWapToken(token,request,UserRoleEnum.ZY_BST);
            view.setViewName("user/th_agent_list");
            view.addObject("token",token);
            return view;
        }catch (Exception e){
            view.addObject(ERROR_NAME,e.getMessage());
            return view;
        }
    }

    @GetMapping(value = "/agent/open")
    public ModelAndView bstOpenAgent(ModelAndView view,String token,HttpServletRequest request){
        view.setViewName(ERROR_PATH);
        try {
            UserApp userApp = userAppService.checkWapToken(token,request,UserRoleEnum.ZY_BST);
            view.setViewName("user/th_agent");
            view.addObject("uid",userApp.getUserId());
            view.addObject("token",token);
            return view;
        }catch (Exception e){
            view.addObject(ERROR_NAME,e.getMessage());
            return view;
        }
    }

    @PostMapping(value = "/agent/save")
    @ResponseBody
    public Result<Object> bstSaveAgent(@RequestParam Map<String,Object> params){
        try {
            userAppService.bstSaveAgent(params);
            return ResultGenerator.genSuccessResult("注册成功");
        }catch (Exception e){
            return ResultGenerator.genFailResult(e.getMessage(),null);
        }
    }

    @PostMapping(value = "/agent/page")
    @ResponseBody
    public Result<Object> page(Page<UserApp> page,
                               String token, HttpServletRequest request){
        try {
            UserApp userApp = userAppService.checkWapToken(token,request,UserRoleEnum.ZY_BST);
            QueryWrapper<UserApp> queryWrapper = new QueryWrapper<UserApp>().eq("parentId",userApp.getUserId())
                    .eq("userType","agent");
            page = userAppService.page(page, queryWrapper);
            return ResultGenerator.genSuccessResult("获取成功",page);
        }catch (Exception e){
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }
}
