package com.diagbot.web;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import com.diagbot.vo.UserInfoPagVO;
import com.diagbot.vo.UserOrganProductPageVO;
import com.diagbot.vo.UsernameVO;

import org.apache.ibatis.annotations.Param;
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.AuthenticationDTO;
import com.diagbot.dto.OrganizationDTO;
import com.diagbot.dto.RespDTO;
import com.diagbot.dto.UserAllDTO;
import com.diagbot.dto.UserInfoDTO;
import com.diagbot.dto.UserOrgizationProductDTO;
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.AmendOrgVO;
import com.diagbot.vo.AmendUserInfoVO;
import com.diagbot.vo.BaseIdVO;
import com.diagbot.vo.OrganizationNameVO;
import com.diagbot.vo.OrganizationVO;
import com.diagbot.vo.UserAndOrgPageVO;
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;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @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,认证的状态authStatus")
    @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
    @ApiIgnore
    public RespDTO<UserInfoOrganizationsVO> updateUserInfoAll(
            @Valid @RequestBody UserInfoOrganizationsVO userInfoOrganizationsVO) {
        return userFacade.updateUserInfoAlls(userInfoOrganizationsVO);
    }*/

    @ApiOperation(value = "认证用户信息和机构信息通过接口(新)[by:wangfeng]", notes = "审核用户信息和机构信息通过")
    @PostMapping("/auditUserInfoAllPass")
    @SysLogger("auditUserInfoAllPass")
    @Transactional
    public RespDTO<Boolean> auditUserInfoAllPass(@Valid @RequestBody UserInfoAuditVO userInfoAuditVO) {
    	int ByType = 1;
    	return userFacade.auditUserInfoAlls(userInfoAuditVO,ByType);
    }
    @ApiOperation(value = "认证用户信息和机构信息不通过接口(新)[by:wangfeng]", notes = "审核用户信息和机构信息不通过")
    @PostMapping("/auditUserInfoAllNopass")
    @SysLogger("auditUserInfoAllNopass")
    @Transactional
    public RespDTO<Boolean> auditUserInfoAllNopass(@Valid @RequestBody UserInfoAuditVO userInfoAuditVO) {
        int ByType = 2;
    	return userFacade.auditUserInfoAlls(userInfoAuditVO,ByType);
    }

    @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<Boolean> addUserInfo(@Valid @RequestBody UserAndOrganizationVO userAndOrganizationVO) {

        return userFacade.addUserInfos(userAndOrganizationVO);
    }

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

        return userFacade.getUserOrganProductAlls(pageVO, pageVO.getOrgName(), pageVO.getAuthStatus());
    }*/
    
    @ApiOperation(value = "分页查询注册用户信息接口(新)[by:wangfeng]", notes = "根据每页显示size条数，默认 10，和当前页current,认证的状态authStatus")
    @PostMapping("/queryUserInformation")
    @SysLogger("queryUserInformation")
    public RespDTO<IPage<UserAllDTO>> queryUserInformation(@RequestBody UserInfoPagVO userInfoPagVO){
    	
    	return userFacade.queryUserInformations(userInfoPagVO,userInfoPagVO.getUserName(), userInfoPagVO.getAuthStatus(),userInfoPagVO.getStartTime(),userInfoPagVO.getEndTime());
    }
    
    
    @ApiOperation(value = "分页查询机构信息接口（新）[by:wangfeng]", notes = "根据每页显示size条数，默认 10，和当前页current")
    @PostMapping("/queryMechanismInformation")
    @SysLogger("queryMechanismInformation")
    public RespDTO<IPage<OrganizationDTO>> queryMechanismInformation(@RequestBody UserInfoPagVO userInfoPagVO){
    	
    	return userFacade.queryMechanismInformations(userInfoPagVO,userInfoPagVO.getOrgName(),userInfoPagVO.getStartTime(),userInfoPagVO.getEndTime());
    }
    
    
    @ApiOperation(value = "分页查询已认证用户信息和机构信息及开通产品接口（新）[by:wangfeng]", notes = "根据每页显示size条数，默认 10，和当前页current")
    @PostMapping("/queryVerifiedUserOrganizationProduct")
    @SysLogger("queryVerifiedUserOrganizationProduct")
    public RespDTO<IPage<UserOrgizationProductDTO>> queryVerifiedUserOrganizationProduct(@RequestBody UserAndOrgPageVO userAndOrgPageVO) {
		
		return userFacade.queryVerifiedUserOrganizationProducts(userAndOrgPageVO,userAndOrgPageVO.getOrgName(),userAndOrgPageVO.getUserName(),userAndOrgPageVO.getStartTime(),userAndOrgPageVO.getEndTime());
	}
   
    @ApiOperation(value = "分页查询认证中的用户信息接口（新）[by:wangfeng]", notes = "根据每页显示size条数，默认 10，和当前页current")
    @PostMapping("/queryAuthentication")
    @SysLogger("queryAuthentication")
	public RespDTO<IPage<AuthenticationDTO>> queryAuthentication(@RequestBody UserAndOrgPageVO userAndOrgPageVO) {
		
		return userFacade.queryAuthentications(userAndOrgPageVO,userAndOrgPageVO.getOrgName(),userAndOrgPageVO.getUserName(),userAndOrgPageVO.getStartTime(),userAndOrgPageVO.getEndTime());
	} 
    
    @ApiOperation(value = "超管修改用户信息接口（新）[by:wangfeng]", notes = "用户id必传")
    @PostMapping("/updateUserInfo")
    @SysLogger("updateUserInfo")
    @Transactional
    public RespDTO<Boolean> updateUserInfo (@RequestBody AmendUserInfoVO amendUserInfoVO){
		return userFacade.updateUserInfos(amendUserInfoVO);
    	
    }
    
    @ApiOperation(value = "机构信息修改接口(新)[by:wangfeng]", notes = "机构id必传")
    @PostMapping("/updateOrganizations")
    @SysLogger("updateOrganizations")
    @Transactional
    public RespDTO<Boolean> updateOrganization (@RequestBody AmendOrgVO amendOrgVO){
		return userFacade.updateOrganizations(amendOrgVO);
    	
    }
}
