/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.cqcis.com</a>
 */
package com.crunii.micro.service.sysmgr.controller;

import com.crunii.micro.common.dto.PageReq;
import com.crunii.micro.common.dto.PageRsp;
import com.crunii.micro.common.dto.Result;
import com.crunii.micro.domain.dto.Worker;
import com.crunii.micro.domain.entity.main.TsmOrg;
import com.crunii.micro.feign.client.dto.WorkerC;
import com.crunii.micro.feign.client.dto.WorkerCreateDTO;
import com.crunii.micro.service.sysmgr.api.domain.dto.OrgNode;
import com.crunii.micro.service.sysmgr.api.domain.dto.worker.WorkerRoleVo;
import com.crunii.micro.service.sysmgr.dto.org.OrgIds;
import com.crunii.micro.service.sysmgr.dto.sysmgr.*;
import com.crunii.micro.service.sysmgr.service.WorkerService;
import com.crunii.micro.web.mvc.CurrWorker;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author 田平 create 2023-02-21 10:48:49
 */
@Api(value = "员工相关")
@Slf4j
@RestController
@RequestMapping("/auth/WorkerController")
public class WorkerController {
    @Autowired
    private WorkerService workerService;


    @ApiOperation(value = "工号的分页")
    @RequestMapping(path = "/pageWorker", method = RequestMethod.POST)
    @ResponseBody
    public Result<PageRsp<WorkerInfo>> pageWorker(@RequestBody @ApiParam PageReq<WorkerReq> req) {
        log.info("req:" + req.toString());
        return Result.ofSuccess(workerService.pageWorker(req));
    }

    @ApiOperation(value = "创建用户")
    @RequestMapping(path = "/createWorker", method = RequestMethod.POST)
    @ResponseBody
    public Result<WorkerCreateDTO> createWorker(@RequestBody @ApiParam WorkerC workerC) {
        return Result.ofSuccess(workerService.createWorker(workerC));
    }

