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

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.factory.vo.SfFactoryInfoPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factory.vo.SfFactoryInfoRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factory.vo.SfFactoryInfoSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factory.SfFactoryInfoDO;
import cn.iocoder.yudao.module.smartfactory.service.factory.SfFactoryInfoService;
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/sf-factory-info")
@Validated
public class SfFactoryInfoController {

    @Resource
    private SfFactoryInfoService sfFactoryInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂信息")
    @PreAuthorize("@ss.hasPermission('factory:sf-factory-info:create')")
    public CommonResult<Long> createSfFactoryInfo(@Valid @RequestBody SfFactoryInfoSaveReqVO createReqVO) {
        return success(sfFactoryInfoService.createSfFactoryInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂信息")
    @PreAuthorize("@ss.hasPermission('factory:sf-factory-info:update')")
    public CommonResult<Boolean> updateSfFactoryInfo(@Valid @RequestBody SfFactoryInfoSaveReqVO updateReqVO) {
        sfFactoryInfoService.updateSfFactoryInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('factory:sf-factory-info:delete')")
    public CommonResult<Boolean> deleteSfFactoryInfo(@RequestParam("id") Long id) {
        sfFactoryInfoService.deleteSfFactoryInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('factory:sf-factory-info:query')")
    public CommonResult<SfFactoryInfoRespVO> getSfFactoryInfo(@RequestParam("id") Long id) {
        SfFactoryInfoDO sfFactoryInfo = sfFactoryInfoService.getSfFactoryInfo(id);
        return success(BeanUtils.toBean(sfFactoryInfo, SfFactoryInfoRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂信息分页")
    @PreAuthorize("@ss.hasPermission('factory:sf-factory-info:query')")
    public CommonResult<PageResult<SfFactoryInfoRespVO>> getSfFactoryInfoPage(@Valid SfFactoryInfoPageReqVO pageReqVO) {
        PageResult<SfFactoryInfoDO> pageResult = sfFactoryInfoService.getSfFactoryInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SfFactoryInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂信息 Excel")
    @PreAuthorize("@ss.hasPermission('factory:sf-factory-info:export')")
    @OperateLog(type = EXPORT)
    public void exportSfFactoryInfoExcel(@Valid SfFactoryInfoPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SfFactoryInfoDO> list = sfFactoryInfoService.getSfFactoryInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂信息.xls", "数据", SfFactoryInfoRespVO.class,
                        BeanUtils.toBean(list, SfFactoryInfoRespVO.class));
    }

}