package org.dromara.mp.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.log.enums.OperatorType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.mp.domain.vo.UserWorkerVo;
import org.dromara.mp.domain.bo.UserWorkerBo;
import org.dromara.mp.service.IUserWorkerService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 自雇者
 * 前端访问路由地址为:/mp/worker
 *
 * @author LionLi
 * @date 2024-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/worker")
public class UserWorkerController extends BaseController {

    private final IUserWorkerService userWorkerService;








    /**
     * 查询自雇者列表
     */
//    @SaCheckPermission("mp:worker:list")
    @GetMapping("/list")
    @Log(title = "查询自雇者列表", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public TableDataInfo<UserWorkerVo> list(UserWorkerBo bo, PageQuery pageQuery) {
        return userWorkerService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出自雇者列表
     */
//    @SaCheckPermission("mp:worker:export")
    @Log(title = "自雇者", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserWorkerBo bo, HttpServletResponse response) {
        List<UserWorkerVo> list = userWorkerService.queryList(bo);
        ExcelUtil.exportExcel(list, "自雇者", UserWorkerVo.class, response);
    }

    /**
     * 获取自雇者详细信息
     *
     * @param id 主键
     */
//    @SaCheckPermission("mp:worker:query")
    @GetMapping("/{id}")
    @Log(title = "获取自雇者详细信息", businessType = BusinessType.SELECT,operatorType= OperatorType.MOBILE)
    public R<UserWorkerVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userWorkerService.queryById(id));
    }

    /**
     * 新增自雇者
     */
//    @SaCheckPermission("mp:worker:add")
    @Log(title = "自雇者", businessType = BusinessType.INSERT)
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody UserWorkerBo bo) {
        return toAjax(userWorkerService.insertByBo(bo));
    }

    /**
     * 修改自雇者
     */
//    @SaCheckPermission("mp:worker:edit")
    @Log(title = "自雇者", businessType = BusinessType.UPDATE)
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserWorkerBo bo) {
        return toAjax(userWorkerService.updateByBo(bo));
    }

    /**
     * 删除自雇者
     *
     * @param ids 主键串
     */
//    @SaCheckPermission("mp:worker:remove")
    @Log(title = "自雇者", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userWorkerService.deleteWithValidByIds(List.of(ids), true));
    }


}
