package com.xjrsoft.module.ledger.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.module.ledger.dto.AddWfSubscriptionDto;
import com.xjrsoft.module.ledger.dto.WfSubscriptionPageDto;
import com.xjrsoft.module.ledger.entity.WfSubscription;
import com.xjrsoft.module.ledger.service.IWfSubscriptionService;
import com.xjrsoft.module.ledger.vo.WfSubscriptionExcelVo;
import com.xjrsoft.module.ledger.vo.WfSubscriptionListInfoVo;
import com.xjrsoft.module.ledger.vo.WfSubscriptionPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.ByteArrayOutputStream;
import java.util.List;

/**
* @title: 物品申购
* @Author dzx
* @Date: 2024-02-19
* @Version 1.0
*/
@RestController
@RequestMapping("/ledger" + "/wfSubscription")
@Api(value = "/ledger"  + "/wfSubscription",tags = "物品申购代码")
@AllArgsConstructor
public class WfSubscriptionController {


    private final IWfSubscriptionService wfSubscriptionService;

    @GetMapping(value = "/page")
    @ApiOperation(value="物品申购台账列表(分页)")
    @SaCheckPermission("wfsubscription:detail")
    public RT<PageOutput<WfSubscriptionPageVo>> page(@Valid WfSubscriptionPageDto dto){
        IPage<WfSubscriptionPageVo> page = wfSubscriptionService.getPage(new Page<>(dto.getLimit(), dto.getSize()), dto);
        PageOutput<WfSubscriptionPageVo> pageOutput = ConventPage.getPageOutput(page, WfSubscriptionPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/subscription-list")
    @ApiOperation(value="查看物品清单")
    @SaCheckPermission("wfsubscription:detail")
    public RT<WfSubscriptionListInfoVo> subscriptionPage(@RequestParam Long id){
        WfSubscriptionListInfoVo wfSubscription = wfSubscriptionService.getSubscriptionList(id);
        if (wfSubscription == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(wfSubscription);
    }


    @PostMapping
    @ApiOperation(value = "新增物品申购")
    @SaCheckPermission("wfsubscription:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfSubscriptionDto dto){
        WfSubscription wfSubscription = BeanUtil.toBean(dto, WfSubscription.class);
        boolean isSuccess = wfSubscriptionService.add(wfSubscription);
        return RT.ok(isSuccess);
    }


    @PostMapping("/export-query")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid @RequestBody WfSubscriptionPageDto dto) {
        List<WfSubscriptionExcelVo> customerList = wfSubscriptionService.getList(dto);
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, WfSubscriptionExcelVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

        return RT.fileStream(bot.toByteArray(), "WfSubscription" + ExcelTypeEnum.XLSX.getValue());
    }
}