    @ApiOperation(value = "更新用户")
    @RequestMapping(path = "/updateWorker", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> updateWorker(@CurrWorker @ApiIgnore Worker worker, @Validated @RequestBody @ApiParam WorkerU workerU, HttpServletRequest request) {
        Result<String> result = new Result<>();
        workerService.updateWorker(workerU, worker.getMobile(), request.getHeader("token"));
        return result;
    }

    @ApiOperation(value = "创建用户")
    @RequestMapping(path = "/createWorkerInfo", method = RequestMethod.POST)
    @ResponseBody
    public Result<Integer> createWorkerInfo(@RequestBody @ApiParam WorkerC workerC) {
        return Result.ofSuccess(workerService.createWorkerInfo(workerC));
    }

    @ApiOperation(value = "重置用户密码为（micro@crunii）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginId", value = "工号ID", required = true)
    })
    @RequestMapping(path = "/resetPwd", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> resetPwd(int loginId) {
        return Result.ofSuccess(workerService.doResetPwd(loginId));
    }

    @ApiOperation(value = "用户修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPwd", value = "原密码", required = true),
            @ApiImplicitParam(name = "newPwd", value = "新密码", required = true)
    })
    @RequestMapping(path = "/modifyPwd", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> modifyPwd(@CurrWorker @ApiIgnore Worker worker, String oldPwd, String newPwd) {
        return workerService.doModifyPwd(worker.getLoginId(), oldPwd, newPwd);
    }

    @ApiOperation(value = "强制解锁密码错误次数限制")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "loginId", value = "工号ID", required = true)
    })
    @RequestMapping(path = "/unlockWebLoginLimit", method = RequestMethod.POST)
    @ResponseBody
    public Result<String> unlockWebLoginLimit(int loginId) {
        Result<String> result = new Result<>();
        workerService.unlockWebLoginLimit(loginId);
        return result;
    }

    @GetMapping(path = "/getTsmWorker")
    @ResponseBody
    public Result<WorkerInfo> getTsmWorker(int loginId) {
        WorkerInfo worker = workerService.getWorkerInfo(loginId);
        Result<WorkerInfo> result = new Result<>();
        result.setData(worker);
        return result;
    }

    @PostMapping(path = "/pageOrgWorker")
    @ApiOperation("分页查询当前组织下的用户")
    public Result<PageRsp<PageWorkerVo>> pageOrgWorker(@RequestBody PageReq<PageOrgWorkerDto> pageRsp) {
        return Result.ofSuccess(workerService.pageOrgWorker(pageRsp));
    }

    @ApiOperation(value = "批量新增修改用户组织权限")
    @PostMapping(path = "/createTsmWorkerOrgAll")
    public Result<String> createTsmWorkerOrgAll(@CurrWorker @ApiIgnore Worker worker,
                                                @RequestBody TsmWorkerOrgAllReqDto tsmWorkerOrgAllReqDto) {
        return this.workerService.createTsmWorkerOrgAll(worker, tsmWorkerOrgAllReqDto);
    }

    @ApiOperation(value = "根据用户id获取权限组织")
    @RequestMapping(path = "/getTsmWorkerOrgAll", method = RequestMethod.POST)
    public Result<WorkerOrgAllDto> getTsmWorkerOrgAll(@CurrWorker @ApiIgnore Worker worker, @RequestBody OrgIds req) {
        return this.workerService.getTsmWorkerOrgAll(worker, req);
    }

    @PostMapping(path = "/getTsmWorkers")
    @ResponseBody
    public Result<List<WorkerInfo>> getTsmWorkers(@RequestBody List<Integer> loginIds) {
        List<WorkerInfo> workerInfos = workerService.getTsmWorkers(loginIds);
        Result<List<WorkerInfo>> result = new Result<>();
        result.setData(workerInfos);
        return result;
    }

    @ApiOperation(value = "跳转（前端选择权限组织后跳转到右侧）")
    @RequestMapping(path = "/getOrgByOrgId", method = RequestMethod.POST)
    public Result<List<TsmOrg>> getOrgByOrgId(@CurrWorker @ApiIgnore Worker worker, @RequestBody OrgIds param) {
        return this.workerService.getOrgByOrgId(worker, param);
    }

    @GetMapping(path = "/getRoleListByWorkerId")
    @ApiOperation("查询用户所属角色")
    public Result<List<WorkerRoleVo>> getRoleListByWorkerId(@RequestParam("workerId") Long workerId) {
        return workerService.getRoleListByWorkerId(workerId);
    }

    @GetMapping(path = "/getOrgListByWorkerId")
    @ApiOperation("查询用户所属组织")
    public Result<List<OrgNode>> getOrgListByWorkerId(@RequestParam("workerId") Long workerId) {
        return workerService.getOrgListByWorkerId(workerId);
    }

    @PostMapping(path = "/getOrgManageWorker")
    @ApiOperation("查询数据权限下用户")
    public Result<List<WorkerListRspDto>> getOrgManageWorker(@RequestBody WorkerManageReq req) {
        return workerService.getOrgManageWorker(req);
    }

    @PostMapping(path = "/pageLabel")
    @ApiOperation(value = "标签数据分页查询")
    public Result<PageRsp<LabelResp>> pageLabel(@RequestBody @ApiParam PageReq<LabelReq> req){
        Result<PageRsp<LabelResp>> result = new Result<>();
        result.setData(workerService.pageLabel(req));
        return result;
    }

    @PostMapping(path = "/doSignature")
    @ApiOperation(value = "保存用户签名(web)")
    public Result<Boolean> doSignature(@CurrWorker @ApiIgnore Worker worker,@RequestBody WorkerSignatureReqVo reqVo){
        return Result.ofSuccess(workerService.doSignature(worker,reqVo));
    }

    @GetMapping(path = "/getSignQrCode")
    @ApiOperation(value = "获取签名二维码")
    public Result<QrCodeRspVo> getSignQrCode(HttpServletRequest httpServletRequest,@CurrWorker @ApiIgnore Worker worker){
        return Result.ofSuccess(workerService.getSignQrCode(worker));
    }

    @GetMapping(path = "/getSignFile")
    @ApiOperation(value = "查询APP端提交的签名文件")
    public Result<String> getSignFile(@CurrWorker @ApiIgnore Worker worker){
        return Result.ofSuccess(workerService.getSignFile(worker));
    }

    @ApiOperation("填写邮寄地址")
    @PostMapping("/doSaveDeliveryAddress")
    public Result doSaveDeliveryAddress(@CurrWorker @ApiIgnore Worker worker,@RequestBody DeliveryAddressReqVo reqVo) {
        return Result.ofSuccess(workerService.doSaveDeliveryAddress(worker,reqVo));
    }

    @ApiOperation("查询邮寄地址")
    @GetMapping("/getDeliveryAddress")
    public Result<DeliveryAddressReqVo> getDeliveryAddress(@CurrWorker @ApiIgnore Worker worker) {
        return Result.ofSuccess(workerService.getDeliveryAddress(worker));
    }

    @PostMapping(path = "/getWorkerByOrgId")
    @ApiOperation("查询组织下用户")
    public Result<List<WorkerListRspDto>> getWorkerByOrgId(@RequestBody WorkerManageReq req) {
        return workerService.getWorkerByOrgId(req);
    }
    
    
    /**
     * 创建用户
     * 会传 用户名 密码 组织id
     * 用户名唯一，无电话
     * @author leixb
     * @since 2024-01-16 10:05
     * @param workerC workerC
     * @return Result<WorkerCreateDTO>
     */
    @ApiOperation(value = "创建用户")
    @RequestMapping(path = "/createWorkerCustomPwd", method = RequestMethod.POST)
    @ResponseBody
    public Result<Integer> createWorkerCustomPwd(@RequestBody @ApiParam WorkerC workerC) {
        return Result.ofSuccess(workerService.createWorkerCustomPwd(workerC));
    }

    @RequestMapping(path = "/getWorkerByDepartment",method =RequestMethod.POST)
    @ApiOperation("根据所属部门查询用户")
    @ResponseBody
    public Result<List<WorkerListRspDto>> getWorkerByDepartment(@RequestBody WorkerManageReq req) {
        return Result.ofSuccess(workerService.getWorkerByDepartment(req));
    }
}
