package cn.iocoder.yudao.module.smartfactory.controller.admin.worktype;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.worktype.vo.WorkTypePageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.worktype.vo.WorkTypeRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.worktype.vo.WorkTypeSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.worktype.WorkTypeDO;
import cn.iocoder.yudao.module.smartfactory.service.worktype.WorkTypeService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 工种")
@RestController
@RequestMapping("/factory/work-type")
@Validated
public class WorkTypeController {

    @Resource
    private WorkTypeService workTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建工种")
    @PreAuthorize("@ss.hasPermission('factory:work-type:create')")
    public CommonResult<Long> createWorkType(@Valid @RequestBody WorkTypeSaveReqVO createReqVO) {
        return success(workTypeService.createWorkType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工种")
    @PreAuthorize("@ss.hasPermission('factory:work-type:update')")
    public CommonResult<Boolean> updateWorkType(@Valid @RequestBody WorkTypeSaveReqVO updateReqVO) {
        workTypeService.updateWorkType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工种")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('factory:work-type:delete')")
    public CommonResult<Boolean> deleteWorkType(@RequestParam("id") Long id) {
        workTypeService.deleteWorkType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工种")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('factory:work-type:query')")
    public CommonResult<WorkTypeRespVO> getWorkType(@RequestParam("id") Long id) {
        WorkTypeDO workType = workTypeService.getWorkType(id);
        return success(BeanUtils.toBean(workType, WorkTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工种分页")
    @PreAuthorize("@ss.hasPermission('factory:work-type:query')")
    public CommonResult<PageResult<WorkTypeRespVO>> getWorkTypePage(@Valid WorkTypePageReqVO pageReqVO) {
        PageResult<WorkTypeDO> pageResult = workTypeService.getWorkTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WorkTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工种 Excel")
    @PreAuthorize("@ss.hasPermission('factory:work-type:export')")
    @OperateLog(type = EXPORT)
    public void exportWorkTypeExcel(@Valid WorkTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkTypeDO> list = workTypeService.getWorkTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工种.xls", "数据", WorkTypeRespVO.class,
                        BeanUtils.toBean(list, WorkTypeRespVO.class));
    }

}