package com.jeeplus.psimanage.loss.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.excel.EasyExcelUtils;
import com.jeeplus.common.excel.ExcelOptions;
import com.jeeplus.common.excel.annotation.ExportMode;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.psimanage.loss.domain.PsiLossDetailed;
import com.jeeplus.psimanage.loss.service.PsiLossService;
import com.jeeplus.psimanage.loss.service.dto.PsiLossDto;
import com.jeeplus.psimanage.loss.service.dto.PsiLossExportDto;
import com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto;
import com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareSummaryExportDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "物资管理-报损申请")
@RequestMapping(value = "/psi/loss")
public class PsiLossController {

    @Resource
    private PsiLossService psiLossService;

    @ApiOperation(value = "根据id修改状态")
    @PostMapping(value = "updateStatusById")
    public void updateStatusById(@RequestBody PsiLossDto dto) {
        psiLossService.updateStatusById(dto);
    }

    @ApiOperation(value = "删除")
    @GetMapping("/remove")
    public ResponseEntity<String> remove(@RequestParam String id) {
        return ResponseEntity.ok(psiLossService.remove(id));
    }

    @ApiOperation(value = "新增、修改")
    @PostMapping("/save")
    public ResponseEntity<String> save(@RequestBody PsiLossDto dto) throws Exception {
        String id = psiLossService.save(dto);
        return ResponseUtil.newInstance()
                .add("businessTable", "psi_management_loss_basics")
                .add("businessId", id)
                .ok("操作成功");
    }

    @ApiOperation(value = "查询")
    @GetMapping("/findById")
    public ResponseEntity<PsiLossDto> findById(@RequestParam String id) throws Exception {
        return ResponseEntity.ok(psiLossService.findById(id));
    }

    @ApiOperation(value = "列表查询")
    @GetMapping("/list")
    public ResponseEntity<IPage<PsiLossDto>> list(Page<PsiLossDto> page, PsiLossDto dto) throws Exception {
        return ResponseEntity.ok(psiLossService.list(page, dto));
    }

    @ApiLog(value = "导出入库数据", type = LogTypeEnum.EXPORT)
    @ApiOperation(value = "导出入库数据")
    @GetMapping("/exportFile")
    public void exportFile(Page<PsiLossDto> page, PsiLossDto dto, ExcelOptions options, HttpServletResponse response) throws Exception{
        String fileName = options.getFilename();
        String sheetName = options.getSheetName();
        List<PsiLossExportDto> result;
        if (ExportMode.current.equals(options.getMode())) {
            result = psiLossService.exportRecordList(page, dto);
        } else if (ExportMode.selected.equals(options.getMode())) {
            result = psiLossService.exportRecordList(page, dto).stream().filter(item ->
                    options.getSelectIds().contains(item.getId())
            ).collect(Collectors.toList());
        } else {
            page.setSize(-1);
            page.setCurrent(0);
            result = psiLossService.exportRecordList(page, dto);
        }
        EasyExcelUtils.newInstance().exportExcel(result, sheetName, PsiLossExportDto.class, fileName, null, response);
    }


    @ApiOperation(value = "根据物品名称查询所有报损数据")
    @GetMapping("/getLossByTradeName")
    public ResponseEntity<IPage<PsiLossDetailed>> getLossByTradeName(Page<PsiLossDetailed> page, PsiLossDetailed dto) throws Exception {
        return ResponseEntity.ok(psiLossService.getLossByTradeName(page, dto));
    }
}
