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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.member.GoodsVisitDetail;
import com.gree.mall.manager.bean.user.UserApplyBean;
import com.gree.mall.manager.bean.user.UserWxBean;
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 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;


    @GetMapping("/list/page")
    @ApiOperation(value = "会员列表")
    public ResponseHelper<Page<User>> page(HttpServletRequest request,
            @ApiParam(value = "名称/电话") @RequestParam(required = false) String keyword,
            @ApiParam(value = "网点") @RequestParam(required = false) List<String> websitIds,
            @ApiParam(value = "商户") @RequestParam(required = false) List<String> companyIds,
            @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,websitIds,companyIds);
        return ResponseHelper.success(page);
    }

    @GetMapping("/apply/page")
    @ApiOperation(value = "业务员申请列表")
    public ResponseHelper<Page<UserApplyBean>> applyPage(HttpServletRequest request,
                                                @ApiParam(value = "名称/电话") @RequestParam(required = false) String keyword,
                                                @ApiParam(value = "状态 WAIT待审核 OK通过 FAIL拒绝") @RequestParam(required = false) String status,
                                                @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
                                                @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<UserApplyBean> page = userLogic.applyPage(request, keyword, status, 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();
    }

    @PostMapping("/change/service/user")
    @ApiOperation(value = "修改会员业务人员状态")
    public ResponseHelper changeServiceUser(
            @ApiParam(value = "用户id", required = true) @RequestParam String userId,
            @ApiParam(value = "网点编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "网点名称") @RequestParam(required = false) String websitName,
            @ApiParam(value = "账号") @RequestParam(required = false) String workUserId,
            @ApiParam(value = "姓名") @RequestParam(required = false) String workName,
            @ApiParam(value = "职位") @RequestParam(required = false) String position,
            @ApiParam(value = "联系电话") @RequestParam(required = false) String mobile,
            @ApiParam(value = "true:业务员,false:非业务员", required = true) @RequestParam Boolean isService)
            throws RemoteServiceException {
        userLogic.changeServiceUser(userId, websitId, websitName, workUserId, workName, position, mobile, isService);
        return ResponseHelper.success();
    }


    @GetMapping("/downLoadUserInnerTimplate")
    @ApiOperation(value = "内部人员批量设置模板")
    public ResponseHelper downLoadUserInnerTimplate(HttpServletResponse response)
            throws RemoteServiceException, IOException {
        CommonUtils.downloadFile("用户批量设置内部人员模板.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();
    }


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

    @PostMapping("/examine/apply")
    @ApiOperation(value = "审核申请业务员")
    public ResponseHelper examineApply(
            @ApiParam(value = "id", required = true) @RequestParam String id,
            @ApiParam(value = "网点编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "网点名称") @RequestParam(required = false) String websitName,
            @ApiParam(value = "账号", required = true) @RequestParam String workUserId,
            @ApiParam(value = "姓名", required = true) @RequestParam String workName,
            @ApiParam(value = "职位", required = true) @RequestParam String position,
            @ApiParam(value = "手机号", required = true) @RequestParam String mobile,
            @ApiParam(value = "true:通过,false:拒绝", required = true) @RequestParam Boolean isOK)
            throws RemoteServiceException {
        userLogic.examineApply(id, websitId, websitName, workUserId, workName, position, mobile, isOK);
        return ResponseHelper.success();
    }

    @PostMapping("/import/member/list")
    @ApiOperation(value = "导入业务员列表")
    public ResponseHelper<IPage<UserServiceWait>> importMemberList(
            @ApiParam(value = "网点编号") @RequestParam(required = false) String websitId,
            @ApiParam(value = "网点名称") @RequestParam(required = false) String websitName,
            @ApiParam(value = "账号") @RequestParam(required = false) String workUserId,
            @ApiParam(value = "姓名") @RequestParam(required = false) String workName,
            @ApiParam(value = "职位") @RequestParam(required = false) String position,
            @ApiParam(value = "手机号") @RequestParam(required = false) String mobile,
            @ApiParam(value = "页号", required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小", required = true) @RequestParam Integer pageSize
    ) throws RemoteServiceException {
        IPage<UserServiceWait> waitIPage = userLogic.importMemberList(websitId, websitName, workUserId, workName, position, mobile, pageNum, pageSize);
        return ResponseHelper.success(waitIPage);
    }

}
