package com.zfire.mall.manager.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zfire.mall.manager.annotation.ApiNotAuth;
import com.zfire.mall.manager.bean.ExcelData;
import com.zfire.mall.manager.bean.SVerification;
import com.zfire.mall.manager.bean.admin.AdminModuleTree;
import com.zfire.mall.manager.bean.admin.AdminUserBean;
import com.zfire.mall.manager.bean.admin.reqDto.AdminCompanyWechatReqBean;
import com.zfire.mall.manager.bean.admin.reqDto.AdminCompanyWechatUpdateReqDto;
import com.zfire.mall.manager.bean.admin.reqDto.AdminUserAddReqBean;
import com.zfire.mall.manager.bean.admin.respDto.AdminUserDto;
import com.zfire.mall.manager.exception.RemoteServiceException;
import com.zfire.mall.manager.helper.ResponseHelper;
import com.zfire.mall.manager.logic.admin.AdminUserLogic;
import com.zfire.mall.manager.logic.common.CommonLogic;
import com.zfire.mall.manager.utils.CommonUtils;
import com.zfire.mall.manager.utils.excel.ExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "系统用户管理", tags ={"后台用户相关API"} )
@RequestMapping(value = "/admin/user", produces = "application/json; charset=utf-8")
public class AdminUserController {

    @Autowired
    AdminUserLogic adminUserLogic;
    @Autowired
    AdminCompanyLogic adminCompanyLogic;
    @Autowired
    CommonLogic commonLogic;

    @ApiNotAuth
    @PostMapping("/login")
    @ApiOperation(value = "登录")
    public ResponseHelper<AdminUserBean> login(
            @ApiParam(value = "帐号,手机号/账号",required = true) @RequestParam(required = true) String userName,
            @ApiParam(value = "密码",required = false) @RequestParam(required = false) String password,
            @ApiParam(value = "验证码code",required = true) @RequestParam(required = true) String code,
            @ApiParam(value = "验证码值",required = true) @RequestParam(required = true) String codeValue,
            @ApiParam(value = "短信验证码",required = false) @RequestParam(required = false) String smsCode,
            @ApiParam(value = "登录类型 mobile=手机号登录,account=账号登录 默认account",required = false) @RequestParam(required = false) String loginType,
            HttpServletRequest request
    ) throws RemoteServiceException, ParseException {
        AdminUserBean adminUserBean = adminUserLogic.login(userName, password,code,codeValue,smsCode,loginType,request);
        return ResponseHelper.success(adminUserBean);
    }



    @ApiNotAuth
    @PostMapping("/changing")
    @ApiOperation(value = "切换账号权限")
    public ResponseHelper changing(
            @ApiParam(value = "切换账号权限 1 弘格权限 2 广州权限 3佛山权限",required = true) @RequestParam(required = true) Integer changeNum,

            HttpServletRequest request
    ) throws RemoteServiceException {
        adminUserLogic.changing(changeNum,request);
        return ResponseHelper.success();
    }


    @ApiNotAuth
    @PostMapping("/smsCode")
    @ApiOperation(value = "登录时发送验证码")
    public ResponseHelper smsCode(
            @ApiParam(value = "手机号",required = true) @RequestParam(required = true) String mobile,
            @ApiParam(value = "验证码code",required = true) @RequestParam(required = true) String code,
            @ApiParam(value = "验证码值",required = true) @RequestParam(required = true) String codeValue
    )  throws  Exception{
        adminUserLogic.smsCode(mobile,code,codeValue);
        return ResponseHelper.success();
    }



    @PostMapping("/password/update")
    @ApiOperation(value = "修改密码")
    public ResponseHelper<AdminUserBean> login(
            @ApiParam(value = "帐号",required = true) @RequestParam(required = true) String userName,
            @ApiParam(value = "密码",required = true) @RequestParam(required = true) String password,
            @ApiParam(value = "新密码",required = true) @RequestParam(required = true) String newPassword
    ) throws RemoteServiceException {
        adminUserLogic.updatePassword(userName, password,newPassword);
        return ResponseHelper.success();
    }

