package cn.ey.biz.module.system.controller.admin.user;

import cn.ey.biz.common.pojo.PageResult;
import cn.ey.biz.common.pojo.R;
import cn.ey.biz.module.system.api.user.dto.AdminUserRespDTO;
import cn.ey.biz.module.system.api.user.dto.UsersForKeywordsReqDTO;
import cn.ey.biz.module.system.controller.admin.user.vo.user.UserDetailRespVO;
import cn.ey.biz.module.system.convert.user.UserConvert;
import cn.ey.biz.module.system.dal.dataobject.user.AdminUserDO;
import cn.ey.biz.module.system.service.user.AdminUserService;
import cn.ey.biz.module.system.service.user.AdminUserSuperService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;

import static cn.ey.biz.common.pojo.R.success;

@Api(tags = "用户中心-其他系统前台对接")
@RestController
@RequestMapping("/system/user/center")
@Validated
public class UserCenterController {

    @Resource
    private AdminUserService userService;

    @Resource
    private AdminUserSuperService adminUserSuperService;

    @GetMapping(value = "/get-user-for-gpn-or-email")
    @ApiOperation("精确查询-通过GPN或邮箱查询用户详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gpn", value = "用户编码-GPN", example = "CN011019640", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "businessEmail", value = "公司邮箱", example = "xxxx@cn.ey.com", required = false, dataTypeClass = String.class)
    })
    public R<UserDetailRespVO> getUserForGpnOrEmail(@RequestParam(value = "gpn", required = false) String gpn,
                                                    @RequestParam(value = "businessEmail", required = false) String businessEmail
    ) {
        return success(UserConvert.INSTANCE.convertUserDetailRespVO(userService.getUserForGpnOrEmail(gpn, businessEmail)));
    }


    @GetMapping(value = "/get-combination-like-users-count")
    @ApiOperation("模糊查询-组合条件-总条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstName", value = "姓", example = "zhang", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "lastName", value = "名", example = "san", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "gpn", value = "gpn标识", example = "gpn", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "legalEntity", value = "公司主体", example = "xxxx", required = false, dataTypeClass = String.class)
    })
    public R<Long> queryCombinationLikeUsersCount(@RequestParam(value = "firstName", required = false) String firstName,
                                                  @RequestParam(value = "lastName", required = false) String lastName,
                                                  @RequestParam(value = "gpn", required = false) String gpn,
                                                  @RequestParam(value = "legalEntity", required = false) String legalEntity) {
        return success(userService.queryCombinationLikeUsersCount(firstName, lastName, gpn, legalEntity));
    }

    @GetMapping(value = "/get-combination-like-users")
    @ApiOperation("模糊查询-组合条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstName", value = "姓", example = "zhang", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "lastName", value = "名", example = "san", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "gpn", value = "gpn标识", example = "gpn", required = false, dataTypeClass = String.class),
            @ApiImplicitParam(name = "legalEntity", value = "公司主体", example = "xxxx", required = false, dataTypeClass = String.class)
    })
    public R<List<UserDetailRespVO>> queryCombinationLikeUsers(@RequestParam(value = "firstName", required = false) String firstName,
                                                               @RequestParam(value = "lastName", required = false) String lastName,
                                                               @RequestParam(value = "gpn", required = false) String gpn,
                                                               @RequestParam(value = "legalEntity", required = false) String legalEntity) {
        return success(UserConvert.INSTANCE.convertUserDetailRespVOList(
                userService.queryCombinationLikeUsers(firstName, lastName, gpn, legalEntity)));
    }

    @PostMapping(value = "/get-user-for-gpns")
    @ApiOperation("精确查询-通过GPN批量查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gpns", value = "用户编码-GPN", example = "1", required = true, dataTypeClass = Collection.class)
    })
    R<List<UserDetailRespVO>> getUserForGpns(@RequestBody Collection<String> gpns) {
        return success(UserConvert.INSTANCE.convertUserDetailRespVOList(userService.getUserForGpns(gpns)));
    }

    @GetMapping("/query-user-for-keywords")
    @ApiOperation("根据关键词模糊查询用户")
    @ApiImplicitParam(name = "keywords", value = "关键词", example = "test", dataTypeClass = String.class)
    public R<List<UserDetailRespVO>> queryUserForKeywords(@RequestParam(name = "keywords") String keywords,
                                                          @RequestParam(name = "isLikeUserName", required = false, defaultValue = "true") boolean isLikeUserName,
                                                          @RequestParam(name = "isLikeGPN", required = false, defaultValue = "true") boolean isLikeGPN,
                                                          @RequestParam(name = "isLikeEmail", required = false, defaultValue = "true") boolean isLikeEmail,
                                                          @RequestParam(name = "isOverseasStaff", required = false, defaultValue = "false") Boolean isOverseasStaff) {
        UsersForKeywordsReqDTO usersForKeywordsReqDTO = new UsersForKeywordsReqDTO();
        usersForKeywordsReqDTO.setKeywords(keywords);
        usersForKeywordsReqDTO.setIsLikeUserName(isLikeUserName);
        usersForKeywordsReqDTO.setIsLikeGPN(isLikeGPN);
        usersForKeywordsReqDTO.setIsLikeEmail(isLikeEmail);
        usersForKeywordsReqDTO.setIsOverseasStaff(isOverseasStaff);
        usersForKeywordsReqDTO.setPageNo(1);
        usersForKeywordsReqDTO.setPageSize(100);
        //使用统一实现类
        PageResult<AdminUserRespDTO> userForKeywords = adminUserSuperService.getUserForKeywords(usersForKeywordsReqDTO);
        return success(UserConvert.INSTANCE.convertVoList(userForKeywords.getList()));
    }


}
