package com.huimv.eartag2.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.eartag2.admin.entity.AccountGroup;
import com.huimv.eartag2.admin.entity.SysAccountMultilevel;
import com.huimv.eartag2.admin.service.IAccountGroupService;
import com.huimv.eartag2.admin.service.ISysAccountMultilevelService;
import com.huimv.eartag2.common.common.token.TokenSign;
import com.huimv.eartag2.common.utils.Result;
import com.huimv.eartag2.common.utils.ResultCode;
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
 */
@RestController
@RequestMapping("/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){


        int pageNum = (Integer)map.get("pageNum");
        int pageSize = (Integer)map.get("pageSize");
        String searchStr = (String)map.get("searchStr");
//        String orgId  = (String)map.get("orgId");
//        if (StringUtils.isBlank(orgId)){
        String  orgId = TokenSign.getOrgId(request);
//        }

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

    }*/
    @PostMapping("/listSubordinate")
    public Result listSubordinate(@RequestBody Map map , HttpServletRequest request){


        int pageNum = (Integer)map.get("pageNum");
        int pageSize = (Integer)map.get("pageSize");
        String searchStr = (String)map.get("searchStr");
//        String orgId  = (String)map.get("orgId");
//        if (StringUtils.isBlank(orgId)){
        Integer  orgId = TokenSign.getMemberIdByJwtToken(request);
//        }

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

    }

    @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 , HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        sysAccountMultilevel.setId(userId);
        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(HttpServletRequest request){
        Integer id = TokenSign.getMemberIdByJwtToken(request);
        return new Result(ResultCode.SUCCESS, accountMultilevelService.getById(id));
    }


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

    /**
     * @description: 【查询】 根据账号查询所有牧场
     * @author: 鱼余
     * @param:
     **/
    @GetMapping("/listById")
    public Result listById(HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return accountMultilevelService.listById(userId);
    }

    /**
     * @description: 【修改】 账号绑定牧场
     * @author:
     * @param: paramsMap
     **/
    @PostMapping("/boundFarm")
    public Result boundFarm(@RequestBody Map<String,String> paramsMap){

        return accountMultilevelService.boundFarm(paramsMap);
    }

    //修改下属密码
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody SysAccountMultilevel sysAccountMultilevel){

        return new Result(ResultCode.SUCCESS,accountMultilevelService.updateById(sysAccountMultilevel));
    }
    //修改密码
    @PostMapping("/updateUserPassword")
    public Result updateUserPassword(@RequestBody Map<String,String> map , HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        return    accountMultilevelService.updateUserPassword(map,userId);
    }
}
