package com.jeeplus.pubmodules.wareHouse.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.logging.annotation.ApiLog;
import com.jeeplus.logging.constant.enums.LogTypeEnum;
import com.jeeplus.pubmodules.purchase.service.MaterialService;
import com.jeeplus.pubmodules.purchase.service.dto.MaterialInfoDto;
import com.jeeplus.pubmodules.wareHouse.domain.WareHouseDetailed;
import com.jeeplus.pubmodules.wareHouse.domain.WarehouseUpHi;
import com.jeeplus.pubmodules.wareHouse.service.WareHouseBasicService;
import com.jeeplus.pubmodules.wareHouse.service.dto.WareHouseDto;
import com.jeeplus.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: 王强
 * @create: 2022-12-29 14:13
 **/
@RestController
@Api(tags ="物资管理-入库")
@RequestMapping(value = "/material/management/wareHouse")
public class WareHouseController {

    @Resource
    private MaterialService service;

    @Resource
    private WareHouseBasicService basicService;

    /**
     * 根据id修改状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改状态值status")
    @PostMapping(value = "/updateStatusById")
    public ResponseEntity<String> updateStatusById(@RequestBody WareHouseDto 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<MaterialInfoDto>> list(Page<MaterialInfoDto> page, MaterialInfoDto dto) throws Exception{
        IPage<MaterialInfoDto> iPage = service.list(page, dto);
        return ResponseEntity.ok(iPage);
    }

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


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

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

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

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

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

    /**
     * 新增/修改
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增、修改")
    @PostMapping("/save")
    public ResponseEntity save(@RequestBody WareHouseDto dto) throws Exception{
        String id = basicService.save(dto);
        return ResponseUtil.newInstance().add("businessTable", "material_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<WareHouseDto> findById(@RequestParam String id, @RequestParam(required = false) String status) throws Exception{
        if (StringUtils.isBlank(status)) {
            status = "false";
        }
        WareHouseDto 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<WarehouseUpHi> list = basicService.findHiById(id);
        return ResponseEntity.ok(list);
    }

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

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

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

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

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

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

}
