package com.gree.mall.manager.controller.member;

import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.annotation.ZfireList;
import com.gree.mall.manager.bean.listvo.User2VO;
import com.gree.mall.manager.bean.listvo.UserCustomerVO;
import com.gree.mall.manager.bean.listvo.UserVO;
import com.gree.mall.manager.bean.listvo.workorder.UserWaitVO;
import com.gree.mall.manager.bean.member.UserApplyBean;
import com.gree.mall.manager.bean.user.UserApplyWorkerBean;
import com.gree.mall.manager.bean.user.UserWxBean;
import com.gree.mall.manager.enums.ExamineStatusEnum;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.goods.GoodsLogic;
import com.gree.mall.manager.logic.user.UserLogic;
import com.gree.mall.manager.plus.entity.*;
import com.gree.mall.manager.utils.CommonUtils;
import com.gree.mall.manager.utils.excel.ExcelUtils;
import com.gree.mall.manager.zfire.bean.WorkerParamBean;
import com.gree.mall.manager.zfire.bean.ZfireParamBean;
import com.gree.mall.manager.zfire.util.FieldUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
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.util.List;

@Slf4j
@RestController
@Api(value = "会员管理和用户管理", tags = {"会员管理和用户管理"})
@RequestMapping(value = "/member", produces = "application/json; charset=utf-8")
public class MemberController {

    @Autowired
    UserLogic userLogic;
    @Autowired
    GoodsLogic goodsLogic;

