package io.hmit.controller;

import io.hmit.common.utils.Result;
import io.hmit.common.validator.ValidatorUtils;
import io.hmit.dto.LoginResDTO;
import io.hmit.dto.RegisterDTO;
import io.hmit.dto.UserDTO;
import io.hmit.dto.UserRegisterDTO;
import io.hmit.entity.UserEntity;
import io.hmit.modules.user.dto.RoleDTO;
import io.hmit.modules.user.entity.RoleEntity;
import io.hmit.modules.user.entity.UserRoleEntity;
import io.hmit.modules.user.service.RoleService;
import io.hmit.modules.user.service.UserRoleService;
import io.hmit.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Optional;

/**
 * 注册接口
 *
 * @author zsh 408538940@qq.com
 */
@RestController
@RequestMapping("/api")
@Api(tags = "注册接口")
public class ApiRegisterController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserRoleService userRoleService;

    @Autowired
    private RoleService roleService;

    @PostMapping("register")
    @ApiOperation("注册")
    public Result register(@RequestBody RegisterDTO dto) {

        //表单校验
        ValidatorUtils.validateEntity(dto);

        UserEntity user = new UserEntity();
        user.setMobile(dto.getMobile());
        user.setUsername(dto.getMobile());
        user.setPassword(DigestUtils.sha256Hex(dto.getPassword()));
        user.setCreateDate(new Date());
        userService.insert(user);

        return new Result();
    }

    @PostMapping("appRegister")
    @ApiOperation("上架前人员注册")
    public Result appRegister(@RequestBody UserRegisterDTO dto) {

        LoginResDTO loginResDTO = new LoginResDTO();
        Optional<RoleDTO>  roleDTO =Optional.ofNullable(roleService.get(dto.getRoleId()));
        roleDTO.ifPresent(item ->loginResDTO.setTempID(item.getTempId()));

        UserEntity entity = userService.getByMobileAIdno(dto.getMobile(), dto.getUserIdno());
        if(entity!=null){
            loginResDTO.setUserID(entity.getId());

            return new Result().ok(loginResDTO);
        }
        UserEntity user = new UserEntity();
        user.setMobile(dto.getMobile());
        user.setUsername(dto.getUsername());
        user.setCreateDate(new Date());
//        user.setCaseId(dto.getCaseId());
        user.setUserIdno(dto.getUserIdno());
        userService.insert(user);

        UserRoleEntity userRoleEntity = new UserRoleEntity();
        userRoleEntity.setRoleId(dto.getRoleId());
        userRoleEntity.setUserId(user.getId());
        userRoleEntity.setCreateDate(new Date());
        userRoleService.insert(userRoleEntity);

        loginResDTO.setUserID(user.getId());
        return new Result().ok(loginResDTO);
    }
}
