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

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.factorydata.vo.FactoryDataPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.FactoryDataRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.FactoryDataSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factorydata.FactoryDataDO;
import cn.iocoder.yudao.module.smartfactory.service.factorydata.FactoryDataService;
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/data")
@Validated
public class FactoryDataController {

    @Resource
    private FactoryDataService dataService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂出库入库数据")
    @PreAuthorize("@ss.hasPermission('factory:data:create')")
    public CommonResult<Long> createData(@Valid @RequestBody FactoryDataSaveReqVO createReqVO) {
        return success(dataService.createData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂出库入库数据")
    @PreAuthorize("@ss.hasPermission('factory:data:update')")
    public CommonResult<Boolean> updateData(@Valid @RequestBody FactoryDataSaveReqVO updateReqVO) {
        dataService.updateData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂出库入库数据")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('factory:data:delete')")
    public CommonResult<Boolean> deleteData(@RequestParam("id") Long id) {
        dataService.deleteData(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂出库入库数据")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('factory:data:query')")
    public CommonResult<FactoryDataRespVO> getData(@RequestParam("id") Long id) {
        FactoryDataDO data = dataService.getData(id);
        return success(BeanUtils.toBean(data, FactoryDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂出库入库数据分页")
    @PreAuthorize("@ss.hasPermission('factory:data:query')")
    public CommonResult<PageResult<FactoryDataRespVO>> getDataPage(@Valid FactoryDataPageReqVO pageReqVO) {
        PageResult<FactoryDataDO> pageResult = dataService.getDataPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FactoryDataRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂出库入库数据 Excel")
    @PreAuthorize("@ss.hasPermission('factory:data:export')")
    @OperateLog(type = EXPORT)
    public void exportDataExcel(@Valid FactoryDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FactoryDataDO> list = dataService.getDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂出库入库数据.xls", "数据", FactoryDataRespVO.class,
                        BeanUtils.toBean(list, FactoryDataRespVO.class));
    }

}