package com.jeeplus.psimanage.psiWareHouse.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.psiWareHouse.service.dto.PsiWareSummaryExportDto;
import com.jeeplus.psimanage.purchase.service.PsiMaterialService;
import com.jeeplus.psimanage.purchase.service.dto.PsiMaterialInfoDto;
import com.jeeplus.psimanage.psiWareHouse.domain.PsiWareHouseDetailed;
import com.jeeplus.psimanage.psiWareHouse.domain.PsiWarehouseUpHi;
import com.jeeplus.psimanage.psiWareHouse.service.PsiWareHouseBasicService;
import com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseDto;
import com.jeeplus.psimanage.psiWareHouse.service.dto.PsiWareHouseExportDto;
import com.jeeplus.utils.StringUtils;
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.Map;
import java.util.stream.Collectors;

/**
 * @author: 王强
 * @create: 2022-12-29 14:13
 **/
@RestController
@Api(tags ="进销存-入库")
@RequestMapping(value = "/psi/management/wareHouse")
public class PsiWareHouseController {

    @Resource
    private PsiMaterialService service;

    @Resource
    private PsiWareHouseBasicService basicService;

    /**
     * 根据id修改状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改状态值status")
    @PostMapping(value = "/updateStatusById")
    public ResponseEntity<String> updateStatusById(@RequestBody PsiWareHouseDto dto) {
        String s = basicService.updateStatusById(dto);
        return ResponseEntity.ok(s);
    }

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

    /**
     * 采购编号列表查询
     * @param dto
     * @return
     */
    @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);
    }

    /**
     * 采购编号列表查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "采购编号列表查询(报销使用)")
    @GetMapping("/reimbursementList")
    public ResponseEntity<IPage<PsiMaterialInfoDto>> reimbursementList(Page<PsiMaterialInfoDto> page, PsiMaterialInfoDto dto) throws Exception{
        IPage<PsiMaterialInfoDto> iPage = service.reimbursementList(page, dto);
        return ResponseEntity.ok(iPage);
    }


    /**
     * 入库列表查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "入库列表查询")
    @GetMapping("/wareHouseList")
    public ResponseEntity<IPage<PsiWareHouseDto>> wareHouseList(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.list(page, dto);
        return ResponseEntity.ok(iPage);
    }

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

    /**
     * 入库记录查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "入库记录查询")
    @GetMapping("/wareHouseHistoryList")
    public ResponseEntity<IPage<PsiWareHouseDto>> wareHouseHistoryList(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.wareHouseHistoryList(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 领用记录查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "领用记录查询")
    @GetMapping("/collectHistoryList")
    public ResponseEntity<IPage<PsiWareHouseDto>> collectHistoryList(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.collectHistoryList(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 入库汇总列表查询
     * @param dto
     * @return
     */
    @ApiOperation(value = "入库汇总列表查询")
    @GetMapping("/wareHouseSummaryList")
    public ResponseEntity<IPage<PsiWareHouseDto>> wareHouseSummaryList(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.summaryList(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 入库汇总列表查询只展示小于等于5的数据
     * @param dto
     * @return
     */
    @ApiOperation(value = "入库汇总列表查询只展示小于等于5的数据")
    @GetMapping("/wareHouseSummaryList2")
    public ResponseEntity<IPage<PsiWareHouseDto>> wareHouseSummaryList2(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.summaryList2(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 根据商品名，分类，按照生产日期分组查询列表数据
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据商品名，分类，按照生产日期分组查询列表数据")
    @GetMapping("/getByProduceDateAndNameGroup")
    public ResponseEntity<IPage<PsiWareHouseDto>> getByProduceDateAndNameGroup(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.getByProduceDateAndName(page, dto);
        return ResponseEntity.ok(iPage);
    }


    /**
     * 根据商品名，分类，按照生产日期分组查询列表数据,不合并数据
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据商品名，分类，按照生产日期分组查询列表数据，不合并数据")
    @GetMapping("/getByProduceDateNotMerge")
    public ResponseEntity<IPage<PsiWareHouseDto>> getByProduceDateNotMerge(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.getByProduceDateNotMerge(page, dto);
        return ResponseEntity.ok(iPage);
    }

    /**
     * 查询所有库存数据，无限制
     * @param dto
     * @return
     */
    @ApiOperation(value = "查询所有库存数据，无限制")
    @GetMapping("/getAllData")
    public ResponseEntity<IPage<PsiWareHouseDto>> getAllData(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) throws Exception{
        IPage<PsiWareHouseDto> iPage = basicService.getAllData(page, dto);
        return ResponseEntity.ok(iPage);
    }



    @ApiOperation(value = "即将过期物资查询")
    @GetMapping("/nearExpiryList")
    public void nearExpiryList(Page<PsiWareHouseDto> page, PsiWareHouseDto dto) {
       basicService.nearExpiryList(page, dto);
    }



    /**
     * 新增/修改
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增、修改")
    @PostMapping("/save")
    public ResponseEntity save(@RequestBody PsiWareHouseDto dto) throws Exception{
        String id = basicService.save(dto);
        return ResponseUtil.newInstance().add("businessTable", "psi_management_warehouse_basics").add("businessId", id).ok ("操作成功");
    }


    @ApiOperation(value = "流程终止、取消修改")
    @GetMapping("/backSourceData")
    public ResponseEntity<Map<String, String>> backSourceData(String id){
        Map<String, String> map = basicService.backSourceData(id);
        return ResponseEntity.ok(map);
    }

    /**
     * 查询
     * @param id
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/findById")
    public ResponseEntity<PsiWareHouseDto> findById(@RequestParam String id, @RequestParam(required = false) String status) throws Exception{
        if (StringUtils.isBlank(status)) {
            status = "false";
        }
        PsiWareHouseDto dto = basicService.findById(id,status);
        return ResponseEntity.ok(dto);
    }

    /**
     * 查询采购明细表id
     * @param purchaseNo
     * @return
     */
    @ApiOperation(value = "查询采购明细表id")
    @GetMapping("/findRequestId")
    public ResponseEntity<String> findRequestId(@RequestParam String purchaseNo) throws Exception{
        String id = basicService.findRequestId(purchaseNo);
        return ResponseEntity.ok(id);
    }

    /**
     * 根据入库表id查询其全部历史修改数据
     * @return
     */
    @ApiOperation(value = "查询历史修改表数据")
    @GetMapping("/findHiById")
    public ResponseEntity findHiById(@RequestParam String id) {
        List<PsiWarehouseUpHi> list = basicService.findHiById(id);
        return ResponseEntity.ok(list);
    }

    /**
     * 根据历史表id查询历史表数据
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询历史修改表数据")
    @GetMapping("/findHiByHiId")
    public ResponseEntity findHiByHiId(@RequestParam String id) {
        PsiWareHouseDto hi = basicService.findHiByHiId(id);
        return ResponseEntity.ok(hi);
    }

    /**
     * 根据入库表id查询最近的一个修改历史
     * @param id
     * @return
     */
    @ApiOperation(value = "根据入库表id查询最近的一个修改历史")
    @GetMapping("/findLastHiByid")
    public ResponseEntity findLastHiByid(@RequestParam String id) {
        PsiWareHouseDto hi = basicService.findLastHiByid(id);
        return ResponseEntity.ok(hi);
    }

    /**
     * 库存提醒状态 启用/停用
     * @return
     */
    @ApiLog(value = "库存提醒状态启用/停用", type = LogTypeEnum.SAVE)
    @PostMapping("updateWarnFlagByTradeNameAndType")
    public ResponseEntity updateWarnFlagByTradeNameAndType(@RequestBody PsiWareHouseDto wareHouseDto) {
        return basicService.updateWarnFlagByTradeNameAndType(wareHouseDto);
    }

    /**
     * 库存提醒数量修改
     * @param wareHouseDto
     * @return
     */
    @ApiLog(value = "库存提醒数量修改", type = LogTypeEnum.SAVE)
    @PostMapping("updateWarnNumByTradeNameAndType")
    public ResponseEntity updateWarnNumByTradeNameAndType(@RequestBody PsiWareHouseDto wareHouseDto) {
        return basicService.updateWarnNumByTradeNameAndType(wareHouseDto);
    }

    /**
     * 根据商品名称修改
     * @param wareHouseDto
     * @return
     */
    @ApiLog(value = "根据商品名称修改", type = LogTypeEnum.SAVE)
    @PostMapping("saveTradeName")
    public ResponseEntity saveTradeName(@RequestBody PsiWareHouseDto wareHouseDto) {
        basicService.saveTradeName(wareHouseDto);
        return ResponseEntity.ok("修改成功");
    }

    /**
     * 根据入库类型查询商品
     * @param typeId
     * @return
     */
    @ApiOperation(value = "根据入库类型查询商品")
    @GetMapping("/findTradeByTypeId")
    public ResponseEntity findTradeByTypeId(@RequestParam String typeId) {
        List<PsiWareHouseDetailed> res = basicService.findTradeByTypeId(typeId);
        return ResponseEntity.ok(res);
    }


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

        EasyExcelUtils.newInstance().exportExcel(result, sheetName, PsiWareSummaryExportDto.class, fileName, null, response);
    }


}
