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

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.factoryinfo.vo.FactoryInfoPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryinfo.vo.FactoryInfoRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryinfo.vo.FactoryInfoSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factoryinfo.vo.FactorySimpleRespVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryinfo.FactoryInfoDO;
import cn.iocoder.yudao.module.smartfactory.service.factoryinfo.FactoryInfoService;
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("/smartfactory/factory-info")
@Validated
public class FactoryInfoController {

    @Resource
    private FactoryInfoService factoryInfoService;

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

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

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

    @GetMapping("/get")
    @Operation(summary = "获得工厂信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-info:query')")
    public CommonResult<FactoryInfoDO> getFactoryInfo(@RequestParam("id") Long id) {
        FactoryInfoDO factoryInfo = factoryInfoService.getFactoryInfo(id);
        return success(factoryInfo);
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得工厂列表 | 用于前端下拉框")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-info:query')")
    public CommonResult<List<FactorySimpleRespVO>> getFactoryList() {

        List<FactoryInfoDO> factoryInfo = factoryInfoService.getFactoryListByType();
        return success(BeanUtils.toBean(factoryInfo, FactorySimpleRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂信息分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-info:query')")
    public CommonResult<PageResult<FactoryInfoRespVO>> getFactoryInfoPage(@Valid FactoryInfoPageReqVO pageReqVO) {
        PageResult<FactoryInfoDO> pageResult = factoryInfoService.getFactoryInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FactoryInfoRespVO.class));
    }

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

}