package cn.iocoder.yudao.module.asset.controller.admin.assetinfo;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
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.module.asset.controller.admin.assetinfo.vo.AssetInfoPageReqVO;
import cn.iocoder.yudao.module.asset.controller.admin.assetinfo.vo.AssetInfoRespVO;
import cn.iocoder.yudao.module.asset.controller.admin.assetinfo.vo.AssetInfoRetireReqVO;
import cn.iocoder.yudao.module.asset.controller.admin.assetinfo.vo.AssetInfoSaveReqVO;
import cn.iocoder.yudao.module.asset.dal.dataobject.assetinfo.AssetInfoDO;
import cn.iocoder.yudao.module.asset.enums.AssetStateEnum;
import cn.iocoder.yudao.module.asset.enums.AssetStatusEnum;
import cn.iocoder.yudao.module.asset.service.assetinfo.AssetInfoService;
import cn.iocoder.yudao.module.employee.api.EmployeeApi;
import cn.iocoder.yudao.module.employee.api.dto.EmployeeRespDTO;
import cn.iocoder.yudao.module.infra.api.file.FileApi;
import cn.iocoder.yudao.module.infra.api.file.dto.FileDTO;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
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.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

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

@Tag(name = "管理后台 - 固定资产信息")
@RestController
@RequestMapping("/finance/asset/info")
@Validated
public class AssetInfoController {

    @Resource
    private AssetInfoService infoService;

    @Resource
    private EmployeeApi employeeApi;

    @Resource
    private FileApi fileApi;

    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建固定资产信息")
    @ApiOperationSupport(order = 1)
    // @PreAuthorize("@ss.hasPermission('asset:info:create')")
    public CommonResult<Long> createInfo(@Valid @RequestBody AssetInfoSaveReqVO createReqVO) {
        return success(infoService.createInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新固定资产信息")
    @ApiOperationSupport(order = 2)
    // @PreAuthorize("@ss.hasPermission('asset:info:update')")
    public CommonResult<Boolean> updateInfo(@Valid @RequestBody AssetInfoSaveReqVO updateReqVO) {
        infoService.updateInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除固定资产信息")
    @ApiOperationSupport(order = 3)
    @Parameter(name = "id", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('asset:info:delete')")
    public CommonResult<Boolean> deleteInfo(@RequestParam("id") Long id) {
        infoService.deleteInfo(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得固定资产信息")
    @ApiOperationSupport(order = 4)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    // @PreAuthorize("@ss.hasPermission('asset:info:query')")
    public CommonResult<AssetInfoRespVO> getInfo(@RequestParam("id") Long id) {
        AssetInfoDO info = infoService.getInfo(id);
        AssetInfoRespVO assetInfoRespVO = BeanUtils.toBean(info, AssetInfoRespVO.class);
        // 归属部门名称
        DeptRespDTO dept = deptApi.getDept(info.getAssetDeptId());
        if (Objects.nonNull(dept)) {
            assetInfoRespVO.setAssetDeptName(dept.getName());
        }
        // 资产负责人名称
        EmployeeRespDTO employee = employeeApi.getEmployeeById(info.getAssetManagerEmployeeId());
        if (Objects.nonNull(employee)) {
            assetInfoRespVO.setAssetManagerEmployeeName(employee.getName());
        }
        // 资产状态
        Map<String, String> assetStateMap = Arrays.stream(AssetStateEnum.values())
                .collect(Collectors.toMap(AssetStateEnum::getState, AssetStateEnum::getDesc));
        assetInfoRespVO.setAssetStateDesc(assetStateMap.getOrDefault(assetInfoRespVO.getAssetState(), "未知"));
        // 单据状态
        Map<String, String> assetStatusMap = Arrays.stream(AssetStatusEnum.values())
                .collect(Collectors.toMap(AssetStatusEnum::getStatus, AssetStatusEnum::getDesc));
        assetInfoRespVO.setStatusDesc(assetStatusMap.getOrDefault(assetInfoRespVO.getStatus(), "未知"));
        // 附件
        List<FileDTO> fileDTOListByBiz = fileApi.getFileDTOListByBiz(info.getAssetInfoUuid());
        assetInfoRespVO.setFileList(fileDTOListByBiz);
        List<Long> fileIdList = fileDTOListByBiz.stream().map(FileDTO::getId).collect(Collectors.toList());
        assetInfoRespVO.setFileIdList(fileIdList);

        return success(assetInfoRespVO);
    }

    @GetMapping("/page")
    @Operation(summary = "获得固定资产信息分页")
    @ApiOperationSupport(order = 5)
    // @PreAuthorize("@ss.hasPermission('asset:info:query')")
    public CommonResult<PageResult<AssetInfoRespVO>> getInfoPage(@Valid @ParameterObject AssetInfoPageReqVO pageReqVO) {
        PageResult<AssetInfoRespVO> pageResult = infoService.getInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AssetInfoRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出固定资产信息 Excel")
    @ApiOperationSupport(order = 6)
    // @PreAuthorize("@ss.hasPermission('asset:info:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInfoExcel(@Valid @ParameterObject AssetInfoPageReqVO pageReqVO,
                                HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AssetInfoRespVO> list = infoService.getInfoPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "固定资产信息.xls", "数据", AssetInfoRespVO.class,
                list);
    }

    @PostMapping("/retire")
    @Operation(summary = "报废固定资产信息")
    @ApiOperationSupport(order = 7)
    // @PreAuthorize("@ss.hasPermission('asset:info:delete')")
    public CommonResult<Boolean> retireInfo(@Valid @RequestBody AssetInfoRetireReqVO retireReqVO) {
        infoService.retireInfo(retireReqVO);
        return success(true);
    }


}