    @PostMapping("/status/update")
    @ApiOperation("修改帐号状态(冻结/正常)")
    public ResponseHelper updateStatus(
            @ApiParam(value = "帐号名称",required = true) @RequestParam(required = true) String adminUserId,
            @ApiParam(value = "true:正常  false:冻结",required = true) @RequestParam(required = true) Boolean status
    ) throws RemoteServiceException {
        adminUserLogic.updateStatus(adminUserId,status);
        return ResponseHelper.success();
    }


    @PostMapping("/addCompanyWechat")
    @ApiOperation("新增商户")
    public ResponseHelper addCompanyWechat(
            @ApiParam(value = "帐号",required = true) @RequestBody AdminCompanyWechatReqBean adminUserBean,
            HttpServletRequest request
    ) throws RemoteServiceException {
        adminCompanyLogic.addCompanyWechat(adminUserBean,request);
        return ResponseHelper.success();
    }

    @PostMapping("/add")
    @ApiOperation("新增帐号")
    public ResponseHelper add(
            @ApiParam(value = "帐号",required = true) @RequestBody AdminUserAddReqBean adminUserBean,
            HttpServletRequest request
    ) throws RemoteServiceException {
        adminUserLogic.add(adminUserBean,request);
        return ResponseHelper.success();
    }


    @PostMapping("/user/updateCompanyWechat")
    @ApiOperation(value = "修改商户信息")
    public ResponseHelper updateCompanyWechat(
            HttpServletRequest request,
            @ApiParam(value = "商户信息", required = true) @RequestBody AdminCompanyWechatUpdateReqDto updateReqDto
    ) throws RemoteServiceException {
        adminCompanyLogic.updateCompanyWechat(updateReqDto);
        return ResponseHelper.success();
    }

    @PostMapping("/user/update")
    @ApiOperation(value = "修改帐号")
    public ResponseHelper updateUser(
            HttpServletRequest request,
            @ApiParam(value = "帐号", required = true) @RequestBody AdminUserAddReqBean adminUserBean
    ) throws RemoteServiceException {
        adminUserLogic.update(request, adminUserBean);
        return ResponseHelper.success();
    }

    @PostMapping("/password/reset")
    @ApiOperation("重置密码")
    public ResponseHelper resetPwd(
            @ApiParam(value = "用户id",required = true) @RequestParam(required = true) String adminUserId,
            @ApiParam(value = "密码",required = true) @RequestParam(required = true) String password,
            HttpServletRequest request
    ) throws RemoteServiceException {
        adminUserLogic.resetPassword(adminUserId,password,request);
        return ResponseHelper.success();
    }

    @PostMapping("/password/firstSet")
    @ApiOperation("首次登录设置密码")
    public ResponseHelper firstSetPwd(@ApiParam(value = "密码",required = true) @RequestParam(required = true) String password){
        adminUserLogic.firstSetPwd(password);
        return ResponseHelper.success();
    }


