package cn.iocoder.yudao.module.flow.controller.admin.outbound;

import cn.iocoder.yudao.framework.common.enums.BlockStateEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.flow.controller.admin.outbound.vo.*;
import cn.iocoder.yudao.module.flow.convert.outbound.OutboundConvert;
import cn.iocoder.yudao.module.flow.dal.dataobject.outbound.OutboundDO;
import cn.iocoder.yudao.module.flow.service.outbound.OutboundService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 出库单")
@RestController
@RequestMapping("/flow/outbound")
@Validated
public class OutboundController {

    @Resource
    private OutboundService outboundService;

    @PostMapping("/create")
    @ApiOperation("创建出库单")
    @PreAuthorize("@ss.hasPermission('flow:outbound:create')")
    public CommonResult<Long> createOutbound(@Valid @RequestBody OutboundCreateReqVO createReqVO) {
        return success(outboundService.createOutbound(createReqVO));
    }

    @PutMapping("/upChain")
    @ApiOperation("上链")
    @ApiImplicitParam(name = "id", value = "id", required = true, example = "1", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('flow:outbound:update')")
    public CommonResult<Boolean> upChain(@RequestParam("id") Long id) {
        outboundService.upChain(id);
        return CommonResult.success(true);
    }

    @PutMapping("/freeze")
    @ApiOperation("冻结")
    @ApiImplicitParam(name = "id", value = "ID", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('flow:outbound:freeze')")
    public CommonResult<Boolean> freeze(@RequestParam("id") Long id) {
        outboundService.updateChainStatus(id, BlockStateEnum.FREEZE.getBlockState());
        return success(Boolean.TRUE);
    }

    @PutMapping("/active")
    @ApiOperation("激活")
    @ApiImplicitParam(name = "id", value = "ID", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:inspection:active')")
    public CommonResult<Boolean> active(@RequestParam("id") Long id) {
        outboundService.updateChainStatus(id, BlockStateEnum.ACTIVATION.getBlockState());
        return success(Boolean.TRUE);
    }

    @PutMapping("/update")
    @ApiOperation("更新出库单")
    @PreAuthorize("@ss.hasPermission('flow:outbound:update')")
    public CommonResult<Boolean> updateOutbound(@Valid @RequestBody OutboundUpdateReqVO updateReqVO) {
        outboundService.updateOutbound(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除出库单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('flow:outbound:delete')")
    public CommonResult<Boolean> deleteOutbound(@RequestParam("id") Long id) {
        outboundService.deleteOutbound(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得出库单")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('flow:outbound:query')")
    public CommonResult<OutboundRespVO> getOutbound(@RequestParam("id") Long id) {
        OutboundRespVO outboundRespVO = outboundService.getOutbound(id);
        return success(outboundRespVO);
    }

    @GetMapping("/getOutboundGoodsInfoById")
    @ApiOperation("根据id获取主表和子表的信息")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('flow:outbound:query')")
    public CommonResult<Map<String,Object>> getOutboundGoodsInfoById(@RequestParam("id") Long id) {
        Map<String, Object> goodsInfoById = outboundService.getOutboundGoodsInfoById(id);
        return success(goodsInfoById);
    }

    @GetMapping("/getOutbound")
    @ApiOperation("根据主出库单号，查询所有子出库单号")
    @ApiImplicitParam(name = "outBoundId", value = "出库单号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('flow:outbound:query')")
     public CommonResult<OutboundDTO> getOutboundGoodsInfoOutBoundId(@RequestParam("outBoundId") String outBoundId) {
        OutboundDTO infoOutBoundId = outboundService.getOutboundGoodsInfoOutBoundId(outBoundId);
        return success(infoOutBoundId);
    }

    @GetMapping("/list")
    @ApiOperation("获得出库单列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('flow:outbound:query')")
    public CommonResult<List<OutboundRespVO>> getOutboundList(@RequestParam("ids") Collection<Long> ids) {
        List<OutboundDO> list = outboundService.getOutboundList(ids);
        return success(OutboundConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得出库单分页")
    @PreAuthorize("@ss.hasPermission('flow:outbound:query')")
    public CommonResult<PageResult<OutboundRespVO>> getOutboundPage(@Valid OutboundPageReqVO pageVO) {
        PageResult<OutboundDO> pageResult = outboundService.getOutboundPage(pageVO);
        return success(OutboundConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/outboundStatistics")
    @ApiOperation("获取首页出库数据记录")
    public CommonResult<OutboundInfoDataRespVO> getOutboundInfoDataStatistics() {
        return CommonResult.success(outboundService.getOutboundInfoDataStatistics());
    }
}
