package com.zcloud.basic.info.web;


import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.jjb.saas.framework.auth.model.SSOUser;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.zcloud.basic.info.api.UserServiceI;
import com.zcloud.basic.info.dto.*;
import com.zcloud.basic.info.dto.clientobject.UserCO;
import com.zcloud.basic.info.persistence.dataobject.UserJobHandoverDO;
import com.zcloud.basic.info.plan.DockingRZUserPlanJob;
import com.zcloud.basic.info.dto.clientobject.UserXmfCO;
import com.zcloud.gbscommon.translateaop.TranslateField;
import com.zcloud.gbscommon.zclouduser.facade.ZcloudUserJobFacade;
import com.zcloud.gbscommon.zclouduser.request.ZcloudUserJobQry;
import com.zcloud.gbscommon.zclouduser.response.ZcloudUserJobCo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * web-adapter
 *
 * @Author zhangyue
 * @Date 2025-11-04 14:07:33
 */
@Api(tags = "用户信息表")
@RequestMapping("/${application.gateway}/user")
@RestController
@AllArgsConstructor
public class UserController {
    private final UserServiceI userService;
    private final TranslateField translateField;

    @ApiOperation("验证用户是否存在")
    @PostMapping("/verifyUser")
    public Response verifyUser(@Validated @RequestBody UserVerifyQryCmd userVerifyQryCmd) {
        return userService.verifyUser(userVerifyQryCmd);
    }

    @ApiOperation("新增用户")
    @PostMapping("/save")
    public SingleResponse<UserCO> add(@Validated @RequestBody UserAddCmd cmd) {
        return userService.add(cmd);
    }

    @ApiOperation("新增相关方用户")
    @PostMapping("/saveXgf")
    public SingleResponse<UserCO> addXgf(@Validated @RequestBody UserXgfAddCmd cmd) {
        SSOUser ssoUser = AuthContext.getCurrentUser();
        return userService.addXgf(cmd);
    }

    @ApiOperation("分页")
    @PostMapping("/list")
    @PreAuthorize("@pms.hasAnyPermission('zhgl2','yhglqyd','qydqyyhzt')")
    public PageResponse<UserCO> page(@RequestBody UserPageQry qry) {
        return userService.listPage(qry);
    }

    @ApiOperation("其他页面的用户分页，不需要权限控制")
    @PostMapping("/pageByNopermission")
    public PageResponse<UserCO> pageByNopermission(@RequestBody UserPageQry qry) {
        return userService.pageByNopermission(qry);
    }

    @ApiOperation("所有数据")
    @GetMapping("/listAll")
    public MultiResponse<UserCO> listAll(UserQryCmd userQryCmd) {
        return userService.listAll(userQryCmd);
    }


    @ApiOperation("详情")
    @GetMapping("/{id}")
    public SingleResponse<UserCO> getInfoById(@PathVariable("id") Long id) {
        return userService.getInfoById(id);
    }
    @ApiOperation("详情")
    @GetMapping("/getInfoByUserName/{userName}")
    public SingleResponse<UserCO> getInfoByUserName(@PathVariable("userName") String userName) {
        return userService.getInfoByUserName(userName);
    }
    @ApiOperation("获取当前登录人信息")
    @GetMapping("/getInfo")
    public SingleResponse<UserCO> getInfo() {
        return userService.getInfoBySession();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public Response remove(@PathVariable("id") Long id) {
        userService.remove(id);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("删除多个")
    @DeleteMapping("/ids")
    public Response removeBatch(@RequestParam Long[] ids) {
        userService.removeBatch(ids);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("修改")
    @PutMapping("/edit")
    public SingleResponse edit(@Validated @RequestBody UserUpdateCmd userUpdateCmd) {
        userService.edit(userUpdateCmd);
        return SingleResponse.buildSuccess();
    }



    @ApiOperation("修改")
    @PutMapping("/editData")
    public SingleResponse editData (UserQryCmd userUpdateCmd) {
        userService.editData(userUpdateCmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("导出用户列表")
    @GetMapping("/exportUserTable")
    public void exportUserTable(@RequestParam(value = "ids") Long[] ids, HttpServletResponse httpServletResponse) {
        userService.exportUserTable(httpServletResponse, ids, AuthContext.getTenantId());
    }

    @ApiOperation("导入用户列表")
    @PostMapping("/importUserTable")
    public Response importUserTable(@RequestPart(value = "file") MultipartFile file) {
        userService.importUserTable(file, 1);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("导入用户列表")
    @PostMapping("/importUserTableByCorp2")
    public Response importUserTableByCorp2(@RequestPart(value = "file") MultipartFile file) {
        userService.importUserTable(file, 2);
        return SingleResponse.buildSuccess();
    }


    @ApiOperation("重置密码")
    @PostMapping("/changePassword/{id}")
    public SingleResponse changePassword(@PathVariable Long id){
        userService.changePassword(id);
        return SingleResponse.buildSuccess();
    }


    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public Response updatePassword(@Validated @RequestBody UserUpdatePassWordCmd userUpdatePassWordCmd){
        return userService.updatePassword(userUpdatePassWordCmd);
    }
    @ApiOperation("根据登录人密码获取查询人手机号和身份证号")
    @PostMapping("/getEncryInfo")
    public SingleResponse<UserCO> getEncryInfo(@Validated @RequestBody UserCheckPassWordCmd userCheckPassWordCmd) {
        return userService.getEncryInfo(userCheckPassWordCmd);
    }

    @ApiOperation("相关方用户列表分页")
    @PostMapping("/listXgf")
    @PreAuthorize("@pms.hasAnyPermission('yhglxgf')")
    public PageResponse<UserXmfCO> listXgf(@RequestBody UserPageQry qry) {
        return userService.listXgf(qry);
    }
    @ApiOperation("PC离职")
    @PostMapping("/resignation")
    public SingleResponse resignation(@RequestBody UserResignationCmd userResignationCmd) {
         userService.resignation(userResignationCmd);
        return SingleResponse.buildSuccess();
    }
    @ApiOperation("PC审核通过-驳回")
    @PostMapping("/reviewStatus")
    public SingleResponse reviewStatus(@RequestBody UserReviewStatusCmd userReviewStatusCmd) {
        userService.reviewStatus(userReviewStatusCmd);
        return SingleResponse.buildSuccess();
    }

    @ApiOperation("根据手机号获取用户userId")
    @PostMapping("/getUserIdByPhone")
    public SingleResponse<UserCO> getUserIdByPhone(@Validated @RequestBody GetUserIdByPhoneCmd getUserIdByPhoneCmd) {
        return userService.getUserIdByPhone(getUserIdByPhoneCmd);
    }

}

