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.dto.RestorationDTO;
import cn.iocoder.yudao.module.system.controller.admin.assets.vo.AssetsSecondmentPageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.assets.vo.AssetsSecondmentRespVO;
import cn.iocoder.yudao.module.system.controller.admin.assets.vo.AssetsSecondmentSaveReqVO;
import cn.iocoder.yudao.module.system.controller.admin.assets.vo.DeptAssetsInOutStockVO;
import cn.iocoder.yudao.module.system.dal.dataobject.assets.AssetsSecondmentDO;
import cn.iocoder.yudao.module.system.service.assets.AssetsSecondmentService;
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/assets-secondment")
@Validated
public class AssetsSecondmentController {

    @Resource
    private AssetsSecondmentService assetsSecondmentService;

    @PostMapping("/create")
    @Operation(summary = "创建资产借调")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment:create')")
    public CommonResult<DeptAssetsInOutStockVO> createAssetsSecondment(@RequestParam Long secondmentDeptId, @Valid @RequestBody List<AssetsSecondmentSaveReqVO> createReqVO) {
        return success(assetsSecondmentService.createAssetsSecondment(secondmentDeptId, createReqVO));
    }


    @PostMapping("/restoration")
    @Operation(summary = "借调归还")
    public CommonResult restoration(@Valid @RequestBody RestorationDTO dto) {
        assetsSecondmentService.restoration(dto);
        return success("ok");
    }

    @PutMapping("/update")
    @Operation(summary = "更新资产借调")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment:update')")
    public CommonResult<Boolean> updateAssetsSecondment(@Valid @RequestBody AssetsSecondmentSaveReqVO updateReqVO) {
        assetsSecondmentService.updateAssetsSecondment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资产借调")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:assets-secondment:delete')")
    public CommonResult<Boolean> deleteAssetsSecondment(@RequestParam("id") Long id) {
        assetsSecondmentService.deleteAssetsSecondment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资产借调")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment:query')")
    public CommonResult<AssetsSecondmentRespVO> getAssetsSecondment(@RequestParam("id") Long id) {
        AssetsSecondmentDO assetsSecondment = assetsSecondmentService.getAssetsSecondment(id);
        return success(BeanUtils.toBean(assetsSecondment, AssetsSecondmentRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得资产借调分页")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment:query')")
    public CommonResult<PageResult<AssetsSecondmentRespVO>> getAssetsSecondmentPage(@Valid AssetsSecondmentPageReqVO pageReqVO) {
        PageResult<AssetsSecondmentRespVO> pageResult = assetsSecondmentService.getAssetsSecondmentPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AssetsSecondmentRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资产借调 Excel")
    @PreAuthorize("@ss.hasPermission('system:assets-secondment:export')")
    @OperateLog(type = EXPORT)
    public void exportAssetsSecondmentExcel(@Valid AssetsSecondmentPageReqVO pageReqVO,
                                            HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AssetsSecondmentRespVO> list = assetsSecondmentService.getAssetsSecondmentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资产借调.xls", "数据", AssetsSecondmentRespVO.class,
                BeanUtils.toBean(list, AssetsSecondmentRespVO.class));
    }

}