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.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.FactoryDataDetailRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.FactoryDataPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.FactoryDataTotalVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.factorydata.vo.sizeData.FactoryDataSizeCreateReqVO;
import cn.iocoder.yudao.module.smartfactory.service.factorydata.FactoryDataService;
import cn.iocoder.yudao.module.smartfactory.service.factorydata.FactoryDataSizeService;
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-data")
@Validated
public class FactoryDataController {

    @Resource
    private FactoryDataService dataService;

    @Resource
    private FactoryDataSizeService factoryDataSizeService;

    @PostMapping("/create-size")
    @Operation(summary = "创建工厂出库入库规格数据")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-data:create')")
    public CommonResult<Long> createSizeData(@Valid @RequestBody FactoryDataSizeCreateReqVO createReqVO) {

        return success(factoryDataSizeService.createData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂出库入库数据")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-data:update')")
    public CommonResult<Boolean> updateData(@Valid @RequestBody FactoryDataSizeCreateReqVO updateReqVO) {

        dataService.updateData(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂出库入库数据")
    @Parameter(name = "factoryId", description = "工厂编号", required = true, example = "1000078")
    @Parameter(name = "date", description = "日期", required = true, example = "2024-05-17")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-data:delete')")
    public CommonResult<Boolean> deleteData(@RequestParam("factoryId") Long factoryId, @RequestParam("date") String date) {

        dataService.deleteData(factoryId, date);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂出库入库数据")
    @Parameter(name = "factoryId", description = "工厂编号", required = true, example = "1000078")
    @Parameter(name = "date", description = "日期", required = true, example = "2024-05-17")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-data:query')")
    public CommonResult<FactoryDataDetailRespVO> getData(@RequestParam("factoryId") Long factoryId, @RequestParam("date") String date) {

        return success(dataService.getData(factoryId, date));
    }

    @GetMapping("/get-total")
    @Operation(summary = "获得出入库数据总数 | 入库总数、出库总数、破损总数")
    @PreAuthorize("@ss.hasPermission('smartfactory:factory-data:query')")
    public CommonResult<FactoryDataTotalVO> getDataTotal() {

        return success(dataService.getDataTotal());
    }

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

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