package cn.iocoder.yudao.module.system.controller.admin.assets;

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.system.controller.admin.assets.vo.*;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.DeptAssetsInOutStockDO;
import cn.iocoder.yudao.module.system.service.assets.DeptAssetsInOutStockService;
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("/system/dept-assets-in-out-stock")
@Validated
public class DeptAssetsInOutStockController {

    @Resource
    private DeptAssetsInOutStockService deptAssetsInOutStockService;

    @PostMapping("/create")
    @Operation(summary = "创建部门资产出入库")
    @PreAuthorize("@ss.hasPermission('system:dept-assets-in-out-stock:create')")
    public CommonResult<DeptAssetsInOutStockVO> createDeptAssetsInOutStock(@RequestParam(required = false) Long outDeptId,
                                                                           @RequestBody List<DeptAssetsInOutStockSaveReqVO> vos) {
        DeptAssetsInOutStockVO vo = deptAssetsInOutStockService.createDeptAssetsInOutStock(outDeptId == null ? -1L : outDeptId, vos);
        return success(vo);
    }


    @GetMapping("/getInOutStockGroupByAssetsId")
    @Operation(summary = "部门资产合计列表")
    @PreAuthorize("@ss.hasPermission('system:dept-assets-in-out-stock:create')")
    public CommonResult<List<InOutStockGroupByAssetsIdVO>> getInOutStockGroupByAssetsId(@RequestParam Long deptId,
                                                                                        @RequestParam Long assetsId) {
        List<InOutStockGroupByAssetsIdVO> vos = deptAssetsInOutStockService.getInOutStockGroupByAssetsId(deptId, assetsId);
        return success(vos);
    }

    @GetMapping("/get")
    @Operation(summary = "获得部门资产出入库")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:dept-assets-in-out-stock:query')")
    public CommonResult<DeptAssetsInOutStockRespVO> getDeptAssetsInOutStock(@RequestParam("id") Long id) {
        DeptAssetsInOutStockDO deptAssetsInOutStock = deptAssetsInOutStockService.getDeptAssetsInOutStock(id);
        return success(BeanUtils.toBean(deptAssetsInOutStock, DeptAssetsInOutStockRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得部门资产出入库分页")
    @PreAuthorize("@ss.hasPermission('system:dept-assets-in-out-stock:query')")
    public CommonResult<PageResult<DeptAssetsInOutStockRespVO>> getDeptAssetsInOutStockPage(@Valid DeptAssetsInOutStockPageReqVO pageReqVO) {
        PageResult<DeptAssetsInOutStockRespVO> pageResult = deptAssetsInOutStockService.getDeptAssetsInOutStockPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeptAssetsInOutStockRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出部门资产出入库 Excel")
    @PreAuthorize("@ss.hasPermission('system:dept-assets-in-out-stock:export')")
    @OperateLog(type = EXPORT)
    public void exportDeptAssetsInOutStockExcel(@Valid DeptAssetsInOutStockPageReqVO pageReqVO,
                                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeptAssetsInOutStockRespVO> list = deptAssetsInOutStockService.getDeptAssetsInOutStockPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "部门资产出入库.xls", "数据", DeptAssetsInOutStockRespVO.class,
                BeanUtils.toBean(list, DeptAssetsInOutStockRespVO.class));
    }

}