package com.cpop.oam.business.controller.backstage;

import com.cpop.core.utils.bean.BeanUtils;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.base.entity.R;
import com.cpop.core.utils.SqlUtils;
import com.cpop.oam.business.bo.TechnologyToolBo;
import com.cpop.oam.business.entity.TechnologyTool;
import com.cpop.oam.business.service.TechnologyToolService;
import com.cpop.oam.business.vo.TechnologyToolPageVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.cpop.core.base.table.table.UserTableDef.USER;
import static com.cpop.oam.business.entity.table.StaffTableDef.STAFF;
import static com.cpop.oam.business.entity.table.TechnologyToolTableDef.TECHNOLOGY_TOOL;

/**
 * oam-技术工具表 控制层。
 *
 * @author DB
 * @since 2023-12-10
 */
@RestController
@Tag(name = "技术工具管理")
@RequestMapping("/backstage/technologyTool")
public class BackstageTechnologyToolController {

    @Autowired
    private TechnologyToolService technologyToolService;

    /**
     * 技术工具分页查询
     * @author DB
     * @since 2023/12/10
     * @return Page<TechnologyToolPageVo>
     */
    @GetMapping("/getTechnologyToolPage")
    @Operation(summary = "技术工具分页查询")
    public R<Page<TechnologyToolPageVo>> getTechnologyToolPage(@RequestParam("toolType") @Parameter(description = "工具类型", required = true) String toolType) {
        PageDomain pageDomain = SqlUtils.getInstance().getPageDomain();
        Page<TechnologyToolPageVo> page = technologyToolService.pageAs(Page.of(pageDomain.getPageNum(), pageDomain.getPageSize()),
                QueryWrapper.create().select(TECHNOLOGY_TOOL.ALL_COLUMNS)
                        .select(STAFF.NAME.as(TechnologyToolPageVo::getCreateUser))
                        .leftJoin(USER).on(USER.ID.eq(TECHNOLOGY_TOOL.CREATE_USER_ID))
                        .leftJoin(STAFF).on(STAFF.USER_ID.eq(USER.ID))
                        .where(TECHNOLOGY_TOOL.TOOL_TYPE.eq(toolType)),
                TechnologyToolPageVo.class);
        return R.ok(page);
    }

    /**
     * 保存技术工具
     * @author DB
     * @since 2023/12/10
     * @param bo 请求参数
     * @return R<Void>
     */
    @PostMapping("/insertTool")
    @Operation(summary = "保存技术工具")
    public R<Void> insertTool(@RequestBody @Validated TechnologyToolBo bo) {
        technologyToolService.save(BeanUtils.mapToClass(bo, TechnologyTool.class));
        return R.ok();
    }

    /**
     * 根据主键更新技术工具
     * @author DB
     * @since 2023/12/10
     * @param bo 请求参数
     * @return R<Void>
     */
    @PutMapping("/updateTool")
    @Operation(summary = "根据主键更新技术工具")
    public R<Void> update(@RequestBody @Validated TechnologyToolBo bo) {
        technologyToolService.updateById(BeanUtils.mapToClass(bo, TechnologyTool.class));
        return R.ok();
    }

    /**
     * 根据主键删除oam-技术工具表。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("/removeById/{id}")
    @Operation(summary = "根据主键删除技术工具")
    public R<Void> remove(@PathVariable @Parameter(description = "技术工具表主键") String id) {
        technologyToolService.removeById(id);
        return R.ok();
    }

}
