package cn.iocoder.yudao.module.basic.controller.admin.workersinfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.basic.controller.admin.workersinfo.vo.WorkersInfoCreateReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.workersinfo.vo.WorkersInfoPageReqVO;
import cn.iocoder.yudao.module.basic.controller.admin.workersinfo.vo.WorkersInfoRespVO;
import cn.iocoder.yudao.module.basic.controller.admin.workersinfo.vo.WorkersInfoUpdateReqVO;
import cn.iocoder.yudao.module.basic.convert.workersinfo.WorkersInfoConvert;
import cn.iocoder.yudao.module.basic.dal.dataobject.workersinfo.WorkersInfoDO;
import cn.iocoder.yudao.module.basic.service.workersinfo.WorkersInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 员工")
@RestController
@RequestMapping("/basic/workers-info")
@Validated
public class WorkersInfoController {

    @Resource
    private WorkersInfoService workersInfoService;

    @PostMapping("/create")
    @ApiOperation("创建员工")
    @PreAuthorize("@ss.hasPermission('basic:workers-info:create')")
    public CommonResult<Long> createWorkersInfo(@Valid @RequestBody WorkersInfoCreateReqVO createReqVO) {
        return success(workersInfoService.createWorkersInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新员工")
    @PreAuthorize("@ss.hasPermission('basic:workers-info:update')")
    public CommonResult<Boolean> updateWorkersInfo(@Valid @RequestBody WorkersInfoUpdateReqVO updateReqVO) {
        workersInfoService.updateWorkersInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除员工")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:workers-info:delete')")
    public CommonResult<Boolean> deleteWorkersInfo(@RequestParam("id") Long id) {
        workersInfoService.deleteWorkersInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得员工")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('basic:workers-info:query')")
    public CommonResult<WorkersInfoRespVO> getWorkersInfo(@RequestParam("id") Long id) {
        WorkersInfoDO workersInfo = workersInfoService.getWorkersInfo(id);
        return success(WorkersInfoConvert.INSTANCE.convert(workersInfo));
    }

    @GetMapping("/page")
    @ApiOperation("获得员工分页")
    @PreAuthorize("@ss.hasPermission('basic:workers-info:query')")
    public CommonResult<PageResult<WorkersInfoRespVO>> getWorkersInfoPage(@Valid WorkersInfoPageReqVO pageVO) {
        PageResult<WorkersInfoDO> pageResult = workersInfoService.getWorkersInfoPage(pageVO);
        return success(WorkersInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getFindByBelongBusinessId")
    @ApiOperation("根据指定的商户ID获取所有的员工列表")
    @PreAuthorize("@ss.hasPermission('basic:workers-info:query')")
    @ApiImplicitParam(name = "belongBusinessId", value = "所属商户id", required = true, example = "1024", dataTypeClass = Long.class)
    public CommonResult<List<WorkersInfoDO>> getFindByBelongBusinessId(@RequestParam("belongBusinessId") Long belongBusinessId) {
        List<WorkersInfoDO> belongBusinessName = workersInfoService.findByBelongBusinessId(belongBusinessId);
        return success(belongBusinessName);
    }

    @PutMapping("/unbindingWX")
    @ApiOperation("解绑微信")
    @PreAuthorize("@ss.hasPermission('basic:workers-info:update')")
    public CommonResult<Boolean> unbindingWX(@RequestParam("id") Long id) {
        workersInfoService.unbindingWX(id);
        return success(true);
    }

}
