package com.fdkankan.platform.user.controller.manager;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fdkankan.common.controller.BaseController;
import com.fdkankan.common.response.ResultData;
import com.fdkankan.platform.user.entity.IncrementLog;
import com.fdkankan.platform.user.entity.Manager;
import com.fdkankan.platform.user.entity.ReceiverInfo;
import com.fdkankan.platform.user.request.RequestIncrement;
import com.fdkankan.platform.user.request.RequestManager;
import com.fdkankan.platform.user.request.RequestUser;
import com.fdkankan.platform.user.request.RequestUserIncrement;
import com.fdkankan.platform.user.service.*;
import com.fdkankan.platform.user.vo.ResponseUser;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 后台管理-用户接口
 */
@Log4j2
@RestController
@RequestMapping("/service/platform/manager/user")
public class ManagerUserController extends BaseController {

    @Autowired
    private IManagerService managerService;

    @Autowired
    private IUserService userService;
    @Autowired
    private IUserSceneService userSceneService;

    @Autowired
    private IUserIncrementService userIncrementService;
    @Autowired
    private IIncrementLogService incrementLogService;

    @Autowired
    private IReceiverInfoService receiverInfoService;

    /**
     * 查询用户列表
     * startDate        开始时间
     * endDate          结束时间
     * searchKey        关键词
     * pageNum          分页
     * pageSize         每页大小
     */
    @RequestMapping(value = {"/list"}, method = RequestMethod.POST)
    public Page<ResponseUser> list(@RequestBody RequestUser param) throws Exception {
        return userService.getPageByParam(param);
    }


    /**
     * 更新发言权限
     * userId         用户id
     * status         0-禁言, 1-正常
     */
    @RequestMapping(value = {"/updateStatus"}, method = RequestMethod.POST)
    public String updateStatus(@RequestBody RequestUser param) throws Exception {
        return userService.updateStatus(param.getUserId(), param.getStatus());
    }

    /**
     * 客户后台管理账号列表
     * @return
     */
    @RequestMapping(value = {"/managerList"}, method = RequestMethod.POST)
    public List<Manager> managerList() throws Exception {
        return managerService.managerList();
    }

    /**
     * 追溯后台管理修改密码
     * userName     手机号
     * password     密码
     */
    @RequestMapping(value = "/companyUpdatePassword", method = RequestMethod.POST)
    public String companyUpdatePassword(@RequestBody RequestManager param) throws Exception {
        return managerService.companyUpdatePassword(param,getToken());
    }

    /**
     * 新增管理后台用户
     * agentFrameworkId     所属架构id
     * managerRoleId        角色id
     * userName             用户账号（手机号）
     * nickname             用户姓名
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(@RequestBody RequestManager param) throws Exception{
        return  managerService.saveManager(param);
    }

    /**
     * 修改管理后台用户
     * id                   管理后台用户id
     * agentFrameworkId     所属架构id
     * managerRoleId        角色id
     * userName             用户账号（手机号）
     * nickname             用户姓名
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(@RequestBody RequestManager param) throws Exception{
        return  managerService.updateManager(param);
    }

    /**
     * 修改管理后台用户的密码
     * id               管理后台用户id
     * password         原来密码
     * newPassword      新密码
     * confirmPwd       二次确认密码
     */
    @RequestMapping(value = "/updatePassword", method = RequestMethod.POST)
    public String updatePassword(@RequestBody RequestManager param) throws Exception{
        return  managerService.updatePassword(param);
    }

    /**
     * 删除管理后台用户
     * id       管理后台用户id
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public String delete(@RequestBody RequestManager param) throws Exception{
        return  managerService.deleteManager(param);
    }

    /**
     * 重置管理后台用户密码
     * id       管理后台用户id
     */
    @RequestMapping(value = "/restPassword", method = RequestMethod.POST)
    public String restPassword(@RequestBody RequestManager param) throws Exception{
        return  managerService.restPassword(param);
    }

    /**
     * 查询用户列表--新接口
     * userName
     * nickname
     * agentFrameworkId
     * managerRoleId
     * @return
     */
    @RequestMapping(value = {"/listNew"}, method = RequestMethod.POST)
    public Page<Manager> listNew(@RequestBody RequestManager param) throws Exception {
        return  managerService.listNew(param);
    }

    /**
     * 可以为客户账号单独新增会员权益的，每次调用就增加1个权益数量，和增加10个下载次数
     * 会员权益 增加1个权益、10个下载
     */
    @RequestMapping(value = "/insertIncrementls", method = RequestMethod.POST)
    public String insertIncrementls(@RequestBody RequestUserIncrement param) throws Exception{
        return  userIncrementService.insertIncrementls(param,getToken());
    }

    /**
     * 查询用户列表   查询权益操作历史
     * @return
     */
    @RequestMapping(value = {"/incrementlist"}, method = RequestMethod.POST)
    public Page<IncrementLog> list(@RequestBody RequestIncrement param) throws Exception {
        return incrementLogService.getPageByStatusAndNum(param);
    }


    /**
     * 校验该场景有无下载资格
     * isTiles   是否下载tiles图
     * sceneNum  大场景序号
     * @return
     */
    @RequestMapping(value = "/checkDownload", method = RequestMethod.POST)
    public ResultData checkDownload(@RequestBody RequestUser param) throws Exception{
        return userSceneService.checkDownload(param);
    }

    /**
     * 校验该场景有无下载资格
     * isTiles   是否下载tiles图
     * sceneNum  大场景序号
     * @return
     */
    @RequestMapping(value = "/downloadScene", method = RequestMethod.POST)
    public Map<String, Object> downloadScene(@RequestBody RequestUser param) throws Exception{
        return userSceneService.downloadSceneAdmin(param, getToken());
    }

    /**
     * 获取下载进度
     * sceneNum     大场景序号
     * @return
     */
    @RequestMapping(value = "/downloadProcess", method = RequestMethod.POST)
    public JSONObject downloadProcess(@RequestBody RequestUser param) throws Exception{
        return userSceneService.getSceneDownloadProcessAdmin(param.getSceneNum());
    }

    /**
     * 获取用户默认地址
     */
    @RequestMapping(value = "/receiverinfo", method = RequestMethod.POST)
    public ReceiverInfo receiverinfo(@RequestBody RequestUser param) throws Exception{
        return receiverInfoService.findDefaultByUserId(param.getUserId());
    }
}