    @GetMapping("/list")
    @ApiOperation(value = "帐号列表")
    public ResponseHelper<IPage<AdminUserDto>> list(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "角色id",required = false) @RequestParam(required = false) String roleId,
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "1=经销商 0=业务员",required = false) @RequestParam(required = false) Boolean isCustomer,
            @ApiParam(value = "true=主账号 false=子账号",required = false) @RequestParam(required = false) Boolean isMaster,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<AdminUserDto> page = adminUserLogic.list(adminWebsitId,roleId,customerId,isCustomer,isMaster,status,userName,pageNum, pageSize,request);
        return ResponseHelper.success(page);
    }


    @GetMapping("/listService")
    @ApiOperation(value = "业务员帐号列表")
    public ResponseHelper<IPage<AdminUserDto>> listService(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "角色id",required = false) @RequestParam(required = false) String roleId,
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "1=经销商 0=业务员",required = false) @RequestParam(required = false) Boolean isCustomer,
            @ApiParam(value = "true=主账号 false=子账号",required = false) @RequestParam(required = false) Boolean isMaster,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<AdminUserDto> page = adminUserLogic.listService(adminWebsitId,roleId,customerId,isCustomer,isMaster,status,userName,pageNum, pageSize,request);
        return ResponseHelper.success(page);
    }



    @GetMapping("/mch/list")
    @ApiOperation(value = "商户列表")
    public ResponseHelper<IPage<AdminUserDto>> list(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "会员帐号",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "用户昵称",required = false) @RequestParam(required = false) String nickName,
            @ApiParam(value = "联系电话",required = false) @RequestParam(required = false) String linkPhone,
            @ApiParam(value = "电子邮箱",required = false) @RequestParam(required = false) String email,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize
    ) throws RemoteServiceException {
        IPage<AdminUserDto> list = adminUserLogic.list(adminWebsitId,userName, nickName, linkPhone, email, status, pageNum, pageSize);
        return ResponseHelper.success(list);
    }

    @GetMapping("/mch/export")
    @ApiOperation(value = "商户列表导出")
    public void exportMch(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "会员帐号",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "用户昵称",required = false) @RequestParam(required = false) String nickName,
            @ApiParam(value = "联系电话",required = false) @RequestParam(required = false) String linkPhone,
            @ApiParam(value = "电子邮箱",required = false) @RequestParam(required = false) String email,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<AdminUserDto> page = adminUserLogic.list(adminWebsitId,userName,nickName,linkPhone,email,status,1, -1);
        ExcelData excelData = adminUserLogic.exportMch(page.getRecords());
        ExcelUtils.exportExcel(request,response,"mch.xlsx",excelData);
    }


    @GetMapping("/module/list")
    @ApiOperation(value = "功能菜单权限列表")
    public ResponseHelper<List<AdminModuleTree>> moduleList(
            @ApiParam(value = "用户id",required = true) @RequestParam(required = true) String adminUserId,
            @ApiParam(value = "menu=菜单 index=首页菜单",required = false) @RequestParam(required = false) String flag
    ) throws RemoteServiceException {
        List<AdminModuleTree> adminModuleTrees = adminUserLogic.queryAdminModule(adminUserId,flag);
        return ResponseHelper.success(adminModuleTrees);
    }

    @GetMapping("/module/id/checked")
    @ApiOperation("查询选中的功能模块ids")
    public ResponseHelper<List<String>> queryModuleIdChecked(
            @ApiParam(value = "角色id",required = true) @RequestParam(required = true) String adminRoleId
    ) throws RemoteServiceException {
        List<String> moduleIds = adminUserLogic.queryModuleIdChecked(adminRoleId);
        return ResponseHelper.success(moduleIds);
    }

    @GetMapping("/module/all")
    @ApiOperation(value = "全部功能菜单权限列表")
    public ResponseHelper<List<AdminModuleTree>> allModuleList(
            @ApiParam(value = "角色",required = true) @RequestParam(required = true) String adminRoleId,
            HttpServletRequest request
    ) throws RemoteServiceException {
        List<AdminModuleTree> adminModuleTrees = adminUserLogic.queryAllAdminModuleTree(adminRoleId,request);
        return ResponseHelper.success(adminModuleTrees);
    }

    @PostMapping("/module/grant")
    @ApiOperation(value = "授权动能菜单权限")
    public ResponseHelper grant(
            @ApiParam(value = "角色id",required = true) @RequestParam(required = true) String adminRoleId,
            @ApiParam(value = "功能模块ids",required = true) @RequestParam(required = true) List<String> adminModuleIds
    ) throws RemoteServiceException {
        adminUserLogic.grantModules(adminRoleId,adminModuleIds);
        return ResponseHelper.success();
    }

    @GetMapping("/detail")
    @ApiOperation(value = "详情")
    public ResponseHelper<AdminUserBean> detail(
            @ApiParam(value = "帐号id",required = true) @RequestParam(required = true) String adminUserId
    ) throws RemoteServiceException {
        AdminUserBean detail = adminUserLogic.detail(adminUserId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/logout")
    @ApiOperation("退出登录")
    public ResponseHelper logout(HttpServletRequest request){
        adminUserLogic.logout(request);
        return ResponseHelper.success();
    }

    @ApiNotAuth
    @GetMapping("/imageVerification")
    @ApiOperation(value = "获取图片验证码")
    public ResponseHelper<SVerification> defaultKaptcha()
            throws Exception {
        SVerification sVerification = adminUserLogic.defaultKaptcha();
        return ResponseHelper.success(sVerification);
    }


    @PostMapping("/engin/checkuser")
    @ApiOperation("检查工程机帐号是否有效")
    public ResponseHelper checkEnginUser(HttpServletRequest request){
        adminUserLogic.checkEnginUser(request);
        return ResponseHelper.success();
    }

    @PostMapping("/engin/bind")
    @ApiOperation("绑定工程机帐号")
    public ResponseHelper toEngin(
            @ApiParam(value = "工程机帐号",required = true) @RequestParam String enginUserName,
            @ApiParam(value = "工程机密码",required = true) @RequestParam String enginPassword,
            @ApiParam(value = "是否重新验证",required = true) @RequestParam(defaultValue = "false") Boolean isReset,
            HttpServletRequest request
    ){
        adminUserLogic.bindEngin(enginUserName,enginPassword, isReset, request);
        return ResponseHelper.success();
    }

    @PostMapping("/engin/account/del")
    @ApiOperation("删除工程机帐号")
    public ResponseHelper toEngin(
            @ApiParam(value = "工程机帐号",required = true) @RequestParam String id
    ){
        adminUserLogic.delEnginAccount(id);
        return ResponseHelper.success();
    }


    @PostMapping("/importUser")
    @ApiOperation("登录用户数据导入")
    public void importUser(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request,HttpServletResponse response
    ) throws Exception {

        List<Object> objects = ExcelUtils.importExcel(file);
        ExcelData export = adminUserLogic.importUser(objects,request);
        ExcelUtils.exportExcel(request,response,"导入失败数据.xlsx",export);
    }


    @PostMapping("/batch/update")
    @ApiOperation("导入-批量修改仓库、存货类别")
    public ResponseHelper batchUpdate(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request,HttpServletResponse response
    ) throws Exception {
        List<Object> objects = ExcelUtils.importExcel(file);
        adminUserLogic.batchUpdate(objects);
       return ResponseHelper.success();
    }


    @GetMapping("/download")
    @ApiOperation("用户数据模板下载")
    public void download(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("admin_user.xlsx",response);
    }


    @PostMapping("/user/child/add")
    @ApiOperation(value = "新建经销商子账号")
    public ResponseHelper addChild(
            @ApiParam(value = "帐号", required = false) @RequestParam String userName,
            @ApiParam(value = "模块id", required = false) @RequestParam List<String> adminModuleIds,
            @ApiParam(value = "密码", required = true) @RequestParam String password
    ) throws RemoteServiceException {
        adminUserLogic.addChildUser(userName, password,adminModuleIds);
        return ResponseHelper.success();
    }


    @PostMapping("/user/child/addsync")
    @ApiOperation(value = "更新子账号权限")
    public ResponseHelper addChild(

    ) throws RemoteServiceException {
        adminUserLogic.addChildUserSync();
        return ResponseHelper.success();
    }


    @PostMapping("/user/child/update")
    @ApiOperation(value = "编辑经销商子账号")
    public ResponseHelper updateChild(
            @ApiParam(value = "帐号id", required = true) @RequestParam String adminUserId,
            @ApiParam(value = "密码", required = false) @RequestParam(required = false) String password,
            @ApiParam(value = "name", required = false) @RequestParam(required = false) String nickName,
            @ApiParam(value = "权限id", required = false) @RequestParam(required = false) List<String> adminModuleIds
    ) throws RemoteServiceException {
        adminUserLogic.updateChildUser(adminUserId, password,adminModuleIds,nickName);
        return ResponseHelper.success();
    }


    @GetMapping("/user/child/module/checked")
    @ApiOperation(value = "经销商子账号的权限")
    public ResponseHelper<List<String>> updateChild(
            @ApiParam(value = "帐号id", required = true) @RequestParam String adminUserId
    ) throws RemoteServiceException {
        List<String> moduleIds = adminUserLogic.moduleChecked(adminUserId);
        return ResponseHelper.success(moduleIds);
    }



    @GetMapping("/user/listExport")
    @ApiOperation("账号-导出")
    public void listExport(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "角色id",required = false) @RequestParam(required = false) String roleId,
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "1=经销商 0=业务员",required = false) @RequestParam(required = false) Boolean isCustomer,
            @ApiParam(value = "true=主账号 false=子账号",required = false) @RequestParam(required = false) Boolean isMaster,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
            HttpServletRequest request, HttpServletResponse response
    ) throws Exception {
        IPage<AdminUserDto> page = adminUserLogic.list(adminWebsitId,roleId,customerId,isCustomer,isMaster,status,userName,1, -1,request);
        ExcelData excelData = adminUserLogic.listExport(page.getRecords());
        ExcelUtils.exportExcel(request,response,"账号信息.xlsx",excelData);
    }

    @ApiNotAuth
    @PostMapping("/check/platform/bind")
    @ApiOperation(value = "检查平台绑定")
    public ResponseHelper checkPlatformBind(
            @ApiParam(value = "帐号",required = true) @RequestParam String userName,
            @ApiParam(value = "密码",required = true) @RequestParam String password,
            @ApiParam(value = "时间戳",required = true) @RequestParam String timestamp,
            @ApiParam(value = "签名",required = true) @RequestParam String sign
    ) throws RemoteServiceException {
        adminUserLogic.checkPlatformBind(userName, password, timestamp, sign);
        return ResponseHelper.success();
    }

    ////////////////////////////////嘉贤账号///////////////////////////////////
    @GetMapping("/listJiaXian")
    @ApiOperation(value = "嘉贤子帐号列表")
    public ResponseHelper<IPage<AdminUserDto>> listJiaXian(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "角色id",required = false) @RequestParam(required = false) String roleId,
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "1=经销商 0=业务员",required = false) @RequestParam(required = false) Boolean isCustomer,
            @ApiParam(value = "true=主账号 false=子账号",required = false) @RequestParam(required = false) Boolean isMaster,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<AdminUserDto> page = adminUserLogic.listJiaXian(adminWebsitId,roleId,customerId,isCustomer,isMaster,status,userName,pageNum, pageSize,request);
        return ResponseHelper.success(page);
    }



    @PostMapping("/user/child/addJiaXian")
    @ApiOperation(value = "新建嘉贤经销商子账号")
    public ResponseHelper addJiaXian(
            @ApiParam(value = "帐号", required = false) @RequestParam String userName,
            @ApiParam(value = "子经销商id", required = true) @RequestParam String customerId,
            @ApiParam(value = "密码", required = true) @RequestParam String password
    ) throws RemoteServiceException {
        adminUserLogic.addJiaXian(userName, password,customerId);
        return ResponseHelper.success();
    }


    @GetMapping("/listGongCheng")
    @ApiOperation(value = "工程子帐号列表")
    public ResponseHelper<IPage<AdminUserDto>> listGongCheng(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "角色id",required = false) @RequestParam(required = false) String roleId,
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "1=经销商 0=业务员",required = false) @RequestParam(required = false) Boolean isCustomer,
            @ApiParam(value = "true=主账号 false=子账号",required = false) @RequestParam(required = false) Boolean isMaster,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request
    ) throws RemoteServiceException {
        IPage<AdminUserDto> page = adminUserLogic.listGongCheng(adminWebsitId,roleId,customerId,isCustomer,isMaster,status,userName,pageNum, pageSize,request);
        return ResponseHelper.success(page);
    }


    @GetMapping("/listGongCheng/export")
    @ApiOperation(value = "工程子帐号列表导出")
    public void exportListGongCheng(
            @ApiParam(value = "部门id",required = false) @RequestParam(required = false) String adminWebsitId,
            @ApiParam(value = "角色id",required = false) @RequestParam(required = false) String roleId,
            @ApiParam(value = "经销商id",required = false) @RequestParam(required = false) String customerId,
            @ApiParam(value = "1=经销商 0=业务员",required = false) @RequestParam(required = false) Boolean isCustomer,
            @ApiParam(value = "true=主账号 false=子账号",required = false) @RequestParam(required = false) Boolean isMaster,
            @ApiParam(value = "状态 true:正常 false:冻结",required = false) @RequestParam(required = false) Boolean status,
            @ApiParam(value = "用户名",required = false) @RequestParam(required = false) String userName,
            @ApiParam(value = "页号",required = true) @RequestParam(required = true) Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam(required = true) Integer pageSize,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        IPage<AdminUserDto> page = adminUserLogic.listGongCheng(adminWebsitId,roleId,customerId,isCustomer,isMaster,status,userName,pageNum, pageSize,request);
        ExcelData excelData = adminUserLogic.listExport(page.getRecords());
        ExcelUtils.exportExcel(request,response,"listGongCheng.xlsx",excelData);
    }


    @GetMapping("/detailGongCheng")
    @ApiOperation(value = "工程子帐号详情")
    public ResponseHelper<AdminUserDto> detailGongCheng(
            @ApiParam(value = "id",required = false) @RequestParam(required = false) String adminUserId

    ) throws RemoteServiceException {
        AdminUserDto page = adminUserLogic.detailGongCheng(adminUserId);
        return ResponseHelper.success(page);
    }



    @PostMapping("/user/child/addGongCheng")
    @ApiOperation(value = "新建工程经销商子账号")
    public ResponseHelper addGongCheng(
            @ApiParam(value = "帐号", required = false) @RequestParam String userName,
            @ApiParam(value = "账号名称", required = false) @RequestParam String nickName,
            @ApiParam(value = "父经销商id", required = true) @RequestParam String parentId,
            @ApiParam(value = "密码", required = true) @RequestParam String password,
            @ApiParam(value = "工装登录名额限制", required = true) @RequestParam Integer workLoginLimit,
            @ApiParam(value = "家装登录名额限制", required = true) @RequestParam Integer homeLoginLimit,
            @ApiParam(value = "跨区登录名额限制", required = true) @RequestParam Integer spanLoginLimit
    ) throws RemoteServiceException {
        adminUserLogic.addGongCheng(userName, password,parentId,nickName,workLoginLimit,homeLoginLimit,spanLoginLimit);
        return ResponseHelper.success();
    }

    @PostMapping("/user/child/updateGongCheng")
    @ApiOperation(value = "编辑工程经销商子账号")
    public ResponseHelper updateGongCheng(
            @ApiParam(value = "账号名称", required = false) @RequestParam String nickName,
            @ApiParam(value = "adminUserId", required = false) @RequestParam String adminUserId,
            @ApiParam(value = "工装登录名额限制", required = true) @RequestParam Integer workLoginLimit,
            @ApiParam(value = "家装登录名额限制", required = true) @RequestParam Integer homeLoginLimit,
            @ApiParam(value = "跨区登录名额限制", required = true) @RequestParam Integer spanLoginLimit
    ) throws RemoteServiceException {
        adminUserLogic.updateGongCheng(adminUserId,nickName,workLoginLimit,homeLoginLimit,spanLoginLimit);
        return ResponseHelper.success();
    }


    @PostMapping("/child/delGongCheng")
    @ApiOperation(value = "删除工程经销商子账号")
    public ResponseHelper delGongCheng(

            @ApiParam(value = "userName", required = false) @RequestParam String userName

    ) throws RemoteServiceException {
        adminUserLogic.delGongCheng(userName);
        return ResponseHelper.success();
    }


    @PostMapping("/import")
    @ApiOperation("导入工程子经销商账号")
    public ResponseHelper importData(
            @ApiParam(required = true, value = "附件") @RequestPart("file") MultipartFile file,
            HttpServletRequest request
    ) throws RemoteServiceException, IOException, ParseException {
        InputStream inputStream = file.getInputStream();
        adminUserLogic.importData(inputStream,request);
        return ResponseHelper.success();
    }

    @GetMapping("/downloadGongcheng")
    @ApiOperation("导入工程子经销商账号模板下载")
    public void downloadGongcheng(
            HttpServletResponse response
    ) throws IOException {
        CommonUtils.downloadFile("daoruzhanghao.xlsx",response);
    }


}
