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

import com.cpop.common.utils.bean.BeanUtils;
import com.cpop.core.base.R;
import com.cpop.core.base.entity.PageDomain;
import com.cpop.core.utils.SpringUtils;
import com.cpop.core.utils.sql.SqlUtils;
import com.cpop.oam.business.bo.EnforceFinishWorkOrderBo;
import com.cpop.oam.business.bo.TechnologyToolBo;
import com.cpop.oam.business.vo.TechnologyToolPageVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.cpop.oam.business.entity.TechnologyTool;
import com.cpop.oam.business.service.TechnologyToolService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.Serializable;
import java.util.List;

import static com.cpop.core.base.table.table.SysUserTableDef.SYS_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
@Api(tags = "技术工具管理")
@RequestMapping("/backstage/technologyTool")
public class BackstageTechnologyToolController {

    @Autowired
    private TechnologyToolService technologyToolService;

    /**
     * 技术工具分页查询
     * @author DB
     * @since 2023/12/10
     * @return Page<TechnologyToolPageVo>
     */
    @GetMapping("/getTechnologyToolPage")
    @ApiOperation("技术工具分页查询")
    public R<Page<TechnologyToolPageVo>> getTechnologyToolPage(@RequestParam("toolType") @ApiParam(value = "工具类型", 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(SYS_USER).on(SYS_USER.ID.eq(TECHNOLOGY_TOOL.CREATE_USER_ID))
                        .leftJoin(STAFF).on(STAFF.USER_ID.eq(SYS_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")
    @ApiOperation("保存技术工具")
    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")
    @ApiOperation("根据主键更新技术工具")
    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}")
    @ApiOperation("根据主键删除技术工具")
    public R<Void> remove(@PathVariable @ApiParam("技术工具表主键") String id) {
        technologyToolService.removeById(id);
        return R.ok();
    }

}
