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

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.smartfactory.controller.admin.accessoriescollar.vo.AccessoriesCollarPageReqVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.accessoriescollar.vo.AccessoriesCollarRespVO;
import cn.iocoder.yudao.module.smartfactory.controller.admin.accessoriescollar.vo.AccessoriesCollarSaveReqVO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.accessoriescollar.AccessoriesCollarDO;
import cn.iocoder.yudao.module.smartfactory.dal.dataobject.factoryinfo.FactoryInfoDO;
import cn.iocoder.yudao.module.smartfactory.service.accessoriescollar.AccessoriesCollarService;
import cn.iocoder.yudao.module.smartfactory.service.factoryinfo.FactoryInfoService;
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.validation.Valid;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 厂区配件领用")
@RestController
@RequestMapping("/smartfactory/accessories-collar")
@Validated
public class AccessoriesCollarController {

    @Resource
    private AccessoriesCollarService accessoriesCollarService;

    @Resource
    private FactoryInfoService factoryInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建厂区配件领用")
    @PreAuthorize("@ss.hasPermission('smartfactory:accessories-collar:create')")
    public CommonResult<Long> createAccessoriesCollar(@Valid @RequestBody AccessoriesCollarSaveReqVO createReqVO) {
        return success(accessoriesCollarService.createAccessoriesCollar(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新厂区配件领用")
    @PreAuthorize("@ss.hasPermission('smartfactory:accessories-collar:update')")
    public CommonResult<Boolean> updateAccessoriesCollar(@Valid @RequestBody AccessoriesCollarSaveReqVO updateReqVO) {
        accessoriesCollarService.updateAccessoriesCollar(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除厂区配件领用")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('smartfactory:accessories-collar:delete')")
    public CommonResult<Boolean> deleteAccessoriesCollar(@RequestParam("id") Long id) {
        accessoriesCollarService.deleteAccessoriesCollar(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得厂区配件领用")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('smartfactory:accessories-collar:query')")
    public CommonResult<AccessoriesCollarRespVO> getAccessoriesCollar(@RequestParam("id") Long id) {
        AccessoriesCollarDO accessoriesCollar = accessoriesCollarService.getAccessoriesCollar(id);
        return success(BeanUtils.toBean(accessoriesCollar, AccessoriesCollarRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得厂区配件领用分页")
    @PreAuthorize("@ss.hasPermission('smartfactory:accessories-collar:query')")
    public CommonResult<PageResult<AccessoriesCollarRespVO>> getAccessoriesCollarPage(@Valid AccessoriesCollarPageReqVO pageReqVO) {
        PageResult<AccessoriesCollarRespVO> pageResult = BeanUtils.toBean(accessoriesCollarService.getAccessoriesCollarPage(pageReqVO), AccessoriesCollarRespVO.class);
        if (CollUtil.isNotEmpty(pageResult.getList())) {
            // 获取工厂编号
            Set<Long> factoryIds = convertSet(pageResult.getList(), AccessoriesCollarRespVO::getFactoryId);
            // 获取工厂信息
            Map<Long, FactoryInfoDO> factoryMap = convertMap(factoryInfoService.getFactoryList(factoryIds), FactoryInfoDO::getId);

            pageResult.getList().forEach(data -> {
                // 设置工厂名称
                data.setFactoryName(factoryMap.containsKey(data.getFactoryId()) ? factoryMap.get(data.getFactoryId()).getShortName() : null);
            });
        }
        return success(BeanUtils.toBean(pageResult, AccessoriesCollarRespVO.class));
    }
}