package com.huimv.admin.system.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.admin.system.entity.AccountGroup;
import com.huimv.admin.system.entity.BaseScreenCameraAccount;
import com.huimv.admin.system.entity.SysAccountMultilevel;
import com.huimv.admin.system.entity.dto.SysAccountMultilevelDto;
import com.huimv.admin.system.entity.dto.UpdateAccountDto;
import com.huimv.admin.system.service.IAccountGroupService;
import com.huimv.admin.system.service.IBaseScreenCameraAccountService;
import com.huimv.admin.system.service.ISysAccountMultilevelService;
import com.huimv.common.token.TokenSign;
import com.huimv.common.utils.Result;
import com.huimv.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;

    @Autowired
    private IBaseScreenCameraAccountService baseScreenCameraAccountService;


    @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("/addApp")
    public Result addApp(@RequestBody SysAccountMultilevelDto sysAccountMultilevel , HttpServletRequest request){

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

    }
    @PostMapping("/updateApp")
    public Result updateApp(@RequestBody SysAccountMultilevelDto sysAccountMultilevel){
        accountMultilevelService.updateApp(sysAccountMultilevel);

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

    }

    @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);
    }

    @GetMapping("/updateVideoId")
    public Result updateVideoId(@RequestParam(name = "cameraId") Integer cameraId ,
                                @RequestParam(name = "farmId") Integer farmId ,
                                HttpServletRequest request){
        Integer userId = TokenSign.getMemberIdByJwtToken(request);
        BaseScreenCameraAccount cameraAccount = baseScreenCameraAccountService.getOne(new QueryWrapper<BaseScreenCameraAccount>().eq("farm_id", farmId).eq("account_id",userId));

        if (ObjectUtil.isEmpty(cameraAccount)){
            BaseScreenCameraAccount cameraAccount1 = new BaseScreenCameraAccount();
            cameraAccount1.setAccountId(userId);
            cameraAccount1.setCameraId(cameraId);
            cameraAccount1.setFarmId(farmId);
            baseScreenCameraAccountService.save(cameraAccount1);
        }else {
            cameraAccount.setCameraId(cameraId);
            baseScreenCameraAccountService.updateById(cameraAccount);
        }


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

    //修改密码
    @PostMapping("/updatePassword")
    public Result updatePassword(@RequestBody UpdateAccountDto account){

        SysAccountMultilevel accountMultilevel = accountMultilevelService.getOne(new QueryWrapper<SysAccountMultilevel>().eq("account", account.getAccount()).eq("password", account.getPassword()));

        if (ObjectUtil.isEmpty(accountMultilevel)){
            return new Result(10001,"账号或密码有误，请检查",false);
        }
        if (accountMultilevel.getPassword().equals(account.getNewPassword())){
            return new Result(10001,"新密码与旧密码相同，请检查",false);
        }

        accountMultilevel.setPassword(account.getNewPassword());
        accountMultilevelService.updateById(accountMultilevel);
        return new Result(10000,"修改成功",true);
    }


}
