package com.diagbot.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.annotation.SysLogger;
import com.diagbot.dto.GetConsoleUserInfoDTO;
import com.diagbot.dto.LoginDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserOrgDTO;
import com.diagbot.entity.Permission;
import com.diagbot.entity.User;
import com.diagbot.facade.PermissionFacade;
import com.diagbot.facade.UserFacade;
import com.diagbot.vo.UserSaveVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

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

/**
 * @Description: 用户操作API
 * @author: gaodm
 * @time: 2018/8/2 13:56
 */
@RestController
@Api(value="用户操作API", tags={"用户操作API"})
@RequestMapping("/user")
@SuppressWarnings("unchecked")
public class UserController {

    @Autowired
    private UserFacade userFacade;
    @Autowired
    private PermissionFacade permissionFacade;

    @ApiOperation(value = "注册[by:zhoutg]",
        notes = "username：用户名，必填<br>" +
                "password：密码，必填<br> " +
                "organization：组织机构，必填<br>" +
                "email：邮箱，必填<br>" +
                "linkman：联系人，必填<br>" +
                "imgId：图片id，必填<br>" +
                "code：图片验证码，必填<br>")
    @PostMapping("/registry")
    @SysLogger("registry")
    @Transactional
    public RespDTO<User> createUser(@RequestBody @Valid UserSaveVO userSaveVO){
        User user = userFacade.createUser(userSaveVO);
        return RespDTO.onSuc(user);
    }


    @ApiOperation(value = "登录[by:zhoutg]",
            notes = "username：用户名,必填<br>" +
                    "password：密码, 必填<br> ")
    @PostMapping("/login")
    @SysLogger("login")
    public RespDTO<LoginDTO> login(@RequestParam String username, @RequestParam String password){
        return  userFacade.login(username, password);
    }


    @ApiOperation(value = "重置密码[by:zhoutg]",
            notes = "username：用户名,必填<br>" +
                    "password：密码, 必填<br> " +
                    "diffPassword：值为Y，表示修改前和修改后密码不相同<br>")
    @PostMapping("/resetPassword")
    @SysLogger("resetPassword")
    @Transactional
    public RespDTO<Boolean> resetPassword(String username, String password, String diffPassword){
        return  userFacade.resetPassword(username, password, diffPassword);
    }



    @PostMapping(value = "/getUserAndOrg")
    @SysLogger("getUserAndOrg")
    @ApiOperation(value = "获取用户和机构信息[by:zhoutg]")
    @ApiIgnore
    RespDTO<Map<Long,UserOrgDTO>> getUserAndOrg(@RequestBody List<Long> userIds) {
        Map<Long, UserOrgDTO> result = userFacade.getUserAndOrg(userIds);
        return RespDTO.onSuc(result);
    }


    @PostMapping(value = "/getUserByOrgName")
    @SysLogger("getUserByOrgName")
    @ApiOperation(value = "远程调用，根据机构名称获取用户信息[by:zhoutg]")
//    @ApiIgnore
    RespDTO<List<Long>> getUserByOrgName(@RequestBody String name) {
        List<Long> data =  userFacade.getUserByOrgNameFac(name);
        return RespDTO.onSuc(data);
    }



    /**
     * @Description: 用来测试分页，未用
     * @Author: ztg
     * @Date: 2018/9/19 18:43
     */
    @PostMapping("/index")
    @SysLogger("index")
    @ApiOperation(value = "用来测试分页，未用[by:zhoutg]")
    @ApiIgnore
    public RespDTO<IPage<User>> indexUser(Page page, UserSaveVO userSaveVO){
        return userFacade.index(page, userSaveVO);
    }

    @ApiOperation(value = "获取资源信息[by:zhoutg]",
            notes = "appkey：用户名,必填<br>" +
                    "secret：密码, 必填<br> ")
    @PostMapping("/getPermission")
    public RespDTO<Permission> getPermission(@RequestParam String appkey, @RequestParam String secret){
        return  permissionFacade.getPermission(appkey, secret);
    }


