package cn.iocoder.yudao.module.product.controller.admin.inspection;

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.product.controller.admin.inspection.vo.InspectionCreateReqVO;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionPageReqVO;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionRespVO;
import cn.iocoder.yudao.module.product.controller.admin.inspection.vo.InspectionUpdateReqVO;
import cn.iocoder.yudao.module.product.convert.inspection.InspectionConvert;
import cn.iocoder.yudao.module.product.dal.dataobject.inspection.InspectionDO;
import cn.iocoder.yudao.module.product.service.inspection.InspectionService;
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.List;

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

@Api(tags = "管理后台 - 检验检疫")
@RestController
@RequestMapping("/product/inspection")
@Validated
public class InspectionController {

    @Resource
    private InspectionService inspectionService;

    @PostMapping("/create")
    @ApiOperation("创建检验检疫")
    @PreAuthorize("@ss.hasPermission('product:inspection:create')")
    public CommonResult<Long> createInspection(@Valid @RequestBody InspectionCreateReqVO createReqVO) {
        return success(inspectionService.createInspection(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新检验检疫")
    @PreAuthorize("@ss.hasPermission('product:inspection:update')")
    public CommonResult<Boolean> updateInspection(@Valid @RequestBody InspectionUpdateReqVO updateReqVO) {
        inspectionService.updateInspection(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除检验检疫")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:inspection:delete')")
    public CommonResult<Boolean> deleteInspection(@RequestParam("id") Long id) {
        inspectionService.deleteInspection(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得检验检疫")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:inspection:query')")
    public CommonResult<InspectionRespVO> getInspection(@RequestParam("id") Long id) {
        return success(inspectionService.getInspection(id));
    }

    @GetMapping("/page")
    @ApiOperation("获得检验检疫分页")
    @PreAuthorize("@ss.hasPermission('product:inspection:query')")
    public CommonResult<PageResult<InspectionRespVO>> getInspectionPage(@Valid InspectionPageReqVO pageVO) {
        PageResult<InspectionDO> pageResult = inspectionService.getInspectionPage(pageVO);
        return success(InspectionConvert.INSTANCE.convertPage(pageResult));
    }

    @PutMapping("/upChain")
    @ApiOperation("上链")
    @ApiImplicitParam(name = "id", value = "ID", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:inspection:upChain')")
    public CommonResult<Boolean> upChain(@RequestParam("id") Long id) {
        inspectionService.upChain(id);
        return success(Boolean.TRUE);
    }

    @PutMapping("/freeze")
    @ApiOperation("冻结")
    @ApiImplicitParam(name = "id", value = "ID", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:inspection:freeze')")
    public CommonResult<Boolean> freeze(@RequestParam("id") Long id) {
        inspectionService.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) {
        inspectionService.updateChainStatus(id, BlockStateEnum.ACTIVATION.getBlockState());
        return success(Boolean.TRUE);
    }

    @GetMapping("/getByHarvestBatchId")
    @ApiOperation("根据批次编码获得检验检疫")
    @ApiImplicitParam(name = "harvestBatchId", value = "批次编码", required = true, example = "HABTBC20230424165426A32RGFEY", dataTypeClass = String.class)
    @PreAuthorize("@ss.hasPermission('product:inspection:query')")
    public CommonResult<List<InspectionRespVO>> getByHarvestBatchId(@RequestParam("harvestBatchId") String harvestBatchId) {
        return success(inspectionService.getByHarvestBatchId(harvestBatchId));
    }
}
