package com.jeeplus.psimanage.purchase.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.purchase.domain.PsiMaterialDetailed;
import com.jeeplus.psimanage.purchase.service.PsiMaterialService;
import com.jeeplus.psimanage.purchase.service.dto.PsiMaterialExportDto;
import com.jeeplus.psimanage.purchase.service.dto.PsiMaterialInfoDto;
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/management")
public class PsiMaterialController {

    @Resource
    private PsiMaterialService service;

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

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

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

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

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

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

    @ApiOperation(value = "根据采购类型查询商品")
    @GetMapping("/findTradeByTypeId")
    public ResponseEntity<List<PsiMaterialDetailed>> findTradeByTypeId(@RequestParam String typeId) {
        List<PsiMaterialDetailed> res = service.findTradeByTypeId(typeId);
        return ResponseEntity.ok(res);
    }
}