    @ApiOperation(value = "根据用户名获取用户(示例)[by:gaodm]", notes = "根据用户名获取用户")
    @PostMapping("/getByUsername")
    @SysLogger("getByUsername")
    @ApiIgnore
//    @PreAuthorize("hasAnyAuthority('ROLE_USER')")
    public RespDTO getUserInfo(@RequestParam String username){
        //参数判读省略
        User user=  userFacade.getUserInfo(username);
        return RespDTO.onSuc(user);
    }

    @ApiOperation(value = "根据用户id获取用户信息(示例)[by:gaodm]", notes = "根据用户id获取用户信息")
    @PostMapping("/getUserInfoByID")
    @SysLogger("getUserInfoByID")
    @ApiIgnore
    public RespDTO getUserInfoByID(Long id, Page page){
        User userVo = new User();
        userVo.setId(id);
        IPage<User> user=  userFacade.selectUserListPage(page, userVo);
        return RespDTO.onSuc(user);
    }

//    @ApiOperation(value = "注册事务", notes = "username和password为必选项")
//    @PostMapping("/registryTran")
//    @SysLogger("registryTran")
//    @Transactional
//    public RespDTO createUserTran(@RequestBody User user){
//        //参数判读省略,判读该用户在数据库是否已经存在省略
//        userFacade.createUser(user);
//        throw new CommonException(CommonErrorCode.FAIL);
////        return RespDTO.onSuc(user);
//    }

//    @Autowired
//    private AmqpTemplate rabbitTemplate;
//    @GetMapping("/test")
//    public void test(){
//        rabbitTemplate.convertAndSend(RabbitConfig.queueName, "Hello from RabbitMQ!");
//    }

    /**
     * 增加用户
     * @param user 用户信息
     * @return 增加是否成功
     */
    @ApiOperation(value = "增加用户(示例)[by:gaodm]", notes = "username和password为必选项")
    @PostMapping("/addUser")
    @SysLogger("addUser")
    @Transactional
    @ApiIgnore
    public RespDTO addUser(@RequestBody User user){
        //参数判读省略,判读该用户在数据库是否已经存在省略
        userFacade.saveUser(user);
        //        throw new CommonException(ErrorCode.FAIL);
        return RespDTO.onSuc(user);
    }

    /**
     * 修改用户
     * @param user 用户信息
     * @return 修改是否成功
     */
    @ApiOperation(value = "修改用户(示例)[by:gaodm]", notes = "username和password为必选项")
    @PostMapping("/updateUser")
    @SysLogger("updateUser")
    @Transactional
    @ApiIgnore
    public RespDTO updateUser(@RequestBody User user){
        //参数判读省略,判读该用户在数据库是否已经存在省略
        userFacade.updateUser(user);
        return RespDTO.onSuc(user);
    }

    /**
     * 查询用户信息
     * @param id 用户ID
     * @return 用户信息
     */
    @ApiOperation(value = "查询用户(示例)[by:gaodm]", notes = "查询用户")
    @GetMapping("/getUser")
    @SysLogger("getUser")
    @Transactional
    @ApiIgnore
    public RespDTO getUser(Long id){
        return RespDTO.onSuc(userFacade.getById(id));
    }

    /**
     * 删除用户
     * @param id 用户ID
     * @return 删除是否成功
     */
    @ApiOperation(value = "删除用户(示例)[by:gaodm]", notes = "删除用户")
    @PostMapping("/delUser")
    @SysLogger("delUser")
    @Transactional
    @ApiIgnore
    public RespDTO delUser(Long id){
        return RespDTO.onSuc(userFacade.removeById(id));
    }
    
    @ApiOperation(value = "控制台账户信息(示例)[by:rengb]", notes="控制台账户信息")
	@PostMapping("/getConsoleUserInfo")
	@SysLogger("getConsoleUserInfo")
	public RespDTO<GetConsoleUserInfoDTO> getConsoleUserInfo() {
		return RespDTO.onSuc(userFacade.getConsoleUserInfo());
	}
}
