package com.fzm.mall.server.admin.user_center.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fzm.mall.server.admin.annotation.AdminLog;
import com.fzm.mall.server.admin.constant.MallResponseError;
import com.fzm.mall.server.admin.enums.log.OperateModuleEnum;
import com.fzm.mall.server.admin.enums.log.OperateTypeEnum;
import com.fzm.mall.server.admin.home_page.entity.dto.MerchantCheckDTO;
import com.fzm.mall.server.admin.home_page.entity.dto.MerchantLockDTO;
import com.fzm.mall.server.admin.home_page.entity.vo.MerchantVO;
import com.fzm.mall.server.admin.home_page.enums.status.MerchantRegisterEnum;
import com.fzm.mall.server.admin.home_page.enums.type.AdminTypeEnum;
import com.fzm.mall.server.admin.home_page.service.IAdminService;
import com.fzm.mall.server.admin.home_page.service.IMerchantService;
import com.fzm.mall.server.admin.repo.ResponseFactory;
import com.fzm.mall.server.admin.repo.ResponseVO;
import com.fzm.mall.server.admin.user_center.entity.dto.MerchantConfigDTO;
import com.fzm.mall.server.admin.user_center.entity.vo.MerchantConfigVO;
import com.fzm.mall.server.admin.user_center.service.IMerchantConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangp
 * @date 2021/3/24 14:33
 * @description 商户
 * @since JDK 1.8
 */
@Api(tags = "用户中心-商户")
@Validated
@RestController
@RequestMapping("/userCenterMerchant")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserCenterMerchantController {

    private final IMerchantConfigService configService;
    private final IMerchantService merchantService;
    private final IAdminService adminService;
    private final ResponseFactory resFac;

    @ApiOperation("编辑商户设置")
    @PostMapping("/merchantSettings/edit")
    @AdminLog(description = "用户中心-编辑商户设置", module = OperateModuleEnum.USER_CENTER)
    public ResponseVO edit(@RequestBody @Valid List<MerchantConfigDTO> merchantConfigDTOList, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        if (merchantConfigDTOList.size() != 3) {
            return resFac.getResponse(MallResponseError.PARAM_ERROR);
        }
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        configService.edit(merchantConfigDTOList);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("查询-设置列表")
    @GetMapping("/merchantSettings/list")
    public ResponseVO getMerchantConfigList(@ApiParam(name = "adminId", value = "管理员id") @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        List<MerchantConfigVO> configVOS = configService.listSelfDefine();
        return resFac.getSuccessResponseWithData(configVOS);
    }

    @ApiOperation("分页查询-商户列表")
    @GetMapping("/merchantList/page")
    public ResponseVO getMerchantList(
            @ApiParam(name = "merchantId", value = "商户id")
            @RequestParam(required = false) String merchantId,
            @ApiParam(name = "account", value = "登陆账号")
            @RequestParam(required = false) String account,
            @ApiParam(name = "linkMan", value = "联系人")
            @RequestParam(required = false) String linkMan,
            @ApiParam(name = "pageNum", value = "当前页数")
            @RequestParam Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页个数")
            @RequestParam Integer pageSize,
            @ApiParam(name = "adminId", value = "管理员id")
            @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        IPage<MerchantVO> page = merchantService.page(merchantId, account, linkMan,
                null, pageNum, pageSize);
        return resFac.getSuccessResponseWithData(page);
    }

    @ApiOperation("分页查询-商户审核列表")
    @GetMapping("/merchantCheck/page")
    public ResponseVO getMerchantCheckList(
            @ApiParam(name = "merchantId", value = "商户id")
            @RequestParam(required = false) String merchantId,
            @ApiParam(name = "account", value = "登陆账号")
            @RequestParam(required = false) String account,
            @ApiParam(name = "linkMan", value = "联系人")
            @RequestParam(required = false) String linkMan,
            @ApiParam(name = "pageNum", value = "当前页数")
            @RequestParam Integer pageNum,
            @ApiParam(name = "pageSize", value = "每页个数")
            @RequestParam Integer pageSize,
            @ApiParam(name = "adminId", value = "管理员id")
            @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        IPage<MerchantVO> page = merchantService.page(merchantId, account, linkMan,
                MerchantRegisterEnum.EXAMINE.getStatus(), pageNum, pageSize);
        return resFac.getSuccessResponseWithData(page);
    }

    //BUG LOCK ==> NOT LOGIN  | LAST LOGIN TIME | ALL LOGIN NOT LOGIN
    //WAIT register-area ==> don't do this in a short time
    //WAIT merchant-level ==> wait design | DEFAULT 1

    @ApiOperation("锁定/解锁")
    @PutMapping("/merchantList/lock")
    @AdminLog(description = "用户中心-商户解锁/锁定", module = OperateModuleEnum.USER_CENTER, opType = OperateTypeEnum.MERCHANT_LOCK)
    public ResponseVO lock(@RequestBody @Valid MerchantLockDTO lockDTO, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        merchantService.lock(lockDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("重置登陆密码")
    @PutMapping("/merchantList/resetLoginPwd/{account}")
    @AdminLog(description = "用户中心-重置商户登陆密码", module = OperateModuleEnum.USER_CENTER, opType = OperateTypeEnum.RESET_PWD)
    public ResponseVO resetLoginPwd(@PathVariable("account") String account, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        //TODO 重置之后短信通知
        adminService.resetLoginPwd(account);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("商户审核")
    @PutMapping("/merchantCheck/check")
    @AdminLog(description = "用户中心-商户审核", module = OperateModuleEnum.USER_CENTER, opType = OperateTypeEnum.MERCHANT_CHECK)
    public ResponseVO check(@RequestBody @Valid MerchantCheckDTO checkDTO, @ApiParam(name = "adminId", value = "管理员id")
    @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        merchantService.check(checkDTO);
        return resFac.getSimpleSuccessResponse();
    }

    @ApiOperation("查询商户身份证")
    @GetMapping("/merchantCheck/getIdCard")
    public ResponseVO getIdCard(@ApiParam(name = "registerPhone", value = "注册手机号") @RequestParam String registerPhone,
                                @ApiParam(name = "adminId", value = "管理员id") @RequestHeader String adminId) {
        adminService.adminAuthChecks(adminId, AdminTypeEnum.getPlatFormTypeArr());
        String idCard = merchantService.getIdCard(registerPhone);
        return resFac.getSuccessResponseWithData(idCard);
    }


}
