package cn.iocoder.yudao.module.member.controller.app.user;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.userexpand.UserExpandDO;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.member.service.userexpand.UserExpandService;
import cn.iocoder.yudao.module.system.api.dishesnutrition.dto.DishesNutritionRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppMemberUserController {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberLevelService levelService;
    @Resource
    private UserExpandService userExpandService;

    @GetMapping("/get")
    @Operation(summary = "获得基本信息")
    @PreAuthenticated
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo() {
        MemberUserDO user = userService.getUser(getLoginUserId());
        MemberLevelDO level = levelService.getLevel(user.getLevelId());
        UserExpandDO userExpandByuserId = userExpandService.getUserExpandByuserId(getLoginUserId());
        AppMemberUserInfoRespVO convert = MemberUserConvert.INSTANCE.convert(user, level);
        convert.setUserExpandDO(userExpandByuserId);
        return success(convert);
    }

    @PutMapping("/update")
    @Operation(summary = "修改基本信息")
    @PreAuthenticated
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile-by-weixin")
    @Operation(summary = "基于微信小程序的授权码，修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobileByWeixin(@RequestBody @Valid AppMemberUserUpdateMobileByWeixinReqVO reqVO) {
        userService.updateUserMobileByWeixin(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserPassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码", description = "用户忘记密码时使用")
    public CommonResult<Boolean> resetUserPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }

    @PutMapping("/bindCard")
    @Operation(summary = "绑卡")
    public CommonResult<Boolean> bindCard(@Valid @RequestBody AppBindCardVO vo){
        return success(userService.bindCard(vo.getCardId()));
    }

    @GetMapping("/checkCard")
    @Operation(summary = "判定有无绑卡")
    public CommonResult<Boolean> checkCard(Long userId){
        return success(userService.checkCard(userId));
    }

    @GetMapping("/nutritionDay")
    @Operation(summary = "营养日报")
    public CommonResult<List<AppNutritionDayVo>> nutritionDay(String start, String end){
        List<AppNutritionDayVo> appNutritionDayVos = userService.nutritionDay(start,end);
        return success(appNutritionDayVos);
    }

    @GetMapping("/NutritionWeek")
    @Operation(summary = "营养周报")
    public CommonResult<AppWeekVO> NutritionWeek(String start, String end){
        return success(userService.NutritionWeek(start, end));
    }
    @GetMapping("/heat")
    @Operation(summary = "获得会员热量分析")
    public CommonResult<List<Map>> getUserHeat(@RequestParam(value = "userId",required = false) Long userId,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "startDate",required = false) LocalDateTime startDate,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @RequestParam(value = "endDate",required = false) LocalDateTime endDate,
                                               @RequestParam(value = "orderId",required = false)Long orderId){
        return success(userService.getUserHeat(userId,startDate,endDate,orderId));
    }
    @GetMapping("/heat/dishes")
    @Operation(summary = "获得会员热量分析")
    public CommonResult<List<DishesNutritionRespDTO>> getUserHeat(@RequestParam(value = "dishesId",required = false)Long dishesId){
        return success( userService.getHeatDishes(dishesId));
    }

    @PutMapping("/bindFace")
    @Operation(summary = "绑脸")
    public CommonResult<Boolean> bindFace(Long userId,Long faceId){
        return success(userService.bindFace(userId,faceId));
    }
    @GetMapping("/dishes")
    public CommonResult<Map<String,String>> getDishesList(@RequestParam("platesNum") String platesNum){
        return success(userService.getDishesList(platesNum));
    }

    @GetMapping("/getCardId")
    @Operation(summary = "获取用户绑定的卡号")
    public CommonResult<String> getCardId(Long userId){
        return success(userService.getCardId(userId));
    }
}

