package com.diagbot.web;

import java.util.List;

import javax.validation.Valid;

import com.diagbot.vo.UserInfoPagVO;
import com.diagbot.vo.UserOrganProductPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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 com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diagbot.annotation.SysLogger;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserInfoDTO;
import com.diagbot.entity.Organization;
import com.diagbot.facade.OrganizationFacade;
import com.diagbot.facade.UserAuthenticationFacade;
import com.diagbot.facade.UserFacade;
import com.diagbot.facade.UserOrganizationFacade;
import com.diagbot.vo.BaseIdVO;
import com.diagbot.vo.OrganizationNameVO;
import com.diagbot.vo.OrganizationVO;
import com.diagbot.vo.UserAndOrganizationVO;
import com.diagbot.vo.UserInfoAuditVO;
import com.diagbot.vo.UserInfoOrganizationsVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @Description: 外部用户信息
 * @author: wangfeng
 * @time: 2018年9月17日 下午1:53:53
 */
@RestController
@Api(value = "客户中心API[by:wangfeng]", tags = { "注册用户信息和机构信息API" })
@RequestMapping("/userInfo")
@SuppressWarnings("unchecked")
public class UserInfoController {

    @Autowired
    private UserFacade userFacade;
    @Autowired
    OrganizationFacade organizationFacade;
    @Autowired
    UserOrganizationFacade userOrganizationFacade;
    @Autowired
    UserAuthenticationFacade userAuthenticationFacade;

    @ApiOperation(value = "分页查询获取用户信息[by:wangfeng]", notes = "根据每页显示size条数，默认 10，和当前页current,认证的状态autStatus")
    @PostMapping("/getUserInfoPag")
    @SysLogger("getUserInfoPag")
    // @ApiIgnore
    public RespDTO<UserInfoDTO> getUserInfoPag(@RequestBody UserInfoPagVO userInfoPagVO) {
        UserInfoDTO userInfo = new UserInfoDTO();
        userInfo.setOrgName(userInfoPagVO.getOrgName());
        userInfo.setAutStatus(userInfoPagVO.getAutStatus());
        IPage<UserInfoDTO> user = userFacade.selectUserInfoListPage(userInfoPagVO, userInfo);
        return RespDTO.onSuc(user);
    }

    @ApiOperation(value = "修改用户信息和机构信息[by:wangfeng]", notes = "用户信息和机构信息")
    @PostMapping("/updateUserInfoAll")
    @SysLogger("updateUserInfoAll")
    @Transactional
    public RespDTO<UserInfoOrganizationsVO> updateUserInfoAll(
            @Valid @RequestBody UserInfoOrganizationsVO userInfoOrganizationsVO) {
        return userFacade.updateUserInfoAlls(userInfoOrganizationsVO);
    }

    @ApiOperation(value = "审核用户信息和机构信息[by:wangfeng]", notes = "审核用户信息和机构信息")
    @PostMapping("/auditUserInfoAll")
    @SysLogger("auditUserInfoAll")
    @Transactional
    public RespDTO<Boolean> auditUserInfoAll(@Valid @RequestBody UserInfoAuditVO userInfoAuditVO) {
        return userFacade.auditUserInfoAlls(userInfoAuditVO);
    }

    @ApiOperation(value = "删除用户信息和机构信息[by:wangfeng]", notes = "用户信息和机构信息")
    @PostMapping("/updateDeleted")
    @SysLogger("updateDeleted")
    @Transactional
    public RespDTO<Boolean> updateDeleted(@RequestBody BaseIdVO baseIdVO) {
        return userFacade.updateDeleteds(baseIdVO);
    }

    @ApiOperation(value = "模糊查询机构信息[by:wangfeng]", notes = "机构信息")
    @PostMapping("/findOrganization")
    @SysLogger("findOrganization")
    public RespDTO<Organization> findOrganization(@RequestBody OrganizationNameVO organizationNameVO) {
        // TODO 返回机构Id和机构
    	String organizationName = organizationNameVO.getOrganizationName();
        List<Organization> organization = organizationFacade.findOrganization(organizationName);

        return RespDTO.onSuc(organization);
    }

    @ApiOperation(value = "添加构信息[by:wangfeng]", notes = "机构信息")
    @PostMapping("/addOrganization")
    @SysLogger("addOrganization")
    @Transactional
    public RespDTO<Boolean> addOrganization(@Valid @RequestBody OrganizationVO organizationVO) {

        return userFacade.addOrganizations(organizationVO);
    }

    @ApiOperation(value = "添加用户信息和机构信息管理员[by:wangfeng]", notes = "用户信息和机构信息的绑定")
    @PostMapping("/addUserInfo")
    @SysLogger("addUserInfo")
    @Transactional
    public RespDTO<UserAndOrganizationVO> addUserInfo(@Valid @RequestBody UserAndOrganizationVO userAndOrganizationVO) {

        return userFacade.addUserInfos(userAndOrganizationVO);
    }

    @ApiOperation(value = "用户信息和机构信息已开通产品[by:wangfeng]", notes = "用户信息和机构信息已开通产品")
    @PostMapping("/getUserOrganProductAll")
    @SysLogger("getUserOrganProductAll")
    @Transactional
    public RespDTO<IPage<UserInfoDTO>> getUserOrganProductAll(@RequestBody UserOrganProductPageVO pageVO) {

        return userFacade.getUserOrganProductAlls(pageVO, pageVO.getOrgName(), pageVO.getAutStatus());
    }
}
