package com.huimv.beeboxs.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.beeboxs.common.token.TokenSign;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.entity.AccountGroup;
import com.huimv.beeboxs.entity.SysAccountMultilevel;
import com.huimv.beeboxs.service.IAccountGroupService;
import com.huimv.beeboxs.service.ISysAccountMultilevelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author astupidcoder
 * @since 2021-10-14
 */
@CrossOrigin
@RestController
@RequestMapping("/admin/accountMultilevel")
public class SysAccountMultilevelController {
    @Autowired
    private ISysAccountMultilevelService accountMultilevelService;

    @Autowired
    private IAccountGroupService accountGroupService;



    @PostMapping("/list")
    public Result list(@RequestBody Map map , HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        int pageNum = (Integer)map.get("pageNum");
        int pageSize = (Integer)map.get("pageSize");
        String searchStr = (String)map.get("searchStr");

        return accountMultilevelService.findUserAccount(searchStr,pageNum,pageSize,userId);

    }

    @PostMapping("/listSubordinate")
    public Result listSubordinate(@RequestBody Map map , HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        int pageNum = (Integer)map.get("pageNum");
        int pageSize = (Integer)map.get("pageSize");
        String searchStr = (String)map.get("searchStr");

        return accountMultilevelService.listSubordinate(searchStr,pageNum,pageSize,userId);

    }

    @PostMapping("/add")
    public Result add(@RequestBody SysAccountMultilevel sysAccountMultilevel , HttpServletRequest request){

        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return accountMultilevelService.add(sysAccountMultilevel,userId);

    }

    @PostMapping("/update")
    public Result update(@RequestBody SysAccountMultilevel sysAccountMultilevel){
        accountMultilevelService.updateById(sysAccountMultilevel);

        return new Result(10000,"修改成功",true);

    }

    @GetMapping("/remove")
    public Result remove(@RequestParam(name ="userIds") List<Integer> userIds){

        accountGroupService.remove(new QueryWrapper<AccountGroup>().in("account_id",userIds));
        accountMultilevelService.removeByIds(userIds);

        return new Result(10000,"删除成功",true);

    }

    @GetMapping("/setAccountStatus")
    public Result setAccountStatus(@RequestParam(name = "ids") List<Integer> ids, @RequestParam(name = "status") Integer status){
        /* 设置账号状态 */
        return accountMultilevelService.setAccountStatus(ids,status);
    }

    @GetMapping("/findOne")
    public Result findOne(Integer id){
        return new Result(ResultCode.SUCCESS, accountMultilevelService.getById(id));
    }


    @GetMapping("/getLastFarmId")
    public Result getLastFarmId(HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return  accountMultilevelService.getLastFarmId(userId);
    }


}
