package com.ruoyi.api.controller.user;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.api.controller.common.AbstractApiController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.RegGroup;
import com.ruoyi.common.enums.FilePathSplicingType;
import com.ruoyi.common.enums.WxMinQrCode;
import com.ruoyi.common.filepathsplicing.FilePathSplicing;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.info.management.domain.ManagementTeacher;
import com.ruoyi.info.management.domain.vo.ManagementTeacherVo;
import com.ruoyi.info.management.service.IManagementTeacherService;
import com.ruoyi.info.nursing.domain.vo.NursingWorkerVo;
import com.ruoyi.info.nursing.service.INursingWorkerService;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.service.ISysOssService;
import com.ruoyi.user.domain.User;
import com.ruoyi.user.domain.bo.PersonalDataBo;
import com.ruoyi.user.domain.bo.UserBo;
import com.ruoyi.user.domain.vo.UserVo;
import com.ruoyi.user.mapper.UserMapper;
import com.ruoyi.user.service.IUserService;
import com.ruoyi.weixin.service.WxMinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 小程序用户管理Controller
 *
 * @author wuchao
 * @date 2022-03-03
 */
@Validated
@Api(value = "小程序用户管理控制器", tags = {"小程序用户管理管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class ApiUserController extends AbstractApiController {

    private final IUserService iUserService;
    private final WxMinService wxMinService;
    private final ISysOssService iSysOssService;
    private final UserMapper baseMapper;

    private final IManagementTeacherService managementTeacherService;

    private final INursingWorkerService nursingWorkerService;

    /**
     * 获取用户管理详细信息
     */
    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
    @ApiOperation("获取用户管理详细信息")
    @GetMapping("/info")
    public R<UserVo> getInfo() {
        if (ObjectUtil.isEmpty(getLoginUser())) {
            return R.ok();
        }
        UserVo user = iUserService.getUserById(getUserId(), true);

        //  判断用户为 普通用户/管理老师/护工，以及启用/禁用情况
        ManagementTeacherVo managementTeacherVo = managementTeacherService.getByPhoneNumber(user.getMobile(), true);
        if (managementTeacherVo != null) {
            user.setTeacherId(managementTeacherVo.getId());
        }
        NursingWorkerVo nursingWorkerVo = nursingWorkerService.getByPhoneNumber(user.getMobile(), true);
        if (nursingWorkerVo != null) {
            user.setWorkerId(nursingWorkerVo.getId());
        }

        return R.ok(user);
    }


    /**
     * 修改个人资料
     */
    @FilePathSplicing(type = FilePathSplicingType.REQUEST)
    @ApiOperation("修改个人资料")
    @PostMapping("/personalData/update")
    public R<Void> personalDataUpdate(@Validated @RequestBody PersonalDataBo bo) {
        iUserService.personalDataUpdate(bo, getUserId());
        return R.ok();
    }

//    @FilePathSplicing(type = FilePathSplicingType.RESPONSE)
//    @ApiOperation("获取地推二维码")
//    @PostMapping("/getQrCode")
//    public R<Map<String,String>> getQrCode(@RequestBody WxMinQrCode bo){
//        Long userId = getUserId();
//        User user = iUserService.getById(userId, true);
//        if(StringUtils.isNotBlank(user.getQrCode()))
//        {
//            Map<String,String> map = new HashMap<>();
//            map.put("url",user.getQrCode());
//            return R.ok(map);
//        }
//        String t = getSceneType(bo);
//        String scene = String.format("t=%s&id=%s", t,userId);
//        byte[] bytes = wxMinService.getWxMinCode(bo.getPath(), scene);
//        String filename = IdUtil.fastSimpleUUID() + ".jpg";
//        SysOss sysOss = iSysOssService.upload(bytes,filename,"image/png");
//        user.setQrCode(sysOss.getUrl());
//        baseMapper.updateById(user);
//        Map<String,String> map = new HashMap<>();
//        map.put("url",sysOss.getUrl());
//        return R.ok(map);
//    }

//    private String getSceneType(WxMinQrCode wxMinQrCode) {
//        switch (wxMinQrCode.getSceneType()){
//            case SHOP_INDEX:
//                return "i";
//            default:
//                return "";
//        }
//    }

}
