package com.ruoyi.web.controller.system;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.web.controller.BaseController;
import com.ruoyi.common.web.domain.PageData;
import com.ruoyi.framework.annotation.RyLog;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.service.ISysPostService;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import org.noear.solon.validation.annotation.Validated;

import java.util.Arrays;
import java.util.List;

/**
 * 岗位 操作处理
 * @author ruoyi
 */
@Controller
@Mapping("system/post")
public class SysPostController extends BaseController {

    @Inject
    ISysPostService sysPostService;

    /**
     * 查询岗位列表
     */
    @AuthPermissions("system:post:list")
    @Get
    @Mapping("list")
    public PageData<SysPost> list(Page<SysPost> page, SysPost sysPost) {
        QueryWrapper qw = getQW(sysPost);
        Page<SysPost> result = sysPostService.page(page, qw);
        return getPageData(result);
    }

    /**
     * 导出岗位列表
     */
    @RyLog(title = "岗位", businessType = BusinessType.EXPORT)
    @AuthPermissions("system:post:export")
    @Post
    @Mapping("export")
    public void export(SysPost sysPost) {
        QueryWrapper qw = getQW(sysPost);
        List<SysPost> list = sysPostService.list(qw);
        ExcelUtil.export(list);
    }

    /**
     * 查询岗位详情
     */
    @AuthPermissions("system:post:query")
    @Get
    @Mapping("{id}")
    public SysPost info(@Path Long id) {
        return sysPostService.getById(id);
    }

    /**
     * 新增岗位
     */
    @RyLog(title = "岗位", businessType = BusinessType.INSERT)
    @AuthPermissions("system:post:add")
    @Post
    @Mapping
    public Result<Void> add(@Body @Validated SysPost sysPost) {
        return toResult(sysPostService.add(sysPost));
    }

    /**
     * 修改岗位
     */
    @RyLog(title = "岗位", businessType = BusinessType.UPDATE)
    @AuthPermissions("system:post:edit")
    @Put
    @Mapping
    public Result<Void> edit(@Body @Validated SysPost sysPost) {
        return toResult(sysPostService.edit(sysPost));
    }

    /**
     * 删除岗位
     */
    @RyLog(title = "岗位", businessType = BusinessType.DELETE)
    @AuthPermissions("system:post:remove")
    @Delete
    @Mapping("{postIds}")
    public Result<Void> delete(@Path Long[] postIds) {
        sysPostService.delete(Arrays.asList(postIds));
        return Result.succeed();
    }

    /**
     * 获取岗位选择框列表
     */
    @Get
    @Mapping("optionselect")
    public List<SysPost> optionselect() {
        return sysPostService.list();
    }

    private QueryWrapper getQW(SysPost sysPost) {
        QueryWrapper qw = QueryWrapper.create();
        qw.and(SysPost::getPostCode).like(sysPost.getPostCode());
        qw.and(SysPost::getPostName).like(sysPost.getPostName());
        qw.and(SysPost::getStatus).eq(sysPost.getStatus());
        return qw;
    }

}
