package cn.iocoder.yudao.module.smartfactory.controller.admin.handlinggroup;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.smartfactory.controller.admin.handlinggroup.vo.HandlingGroupPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.handlinggroup.vo.HandlingGroupRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.handlinggroup.vo.HandlingGroupSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlinggroup.HandlingGroupDO;
import cn.iocoder.yudao.module.smartfactory.service.handlinggroup.HandlingGroupService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
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.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 搬运组")
@RestController
@RequestMapping("/smartfactory/handling-group")
@Validated
public class HandlingGroupController {

    @Resource
    private HandlingGroupService handlingGroupService;

    @PostMapping("/create")
    @Operation(summary = "创建搬运组")
    public CommonResult<Long> createHandlingGroup(@Valid @RequestBody HandlingGroupSaveReqVO createReqVO) {
        return success(handlingGroupService.createHandlingGroup(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新搬运组")
    public CommonResult<Boolean> updateHandlingGroup(@Valid @RequestBody HandlingGroupSaveReqVO updateReqVO) {
        handlingGroupService.updateHandlingGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除搬运组")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteHandlingGroup(@RequestParam("id") Long id) {
        handlingGroupService.deleteHandlingGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得搬运组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<HandlingGroupRespVO> getHandlingGroup(@RequestParam("id") Long id) {
        HandlingGroupDO handlingGroup = handlingGroupService.getHandlingGroup(id);
        return success(BeanUtils.toBean(handlingGroup, HandlingGroupRespVO.class));
    }

    @GetMapping("/getAllByFactoryId")
    @Operation(summary = "根据工程id获得搬运组列表")
    public CommonResult<List<HandlingGroupRespVO>> getAllByFactoryId(@RequestParam("factoryId") Long factoryId) {
        List<HandlingGroupDO> list = handlingGroupService.list(new LambdaQueryWrapper<HandlingGroupDO>().eq(HandlingGroupDO::getFactoryId, factoryId));
        return success(BeanUtils.toBean(list, HandlingGroupRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得搬运组分页")
    public CommonResult<PageResult<HandlingGroupRespVO>> getHandlingGroupPage(@Valid HandlingGroupPageReqVO pageReqVO) {
        PageResult<HandlingGroupDO> pageResult = handlingGroupService.getHandlingGroupPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HandlingGroupRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出搬运组 Excel")
    @OperateLog(type = EXPORT)
    public void exportHandlingGroupExcel(@Valid HandlingGroupPageReqVO pageReqVO,
                                         HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HandlingGroupDO> list = handlingGroupService.getHandlingGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "搬运组.xls", "数据", HandlingGroupRespVO.class,
                BeanUtils.toBean(list, HandlingGroupRespVO.class));
    }

}
