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

import cn.iocoder.yudao.framework.common.enums.BlockStateEnum;
import cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;

import cn.iocoder.yudao.module.product.controller.admin.harvestinfo.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.harvestinfo.HarvestInfoDO;
import cn.iocoder.yudao.module.product.convert.harvestinfo.HarvestInfoConvert;
import cn.iocoder.yudao.module.product.service.harvestinfo.HarvestInfoService;

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

@Api(tags = "管理后台 - 采收入库")
@RestController
@RequestMapping("/product/harvest-info")
@Validated
public class HarvestInfoController {

    @Resource
    private HarvestInfoService harvestInfoService;

    @PostMapping("/create")
    @ApiOperation("创建采收入库")
    @PreAuthorize("@ss.hasPermission('product:harvest-info:create')")
    public CommonResult<Long> createHarvestInfo(@Valid @RequestBody HarvestInfoCreateReqVO createReqVO) {
        return CommonResult.success(harvestInfoService.createHarvestInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新采收入库")
    @PreAuthorize("@ss.hasPermission('product:harvest-info:update')")
    public CommonResult<Boolean> updateHarvestInfo(@Valid @RequestBody HarvestInfoUpdateReqVO updateReqVO) {
        harvestInfoService.updateHarvestInfo(updateReqVO);
        return CommonResult.success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除采收入库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:harvest-info:delete')")
    public CommonResult<Boolean> deleteHarvestInfo(@RequestParam("id") Long id) {
        harvestInfoService.deleteHarvestInfo(id);
        return CommonResult.success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得采收入库")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('product:harvest-info:query')")
    public CommonResult<HarvestInfoRespVO> getHarvestInfo(@RequestParam("id") Long id) {
        return CommonResult.success(harvestInfoService.getHarvestInfo(id));
    }

    @GetMapping("/list")
    @ApiOperation("获得采收入库列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('product:harvest-info:query')")
    public CommonResult<List<HarvestInfoRespVO>> getHarvestInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<HarvestInfoDO> list = harvestInfoService.getHarvestInfoList(ids);
        return CommonResult.success(HarvestInfoConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得采收入库分页")
    @PreAuthorize("@ss.hasPermission('product:harvest-info:query')")
    public CommonResult<PageResult<HarvestInfoRespVO>> getHarvestInfoPage(@Valid HarvestInfoPageReqVO pageVO) {
        PageResult<HarvestInfoDO> pageResult = harvestInfoService.getHarvestInfoPage(pageVO);
        return CommonResult.success(HarvestInfoConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出采收入库 Excel")
    @PreAuthorize("@ss.hasPermission('product:harvest-info:export')")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    public void exportHarvestInfoExcel(@Valid HarvestInfoExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<HarvestInfoDO> list = harvestInfoService.getHarvestInfoList(exportReqVO);
        // 导出 Excel
        List<HarvestInfoExcelVO> datas = HarvestInfoConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "采收入库.xls", "数据", HarvestInfoExcelVO.class, datas);
    }

    @GetMapping("/getHarverstIdList")
    @ApiOperation("获得采收入库编号列表")
    @ApiImplicitParam(name = "count", value = "数量", required = true, example = "1", dataTypeClass = Integer.class)
    @PreAuthorize("@ss.hasPermission('product:harvest-info:query')")
    public CommonResult<List<String>> getHarverstIdList(@RequestParam("count") @NotNull Integer count) {
        return CommonResult.success(harvestInfoService.getHarverstIdList(count));
    }

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

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

}
