package com.jeeplus.pubmodules.collect.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.pubmodules.collect.domain.CollectBasicsReturnHi;
import com.jeeplus.pubmodules.collect.service.CollectService;
import com.jeeplus.pubmodules.collect.service.dto.CollectDto;
import com.jeeplus.pubmodules.collect.service.dto.WareHouseSummaryDto;
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 java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 王强
 * @create: 2023-01-03 16:18
 **/
@RestController
@Api(tags ="物资管理-领用申请")
@RequestMapping(value = "/material/collect")
public class CollectController {

    @Resource
    private CollectService collectService;

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

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

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

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

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

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

    /**
     * 创建领用退回流程表数据
     * @param dto
     * @return
     */
    @ApiOperation(value = "创建领用退回流程表数据")
    @PostMapping("/createReturnData")
    public ResponseEntity<Map<String,String>> createReturnData(@RequestBody CollectDto dto) throws Exception{
        String returnId = collectService.createReturnData(dto.getId());
        Map<String,String> res = new HashMap<String,String>();
        res.put("returnId", returnId);
        return ResponseEntity.ok(res);
    }

    /**
     * 领用-退回  发起
     * @param dto
     * @return
     */
    @ApiOperation(value = "领用-退回  发起")
    @PostMapping("/saveReturn")
    public ResponseEntity saveReturn(@RequestBody CollectDto dto) throws Exception{
        String businessId = collectService.saveReturn(dto);
        if (StringUtils.isNotBlank(businessId)) {
            return ResponseUtil.newInstance().add("businessTable", "material_management_collect_basics_return").add("businessId", businessId).ok ("操作成功");
        } else {
            return ResponseEntity.badRequest().body("操作失败");
        }
    }

    /**
     * 领用-退回  同意
     * @param dto
     * @return
     */
    @ApiOperation(value = "领用-退回  同意")
    @PostMapping("/saveReturnAgree")
    public ResponseEntity saveReturnAgree(@RequestBody CollectDto dto) throws Exception{
        String s = collectService.saveReturnAgree(dto);
        return ResponseUtil.newInstance().add("businessTable", "material_management_collect_basics_return").add("businessId", s).ok ("操作成功");
    }

    /**
     * 领用-退回流程修改状态
     * @param dto
     * @return
     */
    @ApiOperation(value = "领用-退回流程修改状态")
    @PostMapping("/updateStatusByIdReturn")
    public ResponseEntity updateStatusByIdReturn(@RequestBody CollectDto dto) throws Exception{
        collectService.updateStatusByIdReturn(dto.getId(), dto.getStatus());
        return ResponseEntity.ok("修改完成");
    }

    /**
     * 查询
     * @param returnId
     * @return
     */
    @ApiOperation(value = "查询")
    @GetMapping("/findByReturnId")
    public ResponseEntity<CollectDto> findByReturnId(@RequestParam String returnId) throws Exception{
        CollectDto dto = collectService.findByReturnId(returnId);
        return ResponseEntity.ok(dto);
    }

    /**
     * 取消领用退回
     * @param dto
     * @return
     */
    @ApiOperation(value = "取消领用退回")
    @PostMapping("/returnRequest")
    public ResponseEntity returnRequest(@RequestBody CollectDto dto) throws Exception{
        collectService.returnRequest(dto.getId());
        return ResponseEntity.ok("操作完成");
    }

    /**
     * 根据领用表id查询其全部历史修改数据
     * @return
     */
    @ApiOperation(value = "根据领用表id查询其全部历史修改数据")
    @GetMapping("/findHiById")
    public ResponseEntity findHiById(@RequestParam String collectId) {
        List<CollectBasicsReturnHi> hiById = collectService.findHiById(collectId);
        return ResponseEntity.ok(hiById);
    }

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

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