package com.xjrsoft.module.asset.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.common.constant.GlobalConstant;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.model.result.RT;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.asset.dto.AddWfAssetManageInventoryDto;
import com.xjrsoft.module.asset.dto.UpdateWfAssetManageInventoryDto;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.alibaba.excel.EasyExcel;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.springframework.http.ResponseEntity;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;

import com.xjrsoft.module.asset.dto.WfAssetManageInventoryPageDto;
import com.xjrsoft.module.asset.entity.WfAssetManageInventory;
import com.xjrsoft.module.asset.service.IWfAssetManageInventoryService;
import com.xjrsoft.module.asset.vo.WfAssetManageInventoryPageVo;

import com.xjrsoft.module.asset.vo.WfAssetManageInventoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
* @title: 资产管理盘点
* @Author szs
* @Date: 2024-04-07
* @Version 1.0
*/
@RestController
@RequestMapping("/asset" + "/wfAssetManageInventory")
@Api(value = "/asset"  + "/wfAssetManageInventory",tags = "资产管理盘点代码")
@AllArgsConstructor
public class WfAssetManageInventoryController {


    private final IWfAssetManageInventoryService wfAssetManageInventoryService;

    @GetMapping(value = "/page")
    @ApiOperation(value="资产管理盘点列表(分页)")
    @SaCheckPermission("wfassetmanageinventory:detail")
    public RT<PageOutput<WfAssetManageInventoryPageVo>> page(@Valid WfAssetManageInventoryPageDto dto){

        LambdaQueryWrapper<WfAssetManageInventory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper
                    .orderByDesc(WfAssetManageInventory::getId)
                .select(WfAssetManageInventory.class,x -> VoToColumnUtil.fieldsToColumns(WfAssetManageInventoryPageVo.class).contains(x.getProperty()));
        IPage<WfAssetManageInventory> page = wfAssetManageInventoryService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<WfAssetManageInventoryPageVo> pageOutput = ConventPage.getPageOutput(page, WfAssetManageInventoryPageVo.class);
        return RT.ok(pageOutput);
    }

    @GetMapping(value = "/info")
    @ApiOperation(value="根据id查询资产管理盘点信息")
    @SaCheckPermission("wfassetmanageinventory:detail")
    public RT<WfAssetManageInventoryVo> info(@RequestParam Long id){
        WfAssetManageInventory wfAssetManageInventory = wfAssetManageInventoryService.getById(id);
        if (wfAssetManageInventory == null) {
           return RT.error("找不到此数据！");
        }
        return RT.ok(BeanUtil.toBean(wfAssetManageInventory, WfAssetManageInventoryVo.class));
    }


    @PostMapping
    @ApiOperation(value = "新增资产管理盘点")
    @SaCheckPermission("wfassetmanageinventory:add")
    public RT<Boolean> add(@Valid @RequestBody AddWfAssetManageInventoryDto dto){
        WfAssetManageInventory wfAssetManageInventory = BeanUtil.toBean(dto, WfAssetManageInventory.class);
        boolean isSuccess = wfAssetManageInventoryService.save(wfAssetManageInventory);
    return RT.ok(isSuccess);
    }

    @PutMapping
    @ApiOperation(value = "修改资产管理盘点")
    @SaCheckPermission("wfassetmanageinventory:edit")
    public RT<Boolean> update(@Valid @RequestBody UpdateWfAssetManageInventoryDto dto){

        WfAssetManageInventory wfAssetManageInventory = BeanUtil.toBean(dto, WfAssetManageInventory.class);
        return RT.ok(wfAssetManageInventoryService.updateById(wfAssetManageInventory));

    }

    @DeleteMapping
    @ApiOperation(value = "删除资产管理盘点")
    @SaCheckPermission("wfassetmanageinventory:delete")
    public RT<Boolean> delete(@Valid @RequestBody List<Long> ids){
        return RT.ok(wfAssetManageInventoryService.removeBatchByIds(ids));

    }
    @PostMapping("/import")
    @ApiOperation(value = "导入")
    public RT<Boolean> importData(@RequestParam MultipartFile file) throws IOException {
        List<WfAssetManageInventoryPageVo> savedDataList = EasyExcel.read(file.getInputStream()).head(WfAssetManageInventoryPageVo.class).sheet().doReadSync();
        Boolean result = wfAssetManageInventoryService.saveBatch(BeanUtil.copyToList(savedDataList, WfAssetManageInventory.class));
        return RT.ok(result);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ResponseEntity<byte[]> exportData(@Valid WfAssetManageInventoryPageDto dto, @RequestParam(defaultValue = "false") Boolean isTemplate) {
        List<WfAssetManageInventoryPageVo> customerList = isTemplate != null && isTemplate ? new ArrayList<>() : ((PageOutput<WfAssetManageInventoryPageVo>) page(dto).getData()).getList();
        ByteArrayOutputStream bot = new ByteArrayOutputStream();
        EasyExcel.write(bot, WfAssetManageInventoryPageVo.class).automaticMergeHead(false).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(customerList);

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