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

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.packagedata.vo.PackageDataCreateReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackageDataRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.packagedata.vo.PackagePageDataRespVO;
import cn.iocoder.yudao.module.smartfactory.convert.packageData.PackageDataConvert;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.packagedata.PackageDataDO;
import cn.iocoder.yudao.module.smartfactory.service.packagedata.PackageDataService;
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/package-data")
@Validated
public class PackageDataController {

    @Resource
    private PackageDataService packageDataService;

    @PostMapping("/create")
    @Operation(summary = "创建打包线数据")
    @PreAuthorize("@ss.hasPermission('smartfactory:package-data:create')")
    public CommonResult<Long> createPackageData(@Valid @RequestBody PackageDataCreateReqVO createReqVO) {
        return success(packageDataService.createPackageData(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新打包线数据")
    @PreAuthorize("@ss.hasPermission('smartfactory:package-data:update')")
    public CommonResult<Boolean> updatePackageData(@Valid @RequestBody PackageDataCreateReqVO updateReqVO) {
        packageDataService.updatePackageData(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-14")
    @PreAuthorize("@ss.hasPermission('smartfactory:package-data:delete')")
    public CommonResult<Boolean> deletePackageData(@RequestParam("factoryId") Long factoryId, @RequestParam("date") String date) {

        packageDataService.deletePackageData(factoryId, date);
        return success(true);
    }

    @GetMapping("/get-list")
    @Operation(summary = "获得指定 工厂和打包日期的 打包线数据列表")
    @Parameter(name = "factoryId", description = "工厂编号", required = true, example = "1000078")
    @Parameter(name = "date", description = "打包日期", required = true, example = "2024-05-14")
    @PreAuthorize("@ss.hasPermission('smartfactory:package-data:query')")
    public CommonResult<List<PackageDataRespVO>> getListByFactoryIdAndDate(@RequestParam("factoryId") Long factoryId, @RequestParam("date") String date) {

        List<PackageDataDO> packageData = packageDataService.getListByFactoryIdAndDate(factoryId, date);

        return success(BeanUtils.toBean(packageData, PackageDataRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得打包线数据分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:package-data:query')")
    public CommonResult<PageResult<PackagePageDataRespVO>> getPackageDataPage(@Valid PackageDataPageReqVO pageReqVO) {

        PageResult<PackageDataRespVO> pageResult = packageDataService.getPackageDataPage(pageReqVO);

        List<PackagePageDataRespVO> dataRespVOS = PackageDataConvert.INSTANCE.convertList1(pageResult.getList());
        return success(new PageResult<>(dataRespVOS, pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出打包线数据 Excel")
    @PreAuthorize("@ss.hasPermission('smartfactory:package-data:export')")
    @OperateLog(type = EXPORT)
    public void exportPackageDataExcel(@Valid PackageDataPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {

        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PackageDataRespVO> list = packageDataService.getPackageDataPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "打包线数据.xls", "数据", PackageDataRespVO.class, list);
    }

}