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

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.handlingspecifications.vo.*;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryinfo.FactoryInfoDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.handlingspecifications.HandlingSpecificationsDO;
import cn.iocoder.yudao.module.smartfactory.service.factoryinfo.FactoryInfoService;
import cn.iocoder.yudao.module.smartfactory.service.handlingspecifications.HandlingSpecificationsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
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 org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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-specifications")
@Validated
public class HandlingSpecificationsController {

    @Resource
    private HandlingSpecificationsService handlingSpecificationsService;

    @Resource
    private FactoryInfoService factoryInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建工厂搬运规格")
    @PreAuthorize("@ss.hasPermission('smartfactory:handling-specifications:create')")
    public CommonResult<Long> createHandlingSpecifications(@Valid @RequestBody HandlingSpecificationsSaveReqVO createReqVO) {
        return success(handlingSpecificationsService.createHandlingSpecifications(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新工厂搬运规格")
    @PreAuthorize("@ss.hasPermission('smartfactory:handling-specifications:update')")
    public CommonResult<Boolean> updateHandlingSpecifications(@Valid @RequestBody HandlingSpecificationsSaveReqVO updateReqVO) {
        handlingSpecificationsService.updateHandlingSpecifications(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除工厂搬运规格")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:handling-specifications:delete')")
    public CommonResult<Boolean> deleteHandlingSpecifications(@RequestParam("id") Long id) {
        handlingSpecificationsService.deleteHandlingSpecifications(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得工厂搬运规格")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:handling-specifications:query')")
    public CommonResult<HandlingSpecificationsRespVO> getHandlingSpecifications(@RequestParam("id") Long id) {
        HandlingSpecificationsDO handlingSpecifications = handlingSpecificationsService.getHandlingSpecifications(id);
        return success(BeanUtils.toBean(handlingSpecifications, HandlingSpecificationsRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得工厂搬运规格分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:handling-specifications:query')")
    public CommonResult<PageResult<HandlingSpecificationsRespVO>> getHandlingSpecificationsPage(@Valid HandlingSpecificationsPageReqVO pageReqVO) {
        PageResult<HandlingSpecificationsDO> pageResult = handlingSpecificationsService.getHandlingSpecificationsPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, HandlingSpecificationsRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得工厂搬运规格列表")
    public CommonResult<List<HandlingSpecificationsRespVO>> getHandlingSpecificationsList(@Valid HandlingSpecificationsPageReqVO pageReqVO) {
        List<HandlingSpecificationsDO> list = handlingSpecificationsService.getHandlingSpecificationsList(pageReqVO);
        return success(BeanUtils.toBean(list, HandlingSpecificationsRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出工厂搬运规格 Excel")
    @PreAuthorize("@ss.hasPermission('smartfactory:handling-specifications:export')")
    @OperateLog(type = EXPORT)
    public void exportHandlingSpecificationsExcel(@Valid HandlingSpecificationsPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<HandlingSpecificationsDO> list = handlingSpecificationsService.getHandlingSpecificationsPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工厂搬运规格.xls", "数据", HandlingSpecificationsRespVO.class,
                        BeanUtils.toBean(list, HandlingSpecificationsRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入用户模板")
    public void importTemplate(HttpServletResponse response) throws IOException {

        //获取部门名称
        List<FactoryInfoDO> factoryInfoDOS = factoryInfoService.getFactoryListByType();
        List<String> factoryName = factoryInfoDOS.stream()
                .map(item -> item.getId() + ":" + item.getShortName())
                .collect(Collectors.toList());

        // 类型选择
        List<String> type = Arrays.asList("0:数量", "1:重量");

        Map<Integer, List<String>> mapDropDown = new HashMap<>();
        mapDropDown.put(0, factoryName);
        mapDropDown.put(2, type);

        // 输出
        ExcelUtils.write(response, "搬运规格列表模板.xlsx", "搬运规格列表",
                HandlingSpecificationsImportVO.class, null,
                mapDropDown);
    }

    @PostMapping("/import")
    @Operation(summary = "导入搬运规格列表")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<HandlingSpecificationsImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                       @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<HandlingSpecificationsImportVO> list = ExcelUtils.read(file, HandlingSpecificationsImportVO.class);
        return success(handlingSpecificationsService.importList(list, updateSupport));
    }
}
