package shop.alien.gateway.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.StoreUser;
import shop.alien.entity.store.vo.StoreUserVo;
import shop.alien.gateway.config.BaseRedisService;
import shop.alien.gateway.mapper.StoreUserGatewayMapper;
import shop.alien.gateway.service.StoreUserService;

import java.util.Objects;
import java.util.Optional;

/**
 * 门店用户 前端控制器
 *
 * @author ssk
 * @since 2024-12-11
 */
@Slf4j
@Api(tags = {"二期-门店用户"})
@ApiSort(8)
@CrossOrigin
@RestController
@RequestMapping("/store/user")
@RequiredArgsConstructor
public class StoreUserController {

    private final StoreUserService storeUserService;

    private final StoreUserGatewayMapper storeUserMapper;

    private final BaseRedisService baseRedisService;

    @ApiOperation("门店用户登录")
    @ApiOperationSupport(order = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone", value = "手机号", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "password", value = "密码", dataType = "String", paramType = "query", required = true),
            @ApiImplicitParam(name = "isPassword", value = "是否密码登录", dataType = "Boolean", paramType = "query", required = true),
            @ApiImplicitParam(name = "code", value = "验证码", dataType = "String", paramType = "query", required = true)
    })
    @GetMapping("/login")
    public R<StoreUserVo> login(
            @RequestParam("phone") String phone,
            @RequestParam("password") String password,
            @RequestParam("isPassword") Boolean isPassword,
            @RequestParam("code") String code) {
        log.info("StoreUserController.login?phone={}&password={}&isPassword={}&code={}", phone, password, isPassword, code);
        if (!isPassword) {
            // 2025-11-04 验证码-商户端登录
            String cacheCode = baseRedisService.getString("verification_store_login_" + phone);
            if (null == cacheCode) {
                return R.fail("验证码过期或未发送");
            }
            if (!cacheCode.trim().equals(code.trim())) {
                return R.fail("验证码错误");
            }
        }
        StoreUser storeUser = storeUserMapper.selectOne(new LambdaQueryWrapper<StoreUser>()
                .eq(StoreUser::getPhone, phone));
        if (null == storeUser) {
            return R.fail("当前账号不存在，请先去注册账号！");
        }
        if (storeUser.getStatus() == 1) {
            return R.fail("账号被禁用");
        }
        return Optional.ofNullable(storeUser).
                map(user -> isPassword ? checkPassword(user, password) : storeUserService.createToKen(user)).
                orElseGet(() -> R.fail("手机号不存在"));
    }

    /**
     * checkPwd
     *
     * @param user
     * @param password
     * @return
     */
    private R<StoreUserVo> checkPassword(StoreUser user, String password) {
        return Objects.equals(password, user.getPassword())
                ? storeUserService.createToKen(user)
                : R.fail("密码错误");
    }

}