    @ZfireList
    @PostMapping("/list/customer")
    @ApiOperation(value = "客户列表-v2")
    public ResponseHelper<Page<UserCustomerVO>> listCustomer(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<UserCustomerVO> page = userLogic.userCustomerList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<UserCustomerVO>() {
        });
    }

    @PostMapping("/list/customer/export")
    @ApiOperation(value = "客户列表-导出-v2")
    public void listExport(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<UserCustomerVO> page = userLogic.userCustomerList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/list/page2")
    @ApiOperation(value = "师傅列表-v2")
    public ResponseHelper<Page<UserVO>> page1(
            @RequestBody WorkerParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<UserVO> page = userLogic.page2(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<UserVO>() {
        });
    }

    @PostMapping("/list/page2/export")
    @ApiOperation(value = "师傅列表-导出-v2")
    public void page1Export(
            @RequestBody WorkerParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<UserVO> page = userLogic.page2(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/list/service2")
    @ApiOperation(value = "分销员列表-v2")
    public ResponseHelper<Page<User2VO>> page2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<User2VO> page = userLogic.userServiceList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<User2VO>() {
        });
    }


    @PostMapping("/list/service2/export")
    @ApiOperation(value = "分销员列表-导出-v2")
    public void page2Export(
            @RequestBody ZfireParamBean zfireParamBean,
            HttpServletRequest request,
            HttpServletResponse response
    ) throws Exception {
        //2.查询要导出的内容
        IPage<User2VO> page = userLogic.userServiceList(zfireParamBean);
        //3.导出
        FieldUtils.exportData(page.getRecords(), zfireParamBean.getExportFields(), request, response);
    }


    @ZfireList
    @PostMapping("/list/wait2")
    @ApiOperation(value = "师傅分销员资料初始化-列表-v2")
    public ResponseHelper<Page<UserWaitVO>> wait2(
            @RequestBody ZfireParamBean zfireParamBean
    ) throws RemoteServiceException {
        IPage<UserWaitVO> page = userLogic.userWaitList(zfireParamBean);
        return ResponseHelper.success(page, new TypeReference<UserWaitVO>() {
        });
    }

    @PostMapping("/list/wait2/del")
    @ApiOperation(value = "师傅分销员资料初始化-删除-v2")
    public ResponseHelper wait2Del(
            @RequestParam String id
    ) throws RemoteServiceException {
        userLogic.userWaitDel(id);
        return ResponseHelper.success();
    }

    @PostMapping("/list/wait2/import")
    @ApiOperation(value = "师傅分销员资料初始化-导入-v2(模板名称:师傅分销员资料初始化.xlsx)")
    public ResponseHelper wait2Del(
            @RequestParam MultipartFile file
    ) throws RemoteServiceException, IOException {
        List<Object> objects = ExcelUtils.importExcel(file);
        userLogic.importData(objects);
        return ResponseHelper.success();
    }

    @PostMapping("/userWatit/update")
    @ApiOperation("修改师傅初始资料")
    public ResponseHelper workerUpdate(
            @RequestBody UserWait userWait
    ) {
        userLogic.userWaitUpdate(userWait);
        return ResponseHelper.success();
    }


    @PostMapping("/worker/update")
    @ApiOperation("修改师傅资料")
    public ResponseHelper workerUpdate(
            @ApiParam(value = "userId", required = true) @RequestParam String userId,
            @ApiParam(value = "网点id", required = true) @RequestParam String websitId,
            @ApiParam(value = "师傅名称", required = false) @RequestParam(required = false) String name,
            @ApiParam(value = "身份证号", required = false) @RequestParam(required = false) String idCard,
            @ApiParam(value = "身份证图片", required = false) @RequestParam(required = false) String idCardImg,
            @ApiParam(value = "银行卡", required = false) @RequestParam(required = false) String bankAccount,
            @ApiParam(value = "设置师傅二手商品发布权限true=启用 false=禁用") @RequestParam Boolean isEs,
            @ApiParam(value = "师傅编号", required = true) @RequestParam String workerNumber
    ) {
        userLogic.updateWorker(userId, websitId, name, idCard, idCardImg, bankAccount, isEs, workerNumber);
        return ResponseHelper.success();
    }


    @GetMapping("worker/examine/detail")
    @ApiOperation("师傅审核详情")
    public ResponseHelper<UserApplyWorkerBean> workerExamineDetail(@RequestParam String id) {
        UserApplyWorkerBean bean = userLogic.workerExamineDetail(id);
        return ResponseHelper.success(bean);
    }

    @PostMapping("/worker/examine")
    @ApiOperation(value = "工程师审核-v2")
    public ResponseHelper examineWorker(
            @RequestBody UserApplyWorkerBean bean
//            @RequestParam String id,
//            @ApiParam("审核状态 OK=通过 FAIL=驳回 ") @RequestParam ExamineStatusEnum examineStatusEnum,
//            @ApiParam(value = "审核备注", required = false) @RequestParam(required = false) String examineRemark
    ) {
        userLogic.updateWorker(bean.getUserId(), bean.getWebsitId(), bean.getNickName(), bean.getIdCard(), bean.getIdCardImg(),
                bean.getBankAccount(), bean.getIsEs(), bean.getWorkerNumber());
        userLogic.examine(bean.getId(), bean.getExamineStatus(), bean.getExamineRemark());
        return ResponseHelper.success();
    }

    @PostMapping("/share/examine")
    @ApiOperation(value = "分销员审核-v2")
    public ResponseHelper examineShare(
            @ApiParam("用户id") @RequestParam String userId,
            @ApiParam("审核状态 OK=通过 FAIL=驳回 ") @RequestParam ExamineStatusEnum examineStatusEnum,
            @ApiParam(value = "审核备注", required = false) @RequestParam(required = false) String examineRemark
    ) {
        //userLogic.
        userLogic.shareExamine(userId, examineStatusEnum, examineRemark);
        return ResponseHelper.success();
    }


    @PostMapping("/batch/inner")
    @ApiOperation(value = "批量设置或取消内部分销员-v2")
    public ResponseHelper batchInner(
            @ApiParam(value = "true=内部分销员 false=取消内部分销员") @RequestParam Boolean innerr,
            @ApiParam(value = "用户id集合，逗号隔开") @RequestParam List<String> userIds
    ) {
        userLogic.batchInner(userIds, innerr);
        return ResponseHelper.success();
    }


    @PostMapping("/batch/type")
    @ApiOperation(value = "批量设置用户类型-v2")
    public ResponseHelper batchType(
            @RequestBody UserApplyBean userApplyBean
    ) {
        userLogic.batchType(userApplyBean);
        return ResponseHelper.success();
    }


    @PostMapping("/batch/es")
    @ApiOperation(value = "批量设置师傅二手商品发布权限")
    public ResponseHelper batchEs(
            @ApiParam(value = "true=启用 false=禁用") @RequestParam Boolean isEs,
            @ApiParam(value = "用户id集合，逗号隔开") @RequestParam List<String> userIds
    ) {
        userLogic.batchEs(userIds, isEs);
        return ResponseHelper.success();
    }


    @GetMapping("/list/page")
    @ApiOperation(value = "会员列表")
    public ResponseHelper<Page<User>> page(HttpServletRequest request,
                                           @ApiParam(value = "名称/电话") @RequestParam(required = false) String keyword,
                                           @ApiParam(value = "用户角色,GENERAL=普通用户 SERVICE=业务员") @RequestParam(required = false) String type,
                                           @ApiParam(value = "是否为团长(true=是 false=否)") @RequestParam(required = false) Boolean promotionGroupLeader,
                                           @ApiParam(value = "是否为内部人员(true=是 false=否)") @RequestParam(required = false) Boolean innerr,
                                           @ApiParam(value = "网点id", required = false) @RequestParam(required = false) String websitId,
                                           @ApiParam(value = "是否有优惠码权限") @RequestParam(required = false) Boolean isExchangeCode,
                                           @ApiParam(value = "注册开始时间") @RequestParam(required = false) String startTime,
                                           @ApiParam(value = "注册结束时间") @RequestParam(required = false) String endTime,
                                           @ApiParam(value = "排序json格式[{sortColumn: 'ID', type: true },...] " +
                                                   "sortColumn:会员排序对应值 ID=编号 SEX=性别 CREATE=注册时间 STATUS=状态, " +
                                                   "type: true=升序 false=倒序")
                                           @RequestParam(required = false) String sortJson,
                                           @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
                                           @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<User> page = userLogic.page(request, keyword, type, promotionGroupLeader, innerr, startTime, endTime, sortJson, websitId, isExchangeCode, pageNum, pageSize);
        return ResponseHelper.success(page);
    }

    @GetMapping("/export")
    @ApiOperation(value = "会员导出")
    public void export(HttpServletRequest request, HttpServletResponse response,
                       @ApiParam(value = "名称/电话") @RequestParam(required = false) String keyword,
                       @ApiParam(value = "用户角色,GENERAL=普通用户 SERVICE=业务员") @RequestParam(required = false) String type,
                       @ApiParam(value = "是否为团长(true=是 false=否)") @RequestParam(required = false) Boolean promotionGroupLeader,
                       @ApiParam(value = "是否为内部人员(true=是 false=否)") @RequestParam(required = false) Boolean innerr,
                       @ApiParam(value = "网点id", required = false) @RequestParam(required = false) String websitId,
                       @ApiParam(value = "注册开始时间") @RequestParam(required = false) String startTime,
                       @ApiParam(value = "注册结束时间") @RequestParam(required = false) String endTime
    ) throws Exception {
        userLogic.export(request, response, keyword, type, websitId, startTime, endTime);
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改会员")
    public ResponseHelper update(
            @ApiParam(value = "object", required = true) @RequestBody User user)
            throws RemoteServiceException {
        userLogic.update(user);
        return ResponseHelper.success();
    }


    @PostMapping("/updateUserInnerStatus")
    @ApiOperation(value = "修改会员内部人员状态")
    public ResponseHelper updateUserInnerStatus(
            @ApiParam(value = "用户id", required = true) @RequestParam String userId,
            @ApiParam(value = "true:内部人员,false:非内部人员", required = true) @RequestParam Boolean inner)
            throws RemoteServiceException {
        userLogic.updateUserInnerStatus(userId, inner);
        return ResponseHelper.success();
    }


    @GetMapping("/downLoadUserInnerTimplate")
    @ApiOperation(value = "内部人员批量设置模板")
    public ResponseHelper downLoadUserInnerTimplate(HttpServletResponse response)
            throws RemoteServiceException, IOException {
        CommonUtils.downloadFile("/static/用户批量设置内部人员模板.xlsx", response);
        return ResponseHelper.success();
    }

    @PostMapping("/localUpdateInner")
    @ApiOperation("/导入跟新内部人员状态")
    public ResponseHelper<Void> localUpdateInner(
            HttpServletRequest request,
            @RequestParam("file") MultipartFile file
    ) throws IOException, WxErrorException {
        userLogic.localUpdateInner(request, file);
        return ResponseHelper.success();
    }

    @PostMapping("/groupleader")
    @ApiOperation("设置/取消团长")
    public ResponseHelper groupleader(
            @ApiParam(value = "用户id", required = true) @RequestParam String userId,
            @ApiParam(value = "true=设置 false=取消", required = true) @RequestParam Boolean promotionGroupLeader
    ) throws RemoteServiceException {
        userLogic.groupleader(userId, promotionGroupLeader);
        return ResponseHelper.success();
    }

    @PostMapping("/status/update")
    @ApiOperation(value = "更改会员状态")
    public ResponseHelper change(
            @ApiParam(value = "会员id", required = true) @RequestParam String userId,
            @ApiParam(value = "状态:false=正常 true=禁用", required = true) @RequestParam Boolean status
    ) {
        userLogic.updateState(userId, status);
        return ResponseHelper.success();
    }


    @PostMapping("/slave/bind")
    @ApiOperation(value = "绑定辅助工程师")
    public ResponseHelper bindSlave(
            @ApiParam(value = "网点id", required = true) @RequestParam String websitId,
            @ApiParam(value = "师傅id", required = true) @RequestParam String userId,
            @ApiParam(value = "辅助师傅id", required = true) @RequestParam String slaveUserId
    ) {
        userLogic.bindSlaveWorker(websitId, userId, slaveUserId);
        return ResponseHelper.success();
    }


    @GetMapping("/detail")
    @ApiOperation(value = "用户详情")
    public ResponseHelper<UserWxBean> detail(
            @ApiParam(value = "会员id", required = true) @RequestParam String userId
    ) {
        UserWxBean detail = userLogic.detail(userId);
        return ResponseHelper.success(detail);
    }

    @PostMapping("/updateNickName")
    @ApiOperation(value = "修改会员昵称")
    public ResponseHelper<Void> updateNickName(
            @ApiParam(value = "用户id", required = true) @RequestParam String userId,
            @ApiParam(value = "新昵称", required = true) @RequestParam String newNickName
    ) {
        userLogic.updateUserNickName(userId, newNickName);
        return ResponseHelper.success();
    }

    @GetMapping("/address")
    @ApiOperation(value = "会员地址")
    public ResponseHelper<IPage<UserAddress>> addressList(
            @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) {
        IPage<UserAddress> userAddressIPage = userLogic.addressList(userId, pageNum, pageSize);
        return ResponseHelper.success(userAddressIPage);
    }

    @GetMapping("/favorite")
    @ApiOperation(value = "收藏记录")
    public ResponseHelper<IPage<Goods>> favoriteList(
            @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) {
        IPage<Goods> goodsFavoriteIPage = userLogic.goodsFavoriteList(userId, pageNum, pageSize);
        return ResponseHelper.success(goodsFavoriteIPage);
    }

    @GetMapping("/goods/visit")
    @ApiOperation(value = "我的浏览记录")
    public ResponseHelper<IPage<GoodsVisit>> goodsVisitList(
            @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) {
        IPage<GoodsVisit> goodsVisitIPage = userLogic.goodsVisitList(userId, pageNum, pageSize);
        return ResponseHelper.success(goodsVisitIPage);
    }

    @GetMapping("/coupon")
    @ApiOperation(value = "我的优惠券")
    public ResponseHelper<IPage<UserCoupon>> couponList(
            @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) {
        IPage<UserCoupon> userCouponIPage = userLogic.couponList(userId, pageNum, pageSize);
        return ResponseHelper.success(userCouponIPage);
    }

    @GetMapping("/wx/list")
    @ApiOperation(value = "客户列表")
    public ResponseHelper wxCustomerList(HttpServletRequest request,
                                         @ApiParam(required = true, value = "用户id") @RequestParam(required = true) String userId) throws WxErrorException {

        return ResponseHelper.success(userLogic.wxCustomerList(request, userId));
    }

